format: the index does not get to reach outside the model dir

hfget screens the names a repository LISTING carries, but the shard
index is one of those listed files, and nothing screened what its
weight_map names. An index of "../../etc/passwd" led the loader to open
that path -- outside the model directory, as the serving user -- and a
directory merging two checkpoints has every reason to hold a real index.

shard_name_ok now reads every weight_map value before the first open:
no absolute name, no empty, "." or ".." component; relative subdirs,
which real repos do use, still pass. The refusal names the index and
the offending shard. A checkpoint_test case opens a poisoned index in
six shapes and the honest one once, and only the honest one reads.
This commit is contained in:
fl0l0u committed 2026-11-15 20:38:09 +00:00
1 parent 50ea36f283
commit 981cd1e4e1
2 files changed
+95 -3

No files matched your search

+26
View File
@@ -136,6 +136,32 @@ TEST(the_metadata_names_the_architecture_and_the_quantisation) {
CHECK_EQ(ck.dir(), dir);
}
/* The index is checkpoint-supplied data like the tensors, and its weight_map values are paths:
* a poisoned one must not lead the loader to open files outside the model dir as the serving
* user. hfget screens the listing's names; nothing screened the index's until here. */
TEST(a_poisoned_index_cannot_lead_the_shard_list_out_of_the_model_dir) {
auto dir_with_index = [](const char* shard) -> std::string {
const std::string dir = write_checkpoint({ bf16("x", { 2, 2 }, 0) }, kConfig);
const std::string idx = std::string("{\"metadata\":{\"total_size\":8},\"weight_map\":"
"{\"x\":\"") + shard + "\"}}";
/* No REQUIRE in here: its failure spelling returns void, and this lambda returns a
* directory. A null FILE* just fails to write, and every caller checks the open. */
std::FILE* f = std::fopen((dir + "/model.safetensors.index.json").c_str(), "wb");
if (f != nullptr) {
std::fwrite(idx.data(), 1, idx.size(), f);
std::fclose(f);
}
return dir;
};
/* The honest index still reads: the name is the real shard's own. */
Checkpoint ok;
CHECK_EQ(ok.open(dir_with_index("model.safetensors")), RAD_OK);
for (const char* bad : { "../../../../etc/passwd", "/etc/passwd", "..", "a/..", "./x", "" }) {
Checkpoint ck;
CHECK(ck.open(dir_with_index(bad)) < 0);
}
}
TEST(routed_expert_metadata_accepts_the_deepseek_and_glm_config_spellings) {
const std::string dir = write_checkpoint({ bf16("x", {2, 2}, 0) },
"{\"model_type\":\"glm5_next\",\"text_config\":{\"hidden_size\":4096,"