Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9409)

Unified Diff: cipd/client/cipd/common/common.go

Issue 2651173003: [cipd] Move ValidateRoot to common package (Closed)
Patch Set: Add test Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | cipd/client/cipd/common/common_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cipd/client/cipd/common/common.go
diff --git a/cipd/client/cipd/common/common.go b/cipd/client/cipd/common/common.go
index 14764f2705bb0e3e716dc602e229cf51be76803d..7930c5452bb06a83b7ac154b7c599e12f07eb05b 100644
--- a/cipd/client/cipd/common/common.go
+++ b/cipd/client/cipd/common/common.go
@@ -7,6 +7,7 @@ package common
import (
"fmt"
+ "path"
"regexp"
"runtime"
"strings"
@@ -119,6 +120,30 @@ func ValidateInstanceVersion(v string) error {
return fmt.Errorf("bad version (not an instance ID, a ref or a tag): %q", v)
}
+// ValidateRoot returns an error if the string can't be used as an ensure-file
+// root.
+func ValidateRoot(root string) error {
+ if root == "" { // empty is fine
+ return nil
+ }
+ if strings.Contains(root, "\\") {
+ return fmt.Errorf(`bad root path: backslashes not allowed (use "/"): %q`, root)
+ }
+ if strings.Contains(root, ":") {
+ return fmt.Errorf(`bad root path: colons are not allowed: %q`, root)
+ }
+ if cleaned := path.Clean(root); cleaned != root {
+ return fmt.Errorf("bad root path: %q (should be %q)", root, cleaned)
+ }
+ if strings.HasPrefix(root, "./") || strings.HasPrefix(root, "../") || root == "." {
+ return fmt.Errorf(`bad root path: invalid ".": %q`, root)
+ }
+ if strings.HasPrefix(root, "/") {
+ return fmt.Errorf("bad root path: absolute paths not allowed: %q", root)
+ }
+ return nil
+}
+
// GetInstanceTagKey returns key portion of the instance tag or empty string.
func GetInstanceTagKey(t string) string {
chunks := strings.SplitN(t, ":", 2)
« no previous file with comments | « no previous file | cipd/client/cipd/common/common_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698