Merged
Conversation
The Bug
The tokenPath() function in internal/oauth/oauth.go:301-317 used strings.HasPrefix to verify paths stayed within tokensDir, but
didn't detect symlinks. An attacker who could create a symlink inside tokensDir (e.g., tokensDir/evil.json -> /etc/passwd) could
cause token data to be written outside the tokens directory.
The Fix
Added symlink detection using os.Lstat() and filepath.EvalSymlinks():
// Check if path is a symlink that could escape tokensDir
if info, err := os.Lstat(cleanPath); err == nil && info.Mode()&os.ModeSymlink != 0 {
// Path exists and is a symlink - resolve it and verify it stays within tokensDir
resolved, err := filepath.EvalSymlinks(cleanPath)
if err != nil || !isPathWithinDir(resolved, cleanTokensDir) {
// Symlink resolution failed or escapes tokensDir - use hash-based fallback
return filepath.Join(m.tokensDir, fmt.Sprintf("%x.json", sha256.Sum256([]byte(email))))
}
}
Also added a helper function isPathWithinDir() that properly resolves symlinks in the base directory before comparing paths.
Changes Made
- internal/oauth/oauth.go: Added symlink detection in tokenPath() and new isPathWithinDir() helper (13 lines added)
- internal/oauth/oauth_test.go: Added TestTokenPath_SymlinkEscape test case (47 lines added)
- Fix incomplete path prefix check: Replace strings.HasPrefix with hasPathPrefix() that properly handles directory separators, preventing attacks where tokensDir-evil would pass the check for tokensDir - Add hasPathPrefix() helper for safe directory prefix checking without symlink resolution, keeping isPathWithinDir() for resolved symlink checks - Document TOCTOU limitation in symlink check with explanation of the low practical risk due to required attacker capabilities - Improve test to verify exact hash-based fallback path format Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
wesm
approved these changes
Feb 5, 2026
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.
Fix for a security attack
The tokenPath() function in internal/oauth/oauth.go:301-317 used strings.HasPrefix to verify paths stayed within tokensDir, but didn't detect symlinks. An attacker who could create a symlink inside tokensDir (e.g., tokensDir/evil.json -> /etc/passwd) could cause token data to be written outside the tokens directory.