Skip to content

Commit

Permalink
fix: normalize and enforce stdin paths as well
Browse files Browse the repository at this point in the history
See #440 for the last attempt to
do this because (at the time) this "is the file in the tree root?" check
relied upon checking for concrete files in the filesystem. As of
ff3de21,
that's longer the case, so we can make this code a bit more consistent.
  • Loading branch information
jfly committed Oct 12, 2024
1 parent ff3de21 commit 9ce6522
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
38 changes: 19 additions & 19 deletions cmd/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,31 +168,31 @@ func Run(v *viper.Viper, statz *stats.Stats, cmd *cobra.Command, paths []string)
return fmt.Errorf("invalid walk type: %w", err)
}

if walkType == walk.Stdin {
if walkType == walk.Stdin && len(paths) != 1 {
// check we have only received one path arg which we use for the file extension / matching to formatters
if len(paths) != 1 {
return fmt.Errorf("exactly one path should be specified when using the --stdin flag")
return fmt.Errorf("exactly one path should be specified when using the --stdin flag")
}

// checks all paths are contained within the tree root and exist
// also "normalize" paths so they're relative to cfg.TreeRoot
for i, path := range paths {
absolutePath, err := filepath.Abs(path)
if err != nil {
return fmt.Errorf("error computing absolute path of %s: %w", path, err)
}
} else {
// checks all paths are contained within the tree root and exist
// also "normalize" paths so they're relative to cfg.TreeRoot
for i, path := range paths {
absolutePath, err := filepath.Abs(path)
if err != nil {
return fmt.Errorf("error computing absolute path of %s: %w", path, err)
}

relativePath, err := filepath.Rel(cfg.TreeRoot, absolutePath)
if err != nil {
return fmt.Errorf("error computing relative path from %s to %s: %s", cfg.TreeRoot, absolutePath, err)
}
relativePath, err := filepath.Rel(cfg.TreeRoot, absolutePath)
if err != nil {
return fmt.Errorf("error computing relative path from %s to %s: %s", cfg.TreeRoot, absolutePath, err)
}

if strings.HasPrefix(relativePath, "..") {
return fmt.Errorf("path %s not inside the tree root %s", path, cfg.TreeRoot)
}
if strings.HasPrefix(relativePath, "..") {
return fmt.Errorf("path %s not inside the tree root %s", path, cfg.TreeRoot)
}

paths[i] = relativePath
paths[i] = relativePath

if walkType != walk.Stdin {
if _, err = os.Stat(absolutePath); err != nil {
return fmt.Errorf("path %s not found", path)
}
Expand Down
8 changes: 8 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,14 @@ func TestStdin(t *testing.T) {
as.Equal(`{ ...}: "hello"
`, string(out))

// try a file that's outside of the project root
contents = `{ foo, ... }: "hello"`
os.Stdin = test.TempFile(t, "", "stdin", &contents)

out, _, err = treefmt(t, "-C", tempDir, "--allow-missing-formatter", "--stdin", "../test.nix")
as.Errorf(err, "path ../test.nix not inside the tree root %s", tempDir)
as.Equal("", string(out))

// try some markdown instead
contents = `
| col1 | col2 |
Expand Down

0 comments on commit 9ce6522

Please sign in to comment.