Skip to content

Comments

pkg/atomicwriter: disallow symlinks for now, add more tests and touch-up GoDoc#49741

Merged
thaJeztah merged 5 commits intomoby:masterfrom
thaJeztah:atomicwriter_stricter_validate
Apr 4, 2025
Merged

pkg/atomicwriter: disallow symlinks for now, add more tests and touch-up GoDoc#49741
thaJeztah merged 5 commits intomoby:masterfrom
thaJeztah:atomicwriter_stricter_validate

Conversation

@thaJeztah
Copy link
Member


pkg/atomicwriter: disallow symlinked files for now

The implementation uses "os.Rename" to move the temporary file to
the destination, which does not follow symlinks, and because of this
would replace a symlink with a file.

We can consider adding support for symlinked files in future, so that
WriteFile can be used as a drop-in replacement for os.WriteFile()
but in the meantime, let's produce an error so that nobody can depend
on this.

pkg/atomicwriter: error on unknown file-modes

Previously, we were silently discarding this situation and hoping that
it would work; let's produce an error instead (we can add additional
filemodes when they arrive and if we need them)

pkg/atomicwriter: add test for parent dir not being a directory

While the target-file does not have to exist, its parent must, and must
be a directory. This adds a test-case to verify the behavior if the
parent is not a directory.

pkg/atomicwriter: return early if parent directory is invalid

Rewrite validateDestination to first check if the destination path
exists. This slightly simplifies the logic (allowing returning
early in each step of the validation) and slightly improves the
error produced.

Before this, the error confusingly would mention the full path
not being a directory. While this does match what os.Writefile
would return, it's .. confusing:

failed to stat output path: lstat ./not-a-dir/new-file.txt: not a directory

After this, the error would mention the directory that doesn't exist:

invalid output path: stat ./not-a-dir: not a directory

A slight optimization is made as well, now checking for both "."
and ".." as special case, as either path should exist given any current
working directory (unless the working directory has been deleted, but we'd
fail further down the line).

With this change in order, we can also merge validateFileMode into
validateDestination.

- Human readable description for the release notes

- A picture of a cute animal (not mandatory but encouraged)

The implementation uses "os.Rename" to move the temporary file to
the destination, which does not follow symlinks, and because of this
would replace a symlink with a file.

We can consider adding support for symlinked files in future, so that
WriteFile can be used as a drop-in replacement for `os.WriteFile()`
but in the meantime, let's produce an error so that nobody can depend
on this.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Previously, we were silently discarding this situation and hoping that
it would work; let's produce an error instead (we can add additional
filemodes when they arrive and if we need them)

Signed-off-by: Sebastiaan van Stijn <[email protected]>
While the target-file does not have to exist, its parent must, and must
be a directory. This adds a test-case to verify the behavior if the
parent is not a directory.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Rewrite `validateDestination` to first check if the destination path
exists. This slightly simplifies the logic (allowing returning
early in each step of the validation) and slightly improves the
error produced.

Before this, the error confusingly would mention the full path
not being a directory. While this _does_ match what `os.Writefile`
would return, it's .. confusing:

    failed to stat output path: lstat ./not-a-dir/new-file.txt: not a directory

After this, the error would mention the directory that doesn't exist:

    invalid output path: stat ./not-a-dir: not a directory

A slight optimization is made as well, now checking for _both_ "."
and ".." as special case, as either path should exist given any current
working directory (unless the working directory has been deleted, but we'd
fail further down the line).

With this change in order, we can also merge `validateFileMode` into
`validateDestination`.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah thaJeztah added status/2-code-review kind/enhancement Enhancements are not bugs or new features but can improve usability or performance. labels Apr 3, 2025
@thaJeztah thaJeztah added this to the 28.0.5 milestone Apr 3, 2025
@thaJeztah thaJeztah requested review from cpuguy83 and dmcgowan April 3, 2025 20:00
@thaJeztah thaJeztah merged commit d7b743b into moby:master Apr 4, 2025
154 checks passed
@thaJeztah thaJeztah deleted the atomicwriter_stricter_validate branch April 4, 2025 18:11
@thompson-shaun thompson-shaun modified the milestones: 28.0.5, 28.1.0 Apr 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement Enhancements are not bugs or new features but can improve usability or performance. status/2-code-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants