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

Enhance passphrase handling (Fixes #8496) #8605

Merged
merged 9 commits into from
Jan 8, 2025
2 changes: 2 additions & 0 deletions src/borg/crypto/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,12 @@
passphrase = Passphrase.getpass(prompt)
if key.load(target, passphrase):
break
Passphrase.display_debug_info(passphrase)

Check warning on line 374 in src/borg/crypto/key.py

View check run for this annotation

Codecov / codecov/patch

src/borg/crypto/key.py#L374

Added line #L374 was not covered by tests
else:
raise PasswordRetriesExceeded
else:
if not key.load(target, passphrase):
Passphrase.display_debug_info(passphrase)

Check warning on line 379 in src/borg/crypto/key.py

View check run for this annotation

Codecov / codecov/patch

src/borg/crypto/key.py#L379

Added line #L379 was not covered by tests
raise PassphraseWrong
key.init_ciphers(manifest_data)
key._passphrase = passphrase
Expand Down
48 changes: 34 additions & 14 deletions src/borg/helpers/passphrase.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import shlex
import subprocess
import sys
import textwrap

from . import bin_to_hex
from . import Error
Expand Down Expand Up @@ -109,20 +110,39 @@
retry=True,
env_var_override="BORG_DISPLAY_PASSPHRASE",
):
print('Your passphrase (between double-quotes): "%s"' % passphrase, file=sys.stderr)
print("Make sure the passphrase displayed above is exactly what you wanted.", file=sys.stderr)
try:
passphrase.encode("ascii")
except UnicodeEncodeError:
print(
"Your passphrase (UTF-8 encoding in hex): %s" % bin_to_hex(passphrase.encode("utf-8")),
file=sys.stderr,
)
print(
"As you have a non-ASCII passphrase, it is recommended to keep the "
"UTF-8 encoding in hex together with the passphrase at a safe place.",
file=sys.stderr,
)
pw_msg = textwrap.dedent(
f"""\
Your passphrase (between double-quotes): "{passphrase}"
Make sure the passphrase displayed above is exactly what you wanted.
Your passphrase (UTF-8 encoding in hex): {bin_to_hex(passphrase.encode("utf-8"))}
It is recommended to keep the UTF-8 encoding in hex together with the passphrase at a safe place.
In case you should ever run into passphrase issues, it could sometimes help debugging them.
"""
)
print(pw_msg, file=sys.stderr)
Fixed Show fixed Hide fixed
Dismissed Show dismissed Hide dismissed

@staticmethod
def display_debug_info(passphrase):
alighazi288 marked this conversation as resolved.
Show resolved Hide resolved
def fmt_var(env_var):
env_var_value = os.environ.get(env_var)
if env_var_value is not None:
return f'{env_var} = "{env_var_value}"'
else:
return f"# {env_var} is not set"

Check warning on line 131 in src/borg/helpers/passphrase.py

View check run for this annotation

Codecov / codecov/patch

src/borg/helpers/passphrase.py#L131

Added line #L131 was not covered by tests

if os.environ.get("BORG_DEBUG_PASSPHRASE") == "YES":
alighazi288 marked this conversation as resolved.
Show resolved Hide resolved
passphrase_info = textwrap.dedent(
f"""\
Incorrect passphrase!
Passphrase used (between double-quotes): "{passphrase}"
Same, UTF-8 encoded, in hex: {bin_to_hex(passphrase.encode('utf-8'))}
Relevant Environment Variables:
{fmt_var("BORG_PASSPHRASE")}
{fmt_var("BORG_PASSCOMMAND")}
{fmt_var("BORG_PASSPHRASE_FD")}
"""
)
print(passphrase_info, file=sys.stderr)
Dismissed Show dismissed Hide dismissed

@classmethod
def new(cls, allow_empty=False):
Expand Down
39 changes: 39 additions & 0 deletions src/borg/testsuite/helpers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,45 @@ def test_passphrase_new_retries(self, monkeypatch):
def test_passphrase_repr(self):
assert "secret" not in repr(Passphrase("secret"))

def test_passphrase_wrong_debug(self, capsys, monkeypatch):
passphrase = "wrong_passphrase"
monkeypatch.setenv("BORG_DEBUG_PASSPHRASE", "YES")
monkeypatch.setenv("BORG_PASSPHRASE", "env_passphrase")
monkeypatch.setenv("BORG_PASSCOMMAND", "command")
monkeypatch.setenv("BORG_PASSPHRASE_FD", "fd_value")

Passphrase.display_debug_info(passphrase)

out, err = capsys.readouterr()
assert "Incorrect passphrase!" in err
assert passphrase in err
assert bin_to_hex(passphrase.encode("utf-8")) in err
assert 'BORG_PASSPHRASE = "env_passphrase"' in err
assert 'BORG_PASSCOMMAND = "command"' in err
assert 'BORG_PASSPHRASE_FD = "fd_value"' in err

monkeypatch.delenv("BORG_DEBUG_PASSPHRASE", raising=False)
Passphrase.display_debug_info(passphrase)
out, err = capsys.readouterr()

assert "Incorrect passphrase!" not in err
assert passphrase not in err

def test_verification(self, capsys, monkeypatch):
passphrase = "test_passphrase"
hex_value = passphrase.encode("utf-8").hex()

monkeypatch.setenv("BORG_DISPLAY_PASSPHRASE", "no")
alighazi288 marked this conversation as resolved.
Show resolved Hide resolved
Passphrase.verification(passphrase)
out, err = capsys.readouterr()
assert passphrase not in err

monkeypatch.setenv("BORG_DISPLAY_PASSPHRASE", "yes")
Passphrase.verification(passphrase)
out, err = capsys.readouterr()
assert passphrase in err
assert hex_value in err


@pytest.mark.parametrize(
"ec_range,ec_class",
Expand Down
Loading