122 lines
4.3 KiB
Go
122 lines
4.3 KiB
Go
package attestation
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// MUTATION-CHECK (staging, both directions; each must FAIL, not error):
|
|
// - ParsePlatformSignature skips the image_digest comparison →
|
|
// TestAnAnswerAboutAnotherDigestIsNotAttached FAILS;
|
|
// - it skips the payload digest comparison →
|
|
// TestAPayloadAboutAnotherImageIsNotAttached FAILS;
|
|
// - it treats any 2xx (or 0) as signed → TestARefusalNamesTheSlugAndSignsNothing FAILS;
|
|
// - tighten the repository grammar to `[a-z0-9./-]` (drop `:`) →
|
|
// TestLegitimateRequestsAreAccepted FAILS on the `host:5000/...` case;
|
|
// - loosen PlatformSignRequest to accept any repository →
|
|
// TestUnsafeRequestValuesAreRefused FAILS.
|
|
|
|
const goodDigest = "sha256:" + "ab12ab12ab12ab12ab12ab12ab12ab12ab12ab12ab12ab12ab12ab12ab12ab12"
|
|
const otherDigest = "sha256:" + "cd34cd34cd34cd34cd34cd34cd34cd34cd34cd34cd34cd34cd34cd34cd34cd34"
|
|
const svc = "66666666-7777-4888-9999-000000000000"
|
|
|
|
func payloadFor(digest string) string {
|
|
doc := map[string]any{
|
|
"critical": map[string]any{
|
|
"identity": map[string]string{"docker-reference": "registry.acme.test/shop/api"},
|
|
"image": map[string]string{"docker-manifest-digest": digest},
|
|
"type": "cosign container image signature",
|
|
},
|
|
"optional": nil,
|
|
}
|
|
raw, _ := json.Marshal(doc)
|
|
return base64.StdEncoding.EncodeToString(raw)
|
|
}
|
|
|
|
func answer(t *testing.T, imageDigest, payloadDigest string) []byte {
|
|
t.Helper()
|
|
raw, err := json.Marshal(map[string]any{
|
|
"image_digest": imageDigest,
|
|
"payload": payloadFor(payloadDigest),
|
|
"signature": base64.StdEncoding.EncodeToString([]byte("der-signature")),
|
|
"key_fingerprint": "fp",
|
|
"key_version": 1,
|
|
"public_key_pem": "-----BEGIN PUBLIC KEY-----",
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return raw
|
|
}
|
|
|
|
func TestLegitimateRequestsAreAccepted(t *testing.T) {
|
|
for _, repo := range []string{
|
|
"registry.acme.test/shop/api",
|
|
"registry.acme.test:5000/repository/docker/shop-api",
|
|
"10.0.0.5:8082/team/svc_name",
|
|
} {
|
|
body, err := PlatformSignRequest(svc, "3f1c30981b56", repo, goodDigest)
|
|
if err != nil {
|
|
t.Fatalf("%s: %v", repo, err)
|
|
}
|
|
var got map[string]string
|
|
if err := json.Unmarshal(body, &got); err != nil || got["repository"] != repo || got["service_id"] != svc {
|
|
t.Fatalf("%s: body %s", repo, body)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestUnsafeRequestValuesAreRefused(t *testing.T) {
|
|
cases := []struct{ svc, version, repo, digest string }{
|
|
{svc, "v1", "registry.acme.test/shop/api", "sha256:short"},
|
|
{svc, "v1", "registry.acme.test/shop/api@sha256:x", goodDigest},
|
|
{svc, "v1", "registry.acme.test/shop/api;id", goodDigest},
|
|
{svc, "v1", "Registry.acme.test/Shop", goodDigest},
|
|
{svc, "v1;rm", "registry.acme.test/shop/api", goodDigest},
|
|
{"not-a-uuid", "v1", "registry.acme.test/shop/api", goodDigest},
|
|
}
|
|
for _, c := range cases {
|
|
if _, err := PlatformSignRequest(c.svc, c.version, c.repo, c.digest); err == nil {
|
|
t.Fatalf("accepted %+v", c)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAMatchingAnswerIsAttached(t *testing.T) {
|
|
sig, err := ParsePlatformSignature(200, answer(t, goodDigest, goodDigest), goodDigest)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(string(sig.Payload), goodDigest) || sig.SignatureB64 == "" {
|
|
t.Fatalf("%+v", sig)
|
|
}
|
|
}
|
|
|
|
func TestAnAnswerAboutAnotherDigestIsNotAttached(t *testing.T) {
|
|
if _, err := ParsePlatformSignature(200, answer(t, otherDigest, otherDigest), goodDigest); err == nil {
|
|
t.Fatal("an answer about another digest was accepted")
|
|
}
|
|
}
|
|
|
|
func TestAPayloadAboutAnotherImageIsNotAttached(t *testing.T) {
|
|
if _, err := ParsePlatformSignature(200, answer(t, goodDigest, otherDigest), goodDigest); err == nil {
|
|
t.Fatal("a payload about another image was accepted")
|
|
}
|
|
}
|
|
|
|
func TestARefusalNamesTheSlugAndSignsNothing(t *testing.T) {
|
|
body := []byte(`{"type":"https://integraci.co/problems/signing-verdict-block","title":"Signing Refused","status":409,"detail":"cve blocks"}`)
|
|
_, err := ParsePlatformSignature(409, body, goodDigest)
|
|
if err == nil || !strings.Contains(err.Error(), "signing-verdict-block") || !strings.Contains(err.Error(), "409") {
|
|
t.Fatalf("got %v", err)
|
|
}
|
|
if _, err := ParsePlatformSignature(0, nil, goodDigest); err == nil {
|
|
t.Fatal("no answer was taken as signed")
|
|
}
|
|
if _, err := ParsePlatformSignature(202, answer(t, goodDigest, goodDigest), goodDigest); err == nil {
|
|
t.Fatal("a non-200 answer was taken as signed")
|
|
}
|
|
}
|