Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix normalization check for Rlocation path #3378

Merged
merged 1 commit into from
Dec 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions go/runfiles/runfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ func (r *Runfiles) Rlocation(path string) (string, error) {
if path == "" {
return "", errors.New("runfiles: path may not be empty")
}
if !isNormalizedPath(path) {
return "", fmt.Errorf("runfiles: path %q is not normalized", path)
if err := isNormalizedPath(path); err != nil {
return "", err
}

// See https://github.com/bazelbuild/bazel/commit/b961b0ad6cc2578b98d0a307581e23e73392ad02
Expand All @@ -137,10 +137,17 @@ func (r *Runfiles) Rlocation(path string) (string, error) {
return p, nil
}

func isNormalizedPath(s string) bool {
return !strings.HasPrefix(s, "../") && !strings.Contains(s, "/..") &&
!strings.HasPrefix(s, "./") && !strings.HasSuffix(s, "/.") &&
!strings.Contains(s, "/./") && !strings.Contains(s, "//")
func isNormalizedPath(s string) error {
if strings.HasPrefix(s, "../") || strings.Contains(s, "/../") || strings.HasSuffix(s, "/..") {
return fmt.Errorf(`runfiles: path %q must not contain ".." segments`, s)
}
if strings.HasPrefix(s, "./") || strings.Contains(s, "/./") || strings.HasSuffix(s, "/.") {
return fmt.Errorf(`runfiles: path %q must not contain "." segments`, s)
}
if strings.Contains(s, "//") {
return fmt.Errorf(`runfiles: path %q must not contain "//"`, s)
}
return nil
}

// Env returns additional environmental variables to pass to subprocesses.
Expand Down
7 changes: 7 additions & 0 deletions tests/runfiles/runfiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ func TestPath_errors(t *testing.T) {
}
})
}
for _, s := range []string{"foo/..bar", "foo/.bar"} {
t.Run(s, func(t *testing.T) {
if _, err := r.Rlocation(s); err != nil && !os.IsNotExist(err.(runfiles.Error).Err) {
t.Errorf("got %q, want none or 'file not found' error", err)
}
})
}
}

func TestRunfiles_zero(t *testing.T) {
Expand Down