Commit 8b673f7
committed
Fix symlink traversal bug
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)1 parent 84f254a commit 8b673f7
2 files changed
Lines changed: 77 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
306 | 306 | | |
307 | 307 | | |
308 | 308 | | |
| 309 | + | |
309 | 310 | | |
310 | 311 | | |
311 | | - | |
| 312 | + | |
312 | 313 | | |
313 | 314 | | |
314 | 315 | | |
315 | 316 | | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
316 | 327 | | |
317 | 328 | | |
318 | 329 | | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
319 | 347 | | |
320 | 348 | | |
321 | 349 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
249 | 249 | | |
250 | 250 | | |
251 | 251 | | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
252 | 300 | | |
253 | 301 | | |
254 | 302 | | |
| |||
0 commit comments