Archived
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:
1 parent
e46a8ba3d4
commit
f2f94fc6e7
7 files changed
+131
-35
No files matched your search
+29
-12
@@ -93,9 +93,17 @@ std::vector<GenFieldSource> gen_sampling_sources(const GenSampling& stated, cons
|
||||
}
|
||||
|
||||
bool gen_sampling_parse(const std::string& json_text, const char* src, GenSampling* out) {
|
||||
Json j;
|
||||
try { j = Json::parse(json_text); }
|
||||
catch (const std::exception& e) { RAD_WARN("%s: %s; ignoring it", src, e.what()); return false; }
|
||||
/* A model-repo file, and container metadata reaches here too. Read it with the container's
|
||||
* bounds: a file that is refused never becomes a tree -- an unbounded read of a hostile one
|
||||
* could overflow this process's stack on the way to the dump or destructor below. */
|
||||
bool over = false, deep = false;
|
||||
Json j = Json::parse_container(json_text, &over, &deep);
|
||||
if (over || deep) {
|
||||
RAD_WARN("%s: %s; ignoring it", src,
|
||||
over ? "carries far too many JSON values" : "nests far too deep");
|
||||
return false;
|
||||
}
|
||||
if (j.is_discarded()) { RAD_WARN("%s: not valid JSON; ignoring it", src); return false; }
|
||||
if (!j.is_object()) { RAD_WARN("%s: not a JSON object; ignoring it", src); return false; }
|
||||
|
||||
/* `do_sample: false` means greedy, and transformers then ignores every other sampler field,
|
||||
@@ -178,12 +186,16 @@ bool gen_sampling_parse(const std::string& json_text, const char* src, GenSampli
|
||||
}
|
||||
|
||||
bool gen_sampling_from_file(const std::string& path, bool required, GenSampling* out) {
|
||||
std::ifstream f(path);
|
||||
if (!f) {
|
||||
std::string text;
|
||||
const int rst = read_text_file(path, text, kMaxContainerFile);
|
||||
if (rst == RAD_E_IO) {
|
||||
if (required) RAD_ERR("%s: cannot be read", path.c_str());
|
||||
return false;
|
||||
}
|
||||
std::string text((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
|
||||
if (rst != RAD_OK) {
|
||||
RAD_ERR("%s: larger than the 256 MiB a container file may be", path.c_str());
|
||||
return false;
|
||||
}
|
||||
return gen_sampling_parse(text, path.c_str(), out);
|
||||
}
|
||||
|
||||
@@ -204,13 +216,18 @@ bool gen_sampling_from_kv(const std::function<const char*(const char*)>& get, Ge
|
||||
}
|
||||
if (std::strcmp(k, "no_repeat_ngram_whitelist") == 0) {
|
||||
/* Not a number: the container's metadata is string-valued, and the whitelist is a
|
||||
* JSON array spelled as JSON text. */
|
||||
try {
|
||||
j[k] = Json::parse(v);
|
||||
} catch (const std::exception& e) {
|
||||
* JSON array spelled as JSON text. Bounded like every other container input. */
|
||||
bool over = false, deep = false;
|
||||
Json w = Json::parse_container(v, &over, &deep);
|
||||
if (over || deep)
|
||||
RAD_WARN("the container states generation.%s as \"%s\", which is not a JSON "
|
||||
"array of ids (%s); ignoring it", k, v, e.what());
|
||||
}
|
||||
"array of ids (%s); ignoring it",
|
||||
k, log_sanitize(v).c_str(),
|
||||
over ? "it carries far too many values" : "it nests far too deep");
|
||||
else if (!w.is_discarded()) j[k] = std::move(w);
|
||||
else
|
||||
RAD_WARN("the container states generation.%s as \"%s\", which is not a JSON "
|
||||
"array of ids; ignoring it", k, v);
|
||||
continue;
|
||||
}
|
||||
if (std::strcmp(k, "suppress_tokens") == 0) {
|
||||
|
||||
Reference in new issue
Block a user