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

Add a crypto handler #26

Merged
merged 9 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ deployment instructions) is the configuration format:
* `acceptedMimeType` is renamed `scan.allowed_mimetypes`
* `requestHeader` is renamed `download.additional_headers` and turned into a dictionary.

Note that the format of the cryptographic pickle file file and key are compatible between
Note that the format of the cryptographic pickle file and key are compatible between
this project and the legacy Matrix Content Scanner. If no file exist at that path one will
be created automatically.

Expand Down
1 change: 1 addition & 0 deletions config.sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ download:
crypto:
# The path to the Olm pickle file. This file contains the key pair to use when
# encrypting and decrypting encrypted POST request bodies.
# The pickle file is automatically created at startup if it doesn't already exist.
babolivier marked this conversation as resolved.
Show resolved Hide resolved
# Required.
pickle_path: "./pickle"

Expand Down
18 changes: 9 additions & 9 deletions matrix_content_scanner/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,12 @@ def __init__(self, mcs: "MatrixContentScanner") -> None:
# content.
raise ConfigError(
"Configured value for crypto.pickle_key is incorrect or pickle file"
" is corrupted (Olm error code: %s)" % e
f" is corrupted (Olm error code: {e})"
)

logger.info("Loaded Olm key pair from pickle file %s", path)

except OSError as e:
if not isinstance(e, FileNotFoundError):
raise ConfigError(
"Failed to read the pickle file at the location configured for"
" crypto.pickle_path (%s): %s" % (path, e)
)

except FileNotFoundError:
logger.info(
"Pickle file not found, generating a new Olm key pair and storing it in"
" pickle file %s",
Expand All @@ -81,9 +75,15 @@ def __init__(self, mcs: "MatrixContentScanner") -> None:
except OSError as e:
raise ConfigError(
"Failed to write the pickle file at the location configured for"
" crypto.pickle_path (%s): %s" % (path, e)
f" crypto.pickle_path ({path}): {e}"
)

except OSError as e:
raise ConfigError(
"Failed to read the pickle file at the location configured for"
f" crypto.pickle_path ({path}): {e}"
)

self.public_key = self._decryptor.public_key

def decrypt_body(self, ciphertext: str, mac: str, ephemeral: str) -> JsonDict:
Expand Down