Skip to content

Commit 9a3b51f

Browse files
authored
Report level 0 in status when the commit has no VSA (#483)
A SLSA source level is only verified by a VSA. When the latest commit of the branch had none, status fell back to level 1. status now reports level 0 for a commit with no VSA and says why. The eligible level counts the provenance and VSA controls as active when the repository has a provenance workflow, so it reflects what the commit is eligible for once attested and does not drop while a commit waits for its attestations. Signed-off-by: Adolfo Garcia Veytia (puerco) <puerco@carabiner.dev>
1 parent 775a7c8 commit 9a3b51f

4 files changed

Lines changed: 155 additions & 11 deletions

File tree

‎internal/cmd/status.go‎

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,6 @@ sourcetool status myorg/myrepo@mybranch
118118
return fmt.Errorf("fetching active controls: %w", err)
119119
}
120120

121-
// The eligible level is the highest the active controls could
122-
// support, ignoring the repository policy.
123-
toplevel := policy.ComputeEligibleSlsaLevel(controls.GetActiveControls())
124-
125121
// The verified level is what the repository policy actually grants.
126122
// It can be lower than the eligible level when the policy targets a
127123
// lower level or when the policy is not fully met.
@@ -130,7 +126,11 @@ sourcetool status myorg/myrepo@mybranch
130126
if err != nil {
131127
return fmt.Errorf("evaluating controls against policy: %w", err)
132128
}
133-
verifiedLevel := firstSourceLevel(evalResult.VerifiedLevels)
129+
130+
// A level is only verified by a VSA. When the latest commit of the
131+
// branch has none there is no verified level to report.
132+
tipHasVSA := isControlActive(controls, slsa.SLSA_SOURCE_SCS_VSA)
133+
verifiedLevel := verifiedSourceLevel(evalResult.VerifiedLevels, tipHasVSA)
134134

135135
// --level prints just the policy-verified level and exits, for scripting.
136136
if opts.level {
@@ -144,6 +144,10 @@ sourcetool status myorg/myrepo@mybranch
144144
return fmt.Errorf("checking provenance workflows: %w", err)
145145
}
146146

147+
// The eligible level is the highest the active controls could
148+
// support, ignoring the repository policy.
149+
toplevel := eligibleLevel(controls, len(workflows) > 0)
150+
147151
title := fmt.Sprintf(
148152
"\nSLSA Source Status for %s/%s@%s", opts.owner, opts.repository,
149153
ghcontrol.BranchToFullRef(opts.branch),
@@ -203,7 +207,11 @@ sourcetool status myorg/myrepo@mybranch
203207
}
204208

205209
fmt.Println(w("Current SLSA Source level: " + verifiedLevel))
206-
printLevelGap(toplevel, verifiedLevel, evalResult.Shortfall)
210+
if tipHasVSA {
211+
printLevelGap(toplevel, verifiedLevel, evalResult.Shortfall)
212+
} else {
213+
printMissingVSA(toplevel, len(workflows) > 0)
214+
}
207215
printLegacyWorkflows(workflows)
208216
printFailingWorkflows(workflows)
209217
fmt.Println("")
@@ -256,6 +264,57 @@ func firstSourceLevel(levels slsa.SourceVerifiedLevels) string {
256264
return string(slsa.SlsaSourceLevel0)
257265
}
258266

