Skip to content

Commit f5b817c

Browse files
Copilotmbarbero
andauthored
fix: normalize jsonnet import callback byte paths
Agent-Logs-Url: https://github.com/eclipse-csi/otterdog/sessions/fa876869-cace-485b-859f-f347247e0aee Co-authored-by: mbarbero <452625+mbarbero@users.noreply.github.com>
1 parent 63384b5 commit f5b817c

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

otterdog/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,10 +416,12 @@ def __exit__(self, *exc: Any) -> None:
416416
self._token = None
417417

418418

419-
def _make_jsonnet_import_callback(allowed_root: str) -> Callable[[str, str], tuple[str, str | None]]:
419+
def _make_jsonnet_import_callback(allowed_root: str) -> Callable[[str | bytes, str | bytes], tuple[str, str | None]]:
420420
real_root = os.path.realpath(allowed_root)
421421

422-
def callback(base: str, rel: str) -> tuple[str, str | None]:
422+
def callback(base: str | bytes, rel: str | bytes) -> tuple[str, str | None]:
423+
base = os.fsdecode(base)
424+
rel = os.fsdecode(rel)
423425
candidate = rel if os.path.isabs(rel) else os.path.join(base, rel)
424426
# resolve symlinks and ".." traversal before the containment check
425427
resolved = os.path.realpath(candidate)

tests/test_utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@ def test_jsonnet_import_callback_returns_files_inside_root(tmp_path: Path):
145145
assert content2 == "{ x: 1 }"
146146

147147

148+
def test_jsonnet_import_callback_accepts_byte_paths(tmp_path: Path):
149+
(tmp_path / "vendor").mkdir()
150+
inside = tmp_path / "vendor" / "lib.libsonnet"
151+
inside.write_bytes(b"{ x: 1 }")
152+
153+
callback = _make_jsonnet_import_callback(str(tmp_path))
154+
resolved, content = callback(os.fsencode(tmp_path / "vendor"), b"lib.libsonnet")
155+
156+
assert isinstance(resolved, str)
157+
assert resolved == os.path.realpath(str(inside))
158+
assert content == "{ x: 1 }"
159+
160+
148161
def test_jsonnet_import_callback_rejects_absolute_outside_root(tmp_path: Path):
149162
callback = _make_jsonnet_import_callback(str(tmp_path))
150163

0 commit comments

Comments
 (0)