Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/shotbible/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,15 @@ def _listed_refs(bible: Bible) -> set[str]:
def _ref_exists(root: Path, ref: str) -> bool:
if not str(ref).strip():
return False
root = root.resolve()
path = Path(ref)
if path.is_absolute():
return path.exists()
return (root / ref).exists()
candidate = path if path.is_absolute() else root / ref
try:
resolved = candidate.resolve()
resolved.relative_to(root)
except (ValueError, OSError):
return False
return candidate.exists()


def _normalize_look(look: str) -> str:
Expand Down
32 changes: 32 additions & 0 deletions tests/test_ref_escape.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from pathlib import Path

from shotbible.check import check_bible
from shotbible.store import save

from conftest import make_sample_bible, write_dummy_png


def test_check_rejects_relative_path_escape(tmp_path: Path) -> None:
root = tmp_path / "escape"
root.mkdir()
outside = tmp_path / "outside.png"
write_dummy_png(outside)
bible = make_sample_bible(with_ref=False)
bible.characters["mei"].refs = ["../outside.png"]
save(root, bible)

issues = check_bible(root, bible)
assert any(getattr(issue, "code", None) == "MISSING_REF" for issue in issues)


def test_check_rejects_absolute_path_outside_project(tmp_path: Path) -> None:
root = tmp_path / "abs"
root.mkdir()
outside = tmp_path / "outside.png"
write_dummy_png(outside)
bible = make_sample_bible(with_ref=False)
bible.characters["mei"].refs = [str(outside.resolve())]
save(root, bible)

issues = check_bible(root, bible)
assert any(getattr(issue, "code", None) == "MISSING_REF" for issue in issues)
Loading