diff --git a/internal/schemavalidators/internal_schemas/aisecuritycontext/ai-security-context-0.1.schema.json b/internal/schemavalidators/internal_schemas/aisecuritycontext/ai-security-context-0.1.schema.json index d6c77493c..6f378114d 100644 --- a/internal/schemavalidators/internal_schemas/aisecuritycontext/ai-security-context-0.1.schema.json +++ b/internal/schemavalidators/internal_schemas/aisecuritycontext/ai-security-context-0.1.schema.json @@ -891,6 +891,10 @@ "trace_sessions": { "$ref": "#/definitions/trace_sessions", "description": "Chainloop Trace session IDs (session_external_id) declared on the FIX commit via the Chainloop-Trace-Sessions git trailer — the AI coding session(s) that produced the fix. Recorded verbatim by the producer (no DB lookup); sorted + deduped." + }, + "trace_checked": { + "$ref": "#/definitions/trace_checked", + "description": "True when the Chainloop graph lookup of the FIX commit gave a definite result (sessions found or no session found). A later scan skips the lookup for this commit." } } }, @@ -1060,9 +1064,17 @@ "trace_sessions": { "$ref": "#/definitions/trace_sessions", "description": "Chainloop Trace session IDs (session_external_id) declared on this INTRODUCING commit via the Chainloop-Trace-Sessions git trailer \u2014 the AI coding session(s) that introduced the flaw. Recorded verbatim by the producer (no DB lookup); sorted + deduped." + }, + "trace_checked": { + "$ref": "#/definitions/trace_checked", + "description": "True when the Chainloop graph lookup of this INTRODUCING commit gave a definite result (sessions found or no session found). A later scan skips the lookup for this commit." } } }, + "trace_checked": { + "type": "boolean", + "description": "Marks a commit whose AI coding session lookup in the Chainloop graph gave a definite result: at least one session found, or no session found. The producer does not set it when the lookup was denied or failed, so the next scan tries the commit again. Absent means the commit was never looked up, or the artifact was produced before the field existed; a consumer must treat absent and false the same. The mark is not a periodic re-check: a session attested after the lookup of its commit stays unlinked." + }, "commit_sha": { "type": "string", "pattern": "^[0-9a-f]{40}$", diff --git a/internal/schemavalidators/schemavalidators_test.go b/internal/schemavalidators/schemavalidators_test.go index 459a556ce..d5cd22b0f 100644 --- a/internal/schemavalidators/schemavalidators_test.go +++ b/internal/schemavalidators/schemavalidators_test.go @@ -1196,3 +1196,91 @@ func TestValidateSecurityContextTraceSessions(t *testing.T) { }) } } + +// TestValidateSecurityContextTraceChecked covers the optional trace_checked mark +// on a fix commit (fingerprint) and on an introducing commit (introduced_by[]). +// The producer sets it when the AI coding session lookup of the commit gave a +// definite result, so that the next scan can skip the commit (PFM-8487). +func TestValidateSecurityContextTraceChecked(t *testing.T) { + loadPayload := func(t *testing.T) map[string]any { + t.Helper() + f, err := os.ReadFile("./testdata/ai_security_context_valid.json") + require.NoError(t, err) + + var payload map[string]any + require.NoError(t, json.Unmarshal(f, &payload)) + return payload + } + + firstFingerprint := func(t *testing.T, payload map[string]any) map[string]any { + t.Helper() + fps, ok := payload["fingerprints"].([]any) + require.True(t, ok) + require.NotEmpty(t, fps) + fp, ok := fps[0].(map[string]any) + require.True(t, ok) + return fp + } + + firstIntroducedBy := func(t *testing.T, fp map[string]any) map[string]any { + t.Helper() + origins, ok := fp["introduced_by"].([]any) + require.True(t, ok) + require.NotEmpty(t, origins) + origin, ok := origins[0].(map[string]any) + require.True(t, ok) + return origin + } + + t.Run("the fixture carries trace_checked on both commit roles", func(t *testing.T) { + payload := loadPayload(t) + fp := firstFingerprint(t, payload) + require.Equal(t, true, fp["trace_checked"], "fix commit should carry trace_checked") + require.Equal(t, true, firstIntroducedBy(t, fp)["trace_checked"], "introducing commit should carry trace_checked") + require.NoError(t, schemavalidators.ValidateSecurityContext(payload, "")) + }) + + t.Run("a context without trace_checked validates", func(t *testing.T) { + payload := loadPayload(t) + fp := firstFingerprint(t, payload) + delete(fp, "trace_checked") + delete(firstIntroducedBy(t, fp), "trace_checked") + require.NoError(t, schemavalidators.ValidateSecurityContext(payload, "")) + }) + + cases := []struct { + name string + value any + wantErr bool + }{ + {name: "true", value: true}, + {name: "false", value: false}, + {name: "a string", value: "true", wantErr: true}, + {name: "a number", value: 1, wantErr: true}, + {name: "null", value: nil, wantErr: true}, + } + + for _, tc := range cases { + t.Run("fix commit with "+tc.name, func(t *testing.T) { + payload := loadPayload(t) + firstFingerprint(t, payload)["trace_checked"] = tc.value + err := schemavalidators.ValidateSecurityContext(payload, "") + if tc.wantErr { + require.Error(t, err) + return + } + require.NoError(t, err) + }) + + t.Run("introducing commit with "+tc.name, func(t *testing.T) { + payload := loadPayload(t) + firstIntroducedBy(t, firstFingerprint(t, payload))["trace_checked"] = tc.value + err := schemavalidators.ValidateSecurityContext(payload, "") + if tc.wantErr { + require.Error(t, err) + return + } + require.NoError(t, err) + }) + } +} diff --git a/internal/schemavalidators/testdata/ai_security_context_valid.json b/internal/schemavalidators/testdata/ai_security_context_valid.json index 786f7931c..d9fdbd691 100644 --- a/internal/schemavalidators/testdata/ai_security_context_valid.json +++ b/internal/schemavalidators/testdata/ai_security_context_valid.json @@ -185,13 +185,15 @@ "verified": true, "trace_sessions": [ "1f2e3d4c-5b6a-7980-a1b2-c3d4e5f60718" - ] + ], + "trace_checked": true } ], "introduced_to_fixed_seconds": 102, "trace_sessions": [ "8b012929-ba7f-4986-815c-2fbe692a9a98" - ] + ], + "trace_checked": true } ] } diff --git a/pkg/attestation/crafter/materials/aisecuritycontext/aisecuritycontext.go b/pkg/attestation/crafter/materials/aisecuritycontext/aisecuritycontext.go index 6b89ef784..e07aafdf6 100644 --- a/pkg/attestation/crafter/materials/aisecuritycontext/aisecuritycontext.go +++ b/pkg/attestation/crafter/materials/aisecuritycontext/aisecuritycontext.go @@ -235,6 +235,11 @@ type CommitRef struct { // trailer — the AI coding session(s) that introduced the flaw. Recorded // verbatim by the producer with no DB lookup; sorted + deduped. TraceSessions []string `json:"trace_sessions,omitempty"` + + // TraceChecked is true when the AI coding session lookup of this + // introducing commit gave a definite result (sessions found or no session + // found), so a later scan can skip the lookup. + TraceChecked bool `json:"trace_checked,omitempty"` } // Anchor is a byte-verifiable evidence span. A consumer can re-verify it @@ -328,6 +333,11 @@ type Fingerprint struct { // the AI coding session(s) that produced the fix. Recorded verbatim by the // producer with no DB lookup; sorted + deduped. TraceSessions []string `json:"trace_sessions,omitempty"` + + // TraceChecked is true when the AI coding session lookup of the fix commit + // gave a definite result (sessions found or no session found), so a later + // scan can skip the lookup. + TraceChecked bool `json:"trace_checked,omitempty"` } // Data is the AI security context payload: the object the producer writes under diff --git a/pkg/attestation/crafter/materials/testdata/ai-security-context.json b/pkg/attestation/crafter/materials/testdata/ai-security-context.json index 7bc5b5892..e778ddadf 100644 --- a/pkg/attestation/crafter/materials/testdata/ai-security-context.json +++ b/pkg/attestation/crafter/materials/testdata/ai-security-context.json @@ -548,13 +548,15 @@ "verified": true, "trace_sessions": [ "1f2e3d4c-5b6a-7980-a1b2-c3d4e5f60718" - ] + ], + "trace_checked": true } ], "introduced_to_fixed_seconds": 40385717, "trace_sessions": [ "8b012929-ba7f-4986-815c-2fbe692a9a98" - ] + ], + "trace_checked": true }, { "id": "fp_eda80e2676",