-
-
Notifications
You must be signed in to change notification settings - Fork 312
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
Fix file permissions on windows #1758
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -6,6 +6,8 @@ import ( | |||||||||||||||||||||||||
"io/fs" | ||||||||||||||||||||||||||
"os" | ||||||||||||||||||||||||||
"runtime" | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
"github.com/hectane/go-acl" | ||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
const isWindows = runtime.GOOS == "windows" | ||||||||||||||||||||||||||
|
@@ -20,8 +22,7 @@ func EnsureDirectory(path string, perm os.FileMode) error { | |||||||||||||||||||||||||
if f.IsDir() { | ||||||||||||||||||||||||||
// directory exists, check permissions | ||||||||||||||||||||||||||
if isWindows { | ||||||||||||||||||||||||||
// TODO: set correct permission on windows | ||||||||||||||||||||||||||
// acl.Chmod(path, perm) | ||||||||||||||||||||||||||
return acl.Chmod(path, perm) | ||||||||||||||||||||||||||
} else if f.Mode().Perm() != perm { | ||||||||||||||||||||||||||
return os.Chmod(path, perm) | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix potential nil pointer dereference in permission check The permission check in the else condition could cause a panic when the file doesn't exist, as Apply this fix: if isWindows {
return acl.Chmod(path, perm)
-} else if f.Mode().Perm() != perm {
+} else if f != nil && f.Mode().Perm() != perm {
return os.Chmod(path, perm)
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||
|
@@ -38,7 +39,11 @@ func EnsureDirectory(path string, perm os.FileMode) error { | |||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||
return fmt.Errorf("could not create dir %s: %w", path, err) | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
return os.Chmod(path, perm) | ||||||||||||||||||||||||||
if isWindows { | ||||||||||||||||||||||||||
return acl.Chmod(path, perm) | ||||||||||||||||||||||||||
} else if f.Mode().Perm() != perm { | ||||||||||||||||||||||||||
return os.Chmod(path, perm) | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix unreachable and potentially dangerous code There are several issues in this section:
Apply this fix: if isWindows {
return acl.Chmod(path, perm)
-} else if f.Mode().Perm() != perm {
- return os.Chmod(path, perm)
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
// other error opening path | ||||||||||||||||||||||||||
return fmt.Errorf("failed to access %s: %w", path, err) | ||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |||||||||||||
"runtime" | ||||||||||||||
"strings" | ||||||||||||||
|
||||||||||||||
"github.com/hectane/go-acl" | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unused import The - "github.com/hectane/go-acl" 📝 Committable suggestion
Suggested change
🧰 Tools🪛 GitHub Check: Linter[failure] 11-11: [failure] 11-11: |
||||||||||||||
"github.com/safing/portmaster/base/log" | ||||||||||||||
"github.com/safing/portmaster/base/updater" | ||||||||||||||
) | ||||||||||||||
|
@@ -46,11 +47,16 @@ func EnsureChromeSandboxPermissions(reg *updater.ResourceRegistry) error { | |||||||||||||
filepath.Ext(pmElectronUpdate.Path()), | ||||||||||||||
) | ||||||||||||||
sandboxFile := filepath.Join(unpackedPath, "chrome-sandbox") | ||||||||||||||
if err := os.Chmod(sandboxFile, 0o0755|os.ModeSetuid); err != nil { | ||||||||||||||
if runtime.GOOS == "windows" { | ||||||||||||||
err = acl.Chmod(sandboxFile, 0o0755|os.ModeSetuid) | ||||||||||||||
} else { | ||||||||||||||
err = os.Chmod(sandboxFile, 0o0755|os.ModeSetuid) | ||||||||||||||
} | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Inconsistent OS handling and unnecessary Windows logic There are several issues with the Windows-specific changes:
Consider removing the Windows-specific code since:
- if runtime.GOOS == "windows" {
- err = acl.Chmod(sandboxFile, 0o0755|os.ModeSetuid)
- } else {
- err = os.Chmod(sandboxFile, 0o0755|os.ModeSetuid)
- }
+ err = os.Chmod(sandboxFile, 0o0755|os.ModeSetuid) 📝 Committable suggestion
Suggested change
|
||||||||||||||
if err != nil { | ||||||||||||||
log.Errorf(suidBitWarning, 0o0755|os.ModeSetuid, sandboxFile) | ||||||||||||||
|
||||||||||||||
return fmt.Errorf("failed to chmod: %w", err) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
log.Debugf("updates: fixed SUID permission for chrome-sandbox") | ||||||||||||||
|
||||||||||||||
return nil | ||||||||||||||
|
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.
Use filepath.Join instead of path.Join for cross-platform compatibility.
The
path
package is designed for slash-separated paths (like URLs), whilefilepath
is designed for OS-specific path manipulation. On Windows, usingpath.Join
could lead to incorrect path separators.💡 Codebase verification
🛠️ Refactor suggestion
The review comment is correct. The temporary file path is incorrectly constructed.
Based on the code analysis:
renameio.TempFile
creates a temporary file with pattern.{basename}
in the directory determined bytempDir
functiontempDir
function either uses the caller-specified directory or falls back to the destination file's directoryTempDir/dest
, but it's actually at a path returned bytmpFile.Name()
The suggested fix to use
tmpFile.Name()
is the correct approach as it will use the actual temporary file path rather than an incorrect assumption about its location.🔗 Analysis chain
Verify the temporary file path construction.
The code attempts to chmod the file at
TempDir/dest
, butrenameio.TempFile
might use a different path pattern for the temporary file. This could result in permission changes being applied to the wrong file.Additionally, consider refactoring to use the actual temporary file path:
Let me check how the
tempDir
function constructs the temporary directory path.Let me check the complete implementation of
tempDir
to understand how it determines the final temporary directory path.Let me check how the
TempFile
is being used in the atomic.go file to understand the complete context.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 762
Script:
Length of output: 411
Script:
Length of output: 1109
Script:
Length of output: 807