Skip to content

gh-99080: Preserve backslash character not used to escape quotes in .netrc files #99088

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

Open
wants to merge 2 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
5 changes: 3 additions & 2 deletions Lib/netrc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "\\":
Expand All @@ -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

Expand Down
22 changes: 22 additions & 0 deletions Lib/test/test_netrc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Treat backslashes in .netrc files as escaping characters only for quotes in quotes