Skip to content

Commit 704f278

Browse files
aledbfclaude
andcommitted
release: the SBOM names Docker's packages by Docker's repository (#621)
Since #609 the image installs Docker's six packages from Docker's apt repository, pinned in versions.yaml, and the SBOM still built their purls from packages.txt as pkg:deb/ubuntu: a scanner looked for them in Ubuntu's advisories and missed Docker's. Each .deb versions.yaml pins is now pkg:deb/<vendor> with its repository_url, the .deb's SHA-256 and its download URL, and the release refuses an image that did not record a pinned package at the pinned version. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f273377 commit 704f278

3 files changed

Lines changed: 110 additions & 5 deletions

File tree

‎cmd/spin-machine-dev/verbs_test.go‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"testing"
1515

1616
"github.com/spf13/cobra"
17+
"github.com/spin-stack/go-tools/versions"
1718

1819
"github.com/spin-stack/machine/internal/build"
1920
"github.com/spin-stack/machine/internal/release"
@@ -510,7 +511,19 @@ func packed(t *testing.T, dir string) string {
510511
"kernel/patches/0001-k.patch", "image/rootfs.qcow2", "licenses.txt"} {
511512
put(t, filepath.Join(out, f), f, 0o644)
512513
}
513-
put(t, filepath.Join(out, "packages.txt"), "bash 5.2-1 amd64\n", 0o644)
514+
// And Docker's packages as dpkg records those versions.yaml pins: release pack refuses an image without them.
515+
list := "bash 5.2-1 amd64\n"
516+
v, err := versions.Load(versions.File) // checkout's copy, in the working directory
517+
if err != nil {
518+
t.Fatal(err)
519+
}
520+
for _, e := range v.Entries {
521+
if file := filepath.Base(e.URL()); strings.HasSuffix(file, ".deb") {
522+
f := strings.Split(strings.TrimSuffix(file, ".deb"), "_")
523+
list += f[0] + " " + f[1] + " " + f[2] + "\n"
524+
}
525+
}
526+
put(t, filepath.Join(out, "packages.txt"), list, 0o644)
514527
apk := func(only string) string {
515528
var b strings.Builder
516529
for _, p := range strings.Split(only, ",") {

‎internal/release/pack.go‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,9 @@ func BOM(o Options, share string) (*cdx.BOM, error) {
320320
if err != nil {
321321
return nil, err
322322
}
323+
if err := fromTheirRepositories(o.Pins, image); err != nil {
324+
return nil, err
325+
}
323326
parts = append(parts, sbom.New(subject("base-image", o.Version), image, nil, o.Epoch))
324327
// QEMU, qboot, the kernel, systemd and OpenSSH are built with this repository's patches: each is in its
325328
// component's pedigree, so the SBOM does not say upstream's source is what was compiled.
@@ -395,6 +398,46 @@ func BOM(o Options, share string) (*cdx.BOM, error) {
395398
return b, nil
396399
}
397400

401+
// debRepositories are the apt repositories versions.yaml pins a .deb from, each with the vendor a
402+
// package URL names for it.
403+
var debRepositories = map[string]string{"https://download.docker.com/linux/ubuntu": "docker"}
404+
405+
// fromTheirRepositories names each package versions.yaml pins as a .deb by the repository it was
406+
// downloaded from rather than as Ubuntu's: Docker's six (#609) as pkg:deb/docker, with that
407+
// repository's URL and the .deb's SHA-256. Named pkg:deb/ubuntu, a scanner looks for them in
408+
// Ubuntu's advisories, where they are not, and misses Docker's (#621). A pinned .deb the image
409+
// did not record, or recorded at another version, is an image that did not install what
410+
// versions.yaml says it did.
411+
func fromTheirRepositories(v *versions.Versions, image []cdx.Component) error {
412+
for _, e := range v.Entries {
413+
file := filepath.Base(e.URL())
414+
if e.Kind != versions.Download || !strings.HasSuffix(file, ".deb") {
415+
continue
416+
}
417+
repository, _, _ := strings.Cut(e.URL(), "/dists/")
418+
vendor, ok := debRepositories[repository]
419+
if !ok {
420+
return fmt.Errorf("%s: %s is from no apt repository the SBOM knows a vendor for", e.Name, e.URL())
421+
}
422+
pkg, _, _ := strings.Cut(file, "_")
423+
i := slices.IndexFunc(image, func(c cdx.Component) bool { return c.Name == pkg })
424+
if i < 0 {
425+
return fmt.Errorf("%s: versions.yaml pins %s, and the image's package list does not have it", e.Name, pkg)
426+
}
427+
c := &image[i]
428+
// dpkg's version is the pinned one between an epoch and Docker's -1~ubuntu suffix.
429+
rest, ok := strings.CutPrefix(c.Version[strings.Index(c.Version, ":")+1:], e.Version)
430+
if !ok || !(rest == "" || rest[0] == '-' || rest[0] == '~') {
431+
return fmt.Errorf("%s: versions.yaml pins %s %s, and the image has %s", e.Name, pkg, e.Version, c.Version)
432+
}
433+
_, arch, _ := strings.Cut(c.PackageURL, "?arch=")
434+
c.PackageURL = "pkg:deb/" + vendor + "/" + pkg + "@" + c.Version + "?arch=" + arch + "&repository_url=" + repository
435+
c.Hashes = &[]cdx.Hash{{Algorithm: cdx.HashAlgoSHA256, Value: e.Pin}}
436+
c.ExternalReferences = &[]cdx.ExternalReference{{Type: cdx.ERTypeDistribution, URL: e.URL()}}
437+
}
438+
return nil
439+
}
440+
398441
// subject is a BOM's subject as go-tools' sbom command names one.
399442
func subject(name, version string) cdx.Component {
400443
return cdx.Component{Type: cdx.ComponentTypeApplication, Name: name, Version: version,

‎internal/release/release_test.go‎

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io"
1111
"os"
1212
"path/filepath"
13+
"regexp"
1314
"slices"
1415
"strings"
1516
"testing"
@@ -67,7 +68,17 @@ func tree(t *testing.T) (root, out string) {
6768
"kernel/patches/0001-k.patch", "image/rootfs.qcow2", "licenses.txt"} {
6869
put(t, filepath.Join(out, f), f, 0o644)
6970
}
70-
put(t, filepath.Join(out, "packages.txt"), "bash 5.2-1 amd64\nlibc6 2.39-1 amd64\n", 0o644)
71+
// Docker's packages, as dpkg records them: an epoch and Docker's suffix around the pinned version.
72+
// First, so that the first line of the list is one of them.
73+
var list string
74+
for _, e := range pins(t).Entries {
75+
if file := filepath.Base(e.URL()); strings.HasSuffix(file, ".deb") {
76+
// The .deb's name is <package>_<version>_<arch>.deb.
77+
f := strings.Split(strings.TrimSuffix(file, ".deb"), "_")
78+
list += f[0] + " 5:" + f[1] + " " + f[2] + "\n"
79+
}
80+
}
81+
put(t, filepath.Join(out, "packages.txt"), list+"bash 5.2-1 amd64\nlibc6 2.39-1 amd64\n", 0o644)
7182
put(t, filepath.Join(out, "sbom/qemu.apk"), apkDB("glib-static,pcre2-static,libffi-dev,pixman-static,zlib-static,libseccomp-static,libcap-ng-static,liburing-dev,libaio-dev,gettext-static,musl-dev,gcc"), 0o644)
7283
put(t, filepath.Join(out, "sbom/e2fsprogs.apk"), apkDB("libarchive-static,acl-static,zlib-static,xz-static,zstd-static,bzip2-static,lz4-static,expat-static,openssl-libs-static,musl-dev,gcc"), 0o644)
7384
return root, out
@@ -180,12 +191,20 @@ func TestAReleaseShipsEveryPartAndSaysWhatItIs(t *testing.T) {
180191
if err != nil || inner.Metadata.Component.Name != "spin-machine" {
181192
t.Fatalf("the machine's SBOM: %v", err)
182193
}
183-
text, _ := io.ReadAll(mustOpen(t, filepath.Join(out, "release", name, share, "sbom", "machine.cdx.json")))
184-
for _, c := range []string{"pkg:apk/alpine/glib-static@1.0-r0", "pkg:deb/ubuntu/bash@5.2-1", `"name": "qboot.bin"`, `"name": "rootfs.qcow2"`, "0001-k.patch", "mtrr.patch"} {
185-
if !strings.Contains(string(text), c) {
194+
raw, _ := io.ReadAll(mustOpen(t, filepath.Join(out, "release", name, share, "sbom", "machine.cdx.json")))
195+
// encoding/json writes a purl's & as \u0026.
196+
text := strings.ReplaceAll(string(raw), `\u0026`, "&")
197+
docker, _ := pins(t).Get("docker-ce")
198+
for _, c := range []string{"pkg:apk/alpine/glib-static@1.0-r0", "pkg:deb/ubuntu/bash@5.2-1", `"name": "qboot.bin"`, `"name": "rootfs.qcow2"`, "0001-k.patch", "mtrr.patch",
199+
"pkg:deb/docker/docker-ce@5:" + docker.Version + "-1~ubuntu.26.04~resolute?arch=amd64&repository_url=https://download.docker.com/linux/ubuntu", docker.Pin, docker.URL(),
200+
"pkg:deb/docker/containerd.io@"} {
201+
if !strings.Contains(text, c) {
186202
t.Errorf("the machine's SBOM does not name %s", c)
187203
}
188204
}
205+
if strings.Contains(text, "pkg:deb/ubuntu/docker-ce") {
206+
t.Error("the machine's SBOM names Docker's docker-ce as Ubuntu's")
207+
}
189208
}
190209

191210
func maps(a, b map[string]string) bool {
@@ -242,8 +261,38 @@ func TestAReleaseMissingAnythingIsRefused(t *testing.T) {
242261
}
243262
})
244263
}
264+
// A Docker package the image did not record, or recorded at a version versions.yaml does not
265+
// pin, is an image that did not install what the release would say it did.
266+
for name, edit := range map[string]func(string) string{
267+
"without docker-ce": func(list string) string {
268+
return regexp.MustCompile(`(?m)^docker-ce .*\n`).ReplaceAllString(list, "")
269+
},
270+
"another docker-ce": func(list string) string {
271+
return regexp.MustCompile(`(?m)^docker-ce .*`).ReplaceAllString(list, "docker-ce 5:1.0.0-1~ubuntu.26.04~resolute amd64")
272+
},
273+
"a docker-ce whose version only starts with the pin's": func(list string) string {
274+
return regexp.MustCompile(`(?m)^docker-ce 5:([^-]*)-`).ReplaceAllString(list, "docker-ce 5:${1}1-")
275+
},
276+
} {
277+
root, out := tree(t)
278+
put(t, filepath.Join(out, "packages.txt"), edit(read(t, filepath.Join(out, "packages.txt"))), 0o644)
279+
if _, err := Pack(context.Background(), options(t, root, out)); err == nil || !strings.Contains(err.Error(), "docker-ce") {
280+
t.Errorf("an image %s: Pack = %v", name, err)
281+
}
282+
}
283+
// A .deb pinned from a repository the SBOM has no vendor for would be named as Ubuntu's again.
245284
root, out := tree(t)
246285
o := options(t, root, out)
286+
for i, e := range o.Pins.Entries {
287+
if e.Name == "docker-ce" {
288+
o.Pins.Entries[i].Source = strings.Replace(e.Source, "download.docker.com", "example.org", 1)
289+
}
290+
}
291+
if _, err := Pack(context.Background(), o); err == nil || !strings.Contains(err.Error(), "example.org") {
292+
t.Errorf("a .deb from an unknown repository: Pack = %v", err)
293+
}
294+
root, out = tree(t)
295+
o = options(t, root, out)
247296
o.Pins = &versions.Versions{}
248297
if _, err := Pack(context.Background(), o); err == nil {
249298
t.Error("a release with no pins packed")

0 commit comments

Comments
 (0)