From d893fd05d1d652295b375318c5a159274b42e29a Mon Sep 17 00:00:00 2001 From: fl0l0u Date: Sun, 15 Nov 2026 21:26:43 +0000 Subject: [PATCH] diag: the baseline and corpus files are read like the reports they are Both files diagnose consumes are written by other runs and pasted between people: --diagnose-baseline is an earlier report, the corpus a forwarded file. Neither was read like a supplied file. The baseline got the unbounded Json::parse, and compare_outputs then walked it with throwing accessors -- a member it does not expect throws, and nothing between run_diagnose and main catches -- so a crafted "earlier report" aborted a run with std::terminate after hours of benchmarking work. The corpus got a hand-rolled unbounded fread loop and a raw parser per line: a deep line recursed the parse to a stack overflow before the fingerprint check could ever run. Now: capped read and parse_container on both (the corpus's size refusal lands before the bytes are read), a per-line over/deep refusal that names the line, and one try around the comparison so any structure the comparison does not expect is the refusal it should be -- "the baseline is not shaped like a report", parity not compared, the run keeps its own results. diagnose_test pins the deep line. Found by the 2.1.0 review pass (both security axes, independently). --- core/diag/diagnose.cpp | 18 ++++++++++-------- core/engine_diagnose.cpp | 38 ++++++++++++++++++++++++++++---------- tests/diagnose_test.cpp | 8 ++++++++ 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/core/diag/diagnose.cpp b/core/diag/diagnose.cpp index 6ce6c7cc..6ea52d98 100644 --- a/core/diag/diagnose.cpp +++ b/core/diag/diagnose.cpp @@ -3,6 +3,7 @@ #include "diag/diag.h" +#include "rad_internal.h" #include "rad_runtime.h" #include @@ -943,15 +944,13 @@ std::vector builtin_corpus() { } int load_corpus(const std::string& path, std::vector* out, std::string* why) { - FILE* f = std::fopen(path.c_str(), "rb"); - if (!f) { - *why = "cannot open " + path; + /* A corpus is a file someone forwarded: the capped read and bounded parse every supplied + * JSON file gets -- the size refusal lands before the bytes are read. */ + std::string text; + if (read_text_file(path, text, kMaxContainerFile) != RAD_OK) { + *why = "cannot open " + path + ", or it carries more than the largest accepted JSON file"; return RAD_E_IO; } - std::string text; - char b[65536]; - for (size_t n; (n = std::fread(b, 1, sizeof b, f)) > 0;) text.append(b, n); - std::fclose(f); std::vector got; size_t line_no = 0; for (size_t pos = 0; pos <= text.size();) { @@ -962,11 +961,14 @@ int load_corpus(const std::string& path, std::vector* out, std::str ++line_no; const size_t a = line.find_first_not_of(" \t\r"); if (a == std::string::npos || line[a] == '#') continue; - const json j = json::parse(line, nullptr, /*allow_exceptions=*/false); + bool over = false, deep = false; + const json j = json::parse_container(line, &over, &deep); auto bad = [&](const char* what) { *why = path + ":" + std::to_string(line_no) + ": " + what; return RAD_E_INVAL; }; + if (over) return bad("carries far too many values for one prompt line"); + if (deep) return bad("nests far too deep for one prompt line"); if (j.is_discarded() || !j.is_object()) return bad("not a JSON object"); if (!j.contains("category") || !j["category"].is_string() || j["category"].get().empty()) return bad("needs a non-empty string \"category\""); diff --git a/core/engine_diagnose.cpp b/core/engine_diagnose.cpp index 6d310686..a795c7e8 100644 --- a/core/engine_diagnose.cpp +++ b/core/engine_diagnose.cpp @@ -652,19 +652,37 @@ int Engine::run_diagnose() { bool parity_ok = true; if (!cfg_.diagnose_baseline.empty()) { - std::ifstream f(cfg_.diagnose_baseline); - const json base = json::parse(f, nullptr, /*allow_exceptions=*/false); + /* A baseline is a report written by another run -- often on another machine, pasted + * between people: read it like a supplied file (capped read, bounded parse), and treat + * structure the comparison does not expect as a refusal, not as a crash. compare_outputs + * reads its baseline with throwing accessors, and nothing between here and main catches: + * a structurally hostile baseline used to abort a run that had already done its work. */ std::string why; json cmp; - if (!f.is_open() || base.is_discarded()) { + std::string base_text; + const json base = read_text_file(cfg_.diagnose_baseline, base_text, kMaxContainerFile) == RAD_OK + ? json::parse_container(base_text) + : json(); + if (base.is_discarded()) { why = "the baseline file is not readable JSON"; - } else if (diag::compare_outputs(rep, base, &cmp, &why) == RAD_OK) { - cmp["baseline"] = "an earlier report"; - rep["parity"] = cmp; - parity_ok = cmp["verdict"] == "identical" && cmp["acceptance_verdict"] == "no drop"; - RAD_INFO("diagnose: parity %s, %lld of %lld requests identical; draft acceptance: %s", - cmp["verdict"].get().c_str(), (long long)cmp["identical"].get(), - (long long)cmp["compared"].get(), cmp["acceptance_verdict"].get().c_str()); + } else { + int cmp_rc = RAD_E_FORMAT; + try { + cmp_rc = diag::compare_outputs(rep, base, &cmp, &why); + } catch (const json::exception& e) { + why = std::string("the baseline is not shaped like a report: ") + e.what(); + cmp_rc = RAD_E_FORMAT; + } + if (cmp_rc == RAD_OK) { + cmp["baseline"] = "an earlier report"; + rep["parity"] = cmp; + parity_ok = cmp["verdict"] == "identical" && cmp["acceptance_verdict"] == "no drop"; + RAD_INFO("diagnose: parity %s, %lld of %lld requests identical; draft acceptance: %s", + cmp["verdict"].get().c_str(), + (long long)cmp["identical"].get(), + (long long)cmp["compared"].get(), + cmp["acceptance_verdict"].get().c_str()); + } } if (!why.empty()) { rep["parity"] = { { "baseline", "an earlier report" }, diff --git a/tests/diagnose_test.cpp b/tests/diagnose_test.cpp index 91459f7a..9272bff6 100644 --- a/tests/diagnose_test.cpp +++ b/tests/diagnose_test.cpp @@ -464,6 +464,14 @@ TEST(corpus_file_reads_messages_and_prompts) { TEST(corpus_file_refuses_a_bad_line_and_names_it) { std::vector c; std::string why; + { + /* A line nesting past what a prompt line nests: the raw parser recursed for this, and + * the corpus -- a forwarded file -- is exactly the supplied input that stays bounded. */ + TmpFile f(std::string("{\"category\": \"c\", \"prompt\": \"x\", \"tail\": ") + + std::string(1200, '[') + std::string(1200, ']') + "}\n"); + CHECK_EQ(load_corpus(f.path, &c, &why), RAD_E_INVAL); + CHECK(why.find("nests far too deep") != std::string::npos); + } { TmpFile f("{\"category\": \"code\", \"prompt\": \"a\"}\n{\"prompt\": \"no category\"}\n"); CHECK_EQ(load_corpus(f.path, &c, &why), RAD_E_INVAL);