Repository navigation
mcpx(source): safely traverse directory fragments (#1727) #3744
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| //go:build !windows | ||
|
|
||
| package source_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "net" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "syscall" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/developing-today/code/pkgs/mcpx/internal/source" | ||
| ) | ||
|
|
||
| func TestDirectorySkipsFIFO(t *testing.T) { | ||
| if dir := os.Getenv("MCPX_SOURCE_FIFO_TEST_DIR"); dir != "" { | ||
| got, err := source.Resolve("@dir:"+dir, opts(dir)) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(got.Files) != 1 || filepath.Base(got.Files[0]) != "regular.ts" { | ||
| t.Fatalf("FIFO should be skipped while regular files are retained, got %v", got.Files) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| dir := t.TempDir() | ||
| if err := os.WriteFile(filepath.Join(dir, "regular.ts"), []byte("regular"), 0o644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := syscall.Mkfifo(filepath.Join(dir, "fifo.ts"), 0o600); err != nil { | ||
| t.Skipf("cannot create FIFO: %v", err) | ||
| } | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) | ||
| defer cancel() | ||
| cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestDirectorySkipsFIFO$") | ||
| cmd.Env = append(os.Environ(), "MCPX_SOURCE_FIFO_TEST_DIR="+dir) | ||
| output, err := cmd.CombinedOutput() | ||
| if ctx.Err() == context.DeadlineExceeded { | ||
| t.Fatalf("resolving a directory containing a FIFO did not finish within 3s") | ||
| } | ||
| if err != nil { | ||
| t.Fatalf("FIFO helper failed: %v\n%s", err, output) | ||
| } | ||
| } | ||
|
|
||
| func TestDirectorySkipsUnixSocket(t *testing.T) { | ||
| dir := t.TempDir() | ||
| regular := filepath.Join(dir, "regular.ts") | ||
| if err := os.WriteFile(regular, []byte("regular"), 0o644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| listener, err := net.Listen("unix", filepath.Join(dir, "socket.ts")) | ||
| if err != nil { | ||
| t.Skipf("cannot create Unix socket: %v", err) | ||
| } | ||
| defer listener.Close() | ||
|
|
||
| got, err := source.Resolve("@dir:"+dir, opts(dir)) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(got.Files) != 1 || got.Files[0] != regular { | ||
| t.Fatalf("socket should be skipped while regular files are retained, got %v", got.Files) | ||
| } | ||
| } | ||
|
|
||
| func TestDirectoryReportsInaccessibleSymlinkTarget(t *testing.T) { | ||
| if os.Geteuid() == 0 { | ||
| t.Skip("root can traverse directories without execute permission") | ||
| } | ||
|
|
||
| root := t.TempDir() | ||
| private := t.TempDir() | ||
| if err := os.WriteFile(filepath.Join(private, "secret.ts"), []byte("secret"), 0o644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := os.Symlink(filepath.Join(private, "secret.ts"), filepath.Join(root, "inaccessible.ts")); err != nil { | ||
| t.Skipf("symlinks are unavailable: %v", err) | ||
| } | ||
| if err := os.Chmod(private, 0); err != nil { | ||
| t.Skipf("cannot make target directory inaccessible: %v", err) | ||
| } | ||
| defer os.Chmod(private, 0o700) | ||
|
|
||
| _, err := source.Resolve("@dir:"+root, opts(root)) | ||
| if !errors.Is(err, os.ErrPermission) { | ||
| t.Fatalf("inaccessible symlink target should return a permission error, got %v", err) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With
Extensions: []string{".ts"}and nonrecursive traversal, a directory containingregular.tsand a self-referencingloop.txtsymlink now fails withELOOPinstead of reading the TypeScript file. The newos.Statruns before extension filtering and makes errors from excluded links fatal; inaccessible targets cause the same regression. This scenario succeeds against the parent commit and fails against this commit. Avoid rejecting excluded links whose targets do not need to be traversed.Useful? React with 👍 / 👎.