Skip to content
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

LWPCookieJar.save() creates *.lwp file in 644 mode #79096

Closed
aleskva mannequin opened this issue Oct 6, 2018 · 16 comments
Closed

LWPCookieJar.save() creates *.lwp file in 644 mode #79096

aleskva mannequin opened this issue Oct 6, 2018 · 16 comments
Labels
3.10 3.11 3.12 expert-IO stdlib type-security

Comments

@aleskva
Copy link
Mannequin

@aleskva aleskva mannequin commented Oct 6, 2018

BPO 34915
Nosy @vadmium, @tirkarthi

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2018-10-06.13:58:56.935>
labels = ['type-security', '3.8', '3.9', 'expert-IO', '3.7', 'library', '3.10']
title = 'LWPCookieJar.save() creates *.lwp file in 644 mode'
updated_at = <Date 2021-03-14.06:38:53.953>
user = 'https://bugs.python.org/aleskva'

bugs.python.org fields:

activity = <Date 2021-03-14.06:38:53.953>
actor = 'martin.panter'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)', 'IO']
creation = <Date 2018-10-06.13:58:56.935>
creator = 'aleskva'
dependencies = []
files = []
hgrepos = []
issue_num = 34915
keywords = []
message_count = 4.0
messages = ['327246', '339126', '341422', '388664']
nosy_count = 3.0
nosy_names = ['martin.panter', 'aleskva', 'xtreak']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'security'
url = 'https://bugs.python.org/issue34915'
versions = ['Python 3.6', 'Python 3.7', 'Python 3.8', 'Python 3.9', 'Python 3.10']

@aleskva
Copy link
Mannequin Author

@aleskva aleskva mannequin commented Oct 6, 2018

The LWPCookieJar.save() creates an *.lwp file containing session cookies in non-safe 644 mode (everyone can read it). This is not a secure behavior, especially for storing session keys or session cookies. The file should be created in 600 mode in my opinion.

https://github.com/python/cpython/blob/3.7/Lib/http/cookiejar.py#L1872

@aleskva aleskva mannequin assigned tiran Oct 6, 2018
@aleskva aleskva mannequin added expert-SSL 3.7 stdlib expert-IO type-security labels Oct 6, 2018
@tirkarthi
Copy link
Member

@tirkarthi tirkarthi commented Mar 29, 2019

I guess this is a good choice and distutils stores .pypirc [0] in this manner that has username and password.

[0]

