noqt commited on
Commit
e48c8b8
·
1 Parent(s): c249334

fix: detect loader-accepted duplicate safetensors keys

Browse files
CHANGELOG.md CHANGED
@@ -15,6 +15,9 @@
15
  first installation or removal when the exact public command is retried.
16
  - Reject non-canonical ZIP member spellings and ambiguous trailing or
17
  concatenated archive data before privileged installation.
 
 
 
18
  - Keep publication, signing, tagging and external deployment as separate
19
  decisions.
20
 
 
15
  first installation or removal when the exact public command is retried.
16
  - Reject non-canonical ZIP member spellings and ambiguous trailing or
17
  concatenated archive data before privileged installation.
18
+ - Interpret duplicate Safetensors tensor names with the reference loader's
19
+ final-value semantics, then validate the resulting exact layout, so a model
20
+ accepted by the pinned loader cannot evade recognition by repeating a key.
21
  - Keep publication, signing, tagging and external deployment as separate
22
  decisions.
23
 
src/lumi_eggcracker/artifacts.py CHANGED
@@ -173,12 +173,11 @@ def validate_gguf_fd(
173
 
174
 
175
  def _json_pairs(pairs: list[tuple[str, object]]) -> dict[str, object]:
176
- value: dict[str, object] = {}
177
- for key, item in pairs:
178
- if key in value:
179
- raise JsonInputError("Safetensors header contains duplicate JSON keys")
180
- value[key] = item
181
- return value
182
 
183
 
184
  def validate_safetensors_fd(
 
173
 
174
 
175
  def _json_pairs(pairs: list[tuple[str, object]]) -> dict[str, object]:
176
+ # The reference Safetensors loader accepts duplicate JSON names with the
177
+ # final value taking precedence. Mirror that interpretation before
178
+ # validating the resulting tensor layout; otherwise a loader-accepted
179
+ # model can evade content recognition merely by repeating a key.
180
+ return dict(pairs)
 
181
 
182
 
183
  def validate_safetensors_fd(
tests/test_artifacts.py CHANGED
@@ -266,6 +266,19 @@ class ArtifactTests(unittest.TestCase):
266
  for path in (duplicate_file, invalid_dtype, invalid_range, truncated):
267
  self.assertIsNone(validate_path(path))
268
 
 
 
 
 
 
 
 
 
 
 
 
 
 
269
  def test_safetensors_allows_scalar_and_zero_dimension_tensors(self) -> None:
270
  with tempfile.TemporaryDirectory() as raw:
271
  root = Path(raw)
 
266
  for path in (duplicate_file, invalid_dtype, invalid_range, truncated):
267
  self.assertIsNone(validate_path(path))
268
 
269
+ def test_safetensors_duplicate_names_follow_reference_loader_last_value(self) -> None:
270
+ with tempfile.TemporaryDirectory() as raw:
271
+ path = Path(raw) / "duplicate-loader-accepted"
272
+ header = (
273
+ b'{"x":{"dtype":"U8","shape":[1],"data_offsets":[0,1]},'
274
+ b'"x":{"dtype":"U8","shape":[1],"data_offsets":[0,1]}}'
275
+ )
276
+ path.write_bytes(struct.pack("<Q", len(header)) + header + b"x")
277
+ evidence = validate_path(path)
278
+ self.assertIsNotNone(evidence)
279
+ assert evidence is not None
280
+ self.assertEqual("safetensors-v1", evidence.evidence_id)
281
+
282
  def test_safetensors_allows_scalar_and_zero_dimension_tensors(self) -> None:
283
  with tempfile.TemporaryDirectory() as raw:
284
  root = Path(raw)