|
| 1 | +""" |
| 2 | +Regression tests for issue #25 — HTTP error responses must not leak |
| 3 | +exception class names or message internals. |
| 4 | +
|
| 5 | +Three endpoints previously interpolated `f"{type(e).__name__}: {e}"` into |
| 6 | +their JSON error body: |
| 7 | +
|
| 8 | +- GET /api/sessions/<project>/<id> (api/sessions.py) |
| 9 | +- GET /api/sessions/<project>/<id>/stats (api/sessions.py) |
| 10 | +- GET /api/projects/<project>/sessions (api/projects.py — per-session card error_detail) |
| 11 | +
|
| 12 | +This file exercises each via Flask test_client with a payload that triggers |
| 13 | +the failure path, asserts a 500 (or 200 for projects, since the per-session |
| 14 | +error is per-row), and verifies the response body contains no exception |
| 15 | +class names from a defensive blocklist. |
| 16 | +
|
| 17 | +Run: |
| 18 | + pytest tests/test_error_propagation.py -v |
| 19 | +""" |
| 20 | + |
| 21 | +from __future__ import annotations |
| 22 | + |
| 23 | +import json |
| 24 | +import sys |
| 25 | +from pathlib import Path |
| 26 | + |
| 27 | +import pytest |
| 28 | + |
| 29 | +REPO_ROOT = Path(__file__).resolve().parent.parent |
| 30 | +sys.path.insert(0, str(REPO_ROOT)) |
| 31 | + |
| 32 | +from flask import Flask # noqa: E402 |
| 33 | + |
| 34 | +from api.projects import projects_bp # noqa: E402 |
| 35 | +from api.sessions import sessions_bp # noqa: E402 |
| 36 | + |
| 37 | + |
| 38 | +# Defensive blocklist — any of these substrings appearing in a response body |
| 39 | +# would mean the leak regressed. Includes common Python builtin exception |
| 40 | +# class names plus internal-looking shapes. |
| 41 | +_LEAK_TOKENS = [ |
| 42 | + "Exception", |
| 43 | + "Error", |
| 44 | + "KeyError", |
| 45 | + "ValueError", |
| 46 | + "JSONDecodeError", |
| 47 | + "OSError", |
| 48 | + "FileNotFoundError", |
| 49 | + "TypeError", |
| 50 | + "AttributeError", |
| 51 | + "Traceback", |
| 52 | + "<class", |
| 53 | +] |
| 54 | + |
| 55 | + |
| 56 | +def _assert_no_class_name_leak(body_text: str, allow_word_error: bool = True): |
| 57 | + """Assert no exception class name appears in the response body. |
| 58 | +
|
| 59 | + `allow_word_error=True` lets the bare word "Error" pass (common in |
| 60 | + legitimate error messages like "Failed to ..."), but still blocks the |
| 61 | + `*Error` class-name suffixes which always carry a class-name shape. |
| 62 | + """ |
| 63 | + for tok in _LEAK_TOKENS: |
| 64 | + if allow_word_error and tok == "Error": |
| 65 | + continue |
| 66 | + assert tok not in body_text, ( |
| 67 | + f"Response body contains exception-class token {tok!r}: {body_text!r}" |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +@pytest.fixture |
| 72 | +def app(tmp_path, monkeypatch): |
| 73 | + """Minimal Flask app with the two blueprints under test.""" |
| 74 | + app = Flask(__name__) |
| 75 | + app.config["TESTING"] = True |
| 76 | + app.config["CLAUDE_PROJECTS_DIR"] = str(tmp_path) |
| 77 | + app.register_blueprint(sessions_bp) |
| 78 | + app.register_blueprint(projects_bp) |
| 79 | + return app |
| 80 | + |
| 81 | + |
| 82 | +@pytest.fixture |
| 83 | +def client(app): |
| 84 | + return app.test_client() |
| 85 | + |
| 86 | + |
| 87 | +def _write_session(tmp_path, project: str, session_id: str, content: str): |
| 88 | + """Write a session file (any content) under <tmp_path>/<project>/<id>.jsonl.""" |
| 89 | + proj = tmp_path / project |
| 90 | + proj.mkdir(exist_ok=True) |
| 91 | + p = proj / f"{session_id}.jsonl" |
| 92 | + p.write_text(content, encoding="utf-8") |
| 93 | + return p |
| 94 | + |
| 95 | + |
| 96 | +# --------------------------------------------------------------------------- |
| 97 | +# /api/sessions/<project>/<id> |
| 98 | +# --------------------------------------------------------------------------- |
| 99 | + |
| 100 | +class TestGetSessionErrorBody: |
| 101 | + |
| 102 | + def test_500_on_parse_failure_does_not_leak_class_name(self, tmp_path, client, monkeypatch): |
| 103 | + # Force the parser to raise an exception with a class-name + message |
| 104 | + # that WOULD leak through the old f-string interpolation if the fix |
| 105 | + # regressed. (parse_session is normally tolerant — it swallows per-line |
| 106 | + # JSONDecodeError — so we monkeypatch to guarantee we hit the except.) |
| 107 | + _write_session(tmp_path, "proj", "abc", "{}") |
| 108 | + |
| 109 | + def _boom(*args, **kwargs): |
| 110 | + raise KeyError("internal_secret_field_id") |
| 111 | + |
| 112 | + monkeypatch.setattr("api.sessions.parse_session", _boom) |
| 113 | + |
| 114 | + resp = client.get("/api/sessions/proj/abc") |
| 115 | + assert resp.status_code == 500 |
| 116 | + body = resp.get_json() |
| 117 | + assert isinstance(body, dict) |
| 118 | + assert body.get("error") == "Failed to parse session" |
| 119 | + # The exception's args include "internal_secret_field_id" — must not |
| 120 | + # appear in the response body. |
| 121 | + assert "internal_secret_field_id" not in json.dumps(body) |
| 122 | + _assert_no_class_name_leak(json.dumps(body)) |
| 123 | + |
| 124 | + def test_404_on_missing_file_keeps_session_id_safe(self, tmp_path, client): |
| 125 | + # Session ID is part of the URL so it appears in the 404 message — |
| 126 | + # that's fine; what we're guarding is exception-class leakage, which |
| 127 | + # 404 doesn't go through. |
| 128 | + resp = client.get("/api/sessions/proj/nope-doesnt-exist") |
| 129 | + assert resp.status_code == 404 |
| 130 | + body = resp.get_json() |
| 131 | + _assert_no_class_name_leak(json.dumps(body)) |
| 132 | + |
| 133 | + def test_400_on_path_traversal_attempt(self, client): |
| 134 | + # safe_join rejects this with ValueError; the 400 path returns a |
| 135 | + # generic "Invalid path" message and should not leak. |
| 136 | + resp = client.get("/api/sessions/..%2Fevil/abc") |
| 137 | + assert resp.status_code in (400, 404) |
| 138 | + body = resp.get_json() |
| 139 | + _assert_no_class_name_leak(json.dumps(body)) |
| 140 | + |
| 141 | + |
| 142 | +# --------------------------------------------------------------------------- |
| 143 | +# /api/sessions/<project>/<id>/stats |
| 144 | +# --------------------------------------------------------------------------- |
| 145 | + |
| 146 | +class TestGetSessionStatsErrorBody: |
| 147 | + |
| 148 | + def test_500_on_parse_failure_does_not_leak_class_name(self, tmp_path, client, monkeypatch): |
| 149 | + _write_session(tmp_path, "proj", "abc", "{}") |
| 150 | + |
| 151 | + def _boom(*args, **kwargs): |
| 152 | + raise ValueError("invalid literal: '/private/path/secret.json'") |
| 153 | + |
| 154 | + monkeypatch.setattr("api.sessions.parse_session", _boom) |
| 155 | + |
| 156 | + resp = client.get("/api/sessions/proj/abc/stats") |
| 157 | + assert resp.status_code == 500 |
| 158 | + body = resp.get_json() |
| 159 | + assert body.get("error") == "Failed to compute session stats" |
| 160 | + # The exception value contains a fake-secret path — must not leak. |
| 161 | + assert "/private/path" not in json.dumps(body) |
| 162 | + _assert_no_class_name_leak(json.dumps(body)) |
| 163 | + |
| 164 | + |
| 165 | +# --------------------------------------------------------------------------- |
| 166 | +# /api/projects (per-session card) |
| 167 | +# --------------------------------------------------------------------------- |
| 168 | + |
| 169 | +class TestGetProjectsErrorCard: |
| 170 | + |
| 171 | + def test_per_session_error_card_omits_error_detail(self, tmp_path, client, monkeypatch): |
| 172 | + # parse_session is tolerant of malformed lines, so to exercise the |
| 173 | + # except branch deterministically (the one that builds the error |
| 174 | + # card), monkeypatch it to raise — same pattern as the session-level |
| 175 | + # tests above. |
| 176 | + _write_session(tmp_path, "myproj", "deadbeef-aaaa-bbbb-cccc-000000000000", "{}") |
| 177 | + |
| 178 | + def _boom(*args, **kwargs): |
| 179 | + raise KeyError("internal_secret_field_id") |
| 180 | + |
| 181 | + # api/projects.py imports parse_session inside the handler body, |
| 182 | + # so patch the source module rather than the consumer. |
| 183 | + monkeypatch.setattr("utils.jsonl_parser.parse_session", _boom) |
| 184 | + |
| 185 | + resp = client.get("/api/projects/myproj/sessions") |
| 186 | + # Pin the response shape so a future wrapper change (e.g. {"sessions": [...]}) |
| 187 | + # doesn't silently turn this test green by skipping the per-row scan. |
| 188 | + assert resp.status_code == 200 |
| 189 | + body = resp.get_json() |
| 190 | + assert isinstance(body, list), ( |
| 191 | + f"Expected JSON array of session cards; got {type(body).__name__}" |
| 192 | + ) |
| 193 | + _assert_no_class_name_leak(json.dumps(body)) |
| 194 | + error_rows = [r for r in body if isinstance(r, dict) and r.get("error")] |
| 195 | + assert error_rows, "Expected at least one per-session error card from the forced parse failure" |
| 196 | + for row in error_rows: |
| 197 | + assert "error_detail" not in row, ( |
| 198 | + "Per-session error card still includes error_detail (issue #25)" |
| 199 | + ) |
| 200 | + # The exception's args include "internal_secret_field_id" — must not |
| 201 | + # appear anywhere in the response. |
| 202 | + assert "internal_secret_field_id" not in json.dumps(body) |
| 203 | + |
| 204 | + |
| 205 | +# --------------------------------------------------------------------------- |
| 206 | +# Source-level guard |
| 207 | +# --------------------------------------------------------------------------- |
| 208 | + |
| 209 | +class TestNoExceptionInterpolationInSource: |
| 210 | + """Static guard: any future PR that re-introduces the |
| 211 | + `f"...{type(e).__name__}: {e}..."` pattern in api/ fails this test.""" |
| 212 | + |
| 213 | + def test_api_files_dont_interpolate_exception_in_jsonify(self): |
| 214 | + api_dir = REPO_ROOT / "api" |
| 215 | + for py_file in api_dir.glob("*.py"): |
| 216 | + src = py_file.read_text(encoding="utf-8") |
| 217 | + # Look for the specific footgun: jsonify(...) with f-string that |
| 218 | + # contains both `type(e)` or `{e}` AND the word "error". |
| 219 | + offending_patterns = [ |
| 220 | + "type(e).__name__", # the class-name expose |
| 221 | + "{e}\"", # bare {e} ending an f-string |
| 222 | + "{e},", # bare {e} in a dict-value f-string |
| 223 | + ] |
| 224 | + for pat in offending_patterns: |
| 225 | + assert pat not in src, ( |
| 226 | + f"{py_file.name} contains forbidden pattern {pat!r} " |
| 227 | + f"— see issue #25" |
| 228 | + ) |
0 commit comments