with os.fdopen(os.open(rc, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f:

@tirkarthi
Copy link
Member

@tirkarthi tirkarthi commented May 5, 2019

Martin, any thoughts on this change?

@tiran tiran removed their assignment Oct 21, 2020
@vadmium
Copy link
Member

@vadmium vadmium commented Mar 14, 2021

I don't have a strong opinion, but it does seem a sensible change that matches the high-level nature of the "cookiejar" module, with low risk of users relying on the current file permissions. On the other hand, the "curl" command seems to use the default mode when creating a cookies file (in Netscape a.k.a. Mozilla format):

$ curl --cookie-jar cookies https://www.google.com/
[. . .]
$ ls -l cookies
-rw-r--r-- 1 vadmium vadmium 418 Mar 14 17:12 cookies

The MozillaCookieJar class also seems to use the default file mode. I suppose it should be changed as well as the LWP class.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
pSub added a commit to pSub/cpython that referenced this issue Jun 3, 2022
Cookies can store sensitive information and should therefore be protected
against unauthorized third parties. This is also described in issue python#79096.

The filesystem permissions are currently set to 644, everyone can read the
file. This commit changes the permissions to 600, only the creater of the file
can read and modify it. This improves security, because it reduces the attack
surface. Now the attacker needs control of the user that created the cookie or
a ways to circumvent the filesystems permissions.

This change is backwards incompatible. Systems that rely on world-readable
cookies will breake. However, one could argue that those are misconfigured in
the first place.
pSub added a commit to pSub/cpython that referenced this issue Jun 3, 2022
….save()

Cookies can store sensitive information and should therefore be protected
against unauthorized third parties. This is also described in issue python#79096.

The filesystem permissions are currently set to 644, everyone can read the
file. This commit changes the permissions to 600, only the creater of the file
can read and modify it. This improves security, because it reduces the attack
surface. Now the attacker needs control of the user that created the cookie or
a ways to circumvent the filesystems permissions.

This change is backwards incompatible. Systems that rely on world-readable
cookies will breake. However, one could argue that those are misconfigured in
the first place.
pSub added a commit to pSub/cpython that referenced this issue Jun 3, 2022
….save()

Cookies can store sensitive information and should therefore be protected
against unauthorized third parties. This is also described in issue python#79096.

The filesystem permissions are currently set to 644, everyone can read the
file. This commit changes the permissions to 600, only the creater of the file
can read and modify it. This improves security, because it reduces the attack
surface. Now the attacker needs control of the user that created the cookie or
a ways to circumvent the filesystems permissions.

This change is backwards incompatible. Systems that rely on world-readable
cookies will breake. However, one could argue that those are misconfigured in
the first place.
pSub added a commit to pSub/cpython that referenced this issue Jun 3, 2022
….save()

Note: This change is not effective on Microsoft Windows.

Cookies can store sensitive information and should therefore be protected
against unauthorized third parties. This is also described in issue python#79096.

The filesystem permissions are currently set to 644, everyone can read the
file. This commit changes the permissions to 600, only the creater of the file
can read and modify it. This improves security, because it reduces the attack
surface. Now the attacker needs control of the user that created the cookie or
a ways to circumvent the filesystems permissions.

This change is backwards incompatible. Systems that rely on world-readable
cookies will breake. However, one could argue that those are misconfigured in
the first place.
pSub added a commit to pSub/cpython that referenced this issue Jun 3, 2022
….save()

Note: This change is not effective on Microsoft Windows.

Cookies can store sensitive information and should therefore be protected
against unauthorized third parties. This is also described in issue python#79096.

The filesystem permissions are currently set to 644, everyone can read the
file. This commit changes the permissions to 600, only the creater of the file
can read and modify it. This improves security, because it reduces the attack
surface. Now the attacker needs control of the user that created the cookie or
a ways to circumvent the filesystems permissions.

This change is backwards incompatible. Systems that rely on world-readable
cookies will breake. However, one could argue that those are misconfigured in
the first place.
@ambv
Copy link
Contributor

@ambv ambv commented Jun 7, 2022

FWIW Firefox also creates user profile directories with 700 so that looks like precedent to me.

@ambv ambv removed the 3.9 label Jun 7, 2022
@ambv
Copy link
Contributor

@ambv ambv commented Jun 7, 2022

@AA-Turner removed the security branches from this issue suggesting that this isn't a security vulnerability in need of patching. Following this train of thought, , I'm not entirely sold on the idea that this change is even a "bug fix". It looks more like an enhancement to me, which I would err on especially given its backwards incompatibility. It's @pablogsal's call, I would consider it for 3.11 but not for 3.10.

@tiran's got more experience with evaluating security issues so he can also weigh in on what we should do here.

ambv pushed a commit that referenced this issue Jun 7, 2022
GH-93463)

Note: This change is not effective on Microsoft Windows.

Cookies can store sensitive information and should therefore be protected
against unauthorized third parties. This is also described in issue #79096.

The filesystem permissions are currently set to 644, everyone can read the
file. This commit changes the permissions to 600, only the creater of the file
can read and modify it. This improves security, because it reduces the attack
surface. Now the attacker needs control of the user that created the cookie or
a ways to circumvent the filesystems permissions.

This change is backwards incompatible. Systems that rely on world-readable
cookies will breake. However, one could argue that those are misconfigured in
the first place.
@ambv
Copy link
Contributor

@ambv ambv commented Jun 7, 2022

I merged gh-93463 to 3.12. Waiting on @pablogsal and/or @tiran to voice their opinion on what to do with backports here.

@tiran
Copy link
Member

@tiran tiran commented Jun 7, 2022

I share @ambv concerns regarding backward incompatibility. It's a potential security issue iff an application also uses insecure directories to store the cookies. Any security sensitive application should use directories with tight permissions for sensitive data. A strict umask like 0o027 or 0o077 would also create cookie jars that are not accessible to users.

Could you please improve the test code?

  • remove print
  • use os_helper.unlink() instead of try/except block
  • use stat.S_IMODE(status.st_mode) == 0o600 instead of oct()[-3:]

@AA-Turner
Copy link
Member

@AA-Turner AA-Turner commented Jun 7, 2022

@AA-Turner removed the security branches from this issue suggesting that this isn't a security vulnerability in need of patching.

Sorry, I should have added a comment -- to confirm, my thinking was along the same lines as yours.

A

@ambv
Copy link
Contributor

@ambv ambv commented Jun 7, 2022

Oops, since I already landed gh-93463 in 3.12, I'll do the changes @tiran wants in a separate PR.

@tiran
Copy link
Member

@tiran tiran commented Jun 7, 2022

@ambv Could you please add @os_helper.skip_unless_working_chmod decorator, too? It's a new decorator for testing on wasm32-wasi.

tiran added a commit to tiran/cpython that referenced this issue Jun 8, 2022
Fixup of pythonGH-93463:
- remove stray print
- use proper way to check file mode
- add working chmod decorator
ambv added a commit that referenced this issue Jun 8, 2022
Fixup of GH-93463:
- remove stray print
- use proper way to check file mode
- add working chmod decorator

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
@pSub
Copy link
Contributor

@pSub pSub commented Jun 9, 2022

Thank you for your review @tiran, I learned a lot. And thank you @ambv for addressing the review findings.

@ambv
Copy link
Contributor

@ambv ambv commented Jun 9, 2022

@pablogsal, let us know if you'd agree for this to still land in 3.11. Otherwise this can be closed.

@pablogsal
Copy link
Member

@pablogsal pablogsal commented Jun 9, 2022

I'm fine landing this in 3.11 as long as it goes in before next beta

@ambv
Copy link
Contributor

@ambv ambv commented Jun 9, 2022

Alright, I'll do the backport now in one commit.

ambv pushed a commit to ambv/cpython that referenced this issue Jun 9, 2022
….save() (pythonGH-93463)

Note: This change is not effective on Microsoft Windows.

Cookies can store sensitive information and should therefore be protected
against unauthorized third parties. This is also described in issue python#79096.

The filesystem permissions are currently set to 644, everyone can read the
file. This commit changes the permissions to 600, only the creater of the file
can read and modify it. This improves security, because it reduces the attack
surface. Now the attacker needs control of the user that created the cookie or
a ways to circumvent the filesystems permissions.

This change is backwards incompatible. Systems that rely on world-readable
cookies will breake. However, one could argue that those are misconfigured in
the first place.
ambv added a commit to ambv/cpython that referenced this issue Jun 9, 2022
Fixup of pythonGH-93463:
- remove stray print
- use proper way to check file mode
- add working chmod decorator

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
ambv added a commit that referenced this issue Jun 9, 2022
…r.save() (GH-93463) (GH-93636)

Note: This change is not effective on Microsoft Windows.

Cookies can store sensitive information and should therefore be protected
against unauthorized third parties. This is also described in issue #79096.

The filesystem permissions are currently set to 644, everyone can read the
file. This commit changes the permissions to 600, only the creater of the file
can read and modify it. This improves security, because it reduces the attack
surface. Now the attacker needs control of the user that created the cookie or
a ways to circumvent the filesystems permissions.

This change is backwards incompatible. Systems that rely on world-readable
cookies will breake. However, one could argue that those are misconfigured in
the first place.

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
Co-authored-by: Pascal Wittmann <mail@pascal-wittmann.de>
Co-authored-by: Christian Heimes <christian@python.org>
@ambv
Copy link
Contributor

@ambv ambv commented Jun 9, 2022

Alright, this is landed to 3.11 and 3.12. Thanks, @pSub! 🍰

@ambv ambv closed this as completed Jun 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.10 3.11 3.12 expert-IO stdlib type-security
Projects
Status: Done
Development

No branches or pull requests

7 participants