Skip to content

Commit 9bd7db1

Browse files
aledbfclaude
andcommitted
image lifecycle: the test CDN serves only what its indexes list
The fake update CDN took a release file's name from the request and joined it to a path on disk, behind a regex and a slot check. CodeQL (go/path-injection) cannot see a regex as a sanitiser, and a name a release did not publish was served when it matched the pattern. Each index's lines now make the map of what exists, name to path, at construction; a request only looks a name up, and anything the indexes do not list, a path out of the slot included, is 404. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 76c36b6 commit 9bd7db1

2 files changed

Lines changed: 32 additions & 13 deletions

File tree

‎cmd/image/cdn_test.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,19 @@ func TestUpdateCDN(t *testing.T) {
7676
if got := get("/testing/construct_20261004.2.efi"); got != "kernel 20261004.2" || c.redirects.Load() != 1 {
7777
t.Errorf("the kernel image through the redirect: %q, %d redirects", got, c.redirects.Load())
7878
}
79+
// Only what an index lists is a file: a version no release has, a file in another release's
80+
// slot, a release directory's other files, and a path out of it are all nothing.
81+
for _, p := range []string{"/testing/construct_20261004.3.efi", "/assets/v1/construct_20261004.2.efi", "/assets/v2/SHA256SUMS", "/assets/v2/../release.pem"} {
82+
resp, err := http.Get(base + p)
83+
if err != nil {
84+
t.Fatal(err)
85+
}
86+
resp.Body.Close()
87+
if resp.StatusCode != http.StatusNotFound {
88+
t.Errorf("%s: %s, not 404", p, resp.Status)
89+
}
90+
}
91+
if c.redirects.Load() != 1 {
92+
t.Errorf("%d redirects after the refused requests, not 1", c.redirects.Load())
93+
}
7994
}

‎cmd/image/lifecycle.go‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,9 @@ func newUpdateCDN(dir string, versions map[string]string, keyRef, pubring string
495495
if err := os.RemoveAll(root); err != nil {
496496
return nil, err
497497
}
498-
byVersion := map[string]string{}
498+
// What is served is what the indexes list and nothing else, each file by the name a request
499+
// gives, to a path made here from the index's line: a request's text never reaches the disk.
500+
slots, files := map[string]string{}, map[string]string{}
499501
for ch, vs := range map[string][]string{"stable": {"v1"}, "testing": {"v1", "v2"}} {
500502
idx := index{}
501503
for _, v := range vs {
@@ -508,7 +510,11 @@ func newUpdateCDN(dir string, versions map[string]string, keyRef, pubring string
508510
return nil, err
509511
}
510512
idx[versions[v]] = rel[versions[v]]
511-
byVersion[versions[v]] = v
513+
for _, line := range rel[versions[v]] {
514+
name := strings.Fields(line)[1]
515+
slots[name] = v
516+
files[name] = filepath.Join(dir, v+".release", name)
517+
}
512518
}
513519
sums := filepath.Join(root, ch, "SHA256SUMS")
514520
if err := os.MkdirAll(filepath.Dir(sums), 0o755); err != nil {
@@ -522,23 +528,21 @@ func newUpdateCDN(dir string, versions map[string]string, keyRef, pubring string
522528
}
523529
}
524530
c := &updateCDN{}
525-
files := http.FileServer(http.Dir(root))
531+
indexes := http.FileServer(http.Dir(root))
526532
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
527533
parts := strings.Split(strings.TrimPrefix(r.URL.Path, "/"), "/")
528-
if len(parts) == 2 {
529-
if m := releaseFile.FindStringSubmatch(parts[1]); m != nil {
530-
if v, ok := byVersion[m[1]]; ok && parts[0] != "assets" {
531-
c.redirects.Add(1)
532-
http.Redirect(w, r, "/assets/"+v+"/"+parts[1], http.StatusFound)
533-
return
534-
}
534+
if len(parts) == 2 && parts[0] != "assets" {
535+
if slot, ok := slots[parts[1]]; ok {
536+
c.redirects.Add(1)
537+
http.Redirect(w, r, "/assets/"+slot+"/"+parts[1], http.StatusFound)
538+
return
535539
}
536540
}
537-
if len(parts) == 3 && parts[0] == "assets" && releaseFile.MatchString(parts[2]) && (parts[1] == "v1" || parts[1] == "v2") {
538-
http.ServeFile(w, r, filepath.Join(dir, parts[1]+".release", parts[2]))
541+
if len(parts) == 3 && parts[0] == "assets" && slots[parts[2]] == parts[1] {
542+
http.ServeFile(w, r, files[parts[2]])
539543
return
540544
}
541-
files.ServeHTTP(w, r)
545+
indexes.ServeHTTP(w, r)
542546
})
543547
l, err := net.Listen("tcp", "127.0.0.1:0")
544548
if err != nil {

0 commit comments

Comments
 (0)