Skip to content

Multi-tier framework for evaluating AI agent skills and plugins with quality gates, security scanning, harness compatibility checks, semantic overlap detection, synthetic evaluation dataset generation, and live agent evaluation that measures how skills affect agent behavior. Ships reusable CI templates that bring every check to any repository.

25.0 KB
Raw Blame
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

"""URL helpers shared by the MCP, hook, and endpoint policies (no network)."""

from __future__ import annotations

import json
import subprocess
import sys
import textwrap
from pathlib import Path

import pytest
from tests.validators.plugin_analysis_support import analyze_plugin

from skillevaluator.validators.url_policy import (
    MAX_REPORT_CHARS,
    UrlCredentials,
    UserinfoRule,
    has_secret_shape,
    is_credential_key,
    is_env_reference,
    looks_like_inline_secret,
    redact_secrets,
    report_text,
    safe_url,
    url_ambiguities,
    url_credentials,
)

_TOKEN = "ghp_" + "0123456789abcdefghij0123456789abcdef"
# A long random user name, as a token used as the user name of a URL is.
_HEX_USER = "3f2a9c1be47d8a05f6e2b9c4d1a7e3f0b8c6d2a1"


@pytest.mark.parametrize(
    ("url", "rule", "expected"),
    [
        ("https://user:[email protected]/x", "any", True),
        ("https://${USER}:${TOKEN}@h.example/x", "any", True),
        ("https://[email protected]/x", "any", True),
        ("https://admin:[email protected]:99999/x", "any", True),
        ("https://h.example/x", "any", False),
        ("https://user:[email protected]/x", "literal", True),
        ("https://${USER}:${TOKEN}@h.example/x", "literal", False),
        ("https://[email protected]/x", "literal", False),
        ("https://user:${TOKEN}@h.example/x", "literal", True),
        ("https://${USER}:[email protected]/x", "literal", True),
        ("https://[email protected]/x", "literal", True),
        ("https://h.example/x", "literal", False),
        ("https://${USER:-}:${PW:-}@h.example/x", "literal", False),
        ("https://${env:USER}@h.example/x", "literal", False),
        ("https://user:${PW:-hunter2}@h.example/x", "literal", True),
        ("https://user:[email protected]/x", "secret", True),
        ("https://x-access-token:${GITHUB_TOKEN}@github.com/org/repo.git", "secret", False),
        ("https://x-access-token:${GITHUB_TOKEN:-}@github.com/org/repo.git", "secret", False),
        ("https://x-access-token:${GITHUB_TOKEN:-hunter2}@github.com/org/repo.git", "secret", True),
        ("https://[email protected]/x", "secret", False),
        ("https://[email protected]/x", "secret", False),
        (f"https://{_TOKEN}@h.example/x", "secret", True),
        (f"https://{_HEX_USER}@h.example/x", "secret", True),
        # The raw text holds a backslash in the userinfo, which WHATWG clients read as '/'.
        (f"https://{_HEX_USER}\\@h.example/x", "secret", True),
        ("https://169.254.169.254\\@h.example/x", "secret", False),
        ("https://deploy:31337\\@h.example/x", "secret", True),
        ("ssh://[email protected]/org/repo.git", "secret", False),
    ],
)
def test_url_credentials_reads_the_userinfo_as_written(url: str, rule: UserinfoRule, expected: bool) -> None:
    assert url_credentials(url, userinfo_rule=rule).userinfo is expected


@pytest.mark.parametrize(
    ("query", "keys"),
    [
        ("api_key=literal", ("api_key",)),
        (f"q={_TOKEN}", ("q",)),
        ("token=a&page=2&token=b", ("token",)),
        ("api_key=${API_KEY}", ()),
        ("api_key=${API_KEY:-}", ()),
        ("api_key=${API_KEY:-literal}", ("api_key",)),
        ("api_key=", ()),
        ("page=2", ()),
        # 'key' and 'sig' name a credential in a URL query; a setting is not a credential.
        ("key=primary&sig=v2", ("key", "sig")),
        ("token=true", ()),
        ("password_policy=strict", ()),
    ],
)
def test_url_credentials_reads_credential_names_and_secret_shaped_values(query: str, keys: tuple[str, ...]) -> None:
    for rule in ("any", "literal", "secret"):
        assert url_credentials(f"https://h.example/x?{query}", userinfo_rule=rule).query_keys == keys


