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

gh-87888: Make FileCookieJar use ASCII/surrogateescape #92392

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Lib/http/cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1798,7 +1798,10 @@ def load(self, filename=None, ignore_discard=False, ignore_expires=False):
if self.filename is not None: filename = self.filename
else: raise ValueError(MISSING_FILENAME_TEXT)

with open(filename) as f:
# cookie value should be ASCII, but cookiejar file may contain
# non-ASCII comments or invalid cookies.
# We use "surrogateescape" error handler to read them.
with open(filename, encoding="ascii", errors="surrogateescape") as f:
self._really_load(f, filename, ignore_discard, ignore_expires)

def revert(self, filename=None,
Expand Down Expand Up @@ -1892,7 +1895,7 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False):

with os.fdopen(
os.open(filename, os.O_CREAT | os.O_WRONLY | os.O_TRUNC, 0o600),
'w',
'w', encoding="ascii", errors="surrogateescape",
) as f:
# There really isn't an LWP Cookies 2.0 format, but this indicates
# that there is extra information in here (domain_dot and
Expand Down Expand Up @@ -2086,7 +2089,7 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False):

with os.fdopen(
os.open(filename, os.O_CREAT | os.O_WRONLY | os.O_TRUNC, 0o600),
'w',
'w', encoding="ascii", errors="surrogateescape",
) as f:
f.write(NETSCAPE_HEADER_TEXT)
now = time.time()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Changed encoding used by :class:`http.cookiejar.FileCookieJar` and its
subclasses from locale encoding to "ASCII" with "surrogateescape" error handler for reading.
Loading