T09 · Insecure Skill Coding Practices
- Location
scripts/document_hash.py:165- Finding
Document Hash Verification Accepts an Out-of-Scope Hash Value
- Content
View full analysis
Vulnerability Details
File Location:
scripts/document_hash.py:165-168
Vulnerability Type: Improper validation scope
Risk Level: MediumVulnerable Code
python # Extract stored hash match = re.search(r'^\s*document-sha256:\s*["\']?([a-f0-9]{64})["\']?', content, re.MULTILINE) if not match: print("FAIL: No document-sha256 found") return FalseTechnical Analysis
The
verify()function searches the entire document for a line matchingdocument-sha256. It does not require the field to appear inside the document's YAML frontmatter.This conflicts with the declared document format and the behavior of
compute_hash(), which treatsdocument-sha256as a YAML-frontmatter field. Content after<!-- CLARITY_GATE_END -->is outside the hash window, butverify()still searches that content for the stored digest.An attacker can therefore place a matching digest after the end marker. The verifier will compare this out-of-scope value with the computed digest and report
PASS, even though the required hash field is absent from the YAML frontmatter. The regular expression also accepts the first matching line without detecting duplicate or conflicting hash fields.Attack Path
- Prepare a CGD document that has no valid
document-sha256field in its YAML frontmatter. - Run the hash computation mode to obtain the document's digest:
bash python scripts/document_hash.py crafted.cgd.md - Append the resulting digest outside the hash window, after the end marker:
markdown <!-- CLARITY_GATE_END --> Clarity Gate: CLEAR | REVIEWED document-sha256: <computed-64-character-digest> - Run verification:
bash python scripts/document_hash.py --verify crafted.cgd.md verify()finds the appended digest anywhere in the file, whilecompute_hash()excludes the post-marker content.- The values match, and the script incorrectly reports successful verification.
...[truncated 620 chars]
- Prepare a CGD document that has no valid
- Remediation
View remediation
Remediation Suggestions
- Extract and parse only the YAML frontmatter bounded by the document's initial
---delimiters. - Read
document-sha256exclusively from that parsed frontmatter rather than searching the complete file with a multiline regular expression. - Require exactly one
document-sha256field whose value is a scalar containing exactly 64 lowercase hexadecimal characters. - Reject missing, duplicate, multiline, conflicting, or out-of-frontmatter hash declarations.
- Ensure the opening frontmatter delimiter occurs at the valid document boundary after permitted BOM and newline normalization.
- Avoid unsafe general-purpose YAML object construction; use a safe loader or a narrow parser for the required metadata.
- Add negative regression tests covering:
- A hash only in the document body
- A hash only after
<!-- CLARITY_GATE_END --> - Missing frontmatter
- Duplicate hash fields
- Conflicting hash fields
- Quoted and multiline values
- Hash-like text inside fenced code blocks
- Fail closed when structural parsing is ambiguous or invalid.
- Extract and parse only the YAML frontmatter bounded by the document's initial