@pytest.mark.parametrize(
    ("url", "query_keys", "fragment_keys"),
    [
        ("https://h.example/#/cb?access_token=abc", (), ("access_token",)),
        ("https://h.example/x?page=2#access_token=abc&token_type=bearer", (), ("access_token",)),
        ("https://h.example/x?api_key=literal#section-2", ("api_key",), ()),
        ("https://h.example/x#L10-L20", (), ()),
    ],
)
def test_url_credentials_reads_the_query_only_up_to_the_fragment(
    url: str, query_keys: tuple[str, ...], fragment_keys: tuple[str, ...]
) -> None:
    """Regression: the query was read from the first '?' even inside the fragment ('#/cb?access_token=...')."""
    credentials = url_credentials(url, userinfo_rule="literal")
    assert (credentials.query_keys, credentials.fragment_keys) == (query_keys, fragment_keys)
    assert bool(credentials) is bool(query_keys or fragment_keys)


@pytest.mark.parametrize("query", [_TOKEN, f"{_TOKEN}=1", f"page=2&{_TOKEN}"])
def test_url_credentials_flags_a_query_key_shaped_like_a_secret(query: str) -> None:
    """Regression: only query values were checked, so a bare '?ghp_...' component carried no credential."""
    for rule in ("any", "literal", "secret"):
        assert url_credentials(f"https://h.example/sse?{query}", userinfo_rule=rule).query_keys == (_TOKEN,)


@pytest.mark.parametrize(
    ("value", "reference"),
    [
        ("$TOKEN", True),
        ("${TOKEN}", True),
        ("${TOKEN:-}", True),
        ("${env:TOKEN}", True),
        ("${TOKEN:-literal}", False),
        ("Bearer ${TOKEN}", False),
        ("literal", False),
    ],
)
def test_env_reference_forms(value: str, reference: bool) -> None:
    """A reference with no default (or an empty one) carries no value; a default ships with the plugin."""
    assert is_env_reference(value) is reference


@pytest.mark.parametrize(
    ("url", "ambiguous"),
    [
        ("https://h.example/a b", False),
        ("https://h.example/x?q=a b", False),
        ("https://h.example .com/x", True),
        ("https:// h.example/x", True),
        ("https://h.example/a\u00a0b", True),
        ("https://h.example/a\tb", True),
        ("mailto:a [email protected]", True),
    ],
)
def test_a_plain_space_after_the_host_is_not_ambiguous(url: str, ambiguous: bool) -> None:
    """Clients percent-encode a plain space in the path; whitespace before it, or any other kind, still counts."""
    assert bool(url_ambiguities(url)) is ambiguous


def test_url_credentials_is_false_when_the_url_carries_none() -> None:
    assert not url_credentials("https://h.example/x?page=2", userinfo_rule="any")
    assert not UrlCredentials()
    assert UrlCredentials(query_keys=("token",))


@pytest.mark.parametrize(
    ("url", "shown"),
    [
        ("https://admin:[email protected]/x?token=abc#frag", "https://hooks.example.com/x"),
        ("https:admin:[email protected]/mcp", "https://evil.example/mcp"),
        ("https://evil.net\\.example.com/x", "https://evil.net/.example.com/x"),
        ("https://[::1]:8443/mcp", "https://[::1]:8443/mcp"),
        ("https://admin:[email protected]:99999/x", "https://h.example:99999/x"),
        (f"https://hooks.example.com/notify/{_TOKEN}/x", "https://hooks.example.com/notify/github-token-<redacted>/x"),
        (f"https://h.example:bad/{_TOKEN}", "https://h.example:bad/github-token-<redacted>"),
    ],
)
def test_safe_url_shows_where_a_client_connects_without_credentials(url: str, shown: str) -> None:
    assert safe_url(url) == shown


