Skip to content

Commit f0640b1

Browse files
committed
update
Signed-off-by: laurentsimon <[email protected]>
1 parent 5e75163 commit f0640b1

File tree

2 files changed

+57
-60
lines changed

2 files changed

+57
-60
lines changed

verifiers/internal/gha/builder.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ func VerifyWorkflowIdentity(id *WorkflowIdentity,
6464
}
6565

6666
// Issuer verification.
67-
if !strings.EqualFold(id.Issuer, certOidcIssuer) {
67+
if id.Issuer != certOidcIssuer {
6868
return nil, fmt.Errorf("%w: %s", serrors.ErrorInvalidOIDCIssuer, id.Issuer)
6969
}
7070

7171
// The caller repository in the x509 extension is not fully qualified. It only contains
7272
// {org}/{repository}.
7373
expectedSource := strings.TrimPrefix(source, "git+https://")
7474
expectedSource = strings.TrimPrefix(expectedSource, "github.com/")
75-
if !strings.EqualFold(id.CallerRepository, expectedSource) {
75+
if id.CallerRepository != expectedSource {
7676
return nil, fmt.Errorf("%w: expected source '%s', got '%s'", serrors.ErrorMismatchSource,
7777
expectedSource, id.CallerRepository)
7878
}

verifiers/internal/gha/npm.go

+55-58
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ const (
2525

2626
var errrorInvalidAttestations = errors.New("invalid npm attestations")
2727

28+
/*
29+
NOTE: key available at https://registry.npmjs.org/-/npm/v1/keys
30+
31+
https://docs.npmjs.com/about-registry-signatures
32+
{
33+
"keys": [
34+
{
35+
"expires": null,
36+
"keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA",
37+
"keytype": "ecdsa-sha2-nistp256",
38+
"scheme": "ecdsa-sha2-nistp256",
39+
"key": "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE1Olb3zMAFFxXKHiIkQO5cJ3Yhl5i6UPp+IhuteBJbuHcA5UogKo0EWtlWwW6KSaKoTNEYL7JlCQiVnkhBktUgg=="
40+
}
41+
]
42+
}
43+
*/
44+
var npmRegistryPublicKey = "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE1Olb3zMAFFxXKHiIkQO5cJ3Yhl5i6UPp+IhuteBJbuHcA5UogKo0EWtlWwW6KSaKoTNEYL7JlCQiVnkhBktUgg=="
45+
2846
type attestation struct {
2947
PredicateType string `json:"predicateType"`
3048
BundleBytes BundleBytes `json:"bundle"`
@@ -63,28 +81,41 @@ func NpmNew(ctx context.Context, root *TrustedRoot, attestationBytes []byte) (*N
6381
return nil, fmt.Errorf("%w: json.Unmarshal: %v", errrorInvalidAttestations, err)
6482
}
6583

66-
if len(attestations) != 2 {
67-
return nil, fmt.Errorf("%w: invalid number of attestations: %v", errrorInvalidAttestations, len(attestations))
84+
prov, pub, err := extractAttestations(attestations)
85+
if err != nil {
86+
return nil, err
6887
}
88+
return &Npm{
89+
ctx: ctx,
90+
root: root,
91+
provenanceAttestation: prov,
92+
publishAttestation: pub,
93+
}, nil
94+
}
6995

70-
// Attestation type verification.
71-
if attestations[0].PredicateType != slsaprovenance.ProvenanceV02Type {
72-
return nil, fmt.Errorf("%w: invalid predicate type: %v. Expected %v", errrorInvalidAttestations,
73-
attestations[0].PredicateType, slsaprovenance.ProvenanceV02Type)
96+
func extractAttestations(attestations []attestation) (*attestation, *attestation, error) {
97+
if len(attestations) < 2 {
98+
return nil, nil, fmt.Errorf("%w: invalid number of attestations: %v", errrorInvalidAttestations, len(attestations))
7499
}
75100

76-
// Provenance type verification.
77-
if !strings.HasPrefix(attestations[1].PredicateType, publishAttestationV01) {
78-
return nil, fmt.Errorf("%w: invalid predicate type: %v. Expected %v", errrorInvalidAttestations,
79-
attestations[1].PredicateType, publishAttestationV01)
101+
var provenanceAttestation *attestation
102+
var publishAttestation *attestation
103+
for i := range attestations {
104+
att := attestations[i]
105+
// Provenance type verification.
106+
if att.PredicateType == slsaprovenance.ProvenanceV02Type {
107+
provenanceAttestation = &att
108+
}
109+
// Publish type verification.
110+
if strings.HasPrefix(att.PredicateType, publishAttestationV01) {
111+
publishAttestation = &att
112+
}
80113
}
81114

82-
return &Npm{
83-
ctx: ctx,
84-
root: root,
85-
provenanceAttestation: &attestations[0],
86-
publishAttestation: &attestations[1],
87-
}, nil
115+
if provenanceAttestation == nil || publishAttestation == nil {
116+
return nil, nil, fmt.Errorf("%w: invalid attestation types", errrorInvalidAttestations)
117+
}
118+
return provenanceAttestation, publishAttestation, nil
88119
}
89120

90121
func (n *Npm) verifyProvenanceAttestationSignature() error {
@@ -122,24 +153,8 @@ func (n *Npm) verifyPublishAttesttationSignature() error {
122153
// TODO(#496): verify the keyid, both in DSSE and hint.
123154

124155
// Verify the signature.
125-
/*
126-
NOTE: key available at https://registry.npmjs.org/-/npm/v1/keys
127-
https://docs.npmjs.com/about-registry-signatures
128-
{
129-
"keys": [
130-
{
131-
"expires": null,
132-
"keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA",
133-
"keytype": "ecdsa-sha2-nistp256",
134-
"scheme": "ecdsa-sha2-nistp256",
135-
"key": "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE1Olb3zMAFFxXKHiIkQO5cJ3Yhl5i6UPp+IhuteBJbuHcA5UogKo0EWtlWwW6KSaKoTNEYL7JlCQiVnkhBktUgg=="
136-
}
137-
]
138-
}
139-
*/
140156
payloadHash := sha256.Sum256(payload)
141-
b64key := "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE1Olb3zMAFFxXKHiIkQO5cJ3Yhl5i6UPp+IhuteBJbuHcA5UogKo0EWtlWwW6KSaKoTNEYL7JlCQiVnkhBktUgg=="
142-
rawKey, err := base64.StdEncoding.DecodeString(b64key)
157+
rawKey, err := base64.StdEncoding.DecodeString(npmRegistryPublicKey)
143158
if err != nil {
144159
return fmt.Errorf("DecodeString: %w", err)
145160
}
@@ -320,7 +335,7 @@ func verifyProvenanceSubjectVersion(att *SignedAttestation, expectedVersion stri
320335
return err
321336
}
322337

323-
subVersion, err := getPackageVersion(subject)
338+
_, subVersion, err := getPackageNameAndVersion(subject)
324339
if err != nil {
325340
return err
326341
}
@@ -386,7 +401,7 @@ func verifyProvenanceSubjectName(att *SignedAttestation, expectedName string) er
386401
}
387402

388403
func verifyName(actual, expected string) error {
389-
subName, err := getPackageName(actual)
404+
subName, _, err := getPackageNameAndVersion(actual)
390405
if err != nil {
391406
return err
392407
}
@@ -399,40 +414,26 @@ func verifyName(actual, expected string) error {
399414
return nil
400415
}
401416

402-
func getPackageName(name string) (string, error) {
417+
func getPackageNameAndVersion(name string) (string, string, error) {
418+
var pkgname, pkgtag string
403419
n := name
404420
if strings.HasPrefix(name, "@") {
405421
n = n[1:]
406422
}
407423
parts := strings.Split(n, "@")
408424
if len(parts) > 2 {
409-
return "", fmt.Errorf("%w: %v", serrors.ErrorInvalidPackageName, name)
425+
return "", "", fmt.Errorf("%w: %v", serrors.ErrorInvalidPackageName, name)
410426
}
411427

412-
pkgname := parts[0]
428+
pkgname = parts[0]
413429
if strings.HasPrefix(name, "@") {
414430
pkgname = "@" + pkgname
415431
}
416-
417-
return pkgname, nil
418-
}
419-
420-
func getPackageVersion(name string) (string, error) {
421-
n := name
422-
if strings.HasPrefix(name, "@") {
423-
n = n[1:]
424-
}
425-
parts := strings.Split(n, "@")
426-
if len(parts) > 2 {
427-
return "", fmt.Errorf("%w: %v", serrors.ErrorInvalidPackageName, name)
428-
}
429-
430-
var pkgtag string
431432
if len(parts) == 2 {
432433
pkgtag = parts[1]
433434
}
434435

435-
return pkgtag, nil
436+
return pkgname, pkgtag, nil
436437
}
437438

438439
func getSubject(att *SignedAttestation) (string, error) {
@@ -462,7 +463,3 @@ func getSubject(att *SignedAttestation) (string, error) {
462463
}
463464
return subject, err
464465
}
465-
466-
// verifyEnvAndCert
467-
468-
// TODO: intotoheader

0 commit comments

Comments
 (0)