summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/tests/test_custom_headers.py
blob: fe019d522b9214dd5471d446367a1b95d83b2807 (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
"""ANTHROPIC_CUSTOM_HEADERS reach every call the plugin makes."""
from conftest import (
    STUB_VULN, VULN_PY, edit_payload, metrics_of, run_hook, stop_payload, ups_payload,
)

import llm

GATEWAY_KEY = ("x-litellm-api-key", "Bearer sk-gateway")


class TestCustomHeaders:
    def test_parsed_like_claude_code(self, monkeypatch):
        monkeypatch.setenv("ANTHROPIC_CUSTOM_HEADERS",
                           "x-litellm-api-key: Bearer sk-1\r\n"
                           "X-Tenant:  acme:eu \n"
                           "no colon here\n"
                           ": no name\n"
                           "\n"
                           "X-Empty:")
        assert llm._custom_headers() == [
            ("x-litellm-api-key", "Bearer sk-1"), ("X-Tenant", "acme:eu"), ("X-Empty", ""),
        ]

    def test_unsendable_lines_are_skipped(self, monkeypatch):
        monkeypatch.setenv("ANTHROPIC_CUSTOM_HEADERS",
                           "bad name: x\nX-Ok: 1\nX-Uni: caf\u00e9\u2603\nX-Cr: a\rb")
        assert llm._custom_headers() == [("X-Ok", "1")]

    def test_sent_with_the_plugin_credentials(self, monkeypatch):
        monkeypatch.setattr(llm, "ANTHROPIC_AUTH_TOKEN", "oauth")
        monkeypatch.setenv("ANTHROPIC_CUSTOM_HEADERS", "x-litellm-api-key: k")
        h = llm._build_auth_headers(use_token=True)
        assert h["x-litellm-api-key"] == "k"
        assert h["Authorization"] == "Bearer oauth"

    def test_custom_line_wins_over_the_same_header(self, monkeypatch):
        monkeypatch.setattr(llm, "ANTHROPIC_AUTH_TOKEN", "oauth")
        monkeypatch.setenv("ANTHROPIC_CUSTOM_HEADERS", "authorization: Bearer gw")
        h = llm._build_auth_headers(use_token=True)
        assert [v for k, v in h.items() if k.lower() == "authorization"] == ["Bearer gw"]

    def test_custom_beta_lines_are_not_sent(self, monkeypatch):
        monkeypatch.setenv("ANTHROPIC_CUSTOM_HEADERS", "anthropic-beta: extra-1\nX-Ok: 1")
        h = llm._build_auth_headers(use_token=False)
        assert h["anthropic-beta"] == "structured-outputs-2025-11-13"
        assert h["X-Ok"] == "1"

    def test_without_custom_headers_only_the_plugin_headers_are_sent(self, monkeypatch):
        monkeypatch.delenv("ANTHROPIC_CUSTOM_HEADERS", raising=False)
        assert set(llm._build_auth_headers(use_token=False)) == {
            "Content-Type", "anthropic-version", "x-api-key", "anthropic-beta"}

    def test_stop_review_reaches_a_header_keyed_gateway(self, env, gateway, workspace):
        """Every plugin call (model lookup and review) carries the gateway
        key, and the review finds the vuln."""
        _, repo = workspace
        gateway.required = GATEWAY_KEY
        gateway.vulns = [STUB_VULN]
        e = {**env, "ANTHROPIC_CUSTOM_HEADERS": f"{GATEWAY_KEY[0]}: {GATEWAY_KEY[1]}"}
        run_hook(ups_payload(repo), e)
        (repo / "app.py").write_text(VULN_PY)
        run_hook(edit_payload(repo, repo / "app.py", VULN_PY), e)
        rc, so, se = run_hook(stop_payload(repo), e)
        assert rc == 2, (so, se)
        assert "command_injection" in se
        assert metrics_of(so)["gateway_cfg"] == 3  # custom base URL and custom headers
        calls = [(m, p.split("?")[0]) for m, p, _ in gateway.requests]
        assert ("GET", "/v1/models") in calls and ("POST", "/v1/messages") in calls
        for method, path, headers in gateway.requests:
            if path.startswith("/v1/"):
                assert headers.get(GATEWAY_KEY[0]) == GATEWAY_KEY[1], (method, path)

    def test_gateway_cfg_is_a_number_with_no_names_or_values(self, monkeypatch):
        monkeypatch.delenv("ANTHROPIC_BASE_URL", raising=False)
        monkeypatch.delenv("ANTHROPIC_CUSTOM_HEADERS", raising=False)
        assert llm.gateway_cfg() == 0
        for same_host in ("https://api.anthropic.com/", "https://API.anthropic.com:443"):
            monkeypatch.setenv("ANTHROPIC_BASE_URL", same_host)
            assert llm.gateway_cfg() == 0
        monkeypatch.setenv("ANTHROPIC_BASE_URL", "https://gw.example.com")
        assert llm.gateway_cfg() == 1
        monkeypatch.setenv("ANTHROPIC_CUSTOM_HEADERS", "anthropic-beta: extra-1")
        assert llm.gateway_cfg() == 1  # beta lines are not sent
        monkeypatch.setenv("ANTHROPIC_CUSTOM_HEADERS", "X-Ok: 1")
        assert llm.gateway_cfg() == 3
        monkeypatch.delenv("ANTHROPIC_BASE_URL")
        assert llm.gateway_cfg() == 2

    def test_gateway_cfg_is_left_out_when_no_api_call_was_made(self, env, gateway, workspace):
        """Only rows that made API calls carry gateway_cfg."""
        _, repo = workspace
        rc, so, se = run_hook(stop_payload(repo), env)
        assert rc == 0, (so, se)
        row = metrics_of(so)
        assert row["skipped"] is True and "gateway_cfg" not in row