Skip to content

Commit ffb6270

Browse files
leodidoona-agent
andcommitted
fix(signing): validate whitespace-only sub claims
Add strings.TrimSpace() check to reject whitespace-only sub claims, preventing confusing error messages later in the extraction process. - Trim whitespace before checking if sub claim is empty - Add test case for whitespace-only sub claim validation Co-authored-by: Ona <[email protected]>
1 parent a1d91f4 commit ffb6270

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

pkg/leeway/signing/attestation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ func extractBuilderIDFromOIDC(ctx context.Context, githubCtx *GitHubContext) (st
418418

419419
// Extract the sub claim (required for Fulcio certificate identity)
420420
sub, ok := claims["sub"].(string)
421-
if !ok || sub == "" {
421+
if !ok || strings.TrimSpace(sub) == "" {
422422
return "", fmt.Errorf("sub claim not found or empty in OIDC token")
423423
}
424424

pkg/leeway/signing/attestation_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,28 @@ func TestExtractBuilderIDFromOIDC(t *testing.T) {
14171417
expectError: true,
14181418
errorMsg: "sub claim not found",
14191419
},
1420+
{
1421+
name: "whitespace-only sub claim",
1422+
setupServer: func() *httptest.Server {
1423+
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1424+
header := base64EncodeForTest(`{"alg":"RS256","typ":"JWT"}`)
1425+
payload := base64EncodeForTest(`{"sub": " ", "aud": "sigstore"}`)
1426+
signature := base64EncodeForTest("fake-signature")
1427+
token := fmt.Sprintf("%s.%s.%s", header, payload, signature)
1428+
1429+
w.Header().Set("Content-Type", "application/json")
1430+
if err := json.NewEncoder(w).Encode(map[string]string{"value": token}); err != nil {
1431+
t.Errorf("Failed to encode response: %v", err)
1432+
}
1433+
}))
1434+
},
1435+
githubCtx: &GitHubContext{
1436+
ServerURL: "https://github.com",
1437+
Repository: "org/repo",
1438+
},
1439+
expectError: true,
1440+
errorMsg: "sub claim not found or empty",
1441+
},
14201442
{
14211443
name: "job_workflow_ref in top-level claim (not in sub)",
14221444
setupServer: func() *httptest.Server {

0 commit comments

Comments
 (0)