Skip to content

🛡️ Sentinel: [CRITICAL] Fix symlink vulnerability in atomic writes#76

Closed
himattm wants to merge 1 commit intomainfrom
sentinel-fix-atomic-write-symlink-8992919498063563158
Closed

🛡️ Sentinel: [CRITICAL] Fix symlink vulnerability in atomic writes#76
himattm wants to merge 1 commit intomainfrom
sentinel-fix-atomic-write-symlink-8992919498063563158

Conversation

@himattm
Copy link
Copy Markdown
Owner

@himattm himattm commented Apr 30, 2026

🚨 Severity: CRITICAL
💡 Vulnerability: Predictable temporary filenames (path + ".tmp") were used alongside os.WriteFile and os.Rename for atomic writes in shared directories (like os.TempDir()).
🎯 Impact: This created a race condition vulnerability to symlink attacks (CWE-377). An attacker could pre-create a symlink with the predictable name, causing the application to follow it and overwrite or corrupt an arbitrary file with the application's permissions.
🔧 Fix: Replaced the predictable string concatenation with os.CreateTemp(filepath.Dir(path), "prefix-*.tmp"). This relies on O_EXCL at the OS level to ensure the file is newly created. The code also now explicitly applies Chmod to match the expected 0644 permissions (since CreateTemp uses 0600), and importantly, Close()s the file descriptor before calling os.Rename() to prevent "file in use" errors on Windows. Finally, filepath.Dir(path) is used to guarantee the temporary file exists on the same filesystem mount to prevent EXDEV cross-device rename errors.
Verification: Verified by checking that os.CreateTemp is correctly implemented in internal/burnrate/burnrate.go and internal/sparkline/sparkline.go, tests passed successfully (go test ./...), and .jules/sentinel.md has been updated with the learning.


PR created automatically by Jules for task 8992919498063563158 started by @himattm

Fixed a critical security vulnerability where predictable temporary file
names (.tmp) were used for atomic file operations. By switching to
os.CreateTemp with a random suffix, this prevents a malicious actor from
orchestrating a symlink attack (CWE-377) during writes. Furthermore,
we explicitly close the file before os.Rename to ensure cross-platform
compatibility (Windows).

Co-authored-by: himattm <6266621+himattm@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses a predictable temporary file symlink vulnerability by replacing string-concatenated temporary paths with os.CreateTemp in the burnrate and sparkline packages. The review feedback identifies an ignored error from os.Rename in sparkline.go and recommends using f.Chmod on the open file descriptor instead of the file path to prevent potential TOCTOU race conditions.

return
}

os.Rename(tmp, path)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The error returned by os.Rename is currently ignored. If the rename operation fails (e.g., due to a file lock or permission issue), the temporary file will remain on disk and the data will not be persisted. The error should be handled, and the temporary file should be removed on failure to ensure a clean state.

	if err := os.Rename(tmp, path); err != nil {
		os.Remove(tmp)
	}

Comment on lines +80 to 87
if err := tmpFile.Close(); err != nil {
os.Remove(tmpPath)
return nil, false, err
}
if err := os.Chmod(tmpPath, 0644); err != nil {
os.Remove(tmpPath)
return nil, false, err
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-medium medium

To avoid a potential TOCTOU (Time-of-check to time-of-use) race condition, it is safer to use tmpFile.Chmod(0644) on the open file descriptor before closing it, rather than calling os.Chmod(tmpPath, 0644) on the file path after it has been closed. This ensures the permissions are applied to the exact file that was created, which is consistent with the security goals of this PR.

Suggested change
if err := tmpFile.Close(); err != nil {
os.Remove(tmpPath)
return nil, false, err
}
if err := os.Chmod(tmpPath, 0644); err != nil {
os.Remove(tmpPath)
return nil, false, err
}
if err := tmpFile.Chmod(0644); err != nil {
tmpFile.Close()
os.Remove(tmpPath)
return nil, false, err
}
if err := tmpFile.Close(); err != nil {
os.Remove(tmpPath)
return nil, false, err
}

Comment on lines +156 to 164
if err := tmpFile.Close(); err != nil {
os.Remove(tmp)
return
}

if err := os.Chmod(tmp, 0644); err != nil {
os.Remove(tmp)
return
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-medium medium

Similar to the improvement in burnrate.go, using tmpFile.Chmod(0644) before closing the file handle is more secure than calling os.Chmod on the path after closing, as it prevents potential manipulation of the file path between the two operations.

Suggested change
if err := tmpFile.Close(); err != nil {
os.Remove(tmp)
return
}
if err := os.Chmod(tmp, 0644); err != nil {
os.Remove(tmp)
return
}
if err := tmpFile.Chmod(0644); err != nil {
tmpFile.Close()
os.Remove(tmp)
return
}
if err := tmpFile.Close(); err != nil {
os.Remove(tmp)
return
}

@himattm
Copy link
Copy Markdown
Owner Author

himattm commented May 7, 2026

Closing as superseded by direct commit 42f3149 on main, which consolidated the burnrate/sparkline predictable-temp-file fixes and also preserved PR #82's updater temp-file hardening without the generated .jules note.

@himattm himattm closed this May 7, 2026
@himattm himattm deleted the sentinel-fix-atomic-write-symlink-8992919498063563158 branch May 7, 2026 15:35
@google-labs-jules
Copy link
Copy Markdown
Contributor

Closing as superseded by direct commit 42f3149 on main, which consolidated the burnrate/sparkline predictable-temp-file fixes and also preserved PR #82's updater temp-file hardening without the generated .jules note.

Understood. Acknowledging that this work is now obsolete and stopping work on this task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant