From 9697a6b5dd5a5a231f3339b86d8a7142ea849c89 Mon Sep 17 00:00:00 2001 From: "stefano.boriero@gmail.com" Date: Fri, 4 Nov 2022 09:09:39 +0100 Subject: [PATCH 1/2] gh-99080: Preserve backslash character not used to escape quotes Backslashes can be valid characters in password, they are not always used to escape symbols. Treat backslash characters as escape symbols only for quotes in quotes. --- Lib/netrc.py | 5 +++-- Lib/test/test_netrc.py | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Lib/netrc.py b/Lib/netrc.py index b285fd8e357ddb..f75d0adf2bae82 100644 --- a/Lib/netrc.py +++ b/Lib/netrc.py @@ -45,7 +45,10 @@ def get_token(self): if ch == '"': return token elif ch == "\\": + backslash = ch ch = self._read_char() + if ch != '"': + token += backslash token += ch else: if ch == "\\": @@ -54,8 +57,6 @@ def get_token(self): for ch in fiter: if ch in self.whitespace: return token - elif ch == "\\": - ch = self._read_char() token += ch return token diff --git a/Lib/test/test_netrc.py b/Lib/test/test_netrc.py index 573d636de956d1..a864851ebbaa75 100644 --- a/Lib/test/test_netrc.py +++ b/Lib/test/test_netrc.py @@ -156,6 +156,28 @@ def test_token_value_non_ascii(self): machine host.domain.com login log password \xa1\xa2 account acct """, 'password', '\xa1\xa2') + def test_token_value_with_backslash(self): + self._test_token_x("""\ + machine host.domain.com login lo\gin password pass account acct + """, 'login', 'lo\gin') + self._test_token_x("""\ + machine host.domain.com login log password pass account \account + """, 'account', '\account') + self._test_token_x("""\ + machine host.domain.com login log password pass\word account acct + """, 'password', 'pass\word') + + def test_token_value_with_backslash_enclosed_in_quotes(self): + self._test_token_x("""\ + machine host.domain.com login "lo\gin" password pass account acct + """, 'login', 'lo\gin') + self._test_token_x("""\ + machine host.domain.com login log password pass account "acc\ount" + """, 'account', 'acc\ount') + self._test_token_x("""\ + machine host.domain.com login log password "pass\word" account acct + """, 'password', 'pass\word') + def test_token_value_leading_hash(self): self._test_token_x("""\ machine host.domain.com login #log password pass account acct From 79f3d702995f138e55facc4e9efc2173b71b5233 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 4 Nov 2022 08:32:17 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2022-11-04-08-32-16.gh-issue-99080.-a_SsH.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-11-04-08-32-16.gh-issue-99080.-a_SsH.rst diff --git a/Misc/NEWS.d/next/Library/2022-11-04-08-32-16.gh-issue-99080.-a_SsH.rst b/Misc/NEWS.d/next/Library/2022-11-04-08-32-16.gh-issue-99080.-a_SsH.rst new file mode 100644 index 00000000000000..0f43ed12195b84 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-11-04-08-32-16.gh-issue-99080.-a_SsH.rst @@ -0,0 +1 @@ +Treat backslashes in .netrc files as escaping characters only for quotes in quotes