Repository navigation
fix(tools): keep a file's access control when an overwrite replaces it on Windows #1241
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f6b4ec0
fix(tools): keep a file's access control when an overwrite replaces i…
Vasanthdev2004 d966da4
fix(tools): give the side file the replaced file's access control bef…
Vasanthdev2004 533dc3b
fix(tools): create an overwrite's side file private on Windows
Vasanthdev2004 282a908
test(tools): compare the side file's list as it is printed
Vasanthdev2004 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| //go:build !windows | ||
|
|
||
| package tools | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/Twigpine/zero/internal/pathjail" | ||
| ) | ||
|
|
||
| // fileSecurity is the access control an overwrite carries from the file it | ||
| // replaces. Only Windows has anything to carry here: elsewhere the permission | ||
| // bits are the access control this package preserves, and the staged file is | ||
| // given them directly. | ||
| type fileSecurity struct{} | ||
|
|
||
| func captureFileSecurity(*os.File) (*fileSecurity, error) { return nil, nil } | ||
|
|
||
| func applyFileSecurity(*os.File, *fileSecurity) error { return nil } | ||
|
|
||
| // createSideFile creates the side file an overwrite or a create stages its | ||
| // bytes in. The mode is the kernel's exclusive-create mode, so an overwrite's | ||
| // side file is owner-only from the moment it exists. | ||
| func createSideFile(root *os.Root, relativePath string, mode os.FileMode, _ *fileSecurity) (*os.File, string, error) { | ||
| return pathjail.CreateTempMode(root, filepath.Dir(relativePath), "zero-write", ".tmp", mode) | ||
| } |
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,119 @@ | ||
| package tools | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func installFileWriteSecurity(t *testing.T, capture func(*os.File) (*fileSecurity, error), apply func(*os.File, *fileSecurity) error) { | ||
| t.Helper() | ||
| priorCapture, priorApply := fileWriteCaptureSecurity, fileWriteApplySecurity | ||
| if capture != nil { | ||
| fileWriteCaptureSecurity = capture | ||
| } | ||
| if apply != nil { | ||
| fileWriteApplySecurity = apply | ||
| } | ||
| t.Cleanup(func() { fileWriteCaptureSecurity, fileWriteApplySecurity = priorCapture, priorApply }) | ||
| } | ||
|
|
||
| // AN OVERWRITE THAT CANNOT CARRY THE FILE'S ACCESS CONTROL DOES NOT HAPPEN. | ||
| // | ||
| // The overwrite replaces the file with a freshly created one, and a fresh file | ||
| // has whatever the directory gives it. If the original's access control cannot | ||
| // be read, or cannot be put on the staged file, publishing anyway would quietly | ||
| // widen who can read a file its owner had locked down. Both failures therefore | ||
| // refuse the write, leave the observed bytes standing, and remove the side | ||
| // file. These run on every platform: the seams are the same everywhere, and | ||
| // only what the production functions do behind them differs. | ||
| func TestOverwriteRefusesWhenAccessControlCannotBeCarried(t *testing.T) { | ||
| failure := errors.New("injected access control failure") | ||
| for _, testCase := range []struct { | ||
| name string | ||
| capture func(*os.File) (*fileSecurity, error) | ||
| apply func(*os.File, *fileSecurity) error | ||
| }{ | ||
| {name: "reading it fails", capture: func(*os.File) (*fileSecurity, error) { return nil, failure }}, | ||
| {name: "applying it fails", apply: func(*os.File, *fileSecurity) error { return failure }}, | ||
| } { | ||
| t.Run(testCase.name, func(t *testing.T) { | ||
| root := t.TempDir() | ||
| target := filepath.Join(root, "existing.txt") | ||
| if err := os.WriteFile(target, []byte("observed\n"), 0o644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| workspace, err := os.OpenRoot(root) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer workspace.Close() | ||
| info, err := os.Stat(target) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| preimage := "observed\n" | ||
| installFileWriteSecurity(t, testCase.capture, testCase.apply) | ||
|
|
||
| err = commitRootedFileContents(workspace, target, "existing.txt", info, &preimage, "replacement\n") | ||
| if !errors.Is(err, failure) { | ||
| t.Fatalf("commit error = %v, want the injected access control failure", err) | ||
| } | ||
| got, readErr := os.ReadFile(target) | ||
| if readErr != nil || string(got) != "observed\n" { | ||
| t.Fatalf("file = %q, err=%v; the observed bytes must outlive a refused overwrite", got, readErr) | ||
| } | ||
| entries, err := os.ReadDir(root) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(entries) != 1 { | ||
| names := make([]string, 0, len(entries)) | ||
| for _, entry := range entries { | ||
| names = append(names, entry.Name()) | ||
| } | ||
| t.Fatalf("directory holds %s; a refused overwrite must remove its side file", strings.Join(names, ", ")) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // THE CARRY IS FOR THE FILE THAT IS REPLACED, AND ONLY THAT ONE. | ||
| // | ||
| // A create has no original to take access control from and keeps what the | ||
| // directory gives a new file, so it must hand the side file nothing. An | ||
| // overwrite hands it exactly what was read from the observed file. The | ||
| // assertion on the overwrite also proves the seam was reached, so the create | ||
| // half cannot pass by the seam never being called. | ||
| func TestOnlyAnOverwriteCarriesAccessControl(t *testing.T) { | ||
| root := t.TempDir() | ||
| read := &fileSecurity{} | ||
| var applied []*fileSecurity | ||
| installFileWriteSecurity(t, | ||
| func(*os.File) (*fileSecurity, error) { return read, nil }, | ||
| func(_ *os.File, security *fileSecurity) error { | ||
| applied = append(applied, security) | ||
| return nil | ||
| }, | ||
| ) | ||
| tool := NewScopedWriteFileTool(root, nil) | ||
|
|
||
| created := tool.Run(context.Background(), map[string]any{"path": "note.txt", "content": "first\n"}) | ||
| if created.Status != StatusOK { | ||
| t.Fatalf("create = %s: %s", created.Status, created.Output) | ||
| } | ||
| if len(applied) != 1 || applied[0] != nil { | ||
| t.Fatalf("a create handed its side file %v; it has no original to carry access control from", applied) | ||
| } | ||
|
|
||
| overwrote := tool.Run(context.Background(), map[string]any{"path": "note.txt", "content": "second\n", "overwrite": true}) | ||
| if overwrote.Status != StatusOK { | ||
| t.Fatalf("overwrite = %s: %s", overwrote.Status, overwrote.Output) | ||
| } | ||
| if len(applied) != 2 || applied[1] != read { | ||
| t.Fatalf("an overwrite handed its side file %v, want what was read from the file it replaces", applied) | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.