_BACKSLASH_USERINFO = "3f2a9c1be47d8a05f6e2b9c4d1a7e3f0b8c6d2a1"


@pytest.mark.parametrize(
    ("url", "shown"),
    [
        (f"https://{_BACKSLASH_USERINFO}\\@api.example.com/mcp", "https://api.example.com/mcp"),
        ("https://deploy:31337\\@evil.example/mcp", "https://evil.example/mcp"),
        (f"https://{_BACKSLASH_USERINFO}\\@cdn.example.com/server.mcpb", "https://cdn.example.com/server.mcpb"),
        ("https://user:pass@host\\@evil.example/x", "https://evil.example/x"),
    ],
)
def test_safe_url_never_shows_userinfo_that_a_backslash_turns_into_the_host(url: str, shown: str) -> None:
    """Regression: WHATWG reads '\\' as '/', so the userinfo before '\\@' was shown as the host."""
    assert safe_url(url) == shown


@pytest.mark.parametrize(
    ("url", "shown"),
    [
        ("wss:/ops:[email protected]:70000/mcp", "wss://relay.example:70000/mcp"),
        ("https:/ops:[email protected]:bad/mcp", "https://mcp.example:bad/mcp"),
        ("https:\\/ops:[email protected]:bad/mcp", "https://mcp.example:bad/mcp"),
        ("https:/\\ops:hunter2@[::1/mcp", "https://[::1/mcp"),
        ("https:/ops:hunter2\\@evil.example:bad/x", "https://evil.example:bad/x"),
        ("https:/ops:hunter2\\@evil.example/x", "https://evil.example/x"),
        ("https:/ops:[email protected]/mcp", "https://mcp.example/mcp"),
    ],
)
def test_safe_url_never_keeps_userinfo_written_with_fewer_than_two_slashes(url: str, shown: str) -> None:
    """Regression: a malformed URL with one '/' or '\\' after the scheme was shown whole, password included."""
    assert safe_url(url) == shown


def test_safe_url_is_bounded() -> None:
    assert len(safe_url("https://h.example/" + "a" * 10_000)) <= MAX_REPORT_CHARS


def test_report_text_redacts_every_known_secret_shape() -> None:
    text = report_text(
        f"gh auth login --with-token {_TOKEN}; curl https://user:[email protected]/x -H 'X: xoxb-1234567890abc'"
    )

    assert _TOKEN not in text and "xoxb-1234567890abc" not in text and "user:pw" not in text
    assert text.startswith("gh auth login --with-token github-token-<redacted>;")


_KEY_BODY = "b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW"
# Assembled at run time so secret scanners don't read the fixtures below as real key blocks.
_PRIVATE_KEY = "PRIVATE " + "KEY"


@pytest.mark.parametrize("separator", ["\\n", "\n"])
def test_report_text_redacts_a_whole_private_key(separator: str) -> None:
    """Regression: only the BEGIN line was redacted, so the key body and its END line were shown."""
    key = f"-----BEGIN OPENSSH {_PRIVATE_KEY}-----{separator}{_KEY_BODY}{separator}-----END OPENSSH {_PRIVATE_KEY}-----"

    text = report_text(f"printf '%s' '{key}' > ~/.ssh/id")

    assert text == "printf '%s' 'private-key-<redacted>' > ~/.ssh/id"


def test_report_text_withholds_everything_after_a_private_key_it_cannot_read_as_a_block() -> None:
    # 'XPRIVATE KEY' has the secret shape of a private-key header but is not a PEM label redaction reads.
    text = report_text(f"echo '-----BEGIN X{_PRIVATE_KEY}----- {_KEY_BODY} -----END X{_PRIVATE_KEY}-----' > k")

    assert text == "echo 'private-key-<redacted>"


def test_report_text_redacts_a_whole_jwt() -> None:
    """Regression: the JWT's header and payload were redacted, but its signature was shown."""
    signature = "dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U"
    jwt = f"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.{signature}"

    text = report_text(f"notify --session {jwt} --quiet")

    assert signature not in text
    assert text == "notify --session jwt-<redacted> --quiet"


