Repository navigation
Conversation
A path on a drive mapped to a network share resolves (GetFinalPathNameByHandle) to \\server\share form, which the #1000 check refused as UNC. Resolve the drive's root through the same resolution and admit the path only when it stays on that share, its tail passes the Windows shape rules, and it is not on a volume holding this machine's system directories (by volume serial), so a loopback mapping such as \\localhost\C$ stays refused. Fixes #1107
scttfrdmn
marked this pull request as ready for review
October 18, 2026 05:56
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Merged
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #1107
The bug
ValidatePathForReadingresolves an existing path withGetFinalPathNameByHandle(#1036) so that a junction or symlink can't lead into a system directory. On a mapped network drive,Z:\data\xresolves to\\?\UNC\server\share\data\x, andnormaliseFinalPathturns that into\\server\share\data\x. The resolved-path shape check refuses every UNC form, so every existing path on a mapped drive was refused. It failed closed: nothing unsafe got through, it just refused legitimate paths.(The issue calls this an "allowed root" mismatch. In this code the roots are a denylist,
forbiddenReadRoots. The refusal actually comes fromwindowsPathShapeError(resolved)rejecting UNC. The fix follows the issue's direction anyway.)The change
When the resolved path is refused for its shape,
mappedDriveError(path.go, plain string handling, called withresolveExistingandonSystemVolume) decides whether it is a path on a mapped drive. It resolves the drive's root (Z:\) through the sameresolveExisting, so both sides are in\\server\shareform. It admits the path only if all of these hold:absis on a drive letter, and that drive's root resolves to a plain\\server\share[\...]. It must not be\\?\or\\.\, and it must not be a drive form: asubstdrive resolves to the local directory it names, and that keeps the existing comparison.windowsPathShapeError(no streams, device names, or trailing dot/space).onSystemVolume(resolved)is false. This reads the volume serial of the longest existing prefix (GetFileInformationByHandle) and compares it with the serials of the existing forbidden roots. It is true, or an error, so the path is refused, when they match or when no system directory's serial can be read.Nothing is translated back to the drive letter. The resolved path is checked as the place it actually reaches. The existing
underAnyRoot(resolved, roots)check still runs afterwards.Security reasoning
C:\is not UNC). A link on a mapped drive to a different share or folder fails rule 2.subst W: C:\Windowsresolves toC:\Windows\...and is refused by the existing root comparison, because nothing maps it back toW:. A drive mapped to a loopback share is the case the prefix comparison can't see:\\localhost\C$, a share ofC:\Windows, or a share containing a junction into it (the server follows the junction, so the client's final path still names the share). All of these were refused before because they are UNC. Rule 3 keeps them refused: the server reports the serial of the volume the file is really on, so they match the local system volume however they are spelled.C:, and a share on another machine cloned from the same image (the same AMI gives the same serial). Both were refused before this change, so nothing regresses. The CHANGELOG says so.Tests
TestMappedDriveErrorandTestIsPlainUNC(path_test.go, every OS): table tests with a fake resolver and a fake volume check. Admitted: a file on the share, the share root, a case-different spelling, a drive mapped to a sub-folder, a lower-case drive letter. Refused: links to another share, to a sibling share, or out of the mapped folder; a local link to a share; subst, volume-GUID, device and malformed roots; an unresolvable drive; a non-drive path; ADS, device name or trailing dot after the share; on a system volume; volume check error. One more check asserts that the volume check is asked about the resolved path, not the drive letter.TestValidatePathForReading_WindowsMappedNetworkDrive(Windows, real fixture). It attaches a fresh 64 MB NTFS VHD withdiskpart, so the share is not on the system volume. It shares a folder withNew-SmbShareand maps a drive withnet useto\\localhost\<share>. It first asserts thatfinalPathNamereally returns a\\server\sharepath, so the fixture has the Windows: path validators refuse paths on a mapped network drive #1107 shape. Admitted: a file, the root, a new file, and a glob on the drive. Refused: a junction inside the share into%SystemRoot%, both as the local path and through the drive. A second drive mapped to a loopback share of%SystemRoot%is refused too. UnderGITHUB_ACTIONSan unbuildable fixture fails the test. Elsewhere (a non-elevated dev shell) it skips and names the step that needed elevation.TestValidatePathForReading_WindowsSubstDrive(Windows, no elevation needed): a subst drive for a temp dir is admitted, and a subst drive for%SystemRoot%is refused. This is a guard, not a reproduction.substdoes not reproduce Windows: path validators refuse paths on a mapped network drive #1107, because GetFinalPathNameByHandle names the local directory and not a UNC path. The test passes on the old code too. It is there to stop a future fix from "mapping the drive back", which would admitsubst W: C:\Windows.Verified
Locally (macOS):
go test -exec $(pwd)/bin/hermetic-exec ./pkg/security/: pass.go vet ./pkg/security/for linux, darwin and windows: clean.GOOS=windows go test -c -o /dev/null ./pkg/security/: compiles.golangci-lint run ./pkg/security/(native andGOOS=windows): 0 issues.go test -run 'APISurface|PublicSurface|Changelog|Windows' .: pass. pkg/security is not inscripts/windows-test-skips.txt.make check: everything passes except the known Docker-onlyTestGenerateWrapper_ChownFixesCrossUIDStickyUnlink_Docker(no Docker daemon here). Because that failure stopped make, the nested-module step did not run. This PR touches no nested module.mappedDriveErrorrule was watched failing against a compiling mutation: always refuse; no plain-UNC check; no containment check; no tail shape check;if false && sys; volume asked aboutabs; volume error ignored;isPlainUNCaccepting\\?\.On Windows CI only (I could not run this locally):
TestValidatePathForReading_WindowsMappedNetworkDrive, with "UNC and device-namespace paths ... are not allowed". The fixture built (diskpart, New-SmbShare and net use all succeeded), the refused rows passed, and the subst test passed. With the fix restored, Test (Windows) passes.RUNNER~1). The test now uses the long path, and a failed detach is reported as a test error.