Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
01b083e
tests: pin that the HTTP listener decodes no Content-Encoding (BACKLO…
wshallwshall Dec 1, 2026
64e6535
docs: record that the HTTP listener decodes no Content-Encoding (BACK…
wshallwshall Dec 1, 2026
c19c936
tests: pin the Content-Encoding refusal reasons and a plain control (…
wshallwshall Dec 1, 2026
acd5e20
docs: correct the Content-Encoding paragraph after review (BACKLOG #5…
wshallwshall Dec 1, 2026
ca7aad2
tests: pin the NUL-free deflate case and the not-queued half (BACKLOG…
wshallwshall Dec 1, 2026
53c766f
docs: narrow the Content-Encoding paragraph after review round 2 (BAC…
wshallwshall Dec 1, 2026
6530ca4
http_listener: answer Expect: 100-continue once authorised (BACKLOG #…
wshallwshall Dec 1, 2026
1b1ad69
docs: the HTTP listener answers Expect: 100-continue (BACKLOG #6476)
wshallwshall Dec 1, 2026
47b519e
http_listener: no 100 Continue once stop() began; a stalled one reads…
wshallwshall Dec 1, 2026
28d79ff
docs: narrow the Expect: 100-continue paragraph after review (BACKLOG…
wshallwshall Dec 1, 2026
7896419
http_listener: answer only a single Expect: 100-continue line (BACKLO…
wshallwshall Dec 1, 2026
c665c07
docs: say which Expect requests get no 100 Continue (BACKLOG #6476)
wshallwshall Dec 1, 2026
a8b3c76
http_listener: the 100 Continue carries the baseline headers (BACKLOG…
wshallwshall Dec 1, 2026
54097b0
http_listener: a listener-wide in-flight body budget (BACKLOG #6978)
wshallwshall Dec 1, 2026
877bfc5
docs: the HTTP listener's max_inflight_body_bytes (BACKLOG #6978)
wshallwshall Dec 1, 2026
1a21e9c
http_listener: charge the body budget on bytes received (BACKLOG #6978)
wshallwshall Dec 1, 2026
680a87d
docs: max_inflight_body_bytes charges bytes received (BACKLOG #6978)
wshallwshall Dec 1, 2026
b9af83d
http_listener: one body buffer, early share release, factory check (B…
wshallwshall Dec 1, 2026
12bbd90
docs: max_inflight_body_bytes refusals record no message row (BACKLOG…
wshallwshall Dec 1, 2026
3a6cb74
http_listener: a refused body read releases its bytes before the shar…
wshallwshall Dec 2, 2026
b00373f
http_listener: every early exit frees the body before its writes (BAC…
wshallwshall Dec 2, 2026
516b6ea
docs: max_inflight_body_bytes does not charge the stream's own buffer…
wshallwshall Dec 2, 2026
777852e
http_listener: clear the raising frames before an early give-back (BA…
wshallwshall Dec 2, 2026
479c1e9
docs: state how far the uncharged stream buffer can reach (BACKLOG #6…
wshallwshall Dec 2, 2026
6fd10ae
http_listener: refuse intake_auth_health='allow' under mtls_subject (…
wshallwshall Dec 1, 2026
10b946b
docs: intake_auth_health='allow' is refused under mtls_subject (BACKL…
wshallwshall Dec 1, 2026
8aaad04
http_listener: refuse allow under mtls_subject before the TLS build (…
wshallwshall Dec 1, 2026
f11f153
docs: a probe certificate under mtls_subject may submit messages (BAC…
wshallwshall Dec 1, 2026
57e7332
wiring: refuse mtls_subject 'allow' after the TLS and subject checks …
wshallwshall Dec 1, 2026
20c9193
docs: changelog fragment for the mtls_subject 'allow' refusal (BACKLO…
wshallwshall Dec 1, 2026
4d3851c
http_listener: refuse an intake_auth mode the listener does not know …
wshallwshall Dec 2, 2026
3675930
docs: an unknown intake_auth is refused when the connection is built …
wshallwshall Dec 2, 2026
620581d
http_listener: one intake_auth mode set, and a falsy mode refused (BA…
wshallwshall Dec 2, 2026
ffd3949
docs: the unknown-intake_auth changelog states what bounded it (BACKL…
wshallwshall Dec 2, 2026
f7a84ab
http_listener: refuse an explicit empty intake_auth; keep its own mod…
wshallwshall Dec 2, 2026
3cfefb1
docs: an empty intake_auth is refused; leaving it out means none (BAC…
wshallwshall Dec 2, 2026
9eac3df
http_listener, Http(): refuse an intake_auth that is not a plain str …
wshallwshall Dec 2, 2026
ea56c1a
http_listener, Http(), gate: name the plain-str remedy; one reading i…
wshallwshall Dec 2, 2026
8f7ca3b
docs: intake_auth must be a plain string; the changelog names the enu…
wshallwshall Dec 2, 2026
19ffcba
Merge origin/main into b20054-6978b (BACKLOG #6978)
wshallwshall Dec 2, 2026
5fcaced
Merge remote-tracking branch 'origin/b20054-6978b' into b20054-6979c-m
wshallwshall Dec 2, 2026
d46eeee
Merge origin/main into b20054-6979c after PR 3548 landed (BACKLOG #6979)
wshallwshall Dec 2, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions changelog.d/6979.security.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
- **The HTTP listener refuses `intake_auth_health = "allow"` under `intake_auth = "mtls_subject"`.**
The client certificate is required at the TLS handshake and checked when the connection is
accepted, before the request method is read, so the setting exempted no probe. A probe still
needs a listed certificate, and that certificate may submit messages too. The combination is now
refused when the configuration loads and when the connection is built, naming both settings;
remove `intake_auth_health` to load. See the `intake_auth_health` row in
[CONNECTIONS.md](../docs/CONNECTIONS.md). (`BACKLOG #6979`)
- **The HTTP listener refuses an `intake_auth` value it does not know when the connection is
built.** A connection written with `Http()` or in `connections.toml` was already refused at load
for a value that was not one of the modes. A connection built any other way reached the listener,
which checked no credential for a typo such as `"API_KEY"`, an empty value or an explicit null.
Leaving `intake_auth` out still means `none`.
- **`Http()` and the HTTP listener refuse an `intake_auth` that is not a plain string.** An enum
member with a `str` base, equal to a mode such as `"api_key"`, passed both checks, and the
listener then read it under another name, so it checked no credential. That held for
`Http()`-written connections too. Every `str` subclass is now refused, a `StrEnum` member
included, which was read correctly before; the refusal says to pass the string itself, such as
the member's `.value`. `connections.toml` always yields plain strings. See the `intake_auth` row in [CONNECTIONS.md](../docs/CONNECTIONS.md). (`BACKLOG #6979`)
4 changes: 2 additions & 2 deletions docs/CONNECTIONS.md

Large diffs are not rendered by default.

38 changes: 33 additions & 5 deletions messagefoundry/config/wiring.py
Original file line number Diff line number Diff line change
Expand Up @@ -3692,7 +3692,7 @@
| None = None, # mtls_subject allow-list: issuing CA's DN -> QUALIFIED "CN:<v>" / "SAN:DNS:<v>"
intake_auth_health: Literal[
"require", "allow"
] = "require", # whether GET/HEAD probes need the credential too
] = "require", # whether GET/HEAD probes need the credential too ('allow' refused under mtls_subject)
intake_auth_rate_limit: int
| None = INTAKE_AUTH_RATE_LIMIT_DEFAULT, # FAILED intake-auth attempts per minute per peer (0/None disables)
intake_auth_rate_limit_global: int
Expand Down Expand Up @@ -3787,7 +3787,9 @@

Health probes are **inside** the gate by default; ``intake_auth_health="allow"`` exempts ``GET``/
``HEAD`` for a load-balancer check, at the cost of an unauthenticated "is MessageFoundry up, and
where" oracle on a PHI intake socket.
where" oracle on a PHI intake socket. Under ``api_key`` and ``bearer`` only: under
``mtls_subject`` it is refused, since the certificate is checked before the method is read
(BACKLOG #6979).

``trusted_proxies`` (BACKLOG #5525) names the HTTP reverse proxies in front of the listener
that append the client's address to ``X-Forwarded-For``, under ``api_key`` and ``bearer``
Expand Down Expand Up @@ -4115,9 +4117,12 @@

#: The intake-auth modes that carry a shared-secret credential (as opposed to a client certificate).
_INTAKE_KEY_MODES = ("api_key", "bearer")
#: Every intake-auth mode ``Http()`` accepts. The HTTP listener keeps its own copy, the modes it
#: enforces, and refuses any other value too; a test pins the two equal (BACKLOG #6979).
INTAKE_AUTH_MODES = ("none", *_INTAKE_KEY_MODES, "mtls_subject")


def _validate_intake_auth(settings: Mapping[str, Any]) -> None:

Check warning on line 4125 in messagefoundry/config/wiring.py

View workflow job for this annotation

GitHub Actions / complexity triage (advisory)

Complexity increased

`_validate_intake_auth` complexity 17 -> 18 (mccabe threshold 10)
"""Refuse an intake-auth configuration that cannot work, at **factory** time (ADR 0154 D4).

Runs with no store and no posture, so it fires identically in ``messagefoundry check``, in dry-run,
Expand All @@ -4134,15 +4139,27 @@
* a credential configured while ``intake_auth="none"`` — nothing would ever check it, and an
operator reading that config would reasonably believe the listener was protected;
* ``intake_client_subjects`` under ``api_key`` or ``bearer`` — the same reason: only
``mtls_subject`` reads a client certificate (vault BACKLOG #2287).
``mtls_subject`` reads a client certificate (vault BACKLOG #2287);
* ``intake_auth_health="allow"`` under ``mtls_subject`` — the certificate is checked before the
method is read, so the exemption would exempt nothing (BACKLOG #6979);
* an ``intake_auth`` that is not a plain ``str``, such as an enum member equal to a mode: the
listener refuses it at build, so this refuses it at load (BACKLOG #6979).

The secret itself must be an ``env()`` reference with no default and no cast, mirroring
``File(credential_password=...)`` (ADR 0132): a fallback credential is a silent credential.
"""
mode = settings["intake_auth"]
if mode not in ("none", *_INTAKE_KEY_MODES, "mtls_subject"):
# A plain str only, as the listener requires (BACKLOG #6979): a str subclass, such as a
# (str, Enum) member equal to "api_key", passes membership yet str() may render it as another
# string, so refusing it here reports at load what the listener would refuse at build.
if type(mode) is not str or mode not in INTAKE_AUTH_MODES:
hint = (
", as a plain str: pass the string itself, such as an enum member's .value"
if isinstance(mode, str) and type(mode) is not str
else ""
)
raise WiringError(
"Http intake_auth must be one of none/api_key/bearer/mtls_subject (value withheld)"
f"Http intake_auth must be one of {'/'.join(INTAKE_AUTH_MODES)}{hint} (value withheld)"
)
if settings["intake_auth_health"] not in ("require", "allow"):
raise WiringError("Http intake_auth_health must be 'require' or 'allow' (value withheld)")
Expand Down Expand Up @@ -4223,6 +4240,17 @@
"with a pinned SAN; any other name matches nothing and would 403 every request from the "
"partner it names"
)
# Last, so a configuration missing TLS or its subjects hears that first, and the reason
# given below, a certificate checked at accept, is then always true.
if settings["intake_auth_health"] == "allow":
# BACKLOG #6979: the certificate is required at the handshake and checked at accept, before
# the method is read, so no probe could be exempted; honouring it would mean an optional
# certificate at the handshake for every request, which is not done.
raise WiringError(
"Http intake_auth_health='allow' cannot apply under intake_auth='mtls_subject': the "
"client certificate is checked before the request method is read, so a GET/HEAD probe "
"still needs a listed certificate; remove intake_auth_health"
)


def _intake_subjects_by_issuer(
Expand Down
5 changes: 4 additions & 1 deletion messagefoundry/pipeline/wiring_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -17151,7 +17151,10 @@ def _allowlist_weighed(settings: Mapping[str, Any]) -> bool:
``intake_auth`` value but a mode judged alone, ``none`` or one ``Http()`` would have refused.
One reading, so :func:`check_http_intake_auth` names the list's shortfall exactly when
:func:`_has_effective_peer_control` weighed the list."""
return str(settings.get("intake_auth") or "none") not in _INTAKE_MODES_JUDGED_ALONE
mode = settings.get("intake_auth") or "none"
# Only a plain str is judged alone, as only a plain str passes Http() and the listener
# (BACKLOG #6979); str() of an enum member equal to a mode may read as that mode.
return type(mode) is not str or mode not in _INTAKE_MODES_JUDGED_ALONE


def _has_effective_peer_control(settings: Mapping[str, Any]) -> bool:
Expand Down
35 changes: 34 additions & 1 deletion messagefoundry/transports/http_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@
#: The most body bytes one read takes while the budget is charged, so a share grows with the bytes
#: actually received rather than with what a peer declared.
_BODY_CHUNK_BYTES = 64 * 1024
#: The intake_auth modes this listener enforces: ``_authorize_head`` checks the two key modes and
#: ``_authorize_peer_cert`` the certificate one. Any other value is refused when the source is built
#: (BACKLOG #6979). Written here rather than read from the config layer, so a mode added there and
#: not enforced here is refused rather than served unauthenticated; a test pins the two equal.
_INTAKE_AUTH_MODES = ("none", "api_key", "bearer", "mtls_subject")
#: Added to the unknown-mode refusal when the value is a ``str`` subclass, such as an enum member,
#: which is refused for its type alone; it names the remedy without naming the value.
_PLAIN_STR_HINT = ", as a plain str: pass the string itself, such as an enum member's .value"
#: The ``Retry-After`` seconds on the ``503`` refusing a body the in-flight budget has no room for.
#: The budget frees as soon as the bodies ahead of it are committed, so a short wait is enough.
BODY_BUDGET_RETRY_AFTER_SECONDS = 5
Expand Down Expand Up @@ -1106,6 +1114,31 @@ def __init__(self, config: Source) -> None:
self.port: int = port_setting(s["port"], transport="HTTP source")
#: How this source names itself in a log line.
self._log_name: str = config.name or f"{self.host}:{self.port}"
intake_auth = s.get("intake_auth", "none")
if type(intake_auth) is not str or intake_auth not in _INTAKE_AUTH_MODES:
# BACKLOG #6979, as the Http() factory refuses it, for a Source built any other way:
# every check below reads a mode it does not know as no authentication, so a typo
# would check no credential. Only an absent key reads as "none"; an explicit None,
# an empty value, a False or a 0 is refused, as the factory refuses it. Only a plain
# str passes: a str subclass, such as a (str, Enum) member equal to "api_key", is
# equal to a mode yet str() may render it as another string, so the value checked
# here is exactly the value stored and enforced below, with no str() between them.
# The value is withheld, as the factory withholds it.
subclass = isinstance(intake_auth, str) and type(intake_auth) is not str
hint = _PLAIN_STR_HINT if subclass else ""
raise ValueError(
f"HTTP source {self._log_name}: intake_auth must be one of "
f"{'/'.join(_INTAKE_AUTH_MODES)}{hint} (value withheld)"
)
if intake_auth == "mtls_subject" and s.get("intake_auth_health") == "allow":
# BACKLOG #6979, as the Http() factory refuses it, for a Source built any other way, and
# before the TLS build below can log or refuse first. `_authorize_peer_cert` runs at
# accept, before the request method is read, so no probe could be exempted.
raise ValueError(
f"HTTP source {self._log_name}: intake_auth_health='allow' cannot apply under "
"intake_auth='mtls_subject', because the client certificate is checked before the "
"request method is read; remove intake_auth_health"
)
# Caps below: key absent → secure default; None/0 in any spelling (including the string "0"
# an uncast env() yields) → disabled where allowed; a negative or NaN → refused at build
# (BACKLOG #1872).
Expand Down Expand Up @@ -1200,7 +1233,7 @@ def __init__(self, config: Source) -> None:
# this inbound, mints no identity and opens no session. Defaults to "none", which makes every
# path below a no-op and every shipped configuration byte-identical. The env() refs are already
# resolved to strings by the time a Source reaches here (_source_config → resolve_env_settings).
self.intake_auth: str = str(s.get("intake_auth") or "none")
self.intake_auth: str = intake_auth
self._intake_keys: tuple[str | None, str | None] = (
s.get("intake_api_key"),
s.get("intake_api_key_next"),
Expand Down
134 changes: 134 additions & 0 deletions tests/test_inbound_http_intake_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import asyncio
import datetime
import enum
import inspect
import json
from typing import Any
Expand Down Expand Up @@ -142,6 +143,139 @@ def test_credential_configured_without_a_mode_is_refused() -> None:
Http(port=8080, intake_client_subjects={PARTNER_CA: ["CN:partner.example"]})


@pytest.mark.parametrize(
"mode",
["API_KEY", "apikey", "api-key", "mtls", "basic", " bearer", "a-pasted-credential", False, 0],
)
def test_the_listener_refuses_an_intake_auth_mode_it_does_not_know(mode: object) -> None:
"""A Source built other than through ``Http()`` skips the factory's mode check, and the listener
read any mode it did not know as no authentication: a typo checked no credential. It refuses
instead, as the factory does. The whole message is pinned, so no part of the value is in it."""
with pytest.raises(ValueError) as refused:
HttpSource(
Source(
type=ConnectorType.HTTP,
name="IB_MODE",
settings={"host": "127.0.0.1", "port": 0, "intake_auth": mode},
)
)
assert str(refused.value) == (
"HTTP source IB_MODE: intake_auth must be one of "
"none/api_key/bearer/mtls_subject (value withheld)"
)


@pytest.mark.parametrize("mode", [None, ""])
def test_the_listener_refuses_an_explicit_empty_intake_auth(mode: str | None) -> None:
"""Only an absent key reads as ``none``; ``Http()`` refuses an explicit None or empty value."""
with pytest.raises(ValueError, match="intake_auth must be one of"):
HttpSource(
Source(
type=ConnectorType.HTTP,
name="IB_MODE",
settings={"host": "127.0.0.1", "port": 0, "intake_auth": mode},
)
)
with pytest.raises(WiringError, match="intake_auth must be one of"):
Http(port=8080, intake_auth=mode) # type: ignore[arg-type]


@pytest.mark.parametrize("mode", ["absent", "none", "api_key", "bearer"])
def test_the_listener_builds_every_intake_auth_mode_it_knows(mode: str) -> None:
settings: dict[str, Any] = {"host": "127.0.0.1", "port": 0}
if mode != "absent":
settings["intake_auth"] = mode
if mode in ("api_key", "bearer"):
settings["intake_api_key"] = KEY
source = HttpSource(Source(type=ConnectorType.HTTP, name="IB_MODE", settings=settings))
assert source.intake_auth == ("none" if mode == "absent" else mode)


#: Each shape a value equal to a mode can take without being a plain ``str``. The plain strings
#: themselves are built by ``test_the_listener_builds_every_intake_auth_mode_it_knows`` and
#: ``test_valid_configurations_build`` above.
_NOT_PLAIN_STR = {
"str-enum-member": lambda mode: _Mode(mode),
"StrEnum-member": lambda mode: _StrMode(mode),
"str-subclass": lambda mode: _StrSubclass(mode),
}
_PLAIN_STR_REFUSAL = (
"intake_auth must be one of none/api_key/bearer/mtls_subject, as a plain str: pass the "
"string itself, such as an enum member's .value (value withheld)"
)


class _Mode(str, enum.Enum): # noqa: UP042 -- the (str, Enum) shape is the case under test
NONE = "none"
API_KEY = "api_key"
BEARER = "bearer"
MTLS_SUBJECT = "mtls_subject"


class _StrMode(enum.StrEnum):
NONE = "none"
API_KEY = "api_key"
BEARER = "bearer"
MTLS_SUBJECT = "mtls_subject"


class _StrSubclass(str):
pass


@pytest.mark.parametrize("shape", sorted(_NOT_PLAIN_STR))
@pytest.mark.parametrize("mode", ["none", "api_key", "bearer", "mtls_subject"])
def test_an_intake_auth_mode_that_is_not_a_plain_str_is_refused(mode: str, shape: str) -> None:
"""A ``(str, Enum)`` member equal to ``"api_key"`` passed the membership check, but the listener
stored ``str(mode)``, which is ``"_Mode.API_KEY"``, a mode no authorisation check knows, so the
source checked no credential (BACKLOG #6979); under ``mtls_subject`` no certificate was checked.
Both the listener and the factory now refuse any value that is not a plain ``str``, so the
value checked is the value stored and enforced. A ``StrEnum`` member and a ``str`` subclass
rendered correctly before; they are refused too, and the refusal names the remedy."""
value = _NOT_PLAIN_STR[shape](mode)
assert value == mode and type(value) is not str
with pytest.raises(ValueError) as refused:
HttpSource(
Source(
type=ConnectorType.HTTP,
name="IB_MODE",
settings={
"host": "127.0.0.1",
"port": 0,
"intake_auth": value,
"intake_api_key": KEY,
},
)
)
assert str(refused.value) == f"HTTP source IB_MODE: {_PLAIN_STR_REFUSAL}"
with pytest.raises(WiringError) as factory_refused:
Http(port=8080, intake_auth=value, intake_api_key=env("acme_intake_key")) # type: ignore[arg-type]
assert str(factory_refused.value) == f"Http {_PLAIN_STR_REFUSAL}"


@pytest.mark.parametrize("shape", sorted(_NOT_PLAIN_STR))
def test_the_peer_control_gate_weighs_the_allowlist_for_a_mode_that_is_not_a_plain_str(
shape: str,
) -> None:
"""The gate judges a key mode alone only for a value ``Http()`` and the listener accept: a
``StrEnum`` member equal to ``"api_key"`` reads as ``"api_key"`` through ``str()``, so the gate
judged it alone though both refuse it (BACKLOG #6979)."""
from messagefoundry.pipeline.wiring_runner import _allowlist_weighed

assert _allowlist_weighed({"intake_auth": _NOT_PLAIN_STR[shape]("api_key")})
assert not _allowlist_weighed({"intake_auth": "api_key"})


def test_the_listener_accepts_exactly_the_modes_the_factory_accepts() -> None:
"""The listener keeps its own list, the modes it enforces, so a mode added to the factory alone
is refused rather than served unauthenticated; this pins the two lists equal. ``mtls_subject``
is built in ``tests/test_inbound_http_mtls_health.py``, which needs a TLS context."""
from messagefoundry.config.wiring import INTAKE_AUTH_MODES
from messagefoundry.transports.http_listener import _INTAKE_AUTH_MODES

assert _INTAKE_AUTH_MODES == INTAKE_AUTH_MODES


def test_settings_are_carried_onto_the_spec() -> None:
spec = Http(
port=8080,
Expand Down
Loading
Loading