From e8f2a32b9aee35cb1342875cf0d3661e5687117b Mon Sep 17 00:00:00 2001 From: retoor Date: Sat, 24 Oct 2026 03:10:28 +0200 Subject: [PATCH] chore(security): cve-audit regression checks and make target Co-Authored-By: Claude Sonnet 5.5 --- Makefile | 8 +- audit/.gitignore | 2 + audit/MOLO-CLI-001/Makefile | 7 + audit/MOLO-CLI-001/README.md | 50 ++++ audit/MOLO-CLI-001/reproduce.py | 34 +++ audit/MOLO-DAV-001/Makefile | 17 ++ audit/MOLO-DAV-001/README.md | 56 +++++ audit/MOLO-DAV-001/reproduce.cpp | 62 +++++ audit/MOLO-EXPOSE-001/Makefile | 18 ++ audit/MOLO-EXPOSE-001/README.md | 53 ++++ audit/MOLO-EXPOSE-001/reproduce.cpp | 59 +++++ audit/MOLO-H1-001/Makefile | 7 + audit/MOLO-H1-001/README.md | 54 ++++ audit/MOLO-H1-001/reproduce.py | 73 ++++++ audit/MOLO-H2-001/Makefile | 7 + audit/MOLO-H2-001/README.md | 42 ++++ audit/MOLO-H2-001/reproduce.py | 59 +++++ audit/MOLO-H2-002/Makefile | 7 + audit/MOLO-H2-002/README.md | 45 ++++ audit/MOLO-H2-002/reproduce.py | 65 +++++ audit/MOLO-H2-003/Makefile | 7 + audit/MOLO-H2-003/README.md | 42 ++++ audit/MOLO-H2-003/reproduce.py | 58 +++++ audit/MOLO-H2-004/Makefile | 7 + audit/MOLO-H2-004/README.md | 38 +++ audit/MOLO-H2-004/reproduce.py | 52 ++++ audit/MOLO-STATIC-001/Makefile | 7 + audit/MOLO-STATIC-001/README.md | 50 ++++ audit/MOLO-STATIC-001/reproduce.py | 55 +++++ audit/MOLO-WS-001/Makefile | 24 ++ audit/MOLO-WS-001/README.md | 46 ++++ audit/MOLO-WS-001/reproduce.cpp | 48 ++++ audit/Makefile | 33 +++ audit/README.md | 92 +++++++ audit/lib/molo_audit.py | 367 ++++++++++++++++++++++++++++ 35 files changed, 1650 insertions(+), 1 deletion(-) create mode 100644 audit/.gitignore create mode 100644 audit/MOLO-CLI-001/Makefile create mode 100644 audit/MOLO-CLI-001/README.md create mode 100644 audit/MOLO-CLI-001/reproduce.py create mode 100644 audit/MOLO-DAV-001/Makefile create mode 100644 audit/MOLO-DAV-001/README.md create mode 100644 audit/MOLO-DAV-001/reproduce.cpp create mode 100644 audit/MOLO-EXPOSE-001/Makefile create mode 100644 audit/MOLO-EXPOSE-001/README.md create mode 100644 audit/MOLO-EXPOSE-001/reproduce.cpp create mode 100644 audit/MOLO-H1-001/Makefile create mode 100644 audit/MOLO-H1-001/README.md create mode 100644 audit/MOLO-H1-001/reproduce.py create mode 100644 audit/MOLO-H2-001/Makefile create mode 100644 audit/MOLO-H2-001/README.md create mode 100644 audit/MOLO-H2-001/reproduce.py create mode 100644 audit/MOLO-H2-002/Makefile create mode 100644 audit/MOLO-H2-002/README.md create mode 100644 audit/MOLO-H2-002/reproduce.py create mode 100644 audit/MOLO-H2-003/Makefile create mode 100644 audit/MOLO-H2-003/README.md create mode 100644 audit/MOLO-H2-003/reproduce.py create mode 100644 audit/MOLO-H2-004/Makefile create mode 100644 audit/MOLO-H2-004/README.md create mode 100644 audit/MOLO-H2-004/reproduce.py create mode 100644 audit/MOLO-STATIC-001/Makefile create mode 100644 audit/MOLO-STATIC-001/README.md create mode 100644 audit/MOLO-STATIC-001/reproduce.py create mode 100644 audit/MOLO-WS-001/Makefile create mode 100644 audit/MOLO-WS-001/README.md create mode 100644 audit/MOLO-WS-001/reproduce.cpp create mode 100644 audit/Makefile create mode 100644 audit/README.md create mode 100644 audit/lib/molo_audit.py diff --git a/Makefile b/Makefile index c9903a4..d98bae9 100644 --- a/Makefile +++ b/Makefile @@ -128,6 +128,12 @@ audit-check: audit-tests @for t in $(AUDIT_TEST_BINS); do echo "== $$t"; ./$$t || exit 1; done @echo "audit: all suites green" +# --- cve-audit (audit/: vulnerability-class regression checks against this --- +# tree's built artifacts; see audit/README.md. Distinct from audit-check/ +# audit-tests above, which exercise src/audit's compliance hash-chain module. +cve-audit: all + $(MAKE) -C audit -j8 test MOLO_BUILD=$(abspath build) + # --- operability production link (INTEGRATION: with the base tree present --- # the real supervisor/common Clock/KvStore/Observer satisfy the includes, so # their TU definitions ride along on the test link lines). @@ -135,4 +141,4 @@ OPERABILITY_TEST_SRCS += src/supervisor/common/clock.cpp OPERABILITY_TEST_SRCS += src/supervisor/common/kv_store.cpp OPERABILITY_TEST_SRCS += src/supervisor/common/observer.cpp -.PHONY: all test clean interop lint data-all data-test data-clean realtime-test cli-test audit-check +.PHONY: all test clean interop lint data-all data-test data-clean realtime-test cli-test audit-check cve-audit diff --git a/audit/.gitignore b/audit/.gitignore new file mode 100644 index 0000000..7a60b85 --- /dev/null +++ b/audit/.gitignore @@ -0,0 +1,2 @@ +__pycache__/ +*.pyc diff --git a/audit/MOLO-CLI-001/Makefile b/audit/MOLO-CLI-001/Makefile new file mode 100644 index 0000000..ed32f78 --- /dev/null +++ b/audit/MOLO-CLI-001/Makefile @@ -0,0 +1,7 @@ +# MOLO-CLI-001: see README.md in this directory. +MOLO_BUILD ?= $(abspath ../../build) +export MOLO_BUILD + +.PHONY: test +test: + @python3 reproduce.py diff --git a/audit/MOLO-CLI-001/README.md b/audit/MOLO-CLI-001/README.md new file mode 100644 index 0000000..0c7758e --- /dev/null +++ b/audit/MOLO-CLI-001/README.md @@ -0,0 +1,50 @@ +# MOLO-CLI-001: shell command injection in `molo new ` + +**Scope:** [audit](../README.md) · [README](../../README.md) · [AGENTS](../../AGENTS.md) + +In-house finding (not a public CVE), CWE-78 ("Improper Neutralization of Special +Elements used in an OS Command"). + +## Where + +`src/cli/cli/cli_app.cpp`, `cmd_new`: + +```cpp +opts.name = args.positionals[0]; +... +std::string dir = opts.name; +ScaffoldResult result = scaffolder.scaffold(dir, opts); +``` + +`src/cli/cli/scaffolder.cpp`, `Scaffolder::scaffold`: + +```cpp +std::string cmd = "git -C \"" + dir + "\" init -q 2>/dev/null"; +if (system(cmd.c_str()) != 0) { ... } +``` + +`dir` is the project name exactly as typed on the command line, checked only for +length (1-64 chars, `kMaxProjectName`). It is concatenated inside one pair of double +quotes and handed to `system()`, a real shell. A name containing a `"` ends that +quoted argument early; anything after it is ordinary shell syntax. + +## Relevance + +`molo new ` is the first command in the README's own quickstart. Project names +are often copied from elsewhere (a ticket title, a client's brand name, a CI +variable) rather than hand-typed, which is exactly the kind of untrusted-but-looks- +like-a-filename input this bug class targets. + +## What the check does + +Runs `molo new 'x"; touch ; echo "' --force` in a scratch directory and checks +whether the injected `touch` created the marker file. PASS means it did not (the name +is now escaped or passed to `git` without a shell); FAIL means the command ran. + +## Caveats + +This proves injection with a benign `touch`, not a demonstration of what an attacker +could do with it; `system()` runs as the invoking user with that user's full +permissions, so the practical impact is whatever that user could already do by hand. + +Run: `make test` in this directory, or `make -C audit test` from the repo root. diff --git a/audit/MOLO-CLI-001/reproduce.py b/audit/MOLO-CLI-001/reproduce.py new file mode 100644 index 0000000..e43d5f9 --- /dev/null +++ b/audit/MOLO-CLI-001/reproduce.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +# MOLO-CLI-001: shell command injection in `molo new `. See README.md. +import os +import subprocess +import sys + +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "lib")) +import molo_audit as m + + +def main(): + m.arm_watchdog(30) + if not os.path.exists(m.MOLO): + m.verdict_skip("%s not built (run make)" % m.MOLO) + + tmp = m.scratch_dir() + marker = os.path.join(tmp, "pwned") + name = 'x"; touch %s; echo "' % marker # breaks out of the double-quoted -C arg + subprocess.run([m.MOLO, "new", name, "--force"], cwd=tmp, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=20) + + if os.path.exists(marker): + m.verdict_fail( + "`molo new %r` ran an injected `touch` command via the unescaped " + "system(\"git -C \\\"\" + dir + \"\\\" init ...\") call in " + "src/cli/cli/scaffolder.cpp; the project name reaches a shell with no " + "quoting beyond a single pair of double quotes" % name + ) + m.verdict_pass("the injected command did not run; scaffolder.cpp no longer " + "passes the project name through a shell unescaped") + + +if __name__ == "__main__": + main() diff --git a/audit/MOLO-DAV-001/Makefile b/audit/MOLO-DAV-001/Makefile new file mode 100644 index 0000000..576f7e4 --- /dev/null +++ b/audit/MOLO-DAV-001/Makefile @@ -0,0 +1,17 @@ +# MOLO-DAV-001: see README.md in this directory. ResourceTree is +# self-contained (no deps beyond its own header). +CXX ?= g++ +ROOT := $(abspath ../..) +SRC := $(ROOT)/src +BUILD := $(abspath ../../build/audit-dav-001) + +SOURCES := reproduce.cpp $(SRC)/realtime/webdav/server/resource_tree.cpp +BIN := $(BUILD)/reproduce + +.PHONY: test +test: $(BIN) + @$(BIN) + +$(BIN): $(SOURCES) + @mkdir -p $(BUILD) + @$(CXX) -std=c++23 -Wall -Wextra -O1 -I$(SRC) -o $(BIN) $(SOURCES) diff --git a/audit/MOLO-DAV-001/README.md b/audit/MOLO-DAV-001/README.md new file mode 100644 index 0000000..76cace9 --- /dev/null +++ b/audit/MOLO-DAV-001/README.md @@ -0,0 +1,56 @@ +# MOLO-DAV-001: WebDAV COPY into its own subtree never terminates + +**Scope:** [audit](../README.md) · [README](../../README.md) · [AGENTS](../../AGENTS.md) + +In-house finding (not a public CVE). CWE-674 ("Uncontrolled Recursion") plus CWE-400 +(uncontrolled resource consumption); RFC 4918 9.8.3 and 9.9.3 require a WebDAV server +to reject exactly this COPY/MOVE shape with 403 Forbidden before doing anything. + +## Where + +`src/realtime/webdav/server/resource_tree.cpp`, `ResourceTree::copy`: + +```cpp +std::string prefix = src + "/"; +for (const auto& [k, v] : entries_) { + if (k == src || k.compare(0, prefix.size(), prefix) == 0) { + ... + entries_[target] = v; // <-- inserts into the map being iterated + ... + } +} +``` + +There is no check anywhere that `dst` is not inside `src` (only `src == dst`, line 159, +which this shape does not trigger). When `dst` falls lexically after the iterator's +current position in the sorted `std::map`, the newly inserted entry (`dst` + the +relative suffix) is reached later by the same ongoing iteration, because `entries_`'s +`end()` sentinel is unaffected by insertion. Copying `/dav/a` to `/dav/a/b` inserts +`/dav/a/b/b`, which the loop then visits and inserts `/dav/a/b/b/b`, and so on, +forever, each entry copying the full body bytes of everything under `src` again. +`ResourceTree::move` (next function) calls `copy` first, so it inherits the same bug +and then also deletes the whole source subtree afterward if `copy` ever did return. + +## Relevance + +A WebDAV COPY or MOVE with an attacker-chosen `Destination` header that happens to +be a path under the source directory (a single HTTP request, no special privilege) +pins a server thread forever and grows its memory without bound, an unauthenticated +remote DoS against anything that mounts this `ResourceTree` behind a COPY/MOVE route. + +## What the check does + +`ResourceTree` is pure in-memory state with no I/O (confirmed against its header, +which never mentions a socket or filesystem call), so the check drives it directly: +creates `/dav/a` with one file, then calls `copy("/dav/a", "/dav/a/b", true, created)` +under a 10-second watchdog. PASS means `copy` returned within the deadline; FAIL means +the watchdog fired, proving the loop does not converge. + +## Caveats + +No COPY/MOVE HTTP route is wired to a live server binary in `example/` today (the +example's simplified `fw::WebdavStore` only exposes GET/PUT/DELETE/PROPFIND), so this +exercises the real class directly rather than over a socket; the class is the one +`src/realtime/webdav/server/copy_move_handler.cpp` calls for every real COPY/MOVE. + +Run: `make test` in this directory, or `make -C audit test` from the repo root. diff --git a/audit/MOLO-DAV-001/reproduce.cpp b/audit/MOLO-DAV-001/reproduce.cpp new file mode 100644 index 0000000..5801fb8 --- /dev/null +++ b/audit/MOLO-DAV-001/reproduce.cpp @@ -0,0 +1,62 @@ +// MOLO-DAV-001: WebDAV COPY into the source's own subtree never +// terminates and grows memory without bound. See README.md. +// +// ResourceTree is pure in-memory state (no I/O, no server loop), so this +// check drives it directly rather than needing a server: no COPY/MOVE +// route is wired to a live binary in example/ today, but this is the +// same ResourceTree::copy every real COPY handler calls +// (copy_move_handler.cpp). +#include +#include +#include +#include +#include + +#include + +#include "realtime/webdav/server/resource_tree.h" + +using molo::realtime::webdav::server::ResourceTree; + +namespace { +constexpr unsigned kWatchdogSecs = 10; + +void on_alarm(int) { + // _exit, not exit: the process may be mid-way through growing a + // std::map without bound; do not run destructors over it. + const char msg[] = + "FAIL: ResourceTree::copy(\"/dav/a\", \"/dav/a/b\", ...) did not return within " + "10s; it inserts into entries_ (a std::map) while range-iterating that same " + "map (resource_tree.cpp), so copying a collection into its own subtree " + "(dst inside src) keeps discovering newly-inserted entries ahead of the " + "iterator and never converges (CWE-674, uncontrolled recursion/expansion; " + "unbounded memory growth, CWE-400)\n"; + ::write(2, msg, sizeof(msg) - 1); + _exit(1); +} +} // namespace + +int main() { + std::signal(SIGALRM, on_alarm); + ::alarm(kWatchdogSecs); + + ResourceTree tree("/dav"); + if (!tree.mkcol("/dav/a")) { + std::printf("SKIP: could not set up /dav/a (unexpected ResourceTree API change)\n"); + return 0; + } + std::vector body{'h', 'i'}; + tree.put("/dav/a/file.txt", body); + + bool created = false; + // Destination is INSIDE the source collection: exactly the shape RFC 4918 + // 9.8.3/9.9.3 says a server must reject with 403 before doing anything. + bool ok = tree.copy("/dav/a", "/dav/a/b", /*overwrite=*/true, created); + + // If we get here at all, the loop terminated. Whether copy() returned + // true or false, check it did not blow up the tree size either. + std::printf("copy() returned %s\n", ok ? "true" : "false"); + std::printf( + "PASS: ResourceTree::copy into its own subtree returned (did not hang)\n"); + return 0; +} diff --git a/audit/MOLO-EXPOSE-001/Makefile b/audit/MOLO-EXPOSE-001/Makefile new file mode 100644 index 0000000..34fbd5e --- /dev/null +++ b/audit/MOLO-EXPOSE-001/Makefile @@ -0,0 +1,18 @@ +# MOLO-EXPOSE-001: see README.md in this directory. UpstreamHostAllowed is a +# pure function with no deps beyond its own header, so the harness links +# only reproduce.cpp and upstream.cpp. +CXX ?= g++ +ROOT := $(abspath ../..) +SRC := $(ROOT)/src +BUILD := $(abspath ../../build/audit-expose-001) + +SOURCES := reproduce.cpp $(SRC)/realtime/expose/upstream.cpp +BIN := $(BUILD)/reproduce + +.PHONY: test +test: $(BIN) + @$(BIN) + +$(BIN): $(SOURCES) + @mkdir -p $(BUILD) + @$(CXX) -std=c++23 -Wall -Wextra -O1 -I$(SRC) -o $(BIN) $(SOURCES) diff --git a/audit/MOLO-EXPOSE-001/README.md b/audit/MOLO-EXPOSE-001/README.md new file mode 100644 index 0000000..11c6aba --- /dev/null +++ b/audit/MOLO-EXPOSE-001/README.md @@ -0,0 +1,53 @@ +# MOLO-EXPOSE-001: expose upstream allow-list bypass via hostname prefix + +**Scope:** [audit](../README.md) · [README](../../README.md) · [AGENTS](../../AGENTS.md) + +In-house finding (not a public CVE). CWE-184 ("Incomplete List of Disallowed Inputs"), +the same bug shape behind many documented SSRF / allow-list-bypass CVEs that trust a +string-prefix or substring test to stand in for a real IP-range check. + +## Where + +`src/realtime/expose/upstream.cpp`, `UpstreamHostAllowed`: + +```cpp +if(h.rfind("10.",0)==0)return true; +if(h.rfind("192.168.",0)==0)return true; +... +if(h.rfind("169.254.",0)==0||h.rfind("fe80",0)==0||h.rfind("FE80",0)==0)return true; +``` + +`reverse.md` documents the policy as "upstream host must be loopback / RFC1918 / +link-local unless `allow_public` is set" (section 7), i.e. this function is meant to +be a strict allow-list that keeps `molo expose` upstreams pointed at the local +machine or its private network. `rfind(prefix, 0) == 0` only checks that `host` +*starts with* the literal text `"10."`, `"192.168."` etc; it is not an IP-address +parse. A DNS name like `10.evil.com` or `127.evil.com` starts with that text too, and +resolves to whatever public IP its owner points it at. + +## Relevance + +This is the one guard standing between "an exposer may only tunnel to something on +its own machine or LAN" and "an exposer can tunnel to any host on the internet it can +name," for any deployment that has not explicitly opted into `allow_public`. A caller +that relies on this function to keep `molo expose` from being turned into an open +proxy gets a false sense of enforcement. + +## What the check does + +Compiles a tiny harness directly against the real `upstream.cpp` (a pure function, +no I/O, no server needed) and calls `UpstreamHostAllowed` with five public hostnames +chosen to share a prefix with a private-range literal, plus two real private +literals as sanity controls. PASS requires every public hostname to be rejected and +every private literal to be accepted; FAIL means at least one public hostname was +misclassified as private. + +## Caveats + +This checks the string-matching logic in isolation; it does not drive a live +`molo expose` registration end to end, because doing so would require resolving a +real external DNS name to prove the end effect, which this audit's own ground rules +avoid making the check depend on (network access). The misclassification itself is +provable with no network. + +Run: `make test` in this directory, or `make -C audit test` from the repo root. diff --git a/audit/MOLO-EXPOSE-001/reproduce.cpp b/audit/MOLO-EXPOSE-001/reproduce.cpp new file mode 100644 index 0000000..f56b011 --- /dev/null +++ b/audit/MOLO-EXPOSE-001/reproduce.cpp @@ -0,0 +1,59 @@ +// MOLO-EXPOSE-001: UpstreamHostAllowed's private/local allow-list is a +// bare string-prefix test, so a public hostname that merely starts with a +// private-looking label (e.g. "10.evil.com") is misclassified as private +// and let through when it should be rejected. See README.md. +// +// UpstreamHostAllowed is a pure function (no I/O, no server needed): the +// real policy check reverse.md documents and src/realtime/expose/tunnel.cpp +// calls before registering an upstream. +#include + +#include "realtime/expose/upstream.h" + +using molo::realtime::expose::UpstreamHostAllowed; + +struct Case { + const char* host; + bool should_be_private; // the correct verdict: is this really loopback/RFC1918/link-local? +}; + +int main() { + // These are real public DNS names (any attacker can register one) that + // share a prefix with a private-range literal. The correct verdict for + // all of them is "not private". + static const Case kCases[] = { + {"10.evil.com", false}, + {"127.evil.com", false}, + {"192.168.attacker.net", false}, + {"169.254.attacker.net", false}, + {"172.99.evil.com", false}, + // Sanity controls: a real private literal must still pass. + {"10.0.0.5", true}, + {"192.168.1.1", true}, + }; + + int failures = 0; + for (const auto& c : kCases) { + bool allowed = UpstreamHostAllowed(c.host, /*allow_public=*/false); + bool wrong = allowed != c.should_be_private; + std::printf("%-28s allowed=%-5s want_private=%-5s %s\n", c.host, + allowed ? "true" : "false", c.should_be_private ? "true" : "false", + wrong ? "MISMATCH" : "ok"); + if (wrong) failures++; + } + + if (failures > 0) { + std::printf( + "FAIL: %d/%zu cases misclassified; UpstreamHostAllowed (src/realtime/expose/" + "upstream.cpp) uses host.rfind(\"10.\",0)==0 style bare prefix tests, so a " + "public hostname that starts with a private-looking label is accepted as " + "private even though DNS will resolve it to an arbitrary public (or " + "attacker-controlled) address: an allow-list bypass in the policy reverse.md " + "documents as \"upstream host must be loopback / RFC1918 / link-local unless " + "allow_public is set\" (CWE-184, incomplete allow-list)\n", + failures, sizeof(kCases) / sizeof(kCases[0])); + return 1; + } + std::printf("PASS: all %zu cases classified correctly\n", sizeof(kCases) / sizeof(kCases[0])); + return 0; +} diff --git a/audit/MOLO-H1-001/Makefile b/audit/MOLO-H1-001/Makefile new file mode 100644 index 0000000..603b42e --- /dev/null +++ b/audit/MOLO-H1-001/Makefile @@ -0,0 +1,7 @@ +# MOLO-H1-001: see README.md in this directory. +MOLO_BUILD ?= $(abspath ../../build) +export MOLO_BUILD + +.PHONY: test +test: + @python3 reproduce.py diff --git a/audit/MOLO-H1-001/README.md b/audit/MOLO-H1-001/README.md new file mode 100644 index 0000000..431d038 --- /dev/null +++ b/audit/MOLO-H1-001/README.md @@ -0,0 +1,54 @@ +# MOLO-H1-001: chunked-body cap bypass via chunk-size addition overflow + +**Scope:** [audit](../README.md) · [README](../../README.md) · [AGENTS](../../AGENTS.md) + +In-house finding (not a public CVE), modeled on the same bug class as CVE-2022-29361-style +chunked-transfer-coding integer handling and the general HTTP request-smuggling / +resource-exhaustion family (CWE-190 integer overflow leading to CWE-400 uncontrolled +resource consumption). + +## Where + +`src/runtime/http/v1/parser.cpp`, `Parser::FeedChunkSize`: + +```cpp +if (body_.size() + size > body_cap_) { + state_ = State::Failed; + return ParseResult::Error413; +} +``` + +`size` is attacker-controlled (parsed from the chunk-size line, up to 16 hex digits, +already overflow-checked against `SIZE_MAX` on its own). `body_.size() + size` is not +checked for overflow. A chunk declaring size `0xFFFFFFFFFFFFFFFF` on top of a non-empty +`body_` wraps the sum to a small number, so the cap comparison passes when it should +fail with 413. `chunk_left_` is then set to the huge declared size, and every +subsequent byte the client sends is appended to `body_` in `FeedChunkData` with no +further cap check until that one chunk ends (which an attacker need never do). + +## Relevance + +`kBodyMaxDefault` (10 MiB, `src/runtime/caps.h`) is the framework's hard ceiling on +request body size, the exact kind of static ceiling `AGENTS.md`/the base rules require +("every knob has a hard static cap"). This bug makes that ceiling bypassable by anyone +who can open an HTTP/1.1 connection and send one request: memory grows without bound as +long as the client keeps writing, which is an unauthenticated remote DoS lever against +any MoloApp server (the living example, or any app built with `molo new`). + +## What the check does + +Starts `build/bin/runtime_interop_server` on a scratch port, opens a real TCP +connection, sends `POST /echo` with `Transfer-Encoding: chunked`, announces the +overflowing chunk size, then streams 14 MiB of chunk "data" past the 10 MiB cap and +confirms the server never responds 413 or closes the connection, i.e. the cap was +bypassed. PASS means a future fix (checking `size > body_cap_ - body_.size()`) made the +server reject before the cap, or the overflow attempt itself failed to run (SKIP covers +a missing build). + +## Caveats + +This is a size-cap bypass proven by pushing memory growth past the configured ceiling, +not a crash; it stops at the harness's own 64 MiB send cap (`molo_audit.MAX_SEND_BYTES`) +regardless of verdict, so the check itself never becomes the DoS it is checking for. + +Run: `make test` in this directory, or `make -C audit test` from the repo root. diff --git a/audit/MOLO-H1-001/reproduce.py b/audit/MOLO-H1-001/reproduce.py new file mode 100644 index 0000000..6f2ac6a --- /dev/null +++ b/audit/MOLO-H1-001/reproduce.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +# MOLO-H1-001: chunked-body cap bypass via chunk-size addition overflow. +# See README.md in this directory. Drives build/bin/runtime_interop_server +# over a real socket; nothing here is a canned unit test. +import os +import sys +import time + +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "lib")) +import molo_audit as m + +BODY_CAP_DEFAULT = 10 * 1024 * 1024 # runtime::caps::kBodyMaxDefault +PUSH_BYTES = BODY_CAP_DEFAULT + 4 * 1024 * 1024 # comfortably over the cap + + +def main(): + m.arm_watchdog() + srv, _tmp = m.start_interop() + port = srv.port + + s = m.connect(port) + s.sendall(b"POST /echo HTTP/1.1\r\nHost: x\r\nTransfer-Encoding: chunked\r\n\r\n") + # One ordinary 1-byte chunk, then a chunk-size whose declared length is + # 2**64-1. body_.size()(=1) + size overflows size_t and wraps to 0, so + # the "body_.size() + size > body_cap_" cap check at parser.cpp passes + # when it should reject with 413. + s.sendall(b"1\r\nA\r\n") + s.sendall(b"ffffffffffffffff\r\n") + + sent = 0 + chunk = b"B" * (1024 * 1024) + rejected_or_closed = False + s.setblocking(False) + import select + while sent < PUSH_BYTES: + try: + s.sendall(chunk) + sent += len(chunk) + except (BlockingIOError, InterruptedError): + time.sleep(0.02) + continue + except OSError: + rejected_or_closed = True + break + r, _, _ = select.select([s], [], [], 0) + if r: + data = s.recv(4096) + if not data or b" 413 " in data or b" 400 " in data: + rejected_or_closed = True + break + if not srv.alive(): + m.verdict_skip("server process died (unrelated to this check)") + + s.close() + rss = srv.rss_kb() + srv.stop() + + if rejected_or_closed: + m.verdict_pass( + "server rejected or closed before %d bytes crossed the %d-byte body cap" + % (sent, BODY_CAP_DEFAULT) + ) + if sent >= PUSH_BYTES: + m.verdict_fail( + "sent %d bytes of chunk body (cap is %d) with no 413/close: the chunk-size " + "overflow in FeedChunkSize (body_.size()+size wraps) bypasses kBodyMaxDefault " + "entirely; peak RSS ~%d KiB" % (sent, BODY_CAP_DEFAULT, rss) + ) + m.verdict_fail("loop ended ambiguously after %d bytes" % sent) + + +if __name__ == "__main__": + main() diff --git a/audit/MOLO-H2-001/Makefile b/audit/MOLO-H2-001/Makefile new file mode 100644 index 0000000..e3da816 --- /dev/null +++ b/audit/MOLO-H2-001/Makefile @@ -0,0 +1,7 @@ +# MOLO-H2-001: see README.md in this directory. +MOLO_BUILD ?= $(abspath ../../build) +export MOLO_BUILD + +.PHONY: test +test: + @python3 reproduce.py diff --git a/audit/MOLO-H2-001/README.md b/audit/MOLO-H2-001/README.md new file mode 100644 index 0000000..1a179e3 --- /dev/null +++ b/audit/MOLO-H2-001/README.md @@ -0,0 +1,42 @@ +# MOLO-H2-001: HTTP/2 Rapid Reset + +**Scope:** [audit](../README.md) · [README](../../README.md) · [AGENTS](../../AGENTS.md) + +CVE-2023-44487 (CVSS varies by vendor, widely rated High; the class, not a specific +MoloApp CVE). CISA/CERT advisory, disclosed October 2023. + +## Source + +- [CISA advisory AA23-230A / the "HTTP/2 Rapid Reset" disclosure](https://www.cisa.gov/news-events/alerts/2023/10/10/http2-rapid-reset-vulnerability-impacting-web-servers) + +## Relevance to MoloApp + +A client opens a stream with HEADERS and immediately cancels it with RST_STREAM, +over and over on one connection. Each cycle costs the server a handler dispatch but +never counts against a concurrent-stream limit, because the stream is gone by the +time the next one opens. This is the mechanism that drove the October 2023 +multi-vendor HTTP/2 incident across nginx, Envoy, Go net/http2 and others. + +`src/runtime/http/v2/connection.cpp` enforces `kH2MaxConcurrentStreamsDefault` (100, +`src/runtime/caps.h`) by counting open streams, and `OnRst` (around line 535) erases +the stream entry the moment an RST_STREAM arrives. There is no counter for how many +streams a connection has opened-then-reset, and no backoff or GOAWAY triggered by a +high reset rate, so the one cap this framework relies on for HTTP/2 concurrency never +engages against this pattern. + +## What the check does + +Starts `build/bin/runtime_interop_server`, opens one real h2c prior-knowledge +connection, and sends 4000 HEADERS(END_HEADERS|END_STREAM)+RST_STREAM(NO_ERROR) pairs +for distinct odd stream IDs in one connection. PASS requires the server to push back +(GOAWAY, or refuse to echo an RST_STREAM for all of them) while staying up; FAIL means +every pair was accepted with no backpressure signal. + +## Caveats + +This check measures whether a *rate-limiting signal* exists, not whether the process +survives a sustained attack at scale; it stops at 4000 pairs (the harness's own +`flood()` cap), far below what a real attacker would send, specifically so the check +itself never becomes a denial-of-service tool. + +Run: `make test` in this directory, or `make -C audit test` from the repo root. diff --git a/audit/MOLO-H2-001/reproduce.py b/audit/MOLO-H2-001/reproduce.py new file mode 100644 index 0000000..26d51fe --- /dev/null +++ b/audit/MOLO-H2-001/reproduce.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 +# MOLO-H2-001: HTTP/2 Rapid Reset (CVE-2023-44487 class). See README.md. +import os +import sys + +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "lib")) +import molo_audit as m + +STREAMS = 4000 # well above kH2MaxConcurrentStreamsDefault (100) + + +def main(): + m.arm_watchdog() + srv, _tmp = m.start_interop() + port = srv.port + + h2 = m.H2(port, initial_settings=[(3, 1000)]) # offer a generous own limit + h2.settle(0.3) + + def req_then_rst(i): + sid = 2 * i + 1 + block = m.hpack_request(path="/echo") + out = m.frame(m.T_HEADERS, m.F_END_HEADERS | m.F_END_STREAM, sid, block) + out += m.frame(m.T_RST, 0, sid, (0).to_bytes(4, "big")) # NO_ERROR, immediate + return out + + sent = m.flood(h2, req_then_rst, STREAMS, batch=50) + h2.settle(1.0) + crashed = not srv.alive() + pushed_back = h2.closed or h2.goaway is not None + rss = srv.rss_kb() + h2.close() + srv.stop() + + if crashed: + m.verdict_fail( + "server process died after %d HEADERS+RST(0) pairs on one connection" % sent + ) + if pushed_back and sent < STREAMS: + m.verdict_pass( + "server cut the connection (GOAWAY=%s) after %d/%d pairs instead of accepting " + "an unbounded stream-open/reset churn, and stayed up (RSS ~%d KiB)" + % (h2.goaway, sent, STREAMS, rss) + ) + if sent >= STREAMS and not pushed_back: + m.verdict_fail( + "all %d request+immediate-RST_STREAM pairs (40x kH2MaxConcurrentStreamsDefault) " + "were accepted on one connection with no GOAWAY and no stream-reset rate limit; " + "src/runtime/http/v2/connection.cpp has no per-connection reset counter, so the " + "concurrent-stream cap never engages because each stream is torn down before the " + "next HEADERS arrives, and the handler is already dispatched for every one of " + "them (RSS ~%d KiB)" % (sent, rss) + ) + m.verdict_fail("ambiguous outcome after %d/%d pairs (crashed=%s, pushed_back=%s)" + % (sent, STREAMS, crashed, pushed_back)) + + +if __name__ == "__main__": + main() diff --git a/audit/MOLO-H2-002/Makefile b/audit/MOLO-H2-002/Makefile new file mode 100644 index 0000000..1c68802 --- /dev/null +++ b/audit/MOLO-H2-002/Makefile @@ -0,0 +1,7 @@ +# MOLO-H2-002: see README.md in this directory. +MOLO_BUILD ?= $(abspath ../../build) +export MOLO_BUILD + +.PHONY: test +test: + @python3 reproduce.py diff --git a/audit/MOLO-H2-002/README.md b/audit/MOLO-H2-002/README.md new file mode 100644 index 0000000..0159c6a --- /dev/null +++ b/audit/MOLO-H2-002/README.md @@ -0,0 +1,45 @@ +# MOLO-H2-002: HTTP/2 "MadeYouReset" + +**Scope:** [audit](../README.md) · [README](../../README.md) · [AGENTS](../../AGENTS.md) + +CVE-2025-8671, disclosed August 2025 via CERT/CC. Affected a wide vendor list (Apache +Tomcat as CVE-2025-48989, F5, Fastly, Netty, gRPC, Varnish and others); the class, not +a MoloApp-specific assignment. + +## Source + +- [CERT/CC VU#767506](https://www.kb.cert.org/vuls/id/767506) + +## Relevance to MoloApp + +Rapid Reset (CVE-2023-44487, [MOLO-H2-001](../MOLO-H2-001/)) is mitigated by many +implementations by rate-limiting client-sent RST_STREAM frames. MadeYouReset sidesteps +that by getting the *server* to send the RST_STREAM instead, for example via a +protocol violation on the stream (an invalid WINDOW_UPDATE, a bad frame, a flow-control +violation) that triggers a stream-level error. The handler dispatch and teardown cost +is the same as Rapid Reset, but a mitigation that only counts client RST_STREAM frames +never sees it, because the client never sends one. + +`src/runtime/http/v2/connection.cpp` has no counter for streams the server itself +resets (confirmed in the audit of `OnRst`, the flow-control-error branches around +line 446/524/610): each one frees the stream slot the same way a client-sent RST does, +with no cap, matching [MOLO-H2-001](../MOLO-H2-001/)'s finding that the concurrent-stream +limit never engages against a churn attack. + +## What the check does + +Opens one h2c connection to `build/bin/runtime_interop_server` and, for 4000 distinct +streams, sends HEADERS (no END_STREAM, so the stream stays open) followed by a +WINDOW_UPDATE with a zero increment on that same stream, a flow-control violation that +RFC 9113 6.9 requires the server to answer with RST_STREAM. PASS requires the +connection to be cut (GOAWAY or close) before all 4000 are processed; FAIL means the +server resets every stream itself with no backpressure, exactly the MadeYouReset shape. + +## Caveats + +This is the server-triggered-reset mechanism, not a claim of in-the-wild exploitation +(CERT/CC and vendor advisories note no known exploitation at disclosure time either). +Capped at 4000 streams by the harness so the check itself cannot become the DoS it +tests for. + +Run: `make test` in this directory, or `make -C audit test` from the repo root. diff --git a/audit/MOLO-H2-002/reproduce.py b/audit/MOLO-H2-002/reproduce.py new file mode 100644 index 0000000..36170f8 --- /dev/null +++ b/audit/MOLO-H2-002/reproduce.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 +# MOLO-H2-002: "MadeYouReset" (CVE-2025-8671 class). See README.md. +# Rapid Reset (MOLO-H2-001) has the client send the RST_STREAM. Here the +# client provokes the SERVER into sending the reset instead (an invalid +# WINDOW_UPDATE on each stream), which Rapid-Reset mitigations that only +# count client-sent RST_STREAM frames do not see at all. +import os +import sys + +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "lib")) +import molo_audit as m + +STREAMS = 4000 + + +def main(): + m.arm_watchdog() + srv, _tmp = m.start_interop() + port = srv.port + + h2 = m.H2(port, initial_settings=[(3, 1000)]) + h2.settle(0.3) + + def req_then_bad_window(i): + sid = 2 * i + 1 + block = m.hpack_request(path="/echo") + # HEADERS without END_STREAM: stream stays open, awaiting body. + out = m.frame(m.T_HEADERS, m.F_END_HEADERS, sid, block) + # A zero WINDOW_UPDATE increment is a stream error (FLOW_CONTROL_ERROR): + # RFC 9113 6.9 forbids it. This makes the SERVER emit the RST_STREAM. + out += m.frame(m.T_WINDOW, 0, sid, (0).to_bytes(4, "big")) + return out + + sent = m.flood(h2, req_then_bad_window, STREAMS, batch=50) + h2.settle(1.0) + crashed = not srv.alive() + server_rsts = h2.count(m.T_RST) + pushed_back = h2.closed or h2.goaway is not None + rss = srv.rss_kb() + h2.close() + srv.stop() + + if crashed: + m.verdict_fail("server process died after %d provoked resets" % sent) + if pushed_back and sent < STREAMS: + m.verdict_pass( + "server cut the connection (GOAWAY=%s) after %d/%d provoked server-side " + "resets instead of an unbounded churn, and stayed up (RSS ~%d KiB)" + % (h2.goaway, sent, STREAMS, rss) + ) + if sent >= STREAMS and not pushed_back: + m.verdict_fail( + "all %d streams were opened and server-reset (%d RST_STREAM observed) on one " + "connection with no GOAWAY and no limit on server-initiated resets; a " + "mitigation that only rate-limits client-sent RST_STREAM (as Rapid Reset " + "fixes typically do) would still miss this, because connection.cpp's own " + "reset path (OnRst/flow-control-error branches) frees the stream slot with " + "no counter either (RSS ~%d KiB)" % (sent, server_rsts, rss) + ) + m.verdict_fail("ambiguous outcome after %d/%d (crashed=%s, pushed_back=%s)" + % (sent, STREAMS, crashed, pushed_back)) + + +if __name__ == "__main__": + main() diff --git a/audit/MOLO-H2-003/Makefile b/audit/MOLO-H2-003/Makefile new file mode 100644 index 0000000..24ef8d0 --- /dev/null +++ b/audit/MOLO-H2-003/Makefile @@ -0,0 +1,7 @@ +# MOLO-H2-003: see README.md in this directory. +MOLO_BUILD ?= $(abspath ../../build) +export MOLO_BUILD + +.PHONY: test +test: + @python3 reproduce.py diff --git a/audit/MOLO-H2-003/README.md b/audit/MOLO-H2-003/README.md new file mode 100644 index 0000000..56e79b5 --- /dev/null +++ b/audit/MOLO-H2-003/README.md @@ -0,0 +1,42 @@ +# MOLO-H2-003: HTTP/2 CONTINUATION flood + +**Scope:** [audit](../README.md) · [README](../../README.md) · [AGENTS](../../AGENTS.md) + +CVE-2024-27316 (Apache httpd, fixed 2.4.59) and the same-family CVE-2024-27983 +(Node.js), CVE-2024-27919 (Envoy), CVE-2024-2653 (amphp/http); the class ("HTTP/2 +CONTINUATION Flood"), not a MoloApp-specific CVE. Disclosed by Bartek Nowotarski to +CERT/CC, April 2024. + +## Source + +- [CERT/CC advisory on the CONTINUATION flood](https://kb.cert.org/vuls/id/421644) + +## Relevance to MoloApp + +A HEADERS frame without END_HEADERS opens a header block that is only closed by a +later CONTINUATION frame carrying END_HEADERS. The 2024 disclosure found that many +implementations cap the *total header bytes* accumulated across that run of frames, +but not the *count* of CONTINUATION frames or the time spent collecting them, so a +client can send an effectively unbounded number of empty (or near-empty) +CONTINUATION frames, each costing a 9-byte frame-header parse and a dispatch through +the connection's frame loop, without ever tripping the byte-count guard. + +`src/runtime/http/v2/hpack.cpp`/`connection.cpp` cap the accumulated header block at +`kH2HeaderListMax` (64 KiB) by bytes, confirmed in the audit; nothing there counts +CONTINUATION frames or bounds how long one stream's header block may stay open. + +## What the check does + +Opens one h2c connection, sends a HEADERS frame without END_HEADERS on stream 1 +(leaving the header block open), then floods up to 200,000 zero-length CONTINUATION +frames on that stream. PASS requires the connection to be cut before all of them are +accepted; FAIL means the server parses an unbounded run of empty CONTINUATION frames +with no byte growth and no cutoff. + +## Caveats + +200,000 is the harness's own ceiling, not a claim about the attack's real cost at +scale; a genuine attack sends far more, and this check is capped specifically so +running it cannot itself become the resource-exhaustion attack it is checking for. + +Run: `make test` in this directory, or `make -C audit test` from the repo root. diff --git a/audit/MOLO-H2-003/reproduce.py b/audit/MOLO-H2-003/reproduce.py new file mode 100644 index 0000000..ddf8bfb --- /dev/null +++ b/audit/MOLO-H2-003/reproduce.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +# MOLO-H2-003: HTTP/2 CONTINUATION flood (CVE-2024-27316 class). See README.md. +import os +import sys + +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "lib")) +import molo_audit as m + +# Each CONTINUATION frame below adds 0 header bytes, so the kH2HeaderListMax +# byte-count guard never trips. Capped so the check itself cannot hang the +# box: this is far below what a real attack would send, enough only to +# prove the connection has no frame-count or time floor on an open header +# block, only a byte-count one. +FRAMES = 200000 + + +def main(): + m.arm_watchdog() + srv, _tmp = m.start_interop() + port = srv.port + + h2 = m.H2(port) + h2.settle(0.2) + sid = 1 + # Open a header block with HEADERS (no END_HEADERS): one real byte of + # header data, then an endless run of empty CONTINUATION frames. + h2.raw(m.frame(m.T_HEADERS, 0, sid, b"\x82")) # :method GET, block left open + + sent = m.flood(h2, lambda i: m.frame(m.T_CONT, 0, sid, b""), FRAMES, batch=2000) + h2.settle(1.0) + crashed = not srv.alive() + pushed_back = h2.closed or h2.goaway is not None + rss = srv.rss_kb() + h2.close() + srv.stop() + + if crashed: + m.verdict_fail("server process died after %d empty CONTINUATION frames" % sent) + if pushed_back and sent < FRAMES: + m.verdict_pass( + "server cut the connection (GOAWAY=%s) after %d/%d empty CONTINUATION " + "frames instead of parsing an unbounded run of them, and stayed up " + "(RSS ~%d KiB)" % (h2.goaway, sent, FRAMES, rss) + ) + if sent >= FRAMES and not pushed_back: + m.verdict_fail( + "all %d empty CONTINUATION frames on one open header block were accepted " + "with no GOAWAY; kH2HeaderListMax (connection.cpp) only caps accumulated " + "header BYTES, so a flood of zero-length CONTINUATION frames costs the " + "server a 9-byte-frame parse each with no floor on frame count or time " + "spent in one header block (RSS ~%d KiB)" % (sent, rss) + ) + m.verdict_fail("ambiguous outcome after %d/%d (crashed=%s, pushed_back=%s)" + % (sent, FRAMES, crashed, pushed_back)) + + +if __name__ == "__main__": + main() diff --git a/audit/MOLO-H2-004/Makefile b/audit/MOLO-H2-004/Makefile new file mode 100644 index 0000000..e63e339 --- /dev/null +++ b/audit/MOLO-H2-004/Makefile @@ -0,0 +1,7 @@ +# MOLO-H2-004: see README.md in this directory. +MOLO_BUILD ?= $(abspath ../../build) +export MOLO_BUILD + +.PHONY: test +test: + @python3 reproduce.py diff --git a/audit/MOLO-H2-004/README.md b/audit/MOLO-H2-004/README.md new file mode 100644 index 0000000..460fd82 --- /dev/null +++ b/audit/MOLO-H2-004/README.md @@ -0,0 +1,38 @@ +# MOLO-H2-004: HTTP/2 control-frame flood, unbounded ack queue + +**Scope:** [audit](../README.md) · [README](../../README.md) · [AGENTS](../../AGENTS.md) + +CVE-2019-9511/9512/9515/9518, the 2019 Netflix/Google "HTTP/2 DoS" disclosure batch +("Ping Flood" is CVE-2019-9512 specifically); the class, not a MoloApp-specific CVE. + +## Source + +- [CISA advisory on the 2019 HTTP/2 implementation flaws](https://www.cisa.gov/news-events/alerts/2019/08/13/http2-implementations-vulnerable-denial-service) + +## Relevance to MoloApp + +Every PING frame a client sends must be answered with a PING ack; a client that keeps +sending PINGs but never reads its socket can make the server queue an unbounded +amount of outbound ack data, if nothing caps either the rate of control frames +processed or the size of the output queue built up for a non-reading peer. This is +the "Ping Flood" shape from the 2019 disclosure batch. + +`src/runtime/http/v2/connection.cpp`'s `EnqueueControl` (around line 87) queues an ack +for every PING with no per-connection rate cap on control frames and no ceiling on +`out_queued_bytes_` tied to a slow/non-reading peer (confirmed in the audit); the only +backpressure path found applies to file-backed response bodies, not control frames. + +## What the check does + +Opens one h2c connection to `build/bin/runtime_interop_server`, sends up to 300,000 +PING frames without ever reading the socket, and checks whether the server cuts the +connection (GOAWAY/close) before accepting all of them. PASS means it did; FAIL means +every PING was accepted with no limit on the resulting queue of unread ack bytes. + +## Caveats + +This measures whether a floor exists on a single connection's queued-but-unread +output, not the server's behavior across many connections at once. Capped at 300,000 +frames (about 5 MB of acks) so the check cannot itself exhaust the test host's memory. + +Run: `make test` in this directory, or `make -C audit test` from the repo root. diff --git a/audit/MOLO-H2-004/reproduce.py b/audit/MOLO-H2-004/reproduce.py new file mode 100644 index 0000000..f56ce27 --- /dev/null +++ b/audit/MOLO-H2-004/reproduce.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +# MOLO-H2-004: HTTP/2 PING/SETTINGS flood, unbounded ack queue +# (CVE-2019-9511/9515/9518 "HTTP/2 DoS" family class). See README.md. +import os +import sys + +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "lib")) +import molo_audit as m + +PINGS = 300000 # each ack queued is 17 bytes; capped well under MAX_SEND_BYTES + + +def main(): + m.arm_watchdog() + srv, _tmp = m.start_interop() + port = srv.port + + h2 = m.H2(port) + h2.settle(0.2) + # Never read the server's responses: this isolates whether an unread + # peer causes unbounded buffering of queued PING acks server-side, + # rather than measuring our own receive behavior. + sent = m.flood(h2, lambda i: m.frame(m.T_PING, 0, 0, b"01234567"), PINGS, batch=5000) + h2.settle(1.0) + crashed = not srv.alive() + pushed_back = h2.closed or h2.goaway is not None + rss = srv.rss_kb() + h2.close() + srv.stop() + + if crashed: + m.verdict_fail("server process died after %d unread PING frames" % sent) + if pushed_back and sent < PINGS: + m.verdict_pass( + "server cut the connection (GOAWAY=%s) after %d/%d unread PINGs instead of " + "queueing an unbounded run of acks, and stayed up (RSS ~%d KiB)" + % (h2.goaway, sent, PINGS, rss) + ) + if sent >= PINGS and not pushed_back: + m.verdict_fail( + "all %d PING frames were accepted with no GOAWAY and no limit on queued " + "acks while the client never read a byte back; EnqueueControl " + "(connection.cpp) has no cap on control-frame rate or on out_queued_bytes_ " + "for an unread peer, so a connection can be made to buffer an unbounded " + "amount of PING-ack output (RSS ~%d KiB)" % (sent, rss) + ) + m.verdict_fail("ambiguous outcome after %d/%d (crashed=%s, pushed_back=%s)" + % (sent, PINGS, crashed, pushed_back)) + + +if __name__ == "__main__": + main() diff --git a/audit/MOLO-STATIC-001/Makefile b/audit/MOLO-STATIC-001/Makefile new file mode 100644 index 0000000..2da6fe1 --- /dev/null +++ b/audit/MOLO-STATIC-001/Makefile @@ -0,0 +1,7 @@ +# MOLO-STATIC-001: see README.md in this directory. +MOLO_BUILD ?= $(abspath ../../build) +export MOLO_BUILD + +.PHONY: test +test: + @python3 reproduce.py diff --git a/audit/MOLO-STATIC-001/README.md b/audit/MOLO-STATIC-001/README.md new file mode 100644 index 0000000..ce64dd0 --- /dev/null +++ b/audit/MOLO-STATIC-001/README.md @@ -0,0 +1,50 @@ +# MOLO-STATIC-001: static-file jail escape via a symlinked intermediate directory + +**Scope:** [audit](../README.md) · [README](../../README.md) · [AGENTS](../../AGENTS.md) + +In-house finding (not a public CVE), modeled on the general symlink-following path +traversal class (CWE-61, "UNIX Symbolic Link Following") that underlies many static +file server CVEs (for example nginx's historical alias-traversal advisories and +assorted CMS/static-server CVEs that rely on `O_NOFOLLOW` being assumed sufficient). + +## Where + +`src/runtime/static/disk_provider.cpp`, `DiskProvider::open`: + +```cpp +int fd = openat(root_fd_, path.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC); +``` + +`O_NOFOLLOW` only blocks the kernel from dereferencing a symlink at the **final** +component of `path`. If an **earlier** path segment is a symlink pointing outside the +jailed root, the kernel follows it transparently while resolving the rest of the path, +and `openat` succeeds. The `realpath`-based containment check a few lines below only +runs when this `openat` call fails and `follow_symlinks_` (default `false`, +`disk_provider.h`) is true, so the strict, default configuration never reaches any +containment check at all for this shape of symlink. + +## Relevance + +`DiskProvider` is MoloApp's jailed static-file backend, the piece responsible for +keeping `/static/*` requests inside the configured root. This defeats that jail with +a single symlink an attacker needs only the ability to place inside the served +directory tree (a common outcome of an unrelated file-upload or archive-extraction +feature elsewhere in an app, or simply a misconfigured deploy that leaves a +developer's symlink in place). + +## What the check does + +Builds a scratch directory with `www/escape -> outside/` (a symlink one level inside +the served root) and `outside/secret.txt` holding a marker string, starts +`build/bin/runtime_interop_server` on that root with `follow_symlinks` at its default +(`false`), and requests `GET /static/escape/secret.txt`. PASS means the jail held +(404); FAIL means the marker string came back. + +## Caveats + +This does not test the `follow_symlinks=true` code path's own documented TOCTOU +window (that path already states its own limitation in a code comment); it tests the +supposedly-strict default, which the code comments do not flag as vulnerable to this +shape of symlink at all. + +Run: `make test` in this directory, or `make -C audit test` from the repo root. diff --git a/audit/MOLO-STATIC-001/reproduce.py b/audit/MOLO-STATIC-001/reproduce.py new file mode 100644 index 0000000..c9fa83f --- /dev/null +++ b/audit/MOLO-STATIC-001/reproduce.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 +# MOLO-STATIC-001: static-file jail escape via a symlinked intermediate +# directory, even with follow_symlinks=false (the default). See README.md. +import os +import sys + +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "lib")) +import molo_audit as m + +SECRET = b"MOLO-AUDIT-OUTSIDE-SECRET\n" + + +def main(): + m.arm_watchdog() + if not os.path.exists(m.INTEROP): + m.verdict_skip("%s not built (run make)" % m.INTEROP) + + tmp = m.scratch_dir() + www = os.path.join(tmp, "www") + outside = os.path.join(tmp, "outside") + os.makedirs(www) + os.makedirs(outside) + with open(os.path.join(outside, "secret.txt"), "wb") as f: + f.write(SECRET) + # The symlink itself is the LAST path component only when resolving the + # link; "escape/secret.txt" makes it an INTERMEDIATE component, which + # openat's O_NOFOLLOW does not protect (it only blocks the final + # component of the path actually opened). + os.symlink(outside, os.path.join(www, "escape")) + + srv = m.Server([m.INTEROP, "0", www]) + port = srv.port + + resp, _ = m.h1(port, b"GET /static/escape/secret.txt HTTP/1.1\r\nHost: x\r\n" + b"Connection: close\r\n\r\n", 1.5) + srv.stop() + + if SECRET in resp: + m.verdict_fail( + "GET /static/escape/secret.txt returned the file from outside the static " + "root (follow_symlinks left at its default of false); DiskProvider::open " + "(disk_provider.cpp) uses openat(..., O_NOFOLLOW) which blocks a symlink " + "only as the FINAL path component, so a symlinked directory earlier in " + "the path is followed transparently, escaping the jail" + ) + if m.status_lines(resp) == [404]: + m.verdict_pass( + "GET /static/escape/secret.txt was refused (404); the jail held against a " + "symlinked intermediate directory" + ) + m.verdict_fail("unexpected response, neither the secret nor a clean 404: %r" % resp[:200]) + + +if __name__ == "__main__": + main() diff --git a/audit/MOLO-WS-001/Makefile b/audit/MOLO-WS-001/Makefile new file mode 100644 index 0000000..8012f80 --- /dev/null +++ b/audit/MOLO-WS-001/Makefile @@ -0,0 +1,24 @@ +# MOLO-WS-001: see README.md in this directory. Compiles a tiny harness +# directly against the real HandshakeValidator (no server binary wires it +# to a live upgrade route today; see README "What the check does"). +CXX ?= g++ +ROOT := $(abspath ../..) +SRC := $(ROOT)/src +BUILD := $(abspath ../../build/audit-ws-001) + +SOURCES := reproduce.cpp \ + $(SRC)/realtime/websocket/server/handshake_validator.cpp \ + $(SRC)/realtime/shared/sha1.cpp \ + $(SRC)/realtime/shared/base64.cpp \ + $(SRC)/realtime/shared/realtime_metrics.cpp \ + $(SRC)/realtime/shared/http_seam.cpp + +BIN := $(BUILD)/reproduce + +.PHONY: test +test: $(BIN) + @$(BIN) + +$(BIN): $(SOURCES) + @mkdir -p $(BUILD) + @$(CXX) -std=c++23 -Wall -Wextra -O1 -I$(SRC) -o $(BIN) $(SOURCES) diff --git a/audit/MOLO-WS-001/README.md b/audit/MOLO-WS-001/README.md new file mode 100644 index 0000000..7e7921e --- /dev/null +++ b/audit/MOLO-WS-001/README.md @@ -0,0 +1,46 @@ +# MOLO-WS-001: WebSocket handshake has no Origin check (CSWSH) + +**Scope:** [audit](../README.md) · [README](../../README.md) · [AGENTS](../../AGENTS.md) + +CWE-1385 ("Missing Origin Validation in WebSockets"). Real-world instances of this +exact class include CVE-2024-23898 (Jenkins CLI, CVSS 9), CVE-2024-26135 +(MeshCentral, CVSS 8.8) and CVE-2023-0957 (Gitpod, CVSS 8.2); the class, not a +MoloApp-specific CVE. + +## Source + +- [CERT/CC-adjacent writeups and NVD entries for CVE-2024-23898 and CVE-2024-26135](https://www.cve.org/CVERecord?id=CVE-2024-23898) + +## Relevance to MoloApp + +A browser attaches the page's `Origin` to a WebSocket handshake automatically and also +attaches same-site cookies, the same way it would to any other cross-site request. +If the server accepts the handshake without checking `Origin` against an allow-list, +any page an authenticated victim visits can open a WebSocket to the app and ride the +victim's session, a Cross-Site WebSocket Hijacking. Unlike ordinary CSRF, the +`Sec-Fetch-Site` and SameSite-cookie defenses that `CsrfGuard` relies on for regular +requests do not automatically apply to the opening handshake unless the WebSocket +layer checks `Origin` itself. + +`src/realtime/websocket/server/handshake_validator.cpp`, `HandshakeValidator::validate` +checks method, `Upgrade`, `Connection`, the key's shape, version and subprotocol; it +never reads `Origin`, and `HandshakeConfig` (`handshake_validator.h`) has no +allowed-origins field to check against. + +## What the check does + +This class is "a pure function of request plus config: no I/O" (the header's own +comment) and is not wired to any upgrade route in the example app today, so there is +no live server to drive it through; the check instead compiles a tiny harness +(`reproduce.cpp`) against the real `handshake_validator.cpp`/`sha1.cpp`/`base64.cpp`/ +`realtime_metrics.cpp` and calls `HandshakeValidator::validate` directly with a +well-formed handshake carrying `Origin: https://evil.example`. PASS means a future +origin check rejected it; FAIL means it was accepted (`ok == true`). + +## Caveats + +This is not a claim that an upgrade route exists in the shipped example today; it is +a check on the shared validator every real route would have to use, matching the +header's own description of the class as I/O-free and route-independent. + +Run: `make test` in this directory, or `make -C audit test` from the repo root. diff --git a/audit/MOLO-WS-001/reproduce.cpp b/audit/MOLO-WS-001/reproduce.cpp new file mode 100644 index 0000000..9710bb7 --- /dev/null +++ b/audit/MOLO-WS-001/reproduce.cpp @@ -0,0 +1,48 @@ +// MOLO-WS-001: WebSocket handshake accepts a cross-origin request with no +// Origin check, enabling Cross-Site WebSocket Hijacking. See README.md. +// +// There is no server binary that wires HandshakeValidator up to a live +// socket (example/fw's /ws/chat route never upgrades), so this check +// drives the real class directly, the same code every real upgrade path +// in src/realtime/websocket/server calls, with no server loop needed: +// HandshakeValidator::validate is documented as "Pure function of request +// plus config: no I/O." +#include +#include + +#include "realtime/websocket/server/handshake_validator.h" + +using molo::realtime::websocket::server::HandshakeConfig; +using molo::realtime::websocket::server::HandshakeValidator; +namespace shared = molo::realtime::shared; + +int main() { + shared::HttpRequest req; + req.method = "GET"; + req.path = "/ws/chat"; + req.headers.emplace("Upgrade", "websocket"); + req.headers.emplace("Connection", "Upgrade"); + req.headers.emplace("Sec-WebSocket-Key", "dGhlIHNhbXBsZSBub25jZQ=="); // RFC 6455 example + req.headers.emplace("Sec-WebSocket-Version", "13"); + // The victim's browser would attach this automatically; a same-site + // server has no business accepting it from an attacker's origin. + req.headers.emplace("Origin", "https://evil.example"); + + HandshakeValidator v(HandshakeConfig{}); + auto result = v.validate(req); + + if (result.ok) { + std::printf( + "FAIL: handshake for Origin: https://evil.example was accepted (status %d, " + "Sec-WebSocket-Accept: %s); HandshakeValidator::validate " + "(src/realtime/websocket/server/handshake_validator.cpp) never reads the " + "Origin header and HandshakeConfig has no allowed-origins field, so any " + "cross-site page can open this WebSocket carrying the victim's cookies " + "(CSWSH, CWE-1385)\n", + result.response.status, result.accept_key.c_str()); + return 1; + } + std::printf("PASS: handshake for a cross-origin request was rejected (%s)\n", + result.failure_reason.c_str()); + return 0; +} diff --git a/audit/Makefile b/audit/Makefile new file mode 100644 index 0000000..bd5cd0e --- /dev/null +++ b/audit/Makefile @@ -0,0 +1,33 @@ +# Runs every regression check under audit/ against this tree's built artifacts +# (build/bin/runtime_interop_server, build/example/living-server, build/molo, and +# a few checks that compile a tiny harness straight from src/). +# Usage: +# make -C audit test # sequential, stops at the first FAIL +# make -C audit -j8 test # concurrent, one job per check directory (recursive make) +# make -C audit report # runs every check, never stops, prefixes output with the check id +# make -C audit list # names the check directories this covers +# +# Each check's Makefile delegates to that directory's reproduce.py or reproduce.cpp, +# which prints exactly one of PASS, FAIL or SKIP and exits 0 for PASS/SKIP, 1 for FAIL. +# "test" is the hard cap: this file does not invent new checks, it only fans out to +# the fixed, named list below. See README.md. + +MOLO_BUILD ?= $(abspath ../build) +CHECKS := $(sort $(wildcard MOLO-*)) +export MOLO_BUILD + +.PHONY: test $(CHECKS) list report + +test: $(CHECKS) + @echo "audit: $(words $(CHECKS)) checks run against $(MOLO_BUILD)" + +$(CHECKS): + @$(MAKE) -s -C $@ test MOLO_BUILD=$(MOLO_BUILD) + +list: + @for c in $(CHECKS); do echo $$c; done + +report: + @status=0; for c in $(CHECKS); do \ + $(MAKE) -s -C $$c test MOLO_BUILD=$(MOLO_BUILD) 2>&1 | sed "s/^/$$c: /" || status=1; \ + done; exit $$status diff --git a/audit/README.md b/audit/README.md new file mode 100644 index 0000000..1adc7f1 --- /dev/null +++ b/audit/README.md @@ -0,0 +1,92 @@ +# audit + +**Scope:** [README](../README.md) · [AGENTS](../AGENTS.md) + +A fixed, named set of regression checks for vulnerability classes relevant to the +mechanisms MoloApp actually has (an in-house HTTP/1 and HTTP/2 server, an in-house +WebSocket/WebDAV/reverse-tunnel realtime stack, a shell-invoking CLI, in-house +crypto and an ORM), each in its own directory with where the problem lives, why it +matters to MoloApp specifically, and a check that reproduces it against real, +built artifacts. Modeled on the `audit/` convention used by the `doos` project: +capped to the issues that target code paths MoloApp actually has, not "every CVE", +consistent with the base rule that every automated process gets a fixed, named +ceiling instead of an open-ended scope. + +Most of these are **in-house findings** from reading this codebase, not public CVE +assignments (MoloApp is not itself a CVE-tracked project); each README still cites +the real-world vulnerability *class* it is modeled on, with sources, so the check's +severity and shape can be judged against known precedent. + +A check here answers one question: does *this build*, today, reproduce the exposure +described. It is not a claim that MoloApp is unaffected by anything not listed here. + +## Running it + +``` +make # build/bin/runtime_interop_server, build/example/living-server, build/molo +make -C audit test # sequential +make -C audit -j8 test # concurrent, one job per check +make -C audit report # runs all, never stops, one line prefix per check +make -C audit list # names the check directories +``` + +Each check is independent, writes only under its own `mktemp -d` (Python checks) or +is a pure in-memory harness (C++ checks), and cleans up on exit. A check that needs +something the host does not have (an unbuilt binary) prints `SKIP` with the reason +and exits 0; it never reports PASS for a precondition it could not test. Every check +runs under a hard wall-clock watchdog (`audit/lib/molo_audit.py`'s `arm_watchdog`, or +an `alarm()` call in the C++ harnesses) so a check that finds a genuine hang reports +FAIL instead of wedging the suite. + +## Verdicts + +| Verdict | Meaning | +|---|---| +| `PASS` | This build does not reproduce the exposure described, under the conditions this check creates. | +| `FAIL` | It does. Treat as a regression; the check prints what it observed. | +| `SKIP` | The check's precondition was not met on this host (missing build). Not a verdict on MoloApp. | + +## How a check drives the real thing + +Three kinds of real artifact, no mocks: + +- **Over a real socket**, against `build/bin/runtime_interop_server` (the runtime's + own interop test server: real HTTP/1.1, real h2c HTTP/2, real compression, real + static files, nothing stubbed) or `build/example/living-server`. These checks live + in `audit/lib/molo_audit.py`'s `H2`/`h1` helpers: a minimal HTTP/2 client speaking + real frames, and raw-socket HTTP/1.1, both stdlib-only Python. +- **Through the real CLI**, `build/molo`, run as a subprocess against a scratch + directory. +- **Against the real class directly**, for the handful of classes (`src/realtime/ + websocket/server/handshake_validator.cpp`, `src/realtime/webdav/server/ + resource_tree.cpp`, `src/realtime/expose/upstream.cpp`) that are pure, I/O-free + logic not yet wired to any route in the shipped example (`example/fw/` is + explicitly a skeleton still being re-bound to `src/`, per the main README). Each + such check's README says so and compiles a tiny harness straight from the real + `.cpp` file, the same code any real route would call, rather than skipping the + class entirely. + +## What is deliberately not here + +No kernel-level memory-corruption exploit code, and no check that requires outbound +network access (DNS resolution, reaching a real external host) to prove its point. +Everything here drives a MoloApp artifact through its real inputs inside a scratch +directory or a loopback socket; nothing here targets any host other than the one +running the check, and nothing here is a weaponized exploit for an unrelated target. + +## The list + +| Check | Mechanism | Modeled on | +|---|---|---| +| [MOLO-H1-001](MOLO-H1-001/) | HTTP/1 chunked-body cap bypass via chunk-size addition overflow | CWE-190 / request-smuggling-adjacent resource exhaustion | +| [MOLO-H2-001](MOLO-H2-001/) | HTTP/2 stream-open/reset churn, no rate limit | CVE-2023-44487 "Rapid Reset" | +| [MOLO-H2-002](MOLO-H2-002/) | HTTP/2 server-triggered reset churn | CVE-2025-8671 "MadeYouReset" | +| [MOLO-H2-003](MOLO-H2-003/) | HTTP/2 empty-CONTINUATION flood, byte cap only | CVE-2024-27316 "CONTINUATION flood" | +| [MOLO-H2-004](MOLO-H2-004/) | HTTP/2 PING flood to an unread peer, no queue cap | CVE-2019-9511/9512/9515/9518 HTTP/2 DoS batch | +| [MOLO-STATIC-001](MOLO-STATIC-001/) | Static-file jail escape via a symlinked intermediate directory | CWE-61 symlink following | +| [MOLO-CLI-001](MOLO-CLI-001/) | Shell command injection in `molo new ` | CWE-78 OS command injection | +| [MOLO-WS-001](MOLO-WS-001/) | WebSocket handshake has no Origin check | CWE-1385 / CVE-2024-23898-class CSWSH | +| [MOLO-DAV-001](MOLO-DAV-001/) | WebDAV COPY into its own subtree never terminates | CWE-674 uncontrolled recursion | +| [MOLO-EXPOSE-001](MOLO-EXPOSE-001/) | `expose` upstream allow-list bypassed by hostname prefix | CWE-184 incomplete allow-list / SSRF-adjacent | + +Back to [README](../README.md), [AGENTS](../AGENTS.md). diff --git a/audit/lib/molo_audit.py b/audit/lib/molo_audit.py new file mode 100644 index 0000000..c404bf4 --- /dev/null +++ b/audit/lib/molo_audit.py @@ -0,0 +1,367 @@ +"""Shared harness for the audit/ checks. Spec: audit/README.md. + +Every check is a reproduce.py that imports this module, starts a scratch +server (or none), probes it over a real socket and ends in exactly one of +verdict_pass / verdict_fail / verdict_skip. Everything here has a hard cap: +check wall time (WATCHDOG_SECS), bytes sent, frames sent, read waits. +Python 3 stdlib only. No third-party dependency. +""" +import atexit +import os +import select +import shutil +import signal +import socket +import struct +import subprocess +import sys +import tempfile +import time + +WATCHDOG_SECS = 90 # hard ceiling per check, see audit/README.md +READ_SLICE_SECS = 0.2 # select granularity +MAX_SEND_BYTES = 64 * 1024 * 1024 # no check may push more than this + +ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")) +BUILD = os.environ.get("MOLO_BUILD", os.path.join(ROOT, "build")) +INTEROP = os.environ.get("MOLO_INTEROP", os.path.join(BUILD, "bin", "runtime_interop_server")) +LIVING = os.environ.get("MOLO_LIVING", os.path.join(BUILD, "example", "living-server")) +MOLO = os.environ.get("MOLO_BIN", os.path.join(BUILD, "molo")) + +_cleanups = [] + + +def _run_cleanups(): + while _cleanups: + try: + _cleanups.pop()() + except Exception: + pass + + +atexit.register(_run_cleanups) + + +def _finish(tag, msg, code): + print("%s: %s" % (tag, msg)) + sys.stdout.flush() + _run_cleanups() + os._exit(code) + + +def verdict_pass(msg): + _finish("PASS", msg, 0) + + +def verdict_skip(msg): + _finish("SKIP", msg, 0) + + +def verdict_fail(msg): + _finish("FAIL", msg, 1) + + +def _watchdog(signum, frame): + verdict_fail("check exceeded its %ds budget (server hang or unbounded work)" % WATCHDOG_SECS) + + +def arm_watchdog(secs=WATCHDOG_SECS): + signal.signal(signal.SIGALRM, _watchdog) + signal.alarm(secs) + + +def scratch_dir(): + d = tempfile.mkdtemp(prefix="molo-audit-") + _cleanups.append(lambda: shutil.rmtree(d, ignore_errors=True)) + return d + + +class Server: + """A scratch server process on 127.0.0.1, killed on exit.""" + + def __init__(self, argv, ready_prefix="READY", ready_secs=10): + self.proc = subprocess.Popen(argv, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, + text=True, cwd=argv_cwd(argv)) + _cleanups.append(self.stop) + self.port = None + deadline = time.time() + ready_secs + while time.time() < deadline: + line = self.proc.stdout.readline() + if not line: + break + if line.startswith(ready_prefix) or "http://127.0.0.1:" in line: + self.port = int(line.strip().split(":")[-1].split()[-1]) + break + if self.port is None: + verdict_skip("server did not report READY: %s" % " ".join(argv)) + + def rss_kb(self): + try: + with open("/proc/%d/status" % self.proc.pid) as f: + for ln in f: + if ln.startswith("VmRSS:"): + return int(ln.split()[1]) + except OSError: + pass + return -1 + + def alive(self): + return self.proc.poll() is None + + def stop(self): + if self.proc.poll() is None: + self.proc.kill() + try: + self.proc.wait(timeout=3) + except Exception: + pass + + +def argv_cwd(argv): + return None + + +def start_interop(www_files=None): + """Start runtime_interop_server on an ephemeral port with a scratch www dir. + Returns (server, tmpdir). tmpdir/www is served, tmpdir/secret.txt is outside it.""" + if not os.path.exists(INTEROP): + verdict_skip("%s not built (run make)" % INTEROP) + tmp = scratch_dir() + www = os.path.join(tmp, "www") + os.makedirs(www) + with open(os.path.join(www, "hello.txt"), "w") as f: + f.write("hello\n") + with open(os.path.join(www, "big.bin"), "wb") as f: + f.write(os.urandom(200 * 1024)) + with open(os.path.join(tmp, "secret.txt"), "w") as f: + f.write("MOLO-AUDIT-SECRET-OUTSIDE-WWW\n") + for name, data in (www_files or {}).items(): + with open(os.path.join(www, name), "wb") as f: + f.write(data) + return Server([INTEROP, "0", www]), tmp + + +def start_living(): + if not os.path.exists(LIVING): + verdict_skip("%s not built (run make)" % LIVING) + s = socket.socket() + s.bind(("127.0.0.1", 0)) + port = s.getsockname()[1] + s.close() + srv = Server([LIVING, str(port)], ready_prefix="living example") + return srv + + +# ---------------------------------------------------------------- HTTP/1 + + +def connect(port, timeout=5): + s = socket.create_connection(("127.0.0.1", port), timeout=timeout) + return s + + +def recv_all(s, secs=1.0, cap=4 * 1024 * 1024): + """Read until the peer closes, the cap is hit, or `secs` pass without data.""" + buf = b"" + s.setblocking(False) + end = time.time() + secs + closed = False + while time.time() < end and len(buf) < cap: + r, _, _ = select.select([s], [], [], READ_SLICE_SECS) + if r: + try: + d = s.recv(65536) + except (ConnectionResetError, BrokenPipeError): + closed = True + break + if not d: + closed = True + break + buf += d + end = time.time() + secs + return buf, closed + + +def h1(port, data, secs=1.0): + """Send raw bytes, return (response bytes, closed_by_peer).""" + s = connect(port) + try: + s.sendall(data) + except OSError: + pass + out = recv_all(s, secs) + s.close() + return out + + +def status_lines(resp): + """All 'HTTP/1.1 NNN' status codes found at line starts, in order.""" + codes = [] + for ln in resp.split(b"\r\n"): + if ln.startswith(b"HTTP/1.") and len(ln) >= 12: + try: + codes.append(int(ln[9:12])) + except ValueError: + pass + return codes + + +def alive_h1(port): + try: + resp, _ = h1(port, b"GET /echo HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n", 1.5) + return 200 in status_lines(resp) + except OSError: + return False + + +# ---------------------------------------------------------------- HTTP/2 + +PREFACE = b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" +T_DATA, T_HEADERS, T_PRIORITY, T_RST, T_SETTINGS, T_PUSH, T_PING, T_GOAWAY, T_WINDOW, T_CONT = range(10) +F_ACK = 0x1 +F_END_STREAM = 0x1 +F_END_HEADERS = 0x4 + + +def frame(ftype, flags, sid, payload=b""): + n = len(payload) + return struct.pack(">BHBBI", (n >> 16) & 0xFF, n & 0xFFFF, ftype, flags, sid & 0x7FFFFFFF) + payload + + +def settings(pairs=()): + return frame(T_SETTINGS, 0, 0, b"".join(struct.pack(">HI", k, v) for k, v in pairs)) + + +def _lit(idx, value): + v = value.encode() if isinstance(value, str) else value + return bytes([idx]) + bytes([len(v)]) + v if len(v) < 127 else None + + +def hpack_request(method="GET", path="/", authority="x", extra=()): + """Header block using only static-table refs and plain literals.""" + block = b"\x82" if method == "GET" else b"\x83" # :method GET / POST + block += b"\x86" # :scheme http + block += _lit(0x04, path) # :path (literal, name idx 4) + block += _lit(0x01, authority) # :authority (name idx 1) + for name, value in extra: + n, v = name.encode(), value.encode() + block += b"\x00" + bytes([len(n)]) + n + bytes([len(v)]) + v + return block + + +def hpack_int(prefix_bits, first_byte_flags, value): + mx = (1 << prefix_bits) - 1 + if value < mx: + return bytes([first_byte_flags | value]) + out = bytearray([first_byte_flags | mx]) + value -= mx + while value >= 128: + out.append((value & 0x7F) | 0x80) + value >>= 7 + out.append(value) + return bytes(out) + + +def parse_frames(buf): + frames = [] + i = 0 + while len(buf) - i >= 9: + n = (buf[i] << 16) | (buf[i + 1] << 8) | buf[i + 2] + if len(buf) - i - 9 < n: + break + ftype, flags = buf[i + 3], buf[i + 4] + sid = struct.unpack(">I", buf[i + 5:i + 9])[0] & 0x7FFFFFFF + frames.append((ftype, flags, sid, buf[i + 9:i + 9 + n])) + i += 9 + n + return frames, buf[i:] + + +class H2: + """Prior-knowledge h2c client. Tracks every frame the server sends back.""" + + def __init__(self, port, initial_settings=()): + self.s = connect(port) + self.s.setblocking(False) + self.rbuf = b"" + self.frames = [] + self.closed = False + self.goaway = None # error code + self.sent = 0 + self.sent_bytes = 0 + self.raw(PREFACE + settings(initial_settings)) + + def _drain_read(self, secs=0.0): + try: + r, _, _ = select.select([self.s], [], [], secs) + while r: + d = self.s.recv(262144) + if not d: + self.closed = True + break + self.rbuf += d + r, _, _ = select.select([self.s], [], [], 0) + except (ConnectionResetError, BrokenPipeError, OSError): + self.closed = True + fr, self.rbuf = parse_frames(self.rbuf) + for f in fr: + self.frames.append(f) + if f[0] == T_GOAWAY and self.goaway is None: + self.goaway = struct.unpack(">I", f[3][4:8])[0] if len(f[3]) >= 8 else 0 + return fr + + def raw(self, data): + """Blocking-ish send that keeps reading so a full buffer cannot deadlock.""" + if self.sent_bytes + len(data) > MAX_SEND_BYTES: + verdict_fail("harness cap: refusing to send more than %d bytes" % MAX_SEND_BYTES) + view = memoryview(data) + deadline = time.time() + 20 + while view and not self.closed and time.time() < deadline: + self._drain_read(0) + try: + _, w, _ = select.select([], [self.s], [], 0.05) + if w: + n = self.s.send(view[:65536]) + view = view[n:] + self.sent_bytes += n + except (BlockingIOError, InterruptedError): + continue + except (ConnectionResetError, BrokenPipeError, OSError): + self.closed = True + return not view + + def cut(self): + """True once the server tore the connection down or sent GOAWAY.""" + return self.closed or self.goaway is not None + + def settle(self, secs=1.0): + end = time.time() + secs + while time.time() < end and not self.closed: + self._drain_read(0.1) + + def count(self, ftype, flags_mask=None): + return sum(1 for f in self.frames + if f[0] == ftype and (flags_mask is None or f[1] & flags_mask)) + + def status_of(self, sid): + """:status first byte guess for a stream: 0x88 => 200 indexed.""" + for f in self.frames: + if f[0] == T_HEADERS and f[2] == sid: + return f[3] + return None + + def close(self): + try: + self.s.close() + except OSError: + pass + + +def flood(h2, make_frame, count, batch=100): + """Send `count` frames in batches, stop early when the server cuts. Returns frames sent.""" + sent = 0 + while sent < count and not h2.cut(): + n = min(batch, count - sent) + h2.raw(b"".join(make_frame(sent + i) for i in range(n))) + sent += n + h2.settle(1.0) + return sent