267+
// verifiedSourceLevel returns the SLSA source level verified for the latest
268+
// commit of the branch. Without a VSA nothing is verified, so the level is 0
269+
// no matter what the controls and the policy would grant.
270+
func verifiedSourceLevel(levels slsa.SourceVerifiedLevels, tipHasVSA bool) string {
271+
if !tipHasVSA {
272+
return string(slsa.SlsaSourceLevel0)
273+
}
274+
return firstSourceLevel(levels)
275+
}
276+
277+
// isControlActive returns true when the control is active in the set.
278+
func isControlActive(controls *slsa.ControlSet, name slsa.ControlName) bool {
279+
c := controls.GetControl(name)
280+
return c != nil && c.State == slsa.StateActive
281+
}
282+
283+
// eligibleLevel returns the highest level the active controls could support,
284+
// ignoring the repository policy.
285+
//
286+
// The provenance and VSA controls are only active once the latest commit of
287+
// the branch is attested. When the repository is attestable (it has a
288+
// provenance workflow) they count as active, so the level is the one the
289+
// commit is eligible for when attested and does not drop while a commit waits
290+
// for its attestations.
291+
func eligibleLevel(controls *slsa.ControlSet, attestable bool) slsa.SlsaSourceLevel {
292+
active := controls.GetActiveControls()
293+
if attestable {
294+
for _, name := range []slsa.ControlName{slsa.SLSA_SOURCE_SCS_PROVENANCE, slsa.SLSA_SOURCE_SCS_VSA} {
295+
if active.GetControl(name) == nil {
296+
active.AddControl(&slsa.Control{Name: name, State: slsa.StateActive})
297+
}
298+
}
299+
}
300+
return policy.ComputeEligibleSlsaLevel(active)
301+
}
302+
303+
// printMissingVSA explains that no level is verified because the latest
304+
// commit of the branch has no VSA.
305+
func printMissingVSA(eligible slsa.SlsaSourceLevel, attestable bool) {
306+
if !attestable {
307+
fmt.Printf(
308+
"%s The latest commit of the branch has no VSA, so no level is verified for it.\n", w2("⚠️ "),
309+
)
310+
return
311+
}
312+
fmt.Printf(
313+
"%s The latest commit of the branch has no VSA yet, so no level is verified for it. The controls are eligible for %s once it is attested.\n",
314+
w2("⚠️ "), eligible,
315+
)
316+
}
317+
259318
// printRecommendedActions prints the list of recommended actions, if any
260319
func printRecommendedActions(actions []*slsa.ControlRecommendedAction) {
261320
if len(actions) == 0 {

‎internal/cmd/status_test.go‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// SPDX-FileCopyrightText: Copyright 2026 The SLSA Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package cmd
5+
6+
import (
7+
"testing"
8+
9+
"github.com/slsa-framework/source-tool/pkg/slsa"
10+
)
11+
12+
// level3Controls returns the level 3 controls as a backend reports them for
13+
// a commit, with the attestation controls in the given state.
14+
func level3Controls(attestations slsa.ControlState) *slsa.ControlSet {
15+
cs := &slsa.ControlSet{}
16+
for _, name := range slsa.Level3 {
17+
state := slsa.StateActive
18+
if name == slsa.SLSA_SOURCE_SCS_PROVENANCE || name == slsa.SLSA_SOURCE_SCS_VSA {
19+
state = attestations
20+
}
21+
cs.AddControl(&slsa.Control{Name: name, State: state})
22+
}
23+
return cs
24+
}
25+
26+
func TestVerifiedSourceLevel(t *testing.T) {
27+
t.Parallel()
28+
levels := slsa.SourceVerifiedLevels{
29+
slsa.ControlName(slsa.SlsaSourceLevel3), slsa.SLSA_SOURCE_SCS_PROTECTED_REFS,
30+
}
31+
for _, tt := range []struct {
32+
name string
33+
levels slsa.SourceVerifiedLevels
34+
tipHasVSA bool
35+
want slsa.SlsaSourceLevel
36+
}{
37+
{name: "attested commit", levels: levels, tipHasVSA: true, want: slsa.SlsaSourceLevel3},
38+
{name: "no vsa means level 0", levels: levels, tipHasVSA: false, want: slsa.SlsaSourceLevel0},
39+
{name: "no levels", levels: nil, tipHasVSA: true, want: slsa.SlsaSourceLevel0},
40+
} {
41+
t.Run(tt.name, func(t *testing.T) {
42+
t.Parallel()
43+
if got := verifiedSourceLevel(tt.levels, tt.tipHasVSA); got != string(tt.want) {
44+
t.Errorf("verifiedSourceLevel() = %s, want %s", got, tt.want)
45+
}
46+
})
47+
}
48+
}
49+
50+
func TestEligibleLevel(t *testing.T) {
51+
t.Parallel()
52+
for _, tt := range []struct {
53+
name string
54+
controls *slsa.ControlSet
55+
attestable bool
56+
want slsa.SlsaSourceLevel
57+
}{
58+
{
59+
name: "attested commit",
60+
controls: level3Controls(slsa.StateActive), attestable: true,
61+
want: slsa.SlsaSourceLevel3,
62+
},
63+
{
64+
name: "commit waiting for its attestations",
65+
controls: level3Controls(slsa.StateNotEnabled), attestable: true,
66+
want: slsa.SlsaSourceLevel3,
67+
},
68+
{
69+
name: "no provenance workflow",
70+
controls: level3Controls(slsa.StateNotEnabled), attestable: false,
71+
want: slsa.SlsaSourceLevel1,
72+
},
73+
} {
74+
t.Run(tt.name, func(t *testing.T) {
75+
t.Parallel()
76+
if got := eligibleLevel(tt.controls, tt.attestable); got != tt.want {
77+
t.Errorf("eligibleLevel() = %s, want %s", got, tt.want)
78+
}
79+
// The controls passed in are not modified
80+
if tt.controls.GetControl(slsa.SLSA_SOURCE_SCS_VSA).State == slsa.StateActive &&
81+
tt.name != "attested commit" {
82+
t.Errorf("eligibleLevel() activated the VSA control of its input")
83+
}
84+
})
85+
}
86+
}

‎pkg/ghcontrol/checklevel.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package ghcontrol
66
import (
77
"context"
88
"fmt"
9-
"log"
9+
"log/slog"
1010
"net/http"
1111
"slices"
1212
"time"
@@ -111,7 +111,7 @@ func (ghc *GitHubConnection) computeRepoContinuityControl(ctx context.Context, r
111111
}
112112

113113
if oldestDeletion == nil || oldestNoFf == nil {
114-
log.Printf("oldestDeletion (%v) or oldestNoFf (%v) is nil, cannot be L2+", oldestDeletion, oldestNoFf)
114+
slog.Debug(fmt.Sprintf("oldestDeletion (%v) or oldestNoFf (%v) is nil, cannot be L2+", oldestDeletion, oldestNoFf))
115115
return nil, nil
116116
}
117117

‎pkg/sourcetool/backends/vcs/github/github.go‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"context"
88
"errors"
99
"fmt"
10-
"log"
1110
"sync"
1211
"time"
1312

@@ -234,7 +233,7 @@ func (b *Backend) GetBranchControlsAtCommit(ctx context.Context, branch *models.
234233
})
235234
}
236235
} else {
237-
log.Printf("No provenance attestation found on %s", commit.SHA)
236+
attest.Debugf("No provenance attestation found on %s", commit.SHA)
238237
}
239238

240239
_, vsaPred, err := attester.GetRevisionVSA(ctx, branch, commit)
@@ -249,7 +248,7 @@ func (b *Backend) GetBranchControlsAtCommit(ctx context.Context, branch *models.
249248
})
250249
}
251250
} else {
252-
log.Printf("No VSA found for commit")
251+
attest.Debugf("No VSA found for commit %s", commit.SHA)
253252
}
254253

255254
// NewControlSet returns all the controls for the framework in

0 commit comments

Comments
 (0)