-
-
Notifications
You must be signed in to change notification settings - Fork 35k
bpo-38976: Add support for HTTP Only flag in MozillaCookieJar #17471
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
Merged
orsenthil
merged 13 commits into
python:master
from
jacobneiltaylor:mozcookiejar-httponly
Oct 23, 2020
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a9c548f
Add support for HTTP Only flag in MozillaCookieJar
96ce45e
📜🤖 Added by blurb_it.
blurb-it[bot] ef15aba
Cleanup class variables and update comments
jacobneiltaylor cded0a7
Fix reference to header variable
jacobneiltaylor a8a0b51
Add the HttpOnly prefix to module constants
jacobneiltaylor bc77978
Merge pull request #1 from python/master
jacobneiltaylor 48c2a8d
Add support for HTTP Only flag in MozillaCookieJar
5013677
📜🤖 Added by blurb_it.
blurb-it[bot] 4de40c1
Cleanup class variables and update comments
jacobneiltaylor a42631e
Fix reference to header variable
jacobneiltaylor de66b4a
Add the HttpOnly prefix to module constants
jacobneiltaylor 466e0d0
Merge branch 'mozcookiejar-httponly' of github.com:jacobneiltaylor/cp…
jacobneiltaylor 1dc6baa
Fixed whitespace
jacobneiltaylor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,10 +50,18 @@ def _debug(*args): | |
| logger = logging.getLogger("http.cookiejar") | ||
| return logger.debug(*args) | ||
|
|
||
|
|
||
| HTTPONLY_ATTR = "HTTPOnly" | ||
| HTTPONLY_PREFIX = "#HttpOnly_" | ||
| DEFAULT_HTTP_PORT = str(http.client.HTTP_PORT) | ||
| NETSCAPE_MAGIC_RGX = re.compile("#( Netscape)? HTTP Cookie File") | ||
| MISSING_FILENAME_TEXT = ("a filename was not supplied (nor was the CookieJar " | ||
| "instance initialised with one)") | ||
| NETSCAPE_HEADER_TEXT = """\ | ||
| # Netscape HTTP Cookie File | ||
| # http://curl.haxx.se/rfc/cookie_spec.html | ||
| # This is a generated file! Do not edit. | ||
|
|
||
| """ | ||
|
|
||
| def _warn_unhandled_exception(): | ||
| # There are a few catch-all except: statements in this module, for | ||
|
|
@@ -2004,28 +2012,29 @@ class MozillaCookieJar(FileCookieJar): | |
| header by default (Mozilla can cope with that). | ||
|
|
||
| """ | ||
| magic_re = re.compile("#( Netscape)? HTTP Cookie File") | ||
| header = """\ | ||
| # Netscape HTTP Cookie File | ||
| # http://curl.haxx.se/rfc/cookie_spec.html | ||
| # This is a generated file! Do not edit. | ||
|
|
||
| """ | ||
|
|
||
| def _really_load(self, f, filename, ignore_discard, ignore_expires): | ||
| now = time.time() | ||
|
|
||
| magic = f.readline() | ||
| if not self.magic_re.search(magic): | ||
| if not NETSCAPE_MAGIC_RGX.match(f.readline()): | ||
| raise LoadError( | ||
| "%r does not look like a Netscape format cookies file" % | ||
| filename) | ||
|
|
||
| try: | ||
| while 1: | ||
| line = f.readline() | ||
| rest = {} | ||
|
|
||
| if line == "": break | ||
|
|
||
| # httponly is a cookie flag as defined in rfc6265 | ||
| # when encoded in a netscape cookie file, | ||
| # the line is prepended with "#HttpOnly_" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verified this information here - https://curl.haxx.se/libcurl/c/CURLOPT_COOKIELIST.html |
||
| if line.startswith(HTTPONLY_PREFIX): | ||
| rest[HTTPONLY_ATTR] = "" | ||
| line = line[len(HTTPONLY_PREFIX):] | ||
|
|
||
| # last field may be absent, so keep any trailing tab | ||
| if line.endswith("\n"): line = line[:-1] | ||
|
|
||
|
|
@@ -2063,7 +2072,7 @@ def _really_load(self, f, filename, ignore_discard, ignore_expires): | |
| discard, | ||
| None, | ||
| None, | ||
| {}) | ||
| rest) | ||
| if not ignore_discard and c.discard: | ||
| continue | ||
| if not ignore_expires and c.is_expired(now): | ||
|
|
@@ -2083,16 +2092,17 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False): | |
| else: raise ValueError(MISSING_FILENAME_TEXT) | ||
|
|
||
| with open(filename, "w") as f: | ||
| f.write(self.header) | ||
| f.write(NETSCAPE_HEADER_TEXT) | ||
| now = time.time() | ||
| for cookie in self: | ||
| domain = cookie.domain | ||
| if not ignore_discard and cookie.discard: | ||
| continue | ||
| if not ignore_expires and cookie.is_expired(now): | ||
| continue | ||
| if cookie.secure: secure = "TRUE" | ||
| else: secure = "FALSE" | ||
| if cookie.domain.startswith("."): initial_dot = "TRUE" | ||
| if domain.startswith("."): initial_dot = "TRUE" | ||
| else: initial_dot = "FALSE" | ||
| if cookie.expires is not None: | ||
| expires = str(cookie.expires) | ||
|
|
@@ -2107,7 +2117,9 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False): | |
| else: | ||
| name = cookie.name | ||
| value = cookie.value | ||
| if cookie.has_nonstandard_attr(HTTPONLY_ATTR): | ||
| domain = HTTPONLY_PREFIX + domain | ||
| f.write( | ||
| "\t".join([cookie.domain, initial_dot, cookie.path, | ||
| "\t".join([domain, initial_dot, cookie.path, | ||
| secure, expires, name, value])+ | ||
| "\n") | ||
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
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/Library/2019-12-05-05-22-49.bpo-38976.5MG7Uu.rst
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| The :mod:`http.cookiejar` module now supports the parsing of cookies in CURL-style cookiejar files through MozillaCookieJar | ||
| on all platforms. Previously, such cookie entries would be silently ignored when loading a cookiejar with such entries. | ||
|
|
||
| Additionally, the HTTP Only attribute is persisted in the object, and will be correctly written to file if the MozillaCookieJar object is subsequently dumped. |
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This implementation is similar to my alternative approach in #22798 (and see bpo-2190), except:
cookie.has_nonstandard_attr(http.cookiejar.HTTPONLY_ATTR))cookie.set_nonstandard_attr("HTTPOnly", "")cookie.httponly.cookie.httponly = True