feat(policies): structured violation output with typed finding schemas#2954
Conversation
…hemas Policies can now return structured violation objects (maps) in addition to plain strings. When a policy declares `finding_type` in its metadata, the engine validates violations against typed proto schemas (PolicyVulnerabilityFinding, PolicySASTFinding, PolicyLicenseViolationFinding) and stores the validated data in the CAS-stored PolicyEvaluationBundle. Closes chainloop-dev#2948 Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
There was a problem hiding this comment.
4 issues found across 34 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="app/cli/internal/policydevel/eval.go">
<violation number="1" location="app/cli/internal/policydevel/eval.go:51">
P3: Avoid `omitempty` on `warnings` so the output shape is explicit and stable for consumers.
(Based on your team's feedback about preferring explicit states over omission in serialized output.) [FEEDBACK_USED]</violation>
</file>
<file name="pkg/attestation/crafter/api/attestation/v1/crafting_state.proto">
<violation number="1" location="pkg/attestation/crafter/api/attestation/v1/crafting_state.proto:325">
P2: Severity is documented as an enum-like set but not validated, so invalid values can pass typed finding validation.</violation>
<violation number="2" location="pkg/attestation/crafter/api/attestation/v1/crafting_state.proto:327">
P1: `cvss_v3_score` lacks the documented 0.0–10.0 validation range.</violation>
</file>
<file name="app/controlplane/api/workflowcontract/v1/crafting_schema.proto">
<violation number="1" location="app/controlplane/api/workflowcontract/v1/crafting_schema.proto:287">
P2: `finding_type` is documented as a fixed set of values but is not schema-validated, so invalid values can be accepted and only fail later at runtime.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
…rnings field - Add buf.validate range constraint (0.0-10.0) on cvss_v3_score field - Remove omitempty from Warnings JSON tag for stable output shape - Add test for out-of-range CVSS score validation Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
…oto mapping - Add buf.validate string.in constraint on finding_type field - Document the mapping between finding_type values and their proto messages Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
Code reviewFound 1 issue:
The function handles three code paths (string violations, structured map violations, and unknown types) but chainloop/pkg/policies/engine/wasm/engine.go Lines 233 to 252 in 7442bef 🤖 Generated with Claude Code - If this code review was useful, please react with 👍. Otherwise, react with 👎. |
Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
Add a builtin Rego function that creates structured vulnerability findings, providing a cleaner API for policy authors instead of manually constructing violation map objects. Signature: chainloop.vulnerability(message, external_id, package_purl, severity) Optional fields can be added via Rego's object.union. Signed-off-by: Miguel Martinez <miguel@chainloop.dev>
| } | ||
|
|
||
| func vulnerabilityImpl(_ topdown.BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error { | ||
| if _, ok := operands[0].Value.(ast.String); !ok { |
There was a problem hiding this comment.
operands[0] is the name of the function, not the first argument, if I'm not wrong. I think all these validations are not needed, the Rego engine already takes care.
Summary
message+ typed fields) alongside plain stringsfinding_typefield in policy metadata declares the expected output schema (VULNERABILITY,SAST,LICENSE_VIOLATION)PolicyVulnerabilityFinding,PolicySASTFinding,PolicyLicenseViolationFinding) withbuf.validateconstraintsoneof findingonViolationmessage stores validated structured data in the CAS-storedPolicyEvaluationBundleCloses #2948