Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

15.5 KB
Raw Blame
"""Redact credentials embedded in a configured URL before it is logged.

Operators sometimes put an API key straight into an LLM base URL -- as
userinfo (``https://user:key@host/v1``), as a query or fragment parameter
(``?api_key=...``, ``#access_token=...``), as a ``;`` path parameter
(``/v1;api_key=...``), or as a bare path segment (``/sk-.../v1``).
:func:`redact_url_credentials` removes or masks those while keeping the URL
legible for troubleshooting.

Dependency-free on purpose (``re`` / ``urllib`` only), so provider, router
and error-handling modules can all import it without an import cycle.
"""

from __future__ import annotations

import re
from urllib.parse import unquote_plus, urlsplit, urlunsplit

REDACTED = "REDACTED"

# A parameter NAME is a credential name when any of its PARTS is. The
# parts of a PHP/Rails-style array name are the text around its ``[`` /
# ``]`` brackets (``api_key[0]`` -> ``api_key``; ``auth[token]`` ->
# ``auth``, ``token``; ``api_key[prod]`` -> ``api_key``, ``prod``); an
# all-digit or empty part (an index) is ignored, and a stray bracket
# (``api_key]``) simply ends a part. A part is normalised by splitting
# camelCase (``accessToken`` -> ``access_token``, ``XAPIKey`` ->
# ``XAPI_Key``), lower-casing, and splitting into words on every character
# that is not an ASCII letter or digit (``_``, ``-``, ``.``, whitespace --
# ``api+key`` decodes to ``api key`` -- and also ``%00``, ``:`` and the
# like). A part is a credential name when EITHER
#   * its last word is in ``_CREDENTIAL_SUFFIX_WORDS`` (so ``api_key``,
#     ``x-api-key``, ``subscription-key``, ``access_token``, ``authToken``,
#     ``clientSecret``, ``x-amz-signature``, ``db.password``,
#     ``passphrase``, and the OAuth grant secrets ``assertion``,
#     ``client_assertion`` (RFC 7521/7523), ``code_verifier`` (RFC 7636)
#     and ``oauth_verifier`` (RFC 5849) all match), OR
#   * its last word is a run-together compound: it ends in one of
#     ``_RUN_TOGETHER_SUFFIXES`` (``authtoken``, ``APITOKEN``,
#     ``clientsecret``, ``clientassertion``, ``dbpassword``,
#     ``keypassphrase``) or in one of the ``...key`` words of
#     ``_RUN_TOGETHER_KEY_NAMES`` (``apikey``, ``xapikey``, ``secretkey``,
#     ``privatekey``). ``key`` itself is NOT a run-together suffix, because
#     ordinary words end in it (``monkey``, ``turkey``, ``hockey``); a
#     ``...key`` compound outside that list (``fookey``) is not matched, OR
#   * its words, joined without separators, are one of
#     ``_CREDENTIAL_EXACT_NAMES`` -- credential names that cannot be
#     matched by their ending without also matching ordinary names:
#     ``code`` (``error_code``), ``auth`` (``oauth``), ``refresh``
#     (``force_refresh``), ``device_code`` (RFC 8628; ``country_code``),
#     ``id_token_hint`` (it carries an OpenID Connect ID token;
#     ``login_hint``) and the OAuth authorization code ``auth_code`` /
#     ``authorization_code`` (RFC 6749). Joining makes ``device_code``,
#     ``deviceCode`` and the run-together ``devicecode`` / ``DEVICECODE``
#     one name.
# Each of these tests is also run with a trailing number or version taken
# off the part -- final words that are digits or ``v`` + digits are
# dropped, then trailing digits (and a ``v`` just before them) come off the
# last word -- and once more with a plural ``s`` also taken off the last
# word (not when it ends in ``ss``). So ``api_key_2``, ``apiKey2``,
# ``key1``, ``token2``, ``access_token_v2``, ``x-api-key-v2`` and
# ``api_keys`` match, while ``page2``, ``items`` and ``v2`` do not; this
# only ever adds matches.
# Other words match only as a whole word, so ``tokenizer``, ``passthrough``,
# ``monkey`` and ``keyboard`` never match. Errs towards over-redaction: a
# non-secret ``sort_key``, ``sort_keys``, ``max_tokens`` or
# ``api_key[prod]`` is redacted too, and a name longer than
# ``_MAX_PARAM_NAME_LEN`` is treated as a credential name without being
# inspected (fail closed, and bounded work).
_CREDENTIAL_SUFFIX_WORDS = frozenset(
    {
        "key",
        "apikey",
        "token",
        "secret",
        "password",
        "passwd",
        "pwd",
        "pass",
        "passphrase",
        "signature",
        "sig",
        "credential",
        "credentials",
        "jwt",
        "session",
        "assertion",
        "verifier",
    }
)
_RUN_TOGETHER_SUFFIXES = (
    "token",
    "secret",
    "password",
    "passwd",
    "passphrase",
    "assertion",
    "verifier",
)
_RUN_TOGETHER_KEY_NAMES = (
    "apikey",
    "appkey",
    "accesskey",
    "authkey",
    "clientkey",
    "encryptionkey",
    "licensekey",
    "masterkey",
    "passkey",
    "privatekey",
    "secretkey",
    "sessionkey",
    "signingkey",
    "subscriptionkey",
)
# Stored without separators: compared with a part's words joined by "".
_CREDENTIAL_EXACT_NAMES = frozenset(
    name.replace("_", "")
    for name in (
        "sas",
        "auth",
        "authorization",
        "bearer",
        "code",
        "refresh",
        "device_code",
        "id_token_hint",
        "auth_code",
        "authorization_code",
    )
)
# A final name word that is a number or a version (``2``, ``v2``).
_NUMBER_OR_VERSION_WORD_RE = re.compile(r"v?[0-9]+")
# Longest parameter name inspected word by word; a longer one is treated
# as a credential name. Real parameter names are far shorter.
_MAX_PARAM_NAME_LEN = 256
# Longest value :func:`redact_url_credentials` parses; a longer one is
# logged as ``<unknown>``. A configured base URL is far shorter; the cap
# bounds the work done in (often unconditional) log lines on a value a
# user can supply.
MAX_REDACTABLE_URL_LEN = 4096
_CAMEL_BOUNDARY_RE = re.compile(
    r"(?<=[a-z0-9])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])"
)
_NAME_WORD_SEPARATOR_RE = re.compile(r"[^a-z0-9]+")
_ARRAY_BRACKET_RE = re.compile(r"[\[\]]")
# ``?`` is a separator too: ``?x=1?api_key=...`` (a repeated ``?``) and a
# value that embeds a URL (``next=https://h/p?token=...``) both carry their
# credential after a ``?`` that ``urlsplit`` leaves inside the query.
_PARAM_SEPARATORS_RE = re.compile(r"([&;?])")
# A value-less query/fragment item at least this long (``?sk-...``) is
# assumed to be a bare credential.
_BARE_ITEM_REDACT_MIN_LEN = 17
# Path segments shaped like a provider key:
#   * ``sk-`` / ``key-`` in any letter case (``SK-``, ``Key-``) + 16 or
#     more base64url characters, or
#   * 32+ characters of ``[A-Za-z0-9_]`` -- NO ``-`` -- containing both a
#     letter and a digit.
# Excluding ``-`` keeps hyphenated model and deployment names
# (``text-embedding-3-large-2024-preview-model``, UUIDs) legible. The
# trade-offs: a dash-bearing key without an ``sk-``/``key-`` prefix (some
# Google keys) is not caught, and a 32-hex id such as a Cloudflare account
# id IS redacted (over-redaction, the safe direction).
_KEY_LIKE_SEGMENT_RE = re.compile(
    r"^(?:(?:sk|key)-[A-Za-z0-9_\-]{16,}"
    r"|(?=[A-Za-z0-9_]*[A-Za-z])(?=[A-Za-z0-9_]*[0-9])[A-Za-z0-9_]{32,})$",
    # ASCII: without it, IGNORECASE lets the classes match four non-ASCII
    # letters too (U+0130, U+0131, U+017F, U+212A).
    re.IGNORECASE | re.ASCII,
)
_PORT_RE = re.compile(r"[0-9]+")
# Characters that never belong in a host we log: a ``;`` / ``&`` / ``=``
# after the host means parameters (``https://host;api_key=...``) ended up
# inside the parsed authority, where they would not be redacted.
_AUTHORITY_PARAM_CHARS_RE = re.compile(r"[;&=]")
# End of a scheme-less value's leading authority-like part.
_AUTHORITY_END_RE = re.compile(r"[/?#]")
_HTTP_SCHEMES = frozenset({"http", "https"})