def test_report_text_never_shows_a_token_cut_at_the_redaction_window() -> None:
    """Regression: a token cut by the redaction window was too short to match its pattern, and the redacted
    JWT before it shrank the text enough to bring the cut token into view."""
    jwt = f"eyJ{'A' * 150}.eyJ{'A' * 150}.{'B' * 20}"
    head = f"sh -c 'notify {jwt}' "
    cut_inside_token = 23  # 'ghp_' and 19 of the token's 36 characters fall inside the window
    pad = "x" * (2 * MAX_REPORT_CHARS - len(head) - len(" --token ") - cut_inside_token)
    command = f"{head}{pad} --token {_TOKEN}"
    assert command[: 2 * MAX_REPORT_CHARS].endswith(_TOKEN[:cut_inside_token])

    text = report_text(command)

    assert _TOKEN[4:8] not in text
    assert text.startswith("sh -c 'notify jwt-<redacted>' xxx") and text.endswith("...<truncated>")


def test_report_text_marks_a_text_cut_by_the_redaction_window_as_truncated() -> None:
    text = report_text("a " * MAX_REPORT_CHARS + "tail")

    assert len(text) <= MAX_REPORT_CHARS and text.endswith("...<truncated>")


@pytest.mark.parametrize(
    "value",
    [
        "task-550e8400e29b41d4a716446655440000",
        "disk-0123456789abcdef0123",
        "risk-ABCDEFGHIJKLMNOPQRSTUV",
        "x_hf_" + "a" * 34,
        "pnpm_" + "A" * 36,
        "npm_config_cache",
    ],
)
def test_ids_that_contain_a_short_token_prefix_have_no_secret_shape(value: str) -> None:
    """Regression: 'sk-' had no left boundary, so 'task-<hex>' and 'disk-<hex>' ids read as API keys."""
    assert not has_secret_shape(value)
    assert url_credentials(f"https://h.example/mcp?session={value}", userinfo_rule="literal").query_keys == ()


@pytest.mark.parametrize(
    "token",
    [
        "sk-" + "abcdefghijklmnop1234",
        "sk-" + "ant-api03-" + "Ab0_" * 10,
        "sk-" + "ant-admin01-" + "Ab0-" * 10,
        "sk-" + "proj-" + "Ab0_" * 10,
        "sk-" + "svcacct-" + "Ab0-" * 10,
        "sk-" + "admin-" + "Ab0_" * 10,
        "xoxb-" + "1234567890-abcdefghij",
        "xoxe-1-" + "My0xLTEtMTIzNDU2Nzg5MC0xMjM0",
        "hf_" + "AbCdEfGhIjKlMnOpQrStUvWxYz012345",
        "npm_" + "AbCdEfGhIjKlMnOpQrStUvWxYz0123456789",
        "AKIA" + "ABCDEFGHIJ012345",
        "ASIA" + "ABCDEFGHIJ012345",
    ],
)
def test_every_canonical_token_shape_is_a_secret_and_is_redacted(token: str) -> None:
    assert has_secret_shape(token) and has_secret_shape(f"key={token}")
    assert token not in report_text(f"run --flag {token} --next")


@pytest.mark.parametrize("prefix", ["ant-api03-", "ant-admin01-", "proj-", "svcacct-", "admin-"])
def test_current_openai_and_anthropic_keys_have_a_secret_shape(prefix: str) -> None:
    """Regression: 'sk-' needed 16 letters or digits right after it, so 'sk-proj-...' and 'sk-ant-...' did not count."""
    token = "sk-" + prefix + "Ab0_" * 10
    assert url_credentials(f"https://h.example/mcp?q={token}", userinfo_rule="literal").query_keys == ("q",)
    assert token not in redact_secrets(f"node server.js {token}")
    assert not has_secret_shape("sk-" + prefix + "short")


@pytest.mark.parametrize("prefix", ["ghp_", "gho_", "ghu_", "ghs_", "ghr_"])
def test_every_github_token_prefix_has_a_secret_shape(prefix: str) -> None:
    token = prefix + "0123456789abcdefghij0123456789abcdef"
    assert has_secret_shape(token)
    assert url_credentials(f"https://h.example/mcp?q={token}", userinfo_rule="literal").query_keys == ("q",)


