Skip to content

feat(security-analysis): record and verify the OWASP catalog each phase reads - #154

Open
iamsreec wants to merge 6 commits into
mainfrom
sc/rag-support
Open

iamsreec wants to merge 6 commits into
mainfrom
sc/rag-support

Conversation

@iamsreec

@iamsreec iamsreec commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Summary

Phases C and D now read the OWASP catalog through per-phase snapshot files written by --prepare, and the checkpoint verifies each snapshot against the current catalog. analysis_state.json records the hash of every
snapshot and of the catalog, so a run shows exactly which catalog content it used, and a catalog edited after a phase was prepared fails that phase's checkpoint.

This is PR 1 (provenance) of the design in #114. Context-budget work and ranking follow in separate PRs.

Changes

  • Snapshots: --prepare --phase C writes owasp_catalog_C.json (descriptions, aliases and scenarios, with no mitigation or impact fields). --prepare --phase D writes owasp_catalog_D.json, holding exactly the categories Phase C marked Yes or Partial. The step guides point the model at these files instead of the raw catalog.
  • Freshness gate: the checkpoint rebuilds each snapshot from the current catalog and fails if they differ, naming the first changed item (e.g. ASI03#4) and the phase to re-run. guidance_merge refuses
    guidance derived from an older catalog.
  • Strict validation (review fixes):
    • Duplicate rows, non-object rows, non-string IDs and unexpected keys in a snapshot all fail the checkpoint.
    • A non-string ID in the catalog raises ReconciliationError, not a TypeError.
    • Each snapshot is read once, and the recorded hash is of the bytes that were validated.
  • Catalog loader: one parse path (security_catalog.py). It rejects duplicate, missing or malformed ASI01–ASI10 IDs and entries with no attack scenarios. An entry with no scenarios used to drop that category
    from mandatory coverage silently.
  • Packaging: the catalog ships in the wheel (data/*.json). A non-editable install used to fail Phase C with "OWASP catalog is missing".
  • Numbered-section guidance: guidance whose rules are Markdown headings (## 1. Title, as in employee) now works through Phase D. The existing-rule check recognizes the headings, and an addendum must add
    rules as the next numbered sections at the same heading level, so merged guidance keeps the target's format. Previously every reference to an existing rule failed the Phase D checkpoint, and an addendum could only
    be bare bullets appended under the last section. The same bug is on main. This is outside snapshots/provenance; it's included because it blocked the end-to-end run.
  • Tests:
    • Unit tests for each review case and for numbered-section guidance; all of them fail on the previous code.
    • A swap test that replaces the snapshot right after it is read.
    • An integration test that installs the wheel into a clean venv and loads the catalog from site-packages.
  • .gitignore: per-run snapshots are ignored, like analysis_state.json.

End-to-end runs

Two fresh step-9 runs on this branch (tests/run_smith_pipeline.py -s 9 -m aws/claude-sonnet-5). Both passed
Phases A–D.
In both:

  • Phase C read its snapshot, never the raw catalog.
  • The C snapshot has all 10 categories and 61 scenarios, with no mitigation or impact keys. Phase C dispositioned all 61 scenarios.
  • The D snapshot holds exactly the categories Phase C marked applicable.
  • analysis_state.json records hashes for the catalog and both snapshots, and each matches its file.
  • Every failed checkpoint attempt was a model error the checkpoint caught (undeclared arguments, missing citations or dispositions, inconsistent verdicts). None involved the catalog or snapshots.

RagChatbot_MCPServer (plain-numbered guidance; D snapshot ASI01, ASI02, ASI03, ASI08, ASI09). Phase D found no new rules.

Phase Model calls Checkpoint attempts Cache write Cache read Output Total Share
Setup 8 — 54,505 341,585 1,529 397,635 4%
A — Architecture 22 3 86,215 2,289,682 17,323 2,393,264 22%
B — Policy questionnaire 4 1 14,832 569,567 11,115 595,522 5%
C — Threat model 40 3 181,243 3,720,791 52,238 3,954,352 36%
D — Enforcement mapping 29 1 60,051 3,265,904 14,987 3,341,000 30%
Wrap-up 2 — 665 275,115 1,107 276,891 3%
Total 105 8 397,511 10,462,644 98,299 10,958,664

employee (heading-numbered guidance; D snapshot ASI01, ASI02, ASI03, ASI09). Phase D proposed one rule, written in the guidance's own format as the next section:

## 5. Only HR may change a time-off request's status

`update_time_off_status` is allowed only for callers whose `department` is **HR**.
Phase Model calls Checkpoint attempts Cache write Cache read Output Total Share
Setup 5 — 58,876 176,242 1,130 236,258 2%
A — Architecture 17 4 89,543 1,550,449 32,755 1,672,781 17%
B — Policy questionnaire 4 1 14,596 574,902 10,996 600,502 6%
C — Threat model 25 3 86,064 2,417,825 25,820 2,529,759 26%
D — Enforcement mapping 41 4 116,206 4,325,054 31,383 4,472,725 46%
Wrap-up 2 — 1,075 222,049 1,173 224,301 2%
Total 94 366,360 9,266,521 103,257 9,736,326

The per-phase sums equal each run's reported totals. Uncached input was under 210 tokens per run, so it's omitted. Cache reads are about 95% of every phase, so later phases cost more because the session keeps growing. That is the accumulation #114 §5A addresses. The artifacts, step logs and token tables for both runs are in the attached zip.

Checks

  • make ci passes (lint, license headers, 567 unit tests)
  • make test — not applicable; no policy behavior changed
  • CHANGELOG.md updated under ## [Unreleased]
  • PR title uses a conventional prefix
  • Commits are signed off for the DCO

Notes

make ci was run with ruff 0.15.20, the version CI uses. The dev extra
pins ruff==0.17.0, which enables 407 rules by default instead of 59 and
reports 253 existing findings across src/. That mismatch is on main
and worth its own issue.

Signed-off-by: Sai Sree Laya Chukkappalli <saisreelaya.chukkapalli@gmail.com>
@iamsreec
iamsreec marked this pull request as draft November 8, 2026 20:10
Signed-off-by: Sai Sree Laya Chukkappalli <saisreelaya.chukkapalli@gmail.com>
@iamsreec iamsreec changed the title feat: catalog provenance feat(security-analysis): record and verify the OWASP catalog each phase reads Nov 8, 2026
@araujof araujof self-assigned this Nov 9, 2026

@araujof araujof left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a duplicate ASI01 with different scenario text before the original entry, and Phase C still passed. The snapshot is also read again for hashing, so replacing it after validation can leave the state recording bytes that were never checked. Could we validate the whole snapshot and hash the same bytes?

The new changelog entry says the catalog ships in built packages, but the wheel I checked omits the JSON. Could we include the file and test the installed wheel, or remove that claim? Please also share an end-to-end example run for the changed phase guides, as CONTRIBUTING.md requires.

These snapshots are useful groundwork for reducing context use. I've asked for smaller handoffs, bounded batches and context budgets in the proposal review. That work can follow separately, keeping this PR focused on snapshots and provenance.

Comment thread src/smith/tools/security_catalog.py Outdated
Comment on lines +153 to +155
recorded = {
entry.get("id"): entry for entry in actual["threats"] if isinstance(entry, dict)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we validate the whole snapshot before building this dict? Non-object rows are dropped and duplicate IDs collapse to the last entry. I inserted an extra ASI01 with different scenario text before the genuine entry; Phase C still passed with 11 rows and recorded the edited file's hash. It also passed after I added a top-level mitigations field to the Phase C snapshot. Please reject malformed or duplicate rows and unexpected fields so the check covers all the content the model reads.

AttributeError,
) as exc:
raise ReconciliationError(f"OWASP catalog is malformed: {path}: {exc}") from exc
if sorted(ids) != list(CATALOG_IDS):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we check that IDs are strings before sorting them? Replacing ASI01's ID with 1 raises TypeError, bypassing the ReconciliationError path the CLI handles and showing a traceback instead of the malformed-catalog message.

)
continue
try:
recorded = json.loads(path.read_text(encoding="utf-8"))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we keep the snapshot bytes from this read and use their hash in the state record? The file is validated here, then read again for hashing later in checkpoint(). In a simulation that replaces the snapshot between those steps, the checkpoint passes and records the replacement's hash even though its scenario text would fail validation. Hashing the checked bytes would keep the record accurate.

…wheel

Signed-off-by: Sai Sree Laya Chukkappalli <saisreelaya.chukkapalli@gmail.com>
Signed-off-by: Sai Sree Laya Chukkappalli <saisreelaya.chukkapalli@gmail.com>
Signed-off-by: Sai Sree Laya Chukkappalli <saisreelaya.chukkapalli@gmail.com>
… in addenda

Signed-off-by: Sai Sree Laya Chukkappalli <saisreelaya.chukkapalli@gmail.com>
@iamsreec
iamsreec marked this pull request as ready for review November 13, 2026 16:37
@iamsreec

Copy link
Copy Markdown
Member Author

Thanks for the careful review. All three points and the packaging issue are fixed in bb91561:

  • Whole-snapshot validation: snapshot_drift now rejects unexpected top-level keys, rows that aren't objects, IDs that aren't strings, and duplicate IDs. It no longer skips or collapses them. Your two repros now fail the checkpoint: the extra ASI01 placed before the real entry gives duplicate snapshot rows: ASI01, and the top-level mitigations field gives unexpected snapshot keys: mitigations.
  • IDs that aren't strings: load_catalog checks ID types before sorting. An ID of 1 now raises ReconciliationError ("threat IDs must be strings"), so the CLI shows the malformed-catalog message instead of a traceback.
  • Hashing the validated bytes: _snapshot_errors reads each snapshot once and returns the hash of those bytes. checkpoint() records that hash and doesn't read the file again, so a snapshot replaced after validation can't end up in the state record.
  • Catalog in the wheel: I added data/*.json to [tool.setuptools.package-data]. A locally built wheel now contains smith/data/owasp_10_ai_catalog.json.

Each case has regression tests in test_security_catalog_unit.py, test_security_catalog_integration.py and test_security_analysis_checkpoint_unit.py.

One addition since your review: b210158 makes Phase D work with numbered-section guidance and keeps that numbering in the addenda. Please take a look at it as well.

@araujof, could you take another look when you have a chance?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants