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

+12 -2
View File
@@ -804,9 +804,19 @@ int load_adapter(const std::string& dir, const std::vector<Site>& sites, int max
/* ---- the config */
const std::string cfg_path = dir + "/adapter_config.json";
std::string text;
if (!read_file(cfg_path, &text))
const int rst = read_text_file(cfg_path, text, kMaxContainerFile);
if (rst == RAD_E_FORMAT)
return fail(err, RAD_E_FORMAT,
cfg_path + ": larger than the 256 MiB a container file may be");
if (rst != RAD_OK)
return fail(err, RAD_E_IO, cfg_path + ": cannot read (a PEFT adapter directory has one)");
const ojson cfg = ojson::parse(text, nullptr, false);
/* Container-supplied like the generation config, so read with the container's bounds: a
* file past them is refused during the read, never grown into a tree to be walked. */
bool over = false, deep = false;
const ojson cfg = ojson::parse_container(text, &over, &deep);
if (over || deep)
return fail(err, RAD_E_FORMAT,
cfg_path + (over ? ": carries far too many JSON values" : ": nests far too deep"));
if (cfg.is_discarded() || !cfg.is_object())
return fail(err, RAD_E_FORMAT, cfg_path + ": not a JSON object");
auto truthy = [&](const char* k) {