def test_fine_grained_github_token_has_a_secret_shape() -> None:
    assert has_secret_shape("github_pat_" + "11ABCDEFG0123456789_abcdefghijklmnopqrstuvwxyz")
    assert not has_secret_shape("github_pat_short")


# --------------------------------------------------------------------------- #
# redact_secrets on whole plugin lines                                         #
# --------------------------------------------------------------------------- #
@pytest.mark.parametrize(
    ("text", "shown"),
    [
        ("${MCP_URL:-https://u:pw@host/mcp?token=x}", "${MCP_URL:-https://host/mcp}"),
        ("https://h/${A:-x y}?token=t z", "https://h/${A:-x y} z"),
        ("a://x${B:-b://y?token=t}", "a://x${B:-b://y}"),
        ("https://h/?u=https://x:pw@z/w?v=1 next", "https://h/ next"),
        ("see https://h/a?q=1#frag and git+ssh://h/r.git?ref=v1", "see https://h/a#frag and git+ssh://h/r.git"),
        ("--api-key=pw1 TOKEN=pw2 --auth $Y", "--api-key=<redacted> TOKEN=<redacted> --auth $Y"),
    ],
)
def test_redact_secrets_drops_userinfo_and_queries_of_urls_anywhere_in_a_line(text: str, shown: str) -> None:
    assert redact_secrets(text) == shown


@pytest.mark.parametrize(
    "url",
    [
        "http:/deploy:[email protected]/hook",
        "http:\\deploy:[email protected]/hook",
        "http:deploy:[email protected]/hook",
        "http:/\\deploy:[email protected]/hook",
        "http:\\\\deploy:[email protected]/hook",
        "wss:/deploy:[email protected]/hook",
        "https://deploy:[email protected]/hook",
    ],
)
def test_report_text_and_redact_secrets_drop_userinfo_after_any_run_of_slashes(url: str) -> None:
    """Regression: WHATWG reads 'http:/user:pw@host' as userinfo, but only '//' (and, for redact_secrets, no slash)
    introduced it here, so the password stayed in hook targets and MCP command findings."""
    line = f"wget -qO- {url} | bash"
    for shown in (report_text(line), redact_secrets(line), redact_secrets(report_text(line))):
        assert "hunter2" not in shown and "deploy" not in shown
        assert "ci.example.org/hook | bash" in shown


# (prefix, run, repeats, suffix): the text, and what redact_secrets shows of it. Each text took 10 s to minutes to
# redact, because a pattern was tried at every word, URL, slash, or credential word of the run and read to its end.
_LONG_RUNS = [
    (("https://user:pw@h/", "-ab", 33_000, ""), ("https://h/", "-ab", 33_000, "")),
    (("", "-eyJ", 16_384, " --token hunter2"), ("", "-eyJ", 16_384, " --token <redacted>")),
    (("", "a.b", 33_000, " https://u:pw@h/"), ("", "a.b", 33_000, " https://h/")),
    (("", "a://", 25_000, "?token=hunter2"), ("", "a://", 25_000, "")),
    (("", "x://y", 20_000, "?sig=hunter2"), ("", "x://y", 20_000, "")),
    (("", "a://${x}", 12_000, "?token=hunter2"), ("", "a://${x}", 12_000, "")),
    (("", "/", 100_000, "u:pw@h"), ("", "/", 100_000, "h")),
    (("a:", "/", 100_000, "u:pw@h?token=hunter2"), ("a:", "/", 100_000, "h")),
    (("", "--auth", 16_000, "=hunter2"), ("", "--auth", 16_000, "=<redacted>")),
    (("", "TOKEN", 20_000, "=hunter2"), ("", "TOKEN", 20_000, "=<redacted>")),
    # 'https:user:pw@host', a scheme-less 'user:pw@host/path', and a quoted credential assignment.
    (("", "https:", 16_000, "u:pw@h/"), ("", "https:", 16_000, "h/")),
    (("", "=", 100_000, "u:pw@h/x"), ("", "=", 100_000, "u:<redacted>@h/x")),
    (("", "=a:", 33_000, ""), ("", "=a:", 33_000, "")),
    (("", "token']=", 12_000, "'hunter2'"), ("", "token']=", 12_000, "'<redacted>'")),
]


