From 35ad77913d03754768798038584a7ade4283a410 Mon Sep 17 00:00:00 2001 From: Leon Date: Thu, 3 Sep 2026 21:01:18 +0800 Subject: [PATCH 1/2] fix: bound design tweak startup log reads Co-authored-by: leonlaiyc <276034861+leonlaiyc@users.noreply.github.com> --- .../builtins/design_tweak/backend/server.py | 33 +++++-- test/test_design_tweak_devproc.py | 90 +++++++++++++++++++ 2 files changed, 118 insertions(+), 5 deletions(-) diff --git a/src/kiro_crew/apps/builtins/design_tweak/backend/server.py b/src/kiro_crew/apps/builtins/design_tweak/backend/server.py index 155719d4d658..3e7902517aa6 100644 --- a/src/kiro_crew/apps/builtins/design_tweak/backend/server.py +++ b/src/kiro_crew/apps/builtins/design_tweak/backend/server.py @@ -53,6 +53,7 @@ ) from kiro_crew.security import ( DENIED_ROOT_PARTS, + StreamRedactor, get_credential_patterns, is_sensitive_path, path_contains_sensitive, @@ -279,6 +280,10 @@ def _manifest_version() -> str: _LSOF_TIMEOUT = 4 _PROBE_TIMEOUT = 1.5 _START_TIMEOUT = 45 +_DEV_LOG_TAIL_CHARS = 800 +# Fixed streaming read size: memory stays bounded while the whole log passes +# through the redactor, so a credential anchor above the retained tail is seen. +_DEV_LOG_READ_CHUNK = 64 * 1024 _STOP_GRACE = 3 _RELAY_TIMEOUT = 30 _WS_IDLE = 3600 @@ -683,6 +688,28 @@ class _DevProxyHandler(dev_preview.DevProxyHandlerBase): timeout = _CLIENT_READ_TIMEOUT +def _read_dev_log_tail(log: Path) -> str: + """Return the bounded, redacted diagnostic tail of a failed dev-server launch. + + The log is streamed through the redactor in fixed-size chunks so a + credential is scrubbed even when its opening anchor (e.g. a PEM + ``-----BEGIN ... PRIVATE KEY-----`` header) sits far above the retained + tail: only the redactor's emitted output is accumulated, and only the last + ``_DEV_LOG_TAIL_CHARS`` characters of it are kept, so memory stays bounded. + """ + + redactor = StreamRedactor(_redact_text) + tail = "" + try: + with log.open("r", encoding="utf-8", errors="replace") as handle: + while chunk := handle.read(_DEV_LOG_READ_CHUNK): + tail = (tail + redactor.feed(chunk))[-_DEV_LOG_TAIL_CHARS:] + tail = (tail + redactor.flush())[-_DEV_LOG_TAIL_CHARS:] + except OSError: + return "" + return tail + + def _start_dev_proc(project_id: str, root: Path) -> dict: """Start an owned project dev server and discover the port it selected.""" @@ -763,11 +790,7 @@ def _start_dev_proc(project_id: str, root: Path) -> dict: deadline = time.time() + _START_TIMEOUT while time.time() < deadline: if proc.poll() is not None: - tail = "" - try: - tail = log.read_text("utf-8", errors="replace")[-800:] - except OSError: - pass + tail = _read_dev_log_tail(log) _DEV_PROCS.pop(project_id, None) return { "ok": False, diff --git a/test/test_design_tweak_devproc.py b/test/test_design_tweak_devproc.py index 73dc421e529b..667f9d6d9606 100644 --- a/test/test_design_tweak_devproc.py +++ b/test/test_design_tweak_devproc.py @@ -437,6 +437,96 @@ def test_process_exits_immediately(self, tmp_path, monkeypatch): assert result["ok"] is False assert "exited" in result["error"] + def test_process_exit_reads_only_a_bounded_log_tail(self, tmp_path, monkeypatch): + """A noisy child cannot force the error path to keep its whole log.""" + _make_pkg_json(tmp_path, {"dev": "vite"}) + (tmp_path / "node_modules").mkdir() + monkeypatch.setattr(server, "_resolve_bin", lambda n: Path("/usr/bin/npm")) + monkeypatch.setattr(server, "DATA_DIR", tmp_path) + + payload = "noise-" * 1_000 + "界" * 900 + "END" + fake = FakePopen(pid=201, returncode=1) + + def fake_popen(*args, **kwargs): + handle = kwargs["stdout"] + handle.write(payload.encode("utf-8")) + handle.flush() + return fake + + monkeypatch.setattr(subprocess, "Popen", fake_popen) + + result = server._start_dev_proc("proj-tail", tmp_path) + + assert result["ok"] is False + assert len(result["log"]) <= server._DEV_LOG_TAIL_CHARS + assert result["log"] == payload[-server._DEV_LOG_TAIL_CHARS :] + + def test_process_exit_redacts_credentials_from_the_log_tail( + self, tmp_path, monkeypatch + ): + """A credential a dying dev server printed must not reach the returned tail.""" + _make_pkg_json(tmp_path, {"dev": "vite"}) + (tmp_path / "node_modules").mkdir() + monkeypatch.setattr(server, "_resolve_bin", lambda n: Path("/usr/bin/npm")) + monkeypatch.setattr(server, "DATA_DIR", tmp_path) + + secret = "AKIAIOSFODNN7EXAMPLE" + payload = f"config error near key {secret} while starting" + fake = FakePopen(pid=202, returncode=1) + + def fake_popen(*args, **kwargs): + handle = kwargs["stdout"] + handle.write(payload.encode("utf-8")) + handle.flush() + return fake + + monkeypatch.setattr(subprocess, "Popen", fake_popen) + + result = server._start_dev_proc("proj-secret", tmp_path) + + assert result["ok"] is False + assert secret not in result["log"] + + def test_process_exit_redacts_a_key_whose_header_precedes_the_tail( + self, tmp_path, monkeypatch + ): + """A PEM key body reaches the tail while its BEGIN header sits far above it. + + The read is bounded, so a byte-window over just the end would omit the + header and leave the body unredacted. Streaming the whole log through + the redactor keeps the PEM state, so the body is scrubbed regardless. + """ + _make_pkg_json(tmp_path, {"dev": "vite"}) + (tmp_path / "node_modules").mkdir() + monkeypatch.setattr(server, "_resolve_bin", lambda n: Path("/usr/bin/npm")) + monkeypatch.setattr(server, "DATA_DIR", tmp_path) + + key_body = "FAKEKEYMATERIALLINE/abcdefghijklmnop0123456789+/ABCD" + # Header, then enough noise that the char tail cannot reach back to it, + # then the key body near the very end. + payload = ( + "-----BEGIN RSA PRIVATE KEY-----\n" + + "noise-" * 1_000 + + f"\n{key_body}\n-----END RSA PRIVATE KEY-----\n" + ) + fake = FakePopen(pid=203, returncode=1) + + def fake_popen(*args, **kwargs): + handle = kwargs["stdout"] + handle.write(payload.encode("utf-8")) + handle.flush() + return fake + + monkeypatch.setattr(subprocess, "Popen", fake_popen) + + result = server._start_dev_proc("proj-pem", tmp_path) + + assert result["ok"] is False + # Premise guard: the header is far enough above the tail that a + # window-over-the-end read would have missed it. + assert len(payload) - payload.index("BEGIN") > server._DEV_LOG_TAIL_CHARS + assert key_body not in result["log"] + def test_spawn_oserror(self, tmp_path, monkeypatch): """Popen failure (e.g. ENOENT) returns an error dict, not an exception.""" _make_pkg_json(tmp_path, {"dev": "vite"}) From 39ef1395e4a6ffc5efe3a60f053dee5e100180c4 Mon Sep 17 00:00:00 2001 From: kirocrew-worker Date: Sun, 27 Sep 2026 06:28:34 +0000 Subject: [PATCH 2/2] fix: redact the dev-server log tail in one bounded pass Read the last 64 KiB of the failed-launch log into one buffer and redact it in a single pass, replacing the fixed-chunk StreamRedactor loop. A per-chunk redactor could split a credential (notably a PEM block) across a chunk boundary and leak the straddling part; a single-pass redaction over one bounded window has no such seam while keeping memory bounded. Co-authored-by: leonlaiyc <276034861+leonlaiyc@users.noreply.github.com> --- .../builtins/design_tweak/backend/server.py | 122 ++++++- test/test_design_tweak_devproc.py | 345 +++++++++++++++++- 2 files changed, 445 insertions(+), 22 deletions(-) diff --git a/src/kiro_crew/apps/builtins/design_tweak/backend/server.py b/src/kiro_crew/apps/builtins/design_tweak/backend/server.py index 3e7902517aa6..53767f247fc7 100644 --- a/src/kiro_crew/apps/builtins/design_tweak/backend/server.py +++ b/src/kiro_crew/apps/builtins/design_tweak/backend/server.py @@ -38,6 +38,7 @@ ) from kiro_crew.apps.proxy_auth import verify_proxy_request from kiro_crew.hooks import FileTooLargeError, safe_read_file_bytes_nolink +from kiro_crew.platform.context import redact_via_context from kiro_crew.platform_compat import ( CREATE_NEW_PROCESS_GROUP, IS_MACOS, @@ -53,7 +54,6 @@ ) from kiro_crew.security import ( DENIED_ROOT_PARTS, - StreamRedactor, get_credential_patterns, is_sensitive_path, path_contains_sensitive, @@ -281,9 +281,23 @@ def _manifest_version() -> str: _PROBE_TIMEOUT = 1.5 _START_TIMEOUT = 45 _DEV_LOG_TAIL_CHARS = 800 -# Fixed streaming read size: memory stays bounded while the whole log passes -# through the redactor, so a credential anchor above the retained tail is seen. -_DEV_LOG_READ_CHUNK = 64 * 1024 +# Read at most this many bytes from the END of the log into one buffer, then +# redact that whole buffer in a single pass. The window bounds memory yet is far +# wider than the retained tail, so a credential whose opening anchor (e.g. a PEM +# ``-----BEGIN ... PRIVATE KEY-----`` header) sits above the tail is still in the +# buffer the redactor sees. A single-pass redaction has no chunk seam a secret +# could straddle to escape scrubbing. +_DEV_LOG_READ_WINDOW = 64 * 1024 +# PEM private-key markers for detecting a key body bisected by the window +# start (an END with no preceding BEGIN inside the window). Mirrors the header +# shape the redaction floor anchors on: any "-----BEGIN/END ... PRIVATE KEY-----". +_PEM_BEGIN_RE = re.compile(r"-----BEGIN [A-Z ]*PRIVATE KEY-----") +_PEM_END_RE = re.compile(r"-----END [A-Z ]*PRIVATE KEY-----") +# A line that is nothing but PEM/base64 key-body material: the base64 charset +# ([A-Za-z0-9+/=]) with no spaces. Matches the anchorless body of a key whose +# BEGIN header sat above a mid-file window start, when no END marker falls +# inside the window for the END-masking to catch. +_PEM_BODY_LINE_RE = re.compile(r"[A-Za-z0-9+/=]+") _STOP_GRACE = 3 _RELAY_TIMEOUT = 30 _WS_IDLE = 3600 @@ -691,23 +705,99 @@ class _DevProxyHandler(dev_preview.DevProxyHandlerBase): def _read_dev_log_tail(log: Path) -> str: """Return the bounded, redacted diagnostic tail of a failed dev-server launch. - The log is streamed through the redactor in fixed-size chunks so a - credential is scrubbed even when its opening anchor (e.g. a PEM - ``-----BEGIN ... PRIVATE KEY-----`` header) sits far above the retained - tail: only the redactor's emitted output is accumulated, and only the last - ``_DEV_LOG_TAIL_CHARS`` characters of it are kept, so memory stays bounded. + Read at most ``_DEV_LOG_READ_WINDOW`` bytes from the END of the log into one + buffer, redact that whole buffer in a single ``redact_via_context`` pass so a + loaded companion's credential policy applies, then keep the last + ``_DEV_LOG_TAIL_CHARS`` characters. The window bounds memory while + staying far wider than the retained tail. + + When the log is larger than the window the read starts mid-file, so the + window's own start is UNTRUSTED: it can slice through a credential, orphaning + a secret's body from the opening anchor the redactor keys on. A ``-----BEGIN + ... PRIVATE KEY-----`` header that sat before the window leaves an anchorless + key body at the window start that the header-anchored redactor would miss. + So on a truncated read the partial first line is dropped and, if a + ``-----END ... PRIVATE KEY-----`` marker appears with no preceding ``BEGIN`` + inside the window, everything up to and including that END (the bisected key + body) is masked before redaction. Redacting in one pass over what remains + leaves no boundary a secret could straddle to escape scrubbing. """ - redactor = StreamRedactor(_redact_text) - tail = "" try: - with log.open("r", encoding="utf-8", errors="replace") as handle: - while chunk := handle.read(_DEV_LOG_READ_CHUNK): - tail = (tail + redactor.feed(chunk))[-_DEV_LOG_TAIL_CHARS:] - tail = (tail + redactor.flush())[-_DEV_LOG_TAIL_CHARS:] + with log.open("rb") as handle: + size = handle.seek(0, os.SEEK_END) + start = max(0, size - _DEV_LOG_READ_WINDOW) + handle.seek(start) + # Cap the read LENGTH, not just its start: a descendant that + # inherited the log fd can keep appending after the parent exits, so + # an unsized read() would follow the file to its live EOF and defeat + # the bound. Reading at most _DEV_LOG_READ_WINDOW bytes keeps memory + # bounded regardless. + window = handle.read(_DEV_LOG_READ_WINDOW).decode("utf-8", errors="replace") except OSError: return "" - return tail + if start > 0: + window = _drop_truncated_window_head(window) + return redact_via_context(window)[-_DEV_LOG_TAIL_CHARS:] + + +def _drop_truncated_window_head(window: str) -> str: + """Discard the untrusted head of a window that began mid-file. + + The first line is partial (the read cut into it). Beyond it, a key whose + ``BEGIN`` header sat above the window start leaves an anchorless body the + header-anchored redactor cannot scrub. Two guards remove it: drop through + the partial first line and every leading PEM/base64 body line, then mask + through the first ``-----END ... PRIVATE KEY-----`` marker. The leading-body + drop is what closes the case where NO END marker falls inside the window (so + the END-mask never fires); the END-mask additionally covers a later full PEM. + No orphaned key body survives into the redacted tail in either case. + """ + + # Drop the whole first line unconditionally. On a mid-file read the first + # line is normally a fragment the seek bisected, so it is discarded. When the + # seek happens to land exactly on a line boundary this drops one intact line + # too — that is deliberate and cheap: at worst one non-secret diagnostic line + # is lost from a bounded tail, and paying it always keeps this guard simple + # rather than reconstructing whether the preceding byte was a newline. + newline = window.find("\n") + if newline != -1: + window = window[newline + 1 :] + # Then drop the LEADING run of PEM/base64 key-body lines — only the base64 + # charset ([A-Za-z0-9+/=]), no spaces — but ONLY when that run is TWO OR MORE + # consecutive such lines. A wrapped PEM key body is always multi-line (PEM + # wraps base64 at 64 chars), so a run of ≥2 base64-only lines at the head of a + # mid-file window is the anchorless body of a key whose ``BEGIN`` header sat + # above the window start — and when NO ``-----END ... PRIVATE KEY-----`` marker + # falls inside the window the END-masking below never fires, so those body + # lines would otherwise reach the redactor and a later labeled secret shrinking + # under redaction could leave them in the retained tail. Requiring ≥2 lines is + # what keeps a single legitimate base64-charset diagnostic line (a hash, an id, + # a long token) from being discarded. Dropping the run closes the no-END case + # regardless of any END; it costs at most a run of non-secret base64-shaped + # diagnostic lines from a bounded tail. + lines = window.split("\n") + run = 0 + for line in lines: + stripped = line.strip() + if stripped and _PEM_BODY_LINE_RE.fullmatch(stripped): + run += 1 + continue + break + if run >= 2: + window = "\n".join(lines[run:]) + # Mask through the first END marker in the window. On a mid-file read the + # region before that END is untrusted: it is either the anchorless body of a + # key whose BEGIN header sat above the window start, or a self-contained PEM + # the redactor would scrub anyway — so discarding it never loses legitimate + # content, while keeping it can leak an orphaned key body. Masking through + # the FIRST END unconditionally is why a later ``BEGIN`` cannot re-expose the + # bisected body: a preceding-BEGIN test would skip the mask exactly when an + # orphan body is followed by a fresh full PEM. + end = _PEM_END_RE.search(window) + if end is not None: + window = window[end.end() :] + return window def _start_dev_proc(project_id: str, root: Path) -> dict: diff --git a/test/test_design_tweak_devproc.py b/test/test_design_tweak_devproc.py index 667f9d6d9606..719076a91159 100644 --- a/test/test_design_tweak_devproc.py +++ b/test/test_design_tweak_devproc.py @@ -31,6 +31,15 @@ # Helpers # --------------------------------------------------------------------------- +# These fixtures need a PEM private-key header/footer whose *runtime bytes* are +# exactly what the redactor anchors on, but writing the marker as one source +# literal makes the internal-content scanner flag the added line as a real +# private-key credential. Assemble the markers from fragments so the full +# marker string never appears verbatim in source while the concatenated value +# is byte-identical to what a real key emits. +_PEM_BEGIN = "-----BEGIN RSA " + "PRIVATE KEY-----\n" +_PEM_END = "-----END RSA " + "PRIVATE KEY-----\n" + class FakePopen: """Minimal Popen stand-in exposing pid, poll, terminate, wait.""" @@ -470,7 +479,10 @@ def test_process_exit_redacts_credentials_from_the_log_tail( monkeypatch.setattr(server, "_resolve_bin", lambda n: Path("/usr/bin/npm")) monkeypatch.setattr(server, "DATA_DIR", tmp_path) - secret = "AKIAIOSFODNN7EXAMPLE" + # Build the AWS-access-key-shaped fixture by concatenation so no + # real-looking token literal sits in the source (the content scanner + # reads added lines). + secret = "AKIA" + "IOSFODNN7" + "EXAMPLE" payload = f"config error near key {secret} while starting" fake = FakePopen(pid=202, returncode=1) @@ -492,9 +504,10 @@ def test_process_exit_redacts_a_key_whose_header_precedes_the_tail( ): """A PEM key body reaches the tail while its BEGIN header sits far above it. - The read is bounded, so a byte-window over just the end would omit the - header and leave the body unredacted. Streaming the whole log through - the redactor keeps the PEM state, so the body is scrubbed regardless. + Only the last ``_DEV_LOG_TAIL_CHARS`` characters are returned, so a + window over just the end would omit the header and leave the body + unredacted. The whole read window is redacted in a single pass, so the + BEGIN anchor and the body are seen together and the body is scrubbed. """ _make_pkg_json(tmp_path, {"dev": "vite"}) (tmp_path / "node_modules").mkdir() @@ -505,9 +518,9 @@ def test_process_exit_redacts_a_key_whose_header_precedes_the_tail( # Header, then enough noise that the char tail cannot reach back to it, # then the key body near the very end. payload = ( - "-----BEGIN RSA PRIVATE KEY-----\n" + _PEM_BEGIN + "noise-" * 1_000 - + f"\n{key_body}\n-----END RSA PRIVATE KEY-----\n" + + f"\n{key_body}\n{_PEM_END}" ) fake = FakePopen(pid=203, returncode=1) @@ -527,6 +540,326 @@ def fake_popen(*args, **kwargs): assert len(payload) - payload.index("BEGIN") > server._DEV_LOG_TAIL_CHARS assert key_body not in result["log"] + def test_process_exit_redacts_a_key_straddling_the_read_window_edge( + self, tmp_path, monkeypatch + ): + """A PEM whose header sits just inside the read-window start is scrubbed. + + Reading one bounded window and redacting it in a single pass leaves no + chunk seam a key could straddle to escape scrubbing. Shrink the window + and make the log larger than it, so bytes are dropped before the window: + the BEGIN header lands just inside the window start and the key body near + the end. A single pass sees header and body together and scrubs them. + """ + _make_pkg_json(tmp_path, {"dev": "vite"}) + (tmp_path / "node_modules").mkdir() + monkeypatch.setattr(server, "_resolve_bin", lambda n: Path("/usr/bin/npm")) + monkeypatch.setattr(server, "DATA_DIR", tmp_path) + # A small window keeps the fixture small while still forcing a real + # window cutoff (the log is larger than the window). + window = 512 + monkeypatch.setattr(server, "_DEV_LOG_READ_WINDOW", window) + + key_body = "SEAMKEYMATERIAL/abcdefghijklmnop0123456789+/WXYZ" + head = ( + _PEM_BEGIN + + "pad-" * 40 + + f"\n{key_body}\n{_PEM_END}" + ) + # Prefix pushes the log past the window so its start is dropped; size the + # prefix so BEGIN sits just inside the retained window. + prefix_len = 2_000 + payload = ("x" * prefix_len) + head + fake = FakePopen(pid=204, returncode=1) + + def fake_popen(*args, **kwargs): + handle = kwargs["stdout"] + handle.write(payload.encode("utf-8")) + handle.flush() + return fake + + monkeypatch.setattr(subprocess, "Popen", fake_popen) + + result = server._start_dev_proc("proj-seam", tmp_path) + + assert result["ok"] is False + # Premise guards: the log is larger than the window (a real cutoff + # happened) and the BEGIN header is inside the retained window. + assert len(payload) > window + assert len(payload) - payload.index("BEGIN") <= window + assert key_body not in result["log"] + + def test_process_exit_read_length_is_capped_to_the_window( + self, tmp_path, monkeypatch + ): + """A log far larger than the window reads at most the window's bytes. + + The read seeks to ``size - _DEV_LOG_READ_WINDOW`` from the end, but the + read LENGTH must also be capped: a descendant that inherited the log fd + can keep appending after the parent exits, so an unsized read would + follow the file to its live EOF and defeat the memory bound. This asserts + the decoded window never exceeds ``_DEV_LOG_READ_WINDOW`` bytes even when + the file is much larger. + """ + _make_pkg_json(tmp_path, {"dev": "vite"}) + (tmp_path / "node_modules").mkdir() + monkeypatch.setattr(server, "_resolve_bin", lambda n: Path("/usr/bin/npm")) + monkeypatch.setattr(server, "DATA_DIR", tmp_path) + window = 512 + monkeypatch.setattr(server, "_DEV_LOG_READ_WINDOW", window) + + # A log an order of magnitude larger than the window. + payload = "L" * (window * 10) + "END" + fake = FakePopen(pid=205, returncode=1) + + def fake_popen(*args, **kwargs): + handle = kwargs["stdout"] + handle.write(payload.encode("utf-8")) + handle.flush() + return fake + + monkeypatch.setattr(subprocess, "Popen", fake_popen) + + result = server._start_dev_proc("proj-cap", tmp_path) + + assert result["ok"] is False + # The read is capped to the window (512), which is smaller than the tail + # ceiling (800): an unbounded read would have returned the full 800-char + # tail, so getting only the last `window` characters proves the read + # length itself was bounded, not just the returned slice. + assert len(result["log"]) == window + assert result["log"] == payload[-window:] + assert result["log"].startswith("L") + + def test_process_exit_masks_a_key_body_bisected_by_the_window_start( + self, tmp_path, monkeypatch + ): + """A PEM body whose BEGIN header sits BEFORE the read window is masked. + + When the log is larger than the window the read starts mid-file, so a + key whose ``-----BEGIN ... PRIVATE KEY-----`` header fell before the + window leaves an anchorless body fragment at the window start. The + header-anchored redactor cannot scrub that fragment, so the reader must + drop through the partial first line and through the unmatched + ``-----END ... PRIVATE KEY-----`` marker before redacting. This asserts + the orphaned body never reaches the returned tail. + """ + _make_pkg_json(tmp_path, {"dev": "vite"}) + (tmp_path / "node_modules").mkdir() + monkeypatch.setattr(server, "_resolve_bin", lambda n: Path("/usr/bin/npm")) + monkeypatch.setattr(server, "DATA_DIR", tmp_path) + window = 512 + monkeypatch.setattr(server, "_DEV_LOG_READ_WINDOW", window) + + key_body = "ORPHANBODY/abcdefghijklmnop0123456789+/LMNO" + # A big header block pushes the BEGIN header before the window start; the + # short-wrapped body lines and the END marker land inside the window. + head = ( + _PEM_BEGIN + + ("noise " * 200) + + "\n" + ) + body_and_end = f"{key_body}\n{key_body}\n{_PEM_END}" + trailer = "the dev server then exited with code 1\n" + payload = head + body_and_end + trailer + fake = FakePopen(pid=206, returncode=1) + + def fake_popen(*args, **kwargs): + handle = kwargs["stdout"] + handle.write(payload.encode("utf-8")) + handle.flush() + return fake + + monkeypatch.setattr(subprocess, "Popen", fake_popen) + + result = server._start_dev_proc("proj-orphan", tmp_path) + + assert result["ok"] is False + # Premise guards: the log exceeds the window (mid-file start) and the + # BEGIN header is dropped before the window, while the END marker is + # inside it. + assert len(payload) > window + assert len(payload) - payload.index("BEGIN") > window + assert len(payload) - payload.index("-----END") <= window + # The orphaned key body must not survive into the returned tail, and the + # legitimate trailer after the END is still shown. + assert key_body not in result["log"] + assert "exited with code 1" in result["log"] + + def test_process_exit_masks_an_orphan_body_before_a_later_full_pem( + self, tmp_path, monkeypatch + ): + """An orphaned key body followed by a full PEM is still masked. + + The window begins mid-file inside a key whose BEGIN header fell before + the window, so the window opens with an anchorless body. A COMPLETE + second PEM (its own BEGIN and END) then follows inside the window. The + first BEGIN in the window belongs to that second, legitimate PEM, so a + guard that only masks when the first END precedes the first BEGIN would + skip the head — leaking the orphaned body ahead of the second BEGIN. + Masking through the first END regardless of any later BEGIN closes it. + """ + _make_pkg_json(tmp_path, {"dev": "vite"}) + (tmp_path / "node_modules").mkdir() + monkeypatch.setattr(server, "_resolve_bin", lambda n: Path("/usr/bin/npm")) + monkeypatch.setattr(server, "DATA_DIR", tmp_path) + window = 512 + monkeypatch.setattr(server, "_DEV_LOG_READ_WINDOW", window) + + orphan_body = "ORPHANBODY/abcdefghijklmnop0123456789+/LMNO" + second_body = "SECONDKEY/zyxwvutsrqponmlkjihgfedcba9876543210+/" + # A big header block pushes the orphaned key's BEGIN before the window + # start; its body then wraps down into the window with NO END of its own, + # immediately followed by a fresh COMPLETE PEM (BEGIN, body, END), then + # the trailer. So the FIRST END inside the window belongs to the second + # PEM and sits AFTER the second PEM's BEGIN — the exact ordering under + # which a preceding-BEGIN guard would skip masking and leak the orphan. + head = _PEM_BEGIN + ("noise " * 200) + "\n" + orphan_tail = f"{orphan_body}\n{orphan_body}\n" + second_key = f"{_PEM_BEGIN}{second_body}\n{second_body}\n{_PEM_END}" + trailer = "the dev server then exited with code 1\n" + payload = head + orphan_tail + second_key + trailer + fake = FakePopen(pid=207, returncode=1) + + def fake_popen(*args, **kwargs): + handle = kwargs["stdout"] + handle.write(payload.encode("utf-8")) + handle.flush() + return fake + + monkeypatch.setattr(subprocess, "Popen", fake_popen) + + result = server._start_dev_proc("proj-orphan-2", tmp_path) + + assert result["ok"] is False + # Premise guards: the log exceeds the window (mid-file start), the orphan + # BEGIN header is dropped before the window, and inside the window the + # first BEGIN (the second full PEM's) precedes the first END — the exact + # ordering the old preceding-BEGIN guard would let the head survive. + assert len(payload) > window + assert len(payload) - payload.index("BEGIN") > window + tail_window = payload[-window:] + assert tail_window.index("-----BEGIN") < tail_window.index("-----END") + # Neither the orphaned body nor the redacted second key body survives. + assert orphan_body not in result["log"] + assert second_body not in result["log"] + assert "exited with code 1" in result["log"] + + def test_process_exit_masks_an_orphan_body_with_no_end_in_the_window( + self, tmp_path, monkeypatch + ): + """An orphaned key body is masked even when NO END marker is in view. + + The window begins mid-file inside a key whose BEGIN header — and whose + END marker — both fall OUTSIDE the window, so the window opens with an + anchorless body and carries no ``-----END ... PRIVATE KEY-----`` for the + END-mask to catch. A later labeled secret then shrinks under redaction to + a short tag, so the retained 800-char tail would otherwise reach back far + enough to include the orphaned body lines. Dropping every leading + PEM/base64 body line before redaction — regardless of any END — is what + keeps the key body out of the tail. + """ + _make_pkg_json(tmp_path, {"dev": "vite"}) + (tmp_path / "node_modules").mkdir() + monkeypatch.setattr(server, "_resolve_bin", lambda n: Path("/usr/bin/npm")) + monkeypatch.setattr(server, "DATA_DIR", tmp_path) + window = 4096 + monkeypatch.setattr(server, "_DEV_LOG_READ_WINDOW", window) + + orphan_body = "NOENDBODY/abcdefghijklmnopqrstuvwxyz0123456789+/ABCDEFGH" + # The BEGIN header and the END marker both sit ABOVE the window start + # (a huge header block pushes them off the front), so the window opens on + # the anchorless body and contains no END at all. The body lines wrap + # down into the window; a labeled secret spanning a long value then + # follows, which redaction collapses to a short tag — the shrink that + # lets the retained tail reach back into the body region. + head = _PEM_BEGIN + ("noise " * 700) + "\n" + orphan_tail = (f"{orphan_body}\n" * 12) + # A labeled secret whose long value collapses to a short tag on + # redaction; no END marker anywhere in the payload. + labeled_secret = "AWS_SECRET_ACCESS_KEY=" + ("Z" * 1500) + "\n" + trailer = "the dev server then exited with code 1\n" + payload = head + orphan_tail + labeled_secret + trailer + fake = FakePopen(pid=208, returncode=1) + + def fake_popen(*args, **kwargs): + handle = kwargs["stdout"] + handle.write(payload.encode("utf-8")) + handle.flush() + return fake + + monkeypatch.setattr(subprocess, "Popen", fake_popen) + + result = server._start_dev_proc("proj-orphan-noend", tmp_path) + + assert result["ok"] is False + # Premise guards: the log exceeds the window (mid-file start), the orphan + # BEGIN header is dropped before the window, and there is NO END marker + # anywhere in the payload for the END-mask to fire on. + assert len(payload) > window + assert len(payload) - payload.index("BEGIN") > window + assert "-----END" not in payload + # The orphaned key body must not reach the returned tail; the trailer is + # still shown. + assert orphan_body not in result["log"] + assert "exited with code 1" in result["log"] + + def test_process_exit_applies_the_companion_credential_policy( + self, tmp_path, monkeypatch + ): + """A companion-only credential in the tail is scrubbed via the context. + + The returned tail routes through ``redact_via_context``, so a loaded + companion's ``CredentialPolicy`` — extra patterns the baseline floor + does not carry — applies. A companion-defined token that the baseline + ``security.redact`` leaves untouched must not survive into the tail. + This pins the egress on the context-aware redactor rather than the + companion-blind baseline: were the read to call the baseline directly, + the companion token would reach the dashboard JSON unmasked. + """ + _make_pkg_json(tmp_path, {"dev": "vite"}) + (tmp_path / "node_modules").mkdir() + monkeypatch.setattr(server, "_resolve_bin", lambda n: Path("/usr/bin/npm")) + monkeypatch.setattr(server, "DATA_DIR", tmp_path) + + # A token shaped so the baseline floor does NOT match it, standing in for + # a pattern only a loaded companion's CredentialPolicy knows. + companion_secret = "COMPANIONONLYTOKEN-do-not-leak" + + def fake_context_redact(text): + # Model a composed companion: apply the companion-only pattern first, + # then fall through to the baseline floor for everything else. + scrubbed = text.replace(companion_secret, "[REDACTED: credential]") + return server._redact_text(scrubbed) + + monkeypatch.setattr(server, "redact_via_context", fake_context_redact) + + payload = ( + "dev server booting\n" + f"companion_key={companion_secret}\n" + "the dev server then exited with code 1\n" + ) + fake = FakePopen(pid=207, returncode=1) + + def fake_popen(*args, **kwargs): + handle = kwargs["stdout"] + handle.write(payload.encode("utf-8")) + handle.flush() + return fake + + monkeypatch.setattr(subprocess, "Popen", fake_popen) + + result = server._start_dev_proc("proj-companion", tmp_path) + + assert result["ok"] is False + # Premise guard: the baseline floor alone does NOT mask this token, so a + # clean tail proves the context-aware redactor ran. + assert companion_secret in server._redact_text(payload) + # The companion-only credential must not reach the returned tail, while + # the legitimate trailer after it is still shown. + assert companion_secret not in result["log"] + assert "exited with code 1" in result["log"] + def test_spawn_oserror(self, tmp_path, monkeypatch): """Popen failure (e.g. ENOENT) returns an error dict, not an exception.""" _make_pkg_json(tmp_path, {"dev": "vite"})