Files

180 lines
5.4 KiB
Go

package attestation
import (
"encoding/base64"
"encoding/json"
"strings"
"testing"
)
func TestRegistryHost(t *testing.T) {
cases := map[string]string{
"registry.example/team/orders@sha256:" + digestA: "registry.example",
"nexus.example:8082/orders:1": "nexus.example:8082",
"localhost/orders:1": "localhost",
"library/alpine:3.20": "docker.io",
"alpine": "docker.io",
}
for in, want := range cases {
if got := RegistryHost(in); got != want {
t.Errorf("RegistryHost(%q) = %q, want %q", in, got, want)
}
}
}
func TestRegistryAuthConfigLegitimate(t *testing.T) {
raw, err := RegistryAuthConfig("nexus.example:8082", "ci-bot", "p@ss w'rd\"$x")
if err != nil {
t.Fatal(err)
}
var cfg struct {
Auths map[string]struct {
Auth string `json:"auth"`
} `json:"auths"`
}
if err := json.Unmarshal(raw, &cfg); err != nil {
t.Fatal(err)
}
dec, _ := base64.StdEncoding.DecodeString(cfg.Auths["nexus.example:8082"].Auth)
// Shell metacharacters in the password are DATA here, never syntax.
if string(dec) != "ci-bot:p@ss w'rd\"$x" {
t.Fatalf("auth = %q", dec)
}
}
func TestRegistryAuthConfigDockerHubKey(t *testing.T) {
raw, err := RegistryAuthConfig("docker.io", "u", "p")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(raw), "https://index.docker.io/v1/") {
t.Fatalf("docker hub must use the index key: %s", raw)
}
}
func TestRegistryAuthConfigRefuses(t *testing.T) {
bad := []struct{ host, user, pass string }{
{"evil.example; rm -rf /", "u", "p"},
{"host/with/path", "u", "p"},
{"", "u", "p"},
{"registry.example", "", "p"},
{"registry.example", "a:b", "p"},
{"registry.example", "a\nb", "p"},
{"registry.example", "u", ""},
}
for _, c := range bad {
if _, err := RegistryAuthConfig(c.host, c.user, c.pass); err == nil {
t.Errorf("RegistryAuthConfig(%q, %q, …) accepted", c.host, c.user)
}
}
}
func gateEnvelopeLine(t *testing.T, payloadType string) string {
t.Helper()
b, err := json.Marshal(map[string]any{
"payloadType": payloadType,
"payload": base64.StdEncoding.EncodeToString([]byte(`{"_type":"x"}`)),
"signatures": []map[string]string{{"sig": "c2ln"}},
})
if err != nil {
t.Fatal(err)
}
return string(b)
}
func TestGateInputKeepsEnvelopesAndDropsNoise(t *testing.T) {
out := strings.Join([]string{
"Verification for registry.example/team/orders --",
gateEnvelopeLine(t, "application/vnd.in-toto+json"),
"{not json",
`{"payloadType":"","payload":"x"}`,
"",
}, "\n")
raw, err := GateInput(imageA, out, []string{" https://ci.example/job/orders/ ", ""}, true)
if err != nil {
t.Fatal(err)
}
var in struct {
Image map[string]string `json:"image"`
Envelopes []map[string]any `json:"envelopes"`
Policy struct {
TrustedBuilders []string `json:"trusted_builders"`
RequireSBOM bool `json:"require_sbom"`
} `json:"policy"`
}
if err := json.Unmarshal(raw, &in); err != nil {
t.Fatal(err)
}
if in.Image["digest"] != "sha256:"+digestA {
t.Fatalf("digest = %q", in.Image["digest"])
}
if len(in.Envelopes) != 1 {
t.Fatalf("want 1 envelope, got %d", len(in.Envelopes))
}
if len(in.Policy.TrustedBuilders) != 1 || in.Policy.TrustedBuilders[0] != "https://ci.example/job/orders/" {
t.Fatalf("builders = %v", in.Policy.TrustedBuilders)
}
if !in.Policy.RequireSBOM {
t.Fatal("require_sbom lost")
}
}
func TestGateInputEmptyEnvelopeListIsAnArray(t *testing.T) {
raw, err := GateInput(imageA, "", []string{"https://ci.example/"}, false)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(raw), `"envelopes":[]`) {
t.Fatalf("envelopes must be [] not null: %s", raw)
}
}
func TestGateInputRefusesTagAndNoBuilder(t *testing.T) {
if _, err := GateInput("registry.example/team/orders:abc", "", []string{"https://ci.example/"}, false); err == nil {
t.Error("tag-only image accepted")
}
if _, err := GateInput(imageA, "", []string{" ", ""}, false); err == nil {
t.Error("empty trusted builder list accepted")
}
}
func TestGateVerdict(t *testing.T) {
allow := `{"result":[{"expressions":[{"value":{"allowed":true,"deny":[]},"text":"x"}]}]}`
ok, deny, err := GateVerdict([]byte(allow))
if err != nil || !ok || len(deny) != 0 {
t.Fatalf("allow: ok=%v deny=%v err=%v", ok, deny, err)
}
refuse := `{"result":[{"expressions":[{"value":{"allowed":false,"deny":["no provenance"]}}]}]}`
ok, deny, err = GateVerdict([]byte(refuse))
if err != nil || ok || len(deny) != 1 {
t.Fatalf("deny: ok=%v deny=%v err=%v", ok, deny, err)
}
}
func TestGateVerdictFailsClosed(t *testing.T) {
for _, raw := range []string{
`{}`, // package not loaded: empty result set
`{"result":[]}`, // same
`{"result":[{"expressions":[{"value":{}}]}]}`, // no allowed field
`{"result":[{"expressions":[{"value":{"allowed":true,"deny":["x"]}}]}]}`,
`not json`,
} {
if ok, _, err := GateVerdict([]byte(raw)); ok || err == nil {
t.Errorf("GateVerdict(%s) = ok=%v err=%v, want refusal", raw, ok, err)
}
}
}
func TestCheckBlobName(t *testing.T) {
for _, good := range []string{"SHA256SUMS", "sbom.cdx.json", "a_b-c.1"} {
if err := CheckBlobName(good); err != nil {
t.Errorf("%q refused: %v", good, err)
}
}
for _, bad := range []string{"", "../x", "a/b", ".hidden", "a..b", "x y", "a;b"} {
if err := CheckBlobName(bad); err == nil {
t.Errorf("%q accepted", bad)
}
}
}