Skip to content

Commit

Permalink
Get rid of pretty_bad_protocol monkey-patching
Browse files Browse the repository at this point in the history
And just implement them in the code directly now. We still set
the `USERNAME` environment variable via encryption.py since there's
not really a logical place for it in pretty_bad_protocol.

Fixes #6807.
  • Loading branch information
legoktm committed Jun 12, 2023
1 parent 0d52de9 commit d60d89b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 37 deletions.
37 changes: 2 additions & 35 deletions securedrop/encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,8 @@
if typing.TYPE_CHECKING:
from source_user import SourceUser


def _monkey_patch_username_in_env() -> None:
# To fix https://github.com/freedomofpress/securedrop/issues/78
os.environ["USERNAME"] = "www-data"


def _monkey_patch_unknown_status_message() -> None:
# To fix https://github.com/isislovecruft/python-gnupg/issues/250 with Focal gnupg
gnupg._parsers.Verify.TRUST_LEVELS["DECRYPTION_COMPLIANCE_MODE"] = 23


def _monkey_patch_delete_handle_status() -> None:
# To fix https://github.com/freedomofpress/securedrop/issues/4294
def _updated_handle_status(self: gnupg._parsers.DeleteResult, key: str, value: str) -> None:
"""
Parse a status code from the attached GnuPG process.
:raises: :exc:`~exceptions.ValueError` if the status message is unknown.
"""
if key in ("DELETE_PROBLEM", "KEY_CONSIDERED"):
self.status = self.problem_reason.get(value, "Unknown error: %r" % value)
elif key in ("PINENTRY_LAUNCHED"):
self.status = key.replace("_", " ").lower()
else:
raise ValueError("Unknown status message: %r" % key)

gnupg._parsers.DeleteResult._handle_status = _updated_handle_status


def _setup_monkey_patches_for_gnupg() -> None:
_monkey_patch_username_in_env()
_monkey_patch_unknown_status_message()
_monkey_patch_delete_handle_status()


_setup_monkey_patches_for_gnupg()
# To fix https://github.com/freedomofpress/securedrop/issues/78
os.environ["USERNAME"] = "www-data"


class GpgKeyNotFoundError(Exception):
Expand Down
9 changes: 7 additions & 2 deletions securedrop/pretty_bad_protocol/_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1082,12 +1082,14 @@ def __str__(self): # type: ignore[no-untyped-def] # noqa
}

def _handle_status(self, key, value): # type: ignore[no-untyped-def] # noqa
"""Parse a status code from the attached GnuPG process.
"""
Parse a status code from the attached GnuPG process.
:raises: :exc:`~exceptions.ValueError` if the status message is unknown.
"""
if key in ("DELETE_PROBLEM", "KEY_CONSIDERED"):
self.status = self.problem_reason.get(value, "Unknown error: %r" % value)
elif key in ("PINENTRY_LAUNCHED"):
self.status = key.replace("_", " ").lower()
else:
raise ValueError("Unknown status message: %r" % key)

Expand Down Expand Up @@ -1487,13 +1489,16 @@ class Verify:
TRUST_MARGINAL = 2
TRUST_FULLY = 3
TRUST_ULTIMATE = 4
DECRYPTION_COMPLIANCE_MODE = 23

TRUST_LEVELS = {
"TRUST_UNDEFINED": TRUST_UNDEFINED,
"TRUST_NEVER": TRUST_NEVER,
"TRUST_MARGINAL": TRUST_MARGINAL,
"TRUST_FULLY": TRUST_FULLY,
"TRUST_ULTIMATE": TRUST_ULTIMATE,
# To fix https://github.com/isislovecruft/python-gnupg/issues/250 with Focal gnupg
"DECRYPTION_COMPLIANCE_MODE": DECRYPTION_COMPLIANCE_MODE,
}

def __init__(self, gpg): # type: ignore[no-untyped-def] # noqa
Expand Down

0 comments on commit d60d89b

Please sign in to comment.