File size: 2,818 Bytes
a5a31c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""The security guarantees this project claims, asserted."""

from __future__ import annotations

import pytest

from ehekim.security import (
    InvalidApiKeyError,
    REDACTION_MARKER,
    contains_secret,
    redact,
    validate_api_key,
)

VALID = "sk-abcdef0123456789abcdef0123456789"


class TestValidateApiKey:
    def test_accepts_a_normal_key(self):
        assert validate_api_key(VALID) == VALID

    def test_strips_surrounding_whitespace(self):
        assert validate_api_key(f"  {VALID}\n") == VALID

    @pytest.mark.parametrize("bad", [None, "", "   ", "short", "x" * 513])
    def test_rejects_structurally_invalid_values(self, bad):
        with pytest.raises(InvalidApiKeyError):
            validate_api_key(bad)

    @pytest.mark.parametrize(
        "bad",
        [
            "sk-abc123456789012\nX-Injected: evil",   # CRLF header injection
            "sk-abc123456789012\r\nHost: evil.test",
            "sk-abc1234567890\x00trailing",           # NUL byte
            "sk-abc1234567890 with space",
        ],
    )
    def test_rejects_header_injection_attempts(self, bad):
        with pytest.raises(InvalidApiKeyError):
            validate_api_key(bad)

    def test_error_message_never_echoes_the_key(self):
        secret = "sk-supersecretvalue123456789"
        with pytest.raises(InvalidApiKeyError) as info:
            validate_api_key(secret + "\nX: y")
        assert secret not in str(info.value)


class TestRedact:
    @pytest.mark.parametrize(
        "text",
        [
            "Authentication Fails, Your api key: sk-abcdef0123456789 is invalid",
            "Authorization: Bearer sk-or-v1-abcdef0123456789",
            "authorization=sk-abcdef0123456789",
            '{"api_key": "sk-abcdef0123456789"}',
            "token hf_abcdefghijklmnopqrstuvwxyz012345",
            "gsk_abcdefghijklmnopqrstuvwx",
            "AIzaSyA1234567890abcdefghijklmnopqrstu",
            "apiKey='sk-abcdef0123456789'",
        ],
    )
    def test_scrubs_credential_shapes(self, text):
        assert REDACTION_MARKER in redact(text)
        assert contains_secret(text) is True

    def test_leaves_ordinary_text_alone(self):
        text = "Sağlayıcı hız sınırına ulaşıldı."
        assert redact(text) == text
        assert contains_secret(text) is False

    def test_scrubs_the_real_deepseek_error_shape(self):
        """DeepSeek echoes the submitted key back inside its 401 body."""
        secret = "sk-0123456789abcdef0123456789abcdef"
        raw = f'Authentication Fails, Your api key: {secret} is invalid'
        cleaned = redact(raw)
        assert secret not in cleaned
        assert REDACTION_MARKER in cleaned

    def test_handles_non_string_input(self):
        assert redact(ValueError("Bearer sk-abcdef0123456789")) == f"{REDACTION_MARKER}"