def test_redact_secrets_takes_linear_time_on_long_runs() -> None:
    """Regression (CWE-1333): SecurityValidator redacts a SkillSpector snippet or a PII line whole, so one crafted
    100 KB line stalled Tier 1 for minutes. Runs in a subprocess so that a slow redaction fails the timeout instead
    of hanging the suite; the limit is generous because the redactions take well under a second."""
    code = textwrap.dedent(
        """
        import json, sys
        from skillevaluator.validators.url_policy import redact_secrets

        def build(prefix, run, repeats, suffix):
            return prefix + run * repeats + suffix

        for text, expected in json.load(sys.stdin):
            shown = redact_secrets(build(*text))
            print("ok" if shown == build(*expected) else json.dumps([text[1], shown[:60], shown[-60:]]))
        """
    )

    completed = subprocess.run(
        [sys.executable, "-c", code], input=json.dumps(_LONG_RUNS), capture_output=True, text=True, timeout=60
    )

    assert completed.returncode == 0, completed.stderr
    assert completed.stdout.splitlines() == ["ok"] * len(_LONG_RUNS), completed.stdout


# --------------------------------------------------------------------------- #
# One credential rule for MCP server URLs and HTTP hook URLs                   #
# --------------------------------------------------------------------------- #
def _write_plugin(root: Path, mcp_server: dict[str, object], hook: dict[str, object]) -> Path:
    files = {
        ".claude-plugin/plugin.json": {"name": "demo", "version": "1.0.0", "description": "URL credential probe"},
        ".mcp.json": {"mcpServers": {"remote": mcp_server}},
        "hooks/hooks.json": {"hooks": {"PostToolUse": [{"matcher": "Write", "hooks": [hook]}]}},
    }
    for rel, content in files.items():
        (root / rel).parent.mkdir(parents=True, exist_ok=True)
        (root / rel).write_text(json.dumps(content), encoding="utf-8")
    return root


@pytest.mark.parametrize(
    ("url", "flagged"),
    [
        ("https://api.example.com/x?key=rawFAKEvalue0123", True),
        ("https://api.example.com/x?sig=v2", True),
        ("https://api.example.com/x?key=${API_KEY}", False),
        ("https://api.example.com/x?token=true", False),
        ("https://api.example.com/x?password_policy=strict", False),
        ("https://api.example.com/x?q=sk-" + "a1B2" * 5, True),
        (f"https://api.example.com/x?{_TOKEN}", True),
        ("https://api.example.com/x#/cb?access_token=abc123", True),
        ("https://api.example.com/x?page=2", False),
        ("https://admin:[email protected]/x", True),
        ("https://user:${PW}@api.example.com/x", True),
        ("https://admin:hunter2\\@api.example.com/x", True),
        (f"https://{_HEX_USER}\\@api.example.com/x", True),
        ("https://169.254.169.254\\@api.example.com/x", False),
    ],
)
def test_mcp_and_hook_urls_get_the_same_credential_verdict(tmp_path: Path, url: str, flagged: bool) -> None:
    root = _write_plugin(tmp_path / "demo", {"type": "http", "url": url}, {"type": "http", "url": url})

    checks = {finding.check_name for finding in analyze_plugin(root).findings}

    assert ("mcp_url_inline_secret" in checks, "plugin_hook_inline_secret" in checks) == (flagged, flagged)


def test_reference_only_userinfo_is_a_credential_for_a_hook_url_only(tmp_path: Path) -> None:
    """An HTTP hook's client sends any userinfo as Basic auth; an MCP server URL may hold references there."""
    url = "https://${USER}:${PW}@api.example.com/x"
    root = _write_plugin(tmp_path / "demo", {"type": "http", "url": url}, {"type": "http", "url": url})

    checks = {finding.check_name for finding in analyze_plugin(root).findings}

    assert "mcp_url_inline_secret" not in checks and "plugin_hook_inline_secret" in checks


