Repository navigation
fix(log): create log files with 0600 permissions #2297
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
Open
WitteShadovv
wants to merge
1
commit into
safing:development
Choose a base branch
from
WitteShadovv:fix/log-file-permissions
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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,93 @@ | ||
| package log | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "runtime" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| // TestNewFileWriterPermissions checks that log files are only accessible by | ||
| // their owner, even if the log directory already exists with broader | ||
| // permissions. | ||
| func TestNewFileWriterPermissions(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| if runtime.GOOS == "windows" { | ||
| t.Skip("unix file permissions do not apply on Windows") | ||
| } | ||
|
|
||
| // Simulate a log directory that already exists and is readable by others. | ||
| dir := t.TempDir() | ||
| if err := os.Chmod(dir, 0o755); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| writer, err := NewFileWriter(dir) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer writer.Close() | ||
|
|
||
| entries, err := os.ReadDir(dir) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(entries) != 1 { | ||
| t.Fatalf("expected one log file, got %d", len(entries)) | ||
| } | ||
| info, err := entries[0].Info() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if perm := info.Mode().Perm(); perm != 0o600 { | ||
| t.Errorf("log file is created with permissions %o, expected 600", perm) | ||
| } | ||
| } | ||
|
|
||
| // TestNewFileWriterExistingFile checks that an existing log file, which is | ||
| // reused when the writer starts within the same second, is restricted too. | ||
| func TestNewFileWriterExistingFile(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| if runtime.GOOS == "windows" { | ||
| t.Skip("unix file permissions do not apply on Windows") | ||
| } | ||
|
|
||
| // Create readable log files for the next seconds, so that the writer | ||
| // reuses one of them. | ||
| dir := t.TempDir() | ||
| now := time.Now().UTC() | ||
| for i := range 3 { | ||
| logFile := now.Add(time.Duration(i)*time.Second).Format("2006-01-02_15-04-05") + ".log" | ||
| path := filepath.Join(dir, logFile) | ||
| if err := os.WriteFile(path, nil, 0o644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := os.Chmod(path, 0o644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| writer, err := NewFileWriter(dir) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer writer.Close() | ||
|
|
||
| entries, err := os.ReadDir(dir) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(entries) != 3 { | ||
| t.Fatalf("expected the writer to reuse an existing log file, got %d files", len(entries)) | ||
| } | ||
| info, err := os.Stat(writer.file.Name()) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if perm := info.Mode().Perm(); perm != 0o600 { | ||
| t.Errorf("existing log file has permissions %o, expected 600", perm) | ||
| } | ||
| } |
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.
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.
🔒 Security & Privacy | 🟠 Major | ⚡ Quick win
Restrict permissions when the log file already exists.
If
logFilealready exists with mode0644,os.OpenFiletruncates it but does not apply the0600mode argument. A second call within the same second can reuse the filename, so the returned log file can remain readable by other local users. Ensure an existing file is also restricted before writing, and test that case. Based on learnings, runtime-created files that can contain sensitive data need verified restrictive permissions.🤖 Prompt for AI Agents
Source: Learnings