mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-12-03 09:48:41 +00:00
* Fix: sign cache entries so values planted in Redis are never unpickled * Guards against the small window where the suggestion cache could return a version and a None from get_many
127 lines
3.5 KiB
Python
127 lines
3.5 KiB
Python
import pickle
|
|
from collections.abc import Callable
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
import pytest
|
|
from django.conf import settings
|
|
|
|
from paperless.cache import SignedRedisSerializer
|
|
from paperless.signed_pickle import compute_signature
|
|
|
|
|
|
class _Evil:
|
|
def __init__(self, marker: Path) -> None:
|
|
self.marker = marker
|
|
|
|
def __reduce__(self) -> tuple[Any, tuple[str]]:
|
|
return (self.marker.write_text, ("pwned",))
|
|
|
|
|
|
@pytest.fixture
|
|
def serializer() -> SignedRedisSerializer:
|
|
return SignedRedisSerializer()
|
|
|
|
|
|
class TestSignedRedisSerializer:
|
|
def test_round_trip(self, serializer: SignedRedisSerializer) -> None:
|
|
"""
|
|
GIVEN:
|
|
- A nested value
|
|
WHEN:
|
|
- It is serialized and then deserialized
|
|
THEN:
|
|
- The original value is returned
|
|
"""
|
|
value = {"a": [1, 2, 3], "b": ("x", None)}
|
|
|
|
assert serializer.loads(serializer.dumps(value)) == value
|
|
|
|
@pytest.mark.parametrize(
|
|
("value", "stored_type"),
|
|
[
|
|
pytest.param(7, int, id="int"),
|
|
pytest.param(True, bytes, id="bool"),
|
|
],
|
|
)
|
|
def test_only_plain_integers_are_stored_raw(
|
|
self,
|
|
serializer: SignedRedisSerializer,
|
|
value: object,
|
|
stored_type: type,
|
|
) -> None:
|
|
"""
|
|
GIVEN:
|
|
- A plain integer or a bool
|
|
WHEN:
|
|
- The value is serialized
|
|
THEN:
|
|
- Only the integer is stored unpickled, so incr/decr stay atomic
|
|
"""
|
|
assert type(serializer.dumps(value)) is stored_type
|
|
|
|
def test_raw_integer_is_loaded(self, serializer: SignedRedisSerializer) -> None:
|
|
"""
|
|
GIVEN:
|
|
- An integer as stored by Redis incr/decr
|
|
WHEN:
|
|
- It is deserialized
|
|
THEN:
|
|
- The integer is returned without signature verification
|
|
"""
|
|
assert serializer.loads(b"7") == 7
|
|
|
|
@pytest.mark.parametrize(
|
|
"forge",
|
|
[
|
|
pytest.param(lambda data: data, id="unsigned"),
|
|
pytest.param(
|
|
lambda data: compute_signature(b"other data") + data,
|
|
id="signature-for-other-data",
|
|
),
|
|
],
|
|
)
|
|
def test_forged_pickle_is_not_loaded(
|
|
self,
|
|
serializer: SignedRedisSerializer,
|
|
tmp_path: Path,
|
|
caplog: pytest.LogCaptureFixture,
|
|
forge: Callable[[bytes], bytes],
|
|
) -> None:
|
|
"""
|
|
GIVEN:
|
|
- A malicious pickle without a valid signature
|
|
WHEN:
|
|
- It is deserialized
|
|
THEN:
|
|
- It is treated as a cache miss, never executed, and a warning is logged
|
|
"""
|
|
marker = tmp_path / "marker"
|
|
payload = forge(pickle.dumps(_Evil(marker)))
|
|
|
|
assert serializer.loads(payload) is None
|
|
assert not marker.exists()
|
|
assert "invalid signature" in caplog.text
|
|
|
|
|
|
class TestCacheSettings:
|
|
@pytest.mark.parametrize(
|
|
"alias",
|
|
[
|
|
pytest.param("default", id="default"),
|
|
pytest.param("read-cache", id="read-cache"),
|
|
],
|
|
)
|
|
def test_caches_use_signed_serializer(self, alias: str) -> None:
|
|
"""
|
|
GIVEN:
|
|
- The configured cache aliases
|
|
WHEN:
|
|
- Their options are inspected
|
|
THEN:
|
|
- Each uses the signed serializer
|
|
"""
|
|
options = settings.CACHES[alias]["OPTIONS"]
|
|
|
|
assert options["serializer"] == "paperless.cache.SignedRedisSerializer"
|