# --------------------------------------------------------------------------- #
# Nested env defaults                                                          #
# --------------------------------------------------------------------------- #


@pytest.mark.parametrize(
    ("key", "value", "secret"),
    [
        ("API_KEY", "${A:-${B:-${C:-}}}", False),
        ("API_KEY", "${A:-${B:-rawFAKEvalue0123}}", True),
        ("PAGE", "${A:-${B:-2}}", False),
    ],
)
def test_nested_defaults_are_unwrapped(key: str, value: str, secret: bool) -> None:
    assert looks_like_inline_secret(key, value) is secret


def test_deeply_nested_defaults_do_not_exhaust_the_stack() -> None:
    """Regression: each nested '${A:-...}' layer was one recursive call, so about 1,000 layers raised RecursionError."""
    depth = 1200
    nested = "${A:-" * depth + "x" + "}" * depth

    assert looks_like_inline_secret("API_KEY", nested)
    assert url_credentials(f"https://h.example/mcp?token={nested}", userinfo_rule="literal").query_keys == ("token",)


# --------------------------------------------------------------------------- #
# Authorization headers with any scheme                                        #
# --------------------------------------------------------------------------- #
@pytest.mark.parametrize(
    ("key", "value", "secret"),
    [
        ("Authorization", "Token " + "9944b091" * 5, True),
        ("authorization", "token " + "abc123" * 4, True),
        ("Authorization", "ApiKey " + "c2VjcmV0" * 3, True),
        ("Authorization", "Bot " + "x1" * 10, True),
        ("Proxy-Authorization", "Token " + "ab12" * 5, True),
        ("Authorization", "raw" + "0123456789" * 2, True),
        ("Authorization", "ApiKey ${API_KEY:-" + "literal0123" + "}", True),
        ("Authorization", "Bearer ${TOKEN}", False),
        ("Authorization", "Token ${TOKEN}", False),
        ("Authorization", "ApiKey ${API_KEY}", False),
        ("Authorization", "Bot ${BOT_TOKEN}", False),
        ("Proxy-Authorization", "Basic ${env:PROXY_AUTH}", False),
        ("Authorization", "${AUTH_HEADER}", False),
        ("Authorization", "Bearer", False),
        ("Authorization", "Basic ${USER}:${PASS}", False),
        ("Authorization", "${USER}:${PASS}", False),
        ("Authorization", "Bearer ${A}.${B}", False),
        ("Authorization", "Basic ${USER}:" + "literal0123", True),
    ],
)
def test_an_authorization_header_names_a_credential_whatever_its_scheme(key: str, value: str, secret: bool) -> None:
    """Regression: only 'Bearer' and 'Basic' payloads counted, so 'Authorization: Token <hex>' was no credential."""
    assert is_credential_key(key, query=True)
    assert looks_like_inline_secret(key, value, query=True) is secret


def test_an_authorization_env_name_needs_a_value_shaped_like_key_material() -> None:
    assert not is_credential_key("AUTHORIZATION")
    assert not looks_like_inline_secret("AUTHORIZATION", "required", query=False)
    assert looks_like_inline_secret("AUTHORIZATION", "Token" + "9944b091" * 5, query=False)


@pytest.mark.parametrize(
    ("header", "value"),
    [
        ("Authorization", "Token " + "9944b091" * 5),
        ("Authorization", "ApiKey " + "c2VjcmV0" * 3),
        ("Proxy-Authorization", "Token " + "ab12" * 5),
    ],
)
def test_mcp_and_hook_authorization_headers_with_any_scheme_are_inline_secrets(
    tmp_path: Path, header: str, value: str
) -> None:
    headers = {header: value}
    root = _write_plugin(
        tmp_path / "demo",
        {"type": "http", "url": "https://api.example.com/mcp", "headers": headers},
        {"type": "http", "url": "https://hooks.example.com/x", "headers": headers},
    )

    checks = {finding.check_name for finding in analyze_plugin(root).findings}

    assert {"mcp_inline_secret", "plugin_hook_inline_secret"} <= checks