fix #455 #448 IsADirectoryError in file_hash and save_semantic_cache, fix #454 sanitize_label crash on None source_file

This commit is contained in:
Safi
2026-04-21 21:25:13 +01:00
parent d34b329e3c
commit bba301bf1e
3 changed files with 8 additions and 4 deletions
+3 -1
View File
@@ -28,6 +28,8 @@ def file_hash(path: Path, root: Path = Path(".")) -> str:
so metadata-only changes (e.g. reviewed, status, tags) do not invalidate the cache.
"""
p = Path(path)
if not p.is_file():
raise IsADirectoryError(f"file_hash requires a file, got: {p}")
raw = p.read_bytes()
content = _body_content(raw) if p.suffix.lower() == ".md" else raw
h = hashlib.sha256()
@@ -163,7 +165,7 @@ def save_semantic_cache(
p = Path(fpath)
if not p.is_absolute():
p = Path(root) / p
if p.exists():
if p.is_file():
save_cached(p, result, root)
saved += 1
return saved
+1 -1
View File
@@ -380,7 +380,7 @@ def to_html(
"title": _html.escape(label),
"community": cid,
"community_name": sanitize_label((community_labels or {}).get(cid, f"Community {cid}")),
"source_file": sanitize_label(data.get("source_file", "")),
"source_file": sanitize_label(str(data.get("source_file") or "")),
"file_type": data.get("file_type", ""),
"degree": deg,
})
+4 -2
View File
@@ -191,13 +191,15 @@ _CONTROL_CHAR_RE = re.compile(r"[\x00-\x1f\x7f]")
_MAX_LABEL_LEN = 256
def sanitize_label(text: str) -> str:
def sanitize_label(text: str | None) -> str:
"""Strip control characters and cap length.
Safe for embedding in JSON data (inside <script> tags) and plain text.
For direct HTML injection, wrap the result with html.escape().
"""
text = _CONTROL_CHAR_RE.sub("", text)
if text is None:
return ""
text = _CONTROL_CHAR_RE.sub("", str(text))
if len(text) > _MAX_LABEL_LEN:
text = text[:_MAX_LABEL_LEN]
return text