Remove unnecessary pass in validatePath.
Perform a single pass over pathComponents in both validatePath and validateSafePath instead. Test: presubmits Change-Id: I1499183e345ca180900c54a18f9dc8bd534f2588
This commit is contained in:
@@ -1868,10 +1868,14 @@ func (p InstallPaths) Strings() []string {
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
// validateSafePath validates a path that we trust (may contain ninja variables).
|
// validatePathInternal ensures that a path does not leave its component, and
|
||||||
// Ensures that each path component does not attempt to leave its component.
|
// optionally doesn't contain Ninja variables.
|
||||||
func validateSafePath(pathComponents ...string) (string, error) {
|
func validatePathInternal(allowNinjaVariables bool, pathComponents ...string) (string, error) {
|
||||||
for _, path := range pathComponents {
|
for _, path := range pathComponents {
|
||||||
|
if !allowNinjaVariables && strings.Contains(path, "$") {
|
||||||
|
return "", fmt.Errorf("Path contains invalid character($): %s", path)
|
||||||
|
}
|
||||||
|
|
||||||
path := filepath.Clean(path)
|
path := filepath.Clean(path)
|
||||||
if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") {
|
if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") {
|
||||||
return "", fmt.Errorf("Path is outside directory: %s", path)
|
return "", fmt.Errorf("Path is outside directory: %s", path)
|
||||||
@@ -1883,16 +1887,18 @@ func validateSafePath(pathComponents ...string) (string, error) {
|
|||||||
return filepath.Join(pathComponents...), nil
|
return filepath.Join(pathComponents...), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// validateSafePath validates a path that we trust (may contain ninja
|
||||||
|
// variables). Ensures that each path component does not attempt to leave its
|
||||||
|
// component. Returns a joined version of each path component.
|
||||||
|
func validateSafePath(pathComponents ...string) (string, error) {
|
||||||
|
return validatePathInternal(true, pathComponents...)
|
||||||
|
}
|
||||||
|
|
||||||
// validatePath validates that a path does not include ninja variables, and that
|
// validatePath validates that a path does not include ninja variables, and that
|
||||||
// each path component does not attempt to leave its component. Returns a joined
|
// each path component does not attempt to leave its component. Returns a joined
|
||||||
// version of each path component.
|
// version of each path component.
|
||||||
func validatePath(pathComponents ...string) (string, error) {
|
func validatePath(pathComponents ...string) (string, error) {
|
||||||
for _, path := range pathComponents {
|
return validatePathInternal(false, pathComponents...)
|
||||||
if strings.Contains(path, "$") {
|
|
||||||
return "", fmt.Errorf("Path contains invalid character($): %s", path)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return validateSafePath(pathComponents...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func PathForPhony(ctx PathContext, phony string) WritablePath {
|
func PathForPhony(ctx PathContext, phony string) WritablePath {
|
||||||
|
Reference in New Issue
Block a user