util: one bounded read for the files a container supplies

tokenizer.json, gen_config.json, an adapter's adapter_config.json and
the container's generation.* whitelist are all container-supplied, and
all read through the unbounded parser: a hostile file was refused only
by running out of stack -- the tree it grows is walked, dumped and
destroyed recursively on the way.

Json::parse_container is the wire's bounded read sized for files
instead: 2^24 values, because a tokenizer's vocabulary legitimately
carries a million flat strings, and 256 levels, because nothing
legitimate nests deeper. A file past either bound comes back discarded,
like a syntax error, and the four readers map that onto the failure
paths they already had -- ignore with a warning, refuse the adapter,
refuse the vocab. The bound fires during the read; no hostile tree
exists to be walked.
This commit is contained in:
fl0l0u committed 2026-11-15 20:38:09 +00:00
1 parent e46a8ba3d4
commit f2f94fc6e7
7 files changed
+131 -35

No files matched your search

+8
View File
@@ -249,6 +249,14 @@ Json Json::parse_bounded(std::string_view text, int64_t budget, int max_depth, b
return out;
}
Json Json::parse_container(std::string_view text, bool* over, bool* deep) {
bool o = false, d = false;
Json j = parse_bounded(text, 1LL << 24, 256, &o, &d);
if (over) *over = o;
if (deep) *deep = d;
return j;
}
Json Json::parse(std::string_view text) { return parse(text, nullptr, true, false); }
Json Json::parse(std::istream& in) {
+7
View File
@@ -894,6 +894,13 @@ public:
* of a tree when a bound trips; returns discarded on a syntax error. */
static Json parse_bounded(std::string_view text, int64_t budget, int max_depth, bool* over,
bool* deep);
/* A JSON file the container, an adapter or a download supplied. The wire's bounded read,
* sized generously instead: these files legitimately carry huge FLAT value sets (a
* tokenizer's vocabulary), and nothing legitimate nests 256 deep. A file past either bound
* comes back discarded, like a syntax error -- deep enough to refuse, never deep enough
* to overflow a dump's or a destructor's stack on the way there. */
static Json parse_container(std::string_view text, bool* over = nullptr,
bool* deep = nullptr);
private:
using difference_type = std::ptrdiff_t;
+16
View File
@@ -1,6 +1,9 @@
/* rad_util.cpp -- logging, string formatting, Geometry and constraint matching. */
#include <atomic>
#include <chrono>
#include <filesystem>
#include <fstream>
#include <sstream>
#include "../rad_internal.h"
#include <cstdio>
@@ -92,6 +95,19 @@ std::string log_sanitize(const std::string& s) {
return out;
}
int read_text_file(const std::string& path, std::string& out, size_t cap) {
std::error_code ec;
const std::uintmax_t sz = std::filesystem::file_size(path, ec);
if (ec) return RAD_E_IO;
if (sz > cap) return RAD_E_FORMAT; /* refused before the bytes are read */
std::ifstream f(path, std::ios::binary);
if (!f) return RAD_E_IO;
std::ostringstream ss;
ss << f.rdbuf();
out = ss.str();
return RAD_OK;
}
void log_emit(Log l, const char* file, int line, const char* fmt, ...) {
const char* base = std::strrchr(file, '/');
base = base ? base + 1 : file;