Skip to content
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: 7 additions & 2 deletions asyncpg/connect_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,15 @@ def _read_password_file(passfile: pathlib.Path) \
continue
# Backslash escapes both itself and the colon,
# which is a record separator.
# First, temporarily replace \\ with a placeholder
line = line.replace(R'\\', '\n')
# Split on unescaped colons
parts = re.split(r'(?<!\\):', line, maxsplit=4)
# De-escape: replace \n (placeholder for \\) with \
# and \: with :
passtab.append(tuple(
p.replace('\n', R'\\')
for p in re.split(r'(?<!\\):', line, maxsplit=4)
p.replace('\n', '\\').replace(R'\:', ':')
for p in parts
))
except IOError:
pass
Expand Down
11 changes: 6 additions & 5 deletions tests/test_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1514,18 +1514,19 @@ def test_connect_pgpass_regular(self):
)
})

# test escaping
# test escaping - values should be de-escaped
# \: becomes : and \\ becomes \
self.run_testcase({
'host': 'fgh',
'user': R'test\\',
'database': R'test\:db',
'user': R'test\',
'database': R'test:db',
'passfile': passfile.name,
'result': (
[('fgh', 5432)],
{
'password': 'password from pgpass with escapes',
'user': R'test\\',
'database': R'test\:db',
'user': R'test\',
'database': R'test:db',
'target_session_attrs': 'any',
}
)
Expand Down