Harden cookie file permissions in CookieJar.save() - #12312
Conversation
…te test to preserve existing permissions
|
I updated the patch so permissions are enforced only when the cookie file is first created (0600 via os.open + O_CREAT | O_EXCL). |
|
I simplified this further. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #12312 +/- ##
=======================================
Coverage 99.11% 99.11%
=======================================
Files 130 130
Lines 45558 45608 +50
Branches 2404 2405 +1
=======================================
+ Hits 45155 45205 +50
Misses 272 272
Partials 131 131
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
Backport to 3.14: 💔 cherry-picking failed — conflicts found❌ Failed to cleanly apply 47558a3 on top of patchback/backports/3.14/47558a30c88e31cc3b5ff3b57f6127e7984c80bc/pr-12312 Backporting merged PR #12312 into master
🤖 @patchback |
|
Could you handle the 3.14 backport following the above instructions? |
(cherry picked from commit 47558a3)
This change hardens cookie persistence file permissions in CookieJar.save().
Problem
Cookie files may be created with overly permissive permissions depending on the system umask. Since these files can contain authentication and session tokens, this creates a risk of unintended exposure.
Risk
On shared systems, other local users may be able to read persisted cookies and reuse session data.
Fix
New files are now created using os.open with O_CREAT | O_EXCL and mode 0600, ensuring least-privilege permissions at creation time.
If the file already exists, it is reopened for overwrite and its permissions are tightened to 0600 on POSIX systems (using fchmod where available, with a fallback to chmod).
Why this matters
This reduces the risk of leaking sensitive authentication data at rest and makes cookie persistence safer by default.
Testing
Added tests to verify:
Permission checks use stat.S_IMODE and are skipped on unsupported platforms.
Test status
Focused tests pass: