T09 · Insecure Skill Coding Practices
- Location
decision_gate.py:75- Finding
Hash Chain Can Be Rewritten Without Detection
- Content
View full analysis
dict: prev_hash = _last_hash(path) fields = {**fields, "prev_hash": prev_hash} entry_hash = hashlib.sha256(_canonical_bytes(fields)).hexdigest() record = {**fields, "entry_hash": entry_hash} path.parent.mkdir(parents=True, exist_ok=True) with path.open("a", encoding="utf-8") as f: f.write(json.dumps(record, sort_keys=True) + "\n") return record ``` The corresponding verification logic only recalculates the same unkeyed hashes: ```python def verify_chain(log_path: Optional[str] = None) -> tuple[bool, str]: path = _log_path(log_path) if not path.exists(): return True, "empty log — nothing to verify" expected_prev = GENESIS_HASH with path.open("r", encoding="utf-8") as f: for lineno, line in enumerate(f, start=1): line = line.strip() if not line: continue record = json.loads(line) entry_hash = record.pop("entry_hash", None) if record.get("prev_hash") != expected_prev: return False, f"line {lineno}: prev_hash mismatch — chain broken or reordered" recomputed = hashlib.sha256(_canonical_bytes(record)).hexdigest() if recomputed != entry_hash: return False, f"line {lineno}: entry_hash mismatch — entry was edited after being written" expected_prev = entry_hash return True, "chain intact" ``` ### Technical Analysis Each entry is protected only by an unkeyed SHA-256 digest. The digest provides integrity against accidental modification when an attacker cannot update the hash, but it does not authenticate the record or establish an immutable historical checkpoint. Anyone with write access to t ...[truncated 1526 chars]- Remediation
View remediation
