diff --git a/docs/decisions/attestation-process.md b/docs/decisions/attestation-process.md index 17d0ce01a..dbde45738 100644 --- a/docs/decisions/attestation-process.md +++ b/docs/decisions/attestation-process.md @@ -22,7 +22,7 @@ Structure. The index (README.md) has the parts Areas (the area map), Relationshi > Written by `eng-adr`. To change a decision, write a spec or run `eng-adr new`. Do not edit this page by hand. -**Purpose:** The attestation lifecycle of the CLI (`init`, `add`, `status`, `push`, `reset`, `verify`), and how it selects its organization and project. **Owner:** migmartri. **Does not own:** AI coding session evidence (Chainloop Trace), and login and tokens (Authentication). **Specs:** [issue-3063-attestation-repo-config](../specs/issue-3063-attestation-repo-config.md). **Last refresh:** 2026-11-01, commit 4e150ca7. +**Purpose:** The attestation lifecycle of the CLI (`init`, `add`, `status`, `push`, `reset`, `verify`), and how it selects its organization and project. **Owner:** migmartri. **Does not own:** AI coding session evidence (Chainloop Trace), and login and tokens (Authentication). **Specs:** [issue-3063-attestation-repo-config](../specs/issue-3063-attestation-repo-config.md). **Last refresh:** 2026-11-23, commit 81dd68bb. ## Language @@ -62,6 +62,14 @@ The organization from the file has the same effect as `--org` for the run. It is - Source: [issue-3063-attestation-repo-config D-010](../specs/issue-3063-attestation-repo-config.md) - Code: verified: `app/cli/cmd/attestation.go`, `app/cli/internal/token/token.go`, `app/cli/cmd/root.go`. +### ATT-005: A non-JSON ARTIFACT evaluates as an empty object +When the content of an `ARTIFACT` material is not JSON, for example a ZIP file, the policies get an empty object plus `chainloop_metadata`. The material does not fail. A JSON `ARTIFACT` keeps its content. A typed material with content that does not match its format still fails with `failed to parse input`. A policy that reads the content of such an `ARTIFACT` sees an empty object. A policy with a `valid_input` rule reports it as skipped. A policy without one can pass with no message. This also applies to a damaged JSON file. +- Why: a policy with no kind applies to `ARTIFACT`, and some of these policies read only `chainloop_metadata` (for example `upstream-check`). The CLI cannot know which policies read the content. Before this change, a policy that read the content of a non-JSON `ARTIFACT` could not run, because `att add` failed. +- Rejected: a failure for each non-JSON `ARTIFACT` (a policy that reads only metadata then stops `att add`, also with `gate: false`). A list of material kinds in `upstream-check` (then no `ARTIFACT` can use it). +- Deciders: migmartri +- Source: recorded directly, 2026-11-23, [#3843](https://github.com/chainloop-dev/chainloop/pull/3843) +- Code: verified: `GetEvaluableContent` in `pkg/attestation/crafter/api/attestation/v1/crafting_state.go`. The CLI decodes the first JSON value, as before, so a file of JSON lines keeps its first value. Only a failed decode gives the empty object. + Not shown: 6 smaller decisions of [issue-3063-attestation-repo-config](../specs/issue-3063-attestation-repo-config.md). ## Non-Confirmed diff --git a/pkg/attestation/crafter/api/attestation/v1/crafting_state.go b/pkg/attestation/crafter/api/attestation/v1/crafting_state.go index 085fdfcec..7adf1db84 100644 --- a/pkg/attestation/crafter/api/attestation/v1/crafting_state.go +++ b/pkg/attestation/crafter/api/attestation/v1/crafting_state.go @@ -224,7 +224,21 @@ func (m *Attestation_Material) GetEvaluableContent(value string, content []byte) var decodedMaterial any if err = decoder.Decode(&decodedMaterial); err != nil { - return nil, fmt.Errorf("failed to parse input: %w", err) + // A generic ARTIFACT holds arbitrary bytes (a ZIP, a binary, plain text), + // and a policy with no kind applies to it. Some of those policies read + // only chainloop_metadata (for example upstream-check), and the CLI + // cannot tell which ones read the content. So content that is not JSON + // evaluates as an empty object instead of failing the material. + // + // Trade-off (ATT-005): a policy that does read the content sees an empty + // object. One with a valid_input rule reports skipped; one without can + // pass with no message. That includes a damaged JSON file, which used to + // fail the material. Typed materials keep failing: their content must + // match their format. + if m.MaterialType != v1.CraftingSchema_Material_ARTIFACT { + return nil, fmt.Errorf("failed to parse input: %w", err) + } + decodedMaterial = map[string]any{} } inputMap := make(map[string]any) diff --git a/pkg/attestation/crafter/api/attestation/v1/crafting_state_test.go b/pkg/attestation/crafter/api/attestation/v1/crafting_state_test.go index 3183919d4..4d8111dc5 100644 --- a/pkg/attestation/crafter/api/attestation/v1/crafting_state_test.go +++ b/pkg/attestation/crafter/api/attestation/v1/crafting_state_test.go @@ -687,6 +687,115 @@ func TestGetEvaluableContentSuppliedForArtifactlessKind(t *testing.T) { "supplied content must be evaluated whatever the material kind") } +// TestGetEvaluableContentNonJSONArtifact pins that a generic ARTIFACT holds +// arbitrary bytes (a ZIP, a binary, plain text). Its content cannot fail policy +// evaluation: a non-JSON ARTIFACT is evaluated as an empty object plus +// chainloop_metadata, so metadata-only policies such as upstream-check still run. +// A JSON ARTIFACT keeps passing through, and a typed material with invalid +// content still fails. +func TestGetEvaluableContentNonJSONArtifact(t *testing.T) { + zipPath := filepath.Join(t.TempDir(), "dist.zip") + var zipBuf bytes.Buffer + zw := zip.NewWriter(&zipBuf) + f, err := zw.Create("app.jar") + require.NoError(t, err) + _, err = f.Write([]byte("binary")) + require.NoError(t, err) + require.NoError(t, zw.Close()) + require.NoError(t, os.WriteFile(zipPath, zipBuf.Bytes(), 0o600)) + + material := func(kind schemaapi.CraftingSchema_Material_MaterialType, inline []byte) *Attestation_Material { + return &Attestation_Material{ + MaterialType: kind, + M: &Attestation_Material_Artifact_{ + Artifact: &Attestation_Material_Artifact{ + Name: "dist.zip", Digest: "sha256:deadbeef", Content: inline, + }, + }, + InlineCas: inline != nil, + } + } + + cases := []struct { + name string + material *Attestation_Material + path string + // wantContentKeys are the top-level keys of the evaluable content + // besides chainloop_metadata, which every case must have. + wantContentKeys []string + wantErr string + }{ + { + name: "binary artifact read from disk", + material: material(schemaapi.CraftingSchema_Material_ARTIFACT, nil), + path: zipPath, + wantContentKeys: nil, + }, + { + name: "binary artifact stored inline", + material: material(schemaapi.CraftingSchema_Material_ARTIFACT, zipBuf.Bytes()), + wantContentKeys: nil, + }, + { + name: "plain text artifact", + material: material(schemaapi.CraftingSchema_Material_ARTIFACT, []byte("Plain text, not JSON")), + wantContentKeys: nil, + }, + { + // Unchanged: the first JSON value is evaluated, as before, so a + // content-reading policy does not lose input it reads today. + name: "artifact with JSON lines keeps its first value", + material: material(schemaapi.CraftingSchema_Material_ARTIFACT, []byte("{\"a\":1}\n{\"b\":2}\n")), + wantContentKeys: []string{"a"}, + }, + { + name: "json object artifact passes through", + material: material(schemaapi.CraftingSchema_Material_ARTIFACT, []byte(`{"kept":"yes"}`)), + wantContentKeys: []string{"kept"}, + }, + { + name: "json array artifact passes through as elements", + material: material(schemaapi.CraftingSchema_Material_ARTIFACT, []byte(`[1,2]`)), + wantContentKeys: []string{"elements"}, + }, + { + name: "typed material with non-json content still fails", + material: material(schemaapi.CraftingSchema_Material_SARIF, zipBuf.Bytes()), + wantErr: "failed to parse input", + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + content, err := tc.material.GetEvaluableContent(tc.path, nil) + if tc.wantErr != "" { + require.ErrorContains(t, err, tc.wantErr) + return + } + require.NoError(t, err) + + var decoded map[string]json.RawMessage + require.NoError(t, json.Unmarshal(content, &decoded)) + + rawMetadata, ok := decoded["chainloop_metadata"] + require.True(t, ok, "chainloop_metadata must always be present") + delete(decoded, "chainloop_metadata") + + keys := make([]string, 0, len(decoded)) + for k := range decoded { + keys = append(keys, k) + } + assert.ElementsMatch(t, tc.wantContentKeys, keys) + + var metadata struct { + Name string `json:"name"` + } + require.NoError(t, json.Unmarshal(rawMetadata, &metadata)) + assert.Equal(t, "dist.zip", metadata.Name) + }) + } +} + // TestGetEvaluableContentInjectsMetadata pins that supplying the content does // not bypass the projection the policy engine relies on: the chainloop_metadata // descriptor is still injected, so a policy can read the redaction annotations diff --git a/pkg/policies/policies_test.go b/pkg/policies/policies_test.go index 098481a80..58f81d6fa 100644 --- a/pkg/policies/policies_test.go +++ b/pkg/policies/policies_test.go @@ -666,6 +666,41 @@ func (s *testSuite) TestValidInlineMaterial() { s.Len(res[0].Violations, 0) } +// TestMetadataOnlyPolicyOnBinaryArtifact covers a policy with no kind, which +// reads only chainloop_metadata (like upstream-check), on an ARTIFACT that holds +// binary content. The material must evaluate, not fail with a parse error. +func (s *testSuite) TestMetadataOnlyPolicyOnBinaryArtifact() { + zipPath := filepath.Join(s.T().TempDir(), "dist.zip") + // A ZIP starts with "PK", which is not JSON. + s.Require().NoError(os.WriteFile(zipPath, []byte("PK\x03\x04binary"), 0o600)) + + policies := &v12.Policies{ + Materials: []*v12.PolicyAttachment{ + { + Policy: &v12.PolicyAttachment_Ref{Ref: "file://testdata/metadata_only.yaml"}, + // A policy with no kind applies only through a name selector. + Selector: &v12.PolicyAttachment_MaterialSelector{Name: "artifact"}, + }, + }, + } + material := &v1.Attestation_Material{ + Id: "artifact", + M: &v1.Attestation_Material_Artifact_{Artifact: &v1.Attestation_Material_Artifact{ + Name: "dist.zip", Digest: "sha256:deadbeef", + }}, + MaterialType: v12.CraftingSchema_Material_ARTIFACT, + } + + verifier := NewPolicyVerifier(policies, nil, &s.logger) + + res, err := verifier.VerifyMaterial(context.TODO(), material, zipPath, nil) + s.Require().NoError(err) + s.Require().Len(res, 1) + s.Equal("metadata-only", res[0].Name) + s.False(res[0].Skipped) + s.Empty(res[0].Violations) +} + func (s *testSuite) TestInvalidInlineMaterial() { schema := &v12.CraftingSchema{ Materials: []*v12.CraftingSchema_Material{ diff --git a/pkg/policies/testdata/metadata_only.yaml b/pkg/policies/testdata/metadata_only.yaml new file mode 100644 index 000000000..1cc93a293 --- /dev/null +++ b/pkg/policies/testdata/metadata_only.yaml @@ -0,0 +1,22 @@ +apiVersion: chainloop.dev/v1 +kind: Policy +metadata: + name: metadata-only + description: Policy with no kind that reads only chainloop_metadata, like upstream-check +spec: + policies: + - embedded: | + package main + + import rego.v1 + + result := { + "skipped": false, + "violations": violations, + "skip_reason": "", + } + + violations contains msg if { + input.chainloop_metadata.digest.sha256 != "deadbeef" + msg := "unexpected digest" + }