NormalizeAndValidate normalizes and validates the given path. This calls Normalize on the path. Returns Error if the path is not relative or jumps context. This can be used to validate that paths are valid to use with Buckets. The error message is safe to pass to users.
(path string)
| 30 | // This can be used to validate that paths are valid to use with Buckets. |
| 31 | // The error message is safe to pass to users. |
| 32 | func NormalizeAndValidate(path string) (string, error) { |
| 33 | normalizedPath := Normalize(path) |
| 34 | if filepath.IsAbs(normalizedPath) || (len(normalizedPath) > 0 && normalizedPath[0] == '/') { |
| 35 | // the stdlib implementation of `IsAbs` assumes that a volume name is required for a path to |
| 36 | // be absolute, however Windows treats a `/` (normalized) rooted path as absolute _within_ the current volume. |
| 37 | // In the context of validating that a path is _not_ relative, we need to reject a path that begins |
| 38 | // with `/`. |
| 39 | return "", NewError(path, errNotRelative) |
| 40 | } |
| 41 | // https://github.com/bufbuild/buf/issues/51 |
| 42 | if strings.HasPrefix(normalizedPath, normalizedRelPathJumpContextPrefix) || |
| 43 | normalizedPath == normalizedRelPathJumpContextPath { |
| 44 | return "", NewError(path, errOutsideContextDir) |
| 45 | } |
| 46 | return normalizedPath, nil |
| 47 | } |
| 48 | |
| 49 | // EqualsOrContainsPath returns true if the value is equal to or contains the path. |
| 50 | // path is compared at each directory level to value for equivalency under simple unicode |
no test coverage detected
searching dependent graphs…