mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-12-03 17:58:41 +00:00
119 lines
4.5 KiB
Python
119 lines
4.5 KiB
Python
import hmac
|
|
import os
|
|
import pickle
|
|
import subprocess
|
|
import sys
|
|
from hashlib import sha256
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from django.test import override_settings
|
|
|
|
from paperless.celery import signed_pickle_dumps
|
|
from paperless.celery import signed_pickle_loads
|
|
from paperless.signed_pickle import HMAC_SIZE
|
|
from paperless.signed_pickle import compute_signature
|
|
from paperless.signed_pickle import verify_signed
|
|
|
|
|
|
def test_tasks_registered_without_system_checks() -> None:
|
|
# Use a fresh interpreter: Django checks and other tests can import views,
|
|
# hiding tasks that the worker would otherwise never register.
|
|
result = subprocess.run(
|
|
[
|
|
sys.executable,
|
|
"-c",
|
|
"""
|
|
import sys
|
|
|
|
from paperless.celery import app
|
|
|
|
app.loader.import_default_modules()
|
|
expected = {
|
|
"documents.bulk_edit.delete",
|
|
"documents.bulk_edit.restore_archive_serial_numbers_task",
|
|
"documents.signals.handlers.process_cf_select_update",
|
|
"documents.workflows.webhooks.send_webhook",
|
|
"paperless_mail.mail.apply_mail_action",
|
|
"paperless_mail.mail.error_callback",
|
|
}
|
|
missing = expected - set(app.tasks)
|
|
assert not missing, f"Unregistered tasks: {sorted(missing)}"
|
|
assert "paperless.urls" not in sys.modules
|
|
""",
|
|
],
|
|
env={**os.environ, "CELERY_SKIP_CHECKS": "1"},
|
|
cwd=Path(__file__).resolve().parents[2],
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=60,
|
|
check=False,
|
|
)
|
|
assert result.returncode == 0, result.stdout + result.stderr
|
|
|
|
|
|
class TestSignedPickleSerializer:
|
|
def test_roundtrip_simple_types(self) -> None:
|
|
"""Signed pickle can round-trip basic JSON-like types."""
|
|
for obj in [42, "hello", [1, 2, 3], {"key": "value"}, None, True]:
|
|
assert signed_pickle_loads(signed_pickle_dumps(obj)) == obj
|
|
|
|
def test_roundtrip_complex_types(self) -> None:
|
|
"""Signed pickle can round-trip types that JSON cannot."""
|
|
|
|
obj = {"path": Path("/tmp/test"), "data": {1, 2, 3}}
|
|
result = signed_pickle_loads(signed_pickle_dumps(obj))
|
|
assert result["path"] == Path("/tmp/test")
|
|
assert result["data"] == {1, 2, 3}
|
|
|
|
def test_tampered_data_rejected(self) -> None:
|
|
"""Flipping a byte in the data portion causes HMAC failure."""
|
|
payload = signed_pickle_dumps({"task": "test"})
|
|
tampered = bytearray(payload)
|
|
tampered[-1] ^= 0xFF
|
|
with pytest.raises(ValueError, match="HMAC verification failed"):
|
|
signed_pickle_loads(bytes(tampered))
|
|
|
|
def test_tampered_signature_rejected(self) -> None:
|
|
"""Flipping a byte in the signature portion causes HMAC failure."""
|
|
payload = signed_pickle_dumps({"task": "test"})
|
|
tampered = bytearray(payload)
|
|
tampered[0] ^= 0xFF
|
|
with pytest.raises(ValueError, match="HMAC verification failed"):
|
|
signed_pickle_loads(bytes(tampered))
|
|
|
|
def test_verified_data_is_a_view_not_a_copy(self) -> None:
|
|
"""Verified data is a view of the payload, so large payloads aren't copied."""
|
|
data = b"x" * 1024
|
|
payload = compute_signature(data) + data
|
|
verified = verify_signed(payload)
|
|
assert verified.obj is payload
|
|
assert bytes(verified) == data
|
|
|
|
def test_truncated_payload_rejected(self) -> None:
|
|
"""A payload shorter than HMAC_SIZE is rejected."""
|
|
with pytest.raises(ValueError, match="too short"):
|
|
signed_pickle_loads(b"\x00" * (HMAC_SIZE - 1))
|
|
|
|
def test_empty_payload_rejected(self) -> None:
|
|
with pytest.raises(ValueError, match="too short"):
|
|
signed_pickle_loads(b"")
|
|
|
|
@override_settings(SECRET_KEY="different-secret-key")
|
|
def test_wrong_secret_key_rejected(self) -> None:
|
|
"""A message signed with one key cannot be loaded with another."""
|
|
original_key = b"test-secret-key-do-not-use-in-production"
|
|
obj = {"task": "test"}
|
|
data = pickle.dumps(obj, protocol=pickle.HIGHEST_PROTOCOL)
|
|
signature = hmac.new(original_key, data, sha256).digest()
|
|
payload = signature + data
|
|
with pytest.raises(ValueError, match="HMAC verification failed"):
|
|
signed_pickle_loads(payload)
|
|
|
|
def test_forged_pickle_rejected(self) -> None:
|
|
"""A raw pickle payload (no signature) is rejected."""
|
|
raw_pickle = pickle.dumps({"task": "test"})
|
|
# Raw pickle won't have a valid HMAC prefix
|
|
with pytest.raises(ValueError, match="HMAC verification failed"):
|
|
signed_pickle_loads(b"\x00" * HMAC_SIZE + raw_pickle)
|