fix(safeio): retain writable Windows parent handles

This commit is contained in:
2026-08-18 09:05:42 +02:00
parent e7a7f4f066
commit 74b062f1a7
3 changed files with 41 additions and 25 deletions
@@ -16,15 +16,17 @@ func preflightPrivateDirectory(path string) (bool, error) {
handle, err := openWindowsRelativeComponent(parents.handles[len(parents.handles)-1], target, true, windows.GENERIC_READ)
if err != nil {
if isWindowsRelativeNotFound(err) {
writableParent, accessErr := openWindowsComponentWithAccess(
parents.directory,
true,
// A read-only retained handle cannot be upgraded. Re-traverse with the
// directory FILE_ADD_SUBDIRECTORY right, still using RootDirectory-relative
// opens for every component rather than reopening parents by absolute path.
writableParents, _, accessErr := openCanonicalWindowsParentWithFinalAccess(
path,
windows.FILE_APPEND_DATA, // FILE_ADD_SUBDIRECTORY for a directory handle
)
if accessErr != nil {
if accessErr != nil || writableParents == nil {
return false, ErrUnsafeFile
}
_ = windows.CloseHandle(writableParent)
writableParents.Close()
return false, nil
}
return false, ErrUnsafeFile