Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(compaction): reject negative dictionary indices and non-array legends in expand_compacted (#4470)

Parideboy3c1fcbdfde31
diff --git a/headroom/transforms/compaction_codec.py b/headroom/transforms/compaction_codec.py
index 64cf42c32..5413cb7a8 100644
--- a/headroom/transforms/compaction_codec.py
+++ b/headroom/transforms/compaction_codec.py
@@ -383,9 +383,12 @@ def expand_compacted(text: str) -> list[dict[str, Any]] | None:
if not eq or not name.startswith("@"):
return None
try:
- legends[name[1:]] = json.loads(payload)
+ result = json.loads(payload)
except (ValueError, TypeError):
return None
+ if not isinstance(result, list):
+ return None
+ legends[name[1:]] = result
out: list[dict[str, Any]] = []
for cells in _split_data_rows(rest, declared):
@@ -404,7 +407,10 @@ def expand_compacted(text: str) -> list[dict[str, Any]] | None:
_assign(record, col.path, None)
continue
try:
- _assign(record, col.path, legends[col.name][int(value)])
+ idx_val = int(value)
+ if idx_val < 0:
+ return None
+ _assign(record, col.path, legends[col.name][idx_val])
except (ValueError, IndexError):
return None
continue
diff --git a/tests/test_transforms/test_compaction_codec.py b/tests/test_transforms/test_compaction_codec.py
index 45476b59e..fa49077b3 100644
--- a/tests/test_transforms/test_compaction_codec.py
+++ b/tests/test_transforms/test_compaction_codec.py
@@ -15,6 +15,8 @@ from __future__ import annotations
import json
+import pytest
+
from headroom.transforms.compaction_codec import (
contains_removal_marker,
expand_compacted,
@@ -201,3 +203,24 @@ def test_single_column_null_survives_value_factoring() -> None:
factored = factor_values(block)
assert expand_compacted(factored) == expand_compacted(block)
assert expand_compacted(factored) == [{"k": "x"}, {"k": "y"}, {"k": None}]
+
+
+def test_negative_dictionary_index_is_rejected() -> None:
+ # Python's -1 would silently pick the last legend value.
+ assert expand_compacted('[1]{tag:string} __dict:1\n@tag=["first","last"]\n-1\n') is None
+
+
[email protected]("legend", ["42", '"ab"', '{"a":1}', "null"])
+def test_non_array_legend_is_rejected(legend: str) -> None:
+ assert expand_compacted(f"[1]{{tag:string}} __dict:1\n@tag={legend}\n0\n") is None
+
+
+def test_dictionary_cells_still_decode_edge_values() -> None:
+ block = '[5]{tag:string?} __dict:1\n@tag=["first","last"]\n0\n1\n"-1"\nnull\n\n'
+ assert expand_compacted(block) == [
+ {"tag": "first"},
+ {"tag": "last"},
+ {"tag": "-1"},
+ {"tag": None},
+ {},
+ ]