def _name_variants(words: list[str]) -> list[list[str]]:
    """``words`` as given, without a trailing number or version, and
    without that and a plural ``s`` (see the rule above). A last word that
    the stripping would leave empty (``v2``, ``s``) is kept as it was."""
    trimmed = list(words)
    while len(trimmed) > 1 and _NUMBER_OR_VERSION_WORD_RE.fullmatch(
        trimmed[-1]
    ):
        trimmed.pop()
    last = trimmed[-1]
    stem = last.rstrip("0123456789")
    if stem != last and stem.endswith("v"):
        stem = stem[:-1]
    if stem:
        trimmed[-1] = stem
    variants = [words, trimmed]
    last = trimmed[-1]
    if len(last) > 1 and last.endswith("s") and not last.endswith("ss"):
        variants.append([*trimmed[:-1], last[:-1]])
    return variants


def _words_name_a_credential(words: list[str]) -> bool:
    """The three tests of the rule above, for one spelling of a part."""
    if "".join(words) in _CREDENTIAL_EXACT_NAMES:
        return True
    last = words[-1]
    return (
        last in _CREDENTIAL_SUFFIX_WORDS
        or last.endswith(_RUN_TOGETHER_SUFFIXES)
        or last.endswith(_RUN_TOGETHER_KEY_NAMES)
    )


def _is_credential_name_part(part: str) -> bool:
    """The rule above for one bracket-free part of a parameter name."""
    words = [
        w
        for w in _NAME_WORD_SEPARATOR_RE.split(
            _CAMEL_BOUNDARY_RE.sub("_", part).lower()
        )
        if w
    ]
    if not words:
        return False
    return any(_words_name_a_credential(v) for v in _name_variants(words))


def is_credential_param_name(raw_name: str) -> bool:
    """Return True when a URL parameter name names a credential.

    Linear in the length of the name, and a name longer than
    ``_MAX_PARAM_NAME_LEN`` is reported
    as a credential name without further inspection."""
    if len(raw_name) > _MAX_PARAM_NAME_LEN:
        return True
    return any(
        _is_credential_name_part(part)
        for part in _ARRAY_BRACKET_RE.split(unquote_plus(raw_name))
        if part.strip() and not part.strip().isdigit()
    )


def _redact_param_list(params: str, *, redact_bare: bool) -> str:
    """Redact credential values in a ``&`` / ``;`` / ``?`` separated list,
    keeping every other parameter and separator unchanged."""
    if not params:
        return params
    out = []
    for piece in _PARAM_SEPARATORS_RE.split(params):
        if piece in ("&", ";", "?"):
            out.append(piece)
            continue
        name, sep, _value = piece.partition("=")
        if sep and is_credential_param_name(name):
            # nosemgrep: semgrep.rules.sql-string-concatenation, reason: URL parameter text
            piece = f"{name}={REDACTED}"
        elif (
            not sep and redact_bare and len(piece) >= _BARE_ITEM_REDACT_MIN_LEN
        ):
            piece = REDACTED
        out.append(piece)
    return "".join(out)


def _redact_path(path: str) -> str:
    """Redact ``;name=value`` path parameters and key-shaped segments."""
    segments = []
    for segment in path.split("/"):
        head, sep, params = segment.partition(";")
        if _KEY_LIKE_SEGMENT_RE.match(head):
            head = REDACTED
        if sep:
            params = _redact_param_list(params, redact_bare=False)
        segments.append(f"{head}{sep}{params}")
    return "/".join(segments)


def _drop_schemeless_userinfo(url: str) -> str:
    """``user:secret@host/v1`` (no ``//``) parses with ``user`` as the
    scheme and the secret in the path; drop everything up to the ``@`` of
    the leading authority-like part, which ends at the first ``/``, ``?``
    or ``#``. An ``@`` only later in the value (a password containing one
    of those such as ``user:Abc/123@host``, or an ``@`` in the query or
    fragment such as ``host?x=a@b``) is left for the caller's fail-closed
    ``@`` check."""
    head = _AUTHORITY_END_RE.split(url, maxsplit=1)[0]
    if "@" in head:
        return url[head.rindex("@") + 1 :]
    return url


def _authority_is_well_formed(authority: str) -> bool:
    """True when ``host[:port]`` (or ``[v6][:port]``) has an empty or
    all-digit port. A non-digit "port" means ``urlsplit`` cut the
    authority short -- typically at a ``/``, ``?`` or ``#`` inside an
    unencoded password -- so the real userinfo was never isolated. Also
    False when the authority holds a ``;``, ``&`` or ``=``
    (``host;api_key=...``): parameters there are not redacted."""
    if _AUTHORITY_PARAM_CHARS_RE.search(authority):
        return False
    if authority.startswith("["):
        close = authority.find("]")
        if close == -1:
            return False
        rest = authority[close + 1 :]
        if not rest:
            return True
        if not rest.startswith(":"):
            return False
        port = rest[1:]
    else:
        port = authority.rpartition(":")[2] if ":" in authority else ""
    return not port or bool(_PORT_RE.fullmatch(port))


def redact_url_credentials(url: object) -> str:
    """Return ``url`` with embedded credentials removed, for logging.

    * userinfo (``user:pass@``) is dropped -- also for a scheme-less
      ``user:pass@host/v1``;
    * in the query string, the fragment, and ``;`` path parameters, the
      value of every credential-named parameter (see the name rule above;
      ``&``, ``;`` and ``?`` all separate parameters) becomes ``REDACTED``;
    * a value-less query or fragment item of 17+ characters (``?sk-...``)
      becomes ``REDACTED``;
    * a path segment shaped like a provider key (see
      ``_KEY_LIKE_SEGMENT_RE``) becomes ``REDACTED``.

    Fails closed -- returns ``"<unknown>"`` -- when the userinfo cannot be
    isolated reliably: any ``@`` in the path, query or fragment (so the
    count of ``@`` in the value differs from the count in the parsed
    authority -- this includes a harmless ``[email protected]``), or an
    authority whose port is not all digits (an unencoded ``/``, ``?`` or
    ``#`` in a password ends the authority early), or an authority that
    contains ``;``, ``&`` or ``=`` (``https://host;api_key=...``).
    Non-string and empty inputs, and any value longer than
    ``MAX_REDACTABLE_URL_LEN`` characters, also return ``"<unknown>"``;
    the function never raises, and its work is linear in the input.

    The scheme is lower-cased (``urlsplit`` does that); the host (with its
    original case) and the port are kept as given; the rest of the URL is
    kept unchanged except for the replacements above (``urlsplit`` also
    drops an empty trailing ``?`` or ``#``).

    Limits: a credential under a name the rule does not recognise, a short
    or non key-shaped path segment, and percent-encoded separators inside a
    value are not detected.
    """
    if not isinstance(url, str) or not url or len(url) > MAX_REDACTABLE_URL_LEN:
        return "<unknown>"
    try:
        parsed = urlsplit(url)
        if not parsed.netloc and parsed.scheme.lower() not in _HTTP_SCHEMES:
            stripped = _drop_schemeless_userinfo(url)
            if stripped != url:
                url = stripped
                parsed = urlsplit(url)
    except ValueError:
        return "<unknown>"
    if url.count("@") != parsed.netloc.count("@"):
        # An @ outside the authority (path, query or fragment): either the
        # userinfo was not isolated -- a password containing ``@`` and then
        # ``/``, ``?`` or ``#`` splits across the authority boundary -- or a
        # scheme-less value still carries one after stripping. Also covers
        # an innocent ``[email protected]``: withheld rather than guessed.
        return "<unknown>"
    redacted = parsed._replace(
        path=_redact_path(parsed.path),
        query=_redact_param_list(parsed.query, redact_bare=True),
        fragment=_redact_param_list(parsed.fragment, redact_bare=True),
    )
    if parsed.netloc:
        authority = parsed.netloc.rpartition("@")[2]
        if not _authority_is_well_formed(authority):
            return "<unknown>"
        redacted = redacted._replace(netloc=authority)
    elif redacted == parsed:
        # Nothing to redact: pass a scheme-less value through as given.
        return url or "<unknown>"
    return urlunsplit(redacted) or "<unknown>"