Skip to content

Commit 605c534

Browse files
authored
fix: add support for sha512-only archives (#306)
- [x] Have you signed the [CLA](http://www.ubuntu.com/legal/contributors/)? ----- Attempted fix for #305. Resolve digests through a small preference list (strongest first: `SHA512`, then `SHA256`) instead of hardcoding one field, and carry the hash kind alongside the digest so downloads are verified and cached with the matching algorithm. This covers: - the Release index table (`Packages` digest), - the per-package digest (the `.deb`), and - the `Acquire-By-Hash` path merged from main, which now builds `by-hash/<algo>/<digest>` URLs from the resolved field rather than assuming `by-hash/SHA256/`. Archives only guarantee a by-hash directory for the *strongest* hash they advertise, so the by-hash URL must be built from that one -- thanks @FinnRG for catching this. Since the preference list is strongest-first, the resolved field is already the right one, and a single ordering serves both purposes. As of this writing Stonking publishes SHA512 only, in the base suite and in `-security`/`-updates` alike, so the whole suite resolves via SHA512. Resolute and earlier remain SHA256-only An archive advertising *both* digests, content is now verified and cached under its SHA512 digest rather than its SHA256 one. Cache entries written by an older chisel are keyed by SHA256, so they go cold and are fetched once more, then expire normally (which, at the moment means they don't expire #261, but that's a separate issue). Nothing gets corrupted. Importantly, at present no archive Chisel targets advertises both digests (Resolute is `SHA256`-only, Stonking is `SHA512`-only), so in practice this is not a behaviour change. ### Testing Added unit tests for the new behaviour and tested manually against the Stonking archive. The by-hash tests cover the four archive shapes: a by-hash directory for the strongest hash only, directories for several hashes (as Stonking actually publishes), a directory for a weaker hash only (a buggy archive, falls back to the named path), and no by-hash directories at all. ### Out of scope Something i've deliberately decided is out of scope but really **should be fixed** in a followup: The manifest's per-package digest still reads `SHA256`, so on a SHA512-only archive it still errors. Most `chisel cut`'s still work unless the user requests the manifest to be cut too.
1 parent 97c0d1d commit 605c534

5 files changed

Lines changed: 439 additions & 49 deletions

File tree

‎internal/archive/archive.go‎

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ func (a *ubuntuArchive) Fetch(pkg string) (io.ReadSeekCloser, *PackageInfo, erro
139139
return nil, nil, err
140140
}
141141
path := section.Get("Filename")
142+
digest, digestKind := packageDigest(section)
142143
logf("Fetching %s...", path)
143-
reader, err := index.fetch(path, section.Get("SHA256"), fetchBulk)
144+
reader, err := index.fetch(path, digest, digestKind, fetchBulk)
144145
if err != nil {
145146
return nil, nil, err
146147
}
@@ -280,7 +281,9 @@ func openUbuntu(options *Options) (Archive, error) {
280281

281282
func (index *ubuntuIndex) fetchRelease() error {
282283
logf("Fetching %s %s %s suite details...", index.displayName(), index.version, index.suite)
283-
reader, err := index.fetch(index.distPath("InRelease"), "", fetchDefault)
284+
// InRelease has no digest to check against (it is verified by its PGP
285+
// signature below), so the digest kind here is arbitrary.
286+
reader, err := index.fetch(index.distPath("InRelease"), "", cache.SHA256, fetchDefault)
284287
if err != nil {
285288
return err
286289
}
@@ -328,10 +331,47 @@ func (index *ubuntuIndex) fetchRelease() error {
328331
return nil
329332
}
330333

334+
// digestField is an archive checksum field Chisel can verify. Its name
335+
// doubles as the by-hash directory name in the archive layout.
336+
type digestField struct {
337+
name string
338+
kind cache.DigestKind
339+
}
340+
341+
// digestFields lists the checksum fields Chisel can verify, in order of
342+
// preference: strongest first. The order also matches the by-hash archive
343+
// layout, where only the by-hash directory of the strongest advertised hash
344+
// is guaranteed to exist.
345+
var digestFields = []digestField{
346+
{"SHA512", cache.SHA512},
347+
{"SHA256", cache.SHA256},
348+
}
349+
350+
// findDigest returns the digest recorded for path in the release, along with
351+
// the field it was found in, trying the given fields in order.
352+
func findDigest(release control.Section, path string, order []digestField) (digest string, field digestField) {
353+
for _, f := range order {
354+
if d, _, ok := control.ParsePathInfo(release.Get(f.name), path); ok {
355+
return d, f
356+
}
357+
}
358+
return "", digestField{}
359+
}
360+
361+
func packageDigest(section control.Section) (digest string, kind cache.DigestKind) {
362+
for _, f := range digestFields {
363+
if d := section.Get(f.name); d != "" {
364+
return d, f.kind
365+
}
366+
}
367+
// No digest advertised; fall back to SHA256 so the package can still be
368+
// cached and retrieved by its computed digest.
369+
return "", cache.SHA256
370+
}
371+
331372
func (index *ubuntuIndex) fetchIndex() error {
332-
releaseDigests := index.release.Get("SHA256")
333373
packagesPath := fmt.Sprintf("%s/binary-%s/Packages", index.component, index.arch)
334-
packagesDigest, _, _ := control.ParsePathInfo(releaseDigests, packagesPath)
374+
packagesDigest, field := findDigest(index.release, packagesPath, digestFields)
335375
if packagesDigest == "" {
336376
return fmt.Errorf("%s is missing from %s %s component digests", packagesPath, index.suite, index.component)
337377
}
@@ -345,10 +385,14 @@ func (index *ubuntuIndex) fetchIndex() error {
345385
packagesGzPath := packagesPath + ".gz"
346386
var reader io.ReadSeekCloser
347387
if index.release.Get("Acquire-By-Hash") == "yes" {
348-
packagesGzDigest, _, _ := control.ParsePathInfo(releaseDigests, packagesGzPath)
388+
// By-hash directories are only guaranteed to exist for the strongest
389+
// hash the archive advertises, which is what findDigest prefers. If
390+
// the archive advertises a hash stronger than any Chisel knows, the
391+
// URL may 404 and the named-path fallback below applies.
392+
packagesGzDigest, byHashField := findDigest(index.release, packagesGzPath, digestFields)
349393
if packagesGzDigest != "" {
350-
packagesByHashPath := fmt.Sprintf("%s/binary-%s/by-hash/SHA256/%s", index.component, index.arch, packagesGzDigest)
351-
r, err := index.fetch(index.distPath(packagesByHashPath), packagesDigest, fetchBulk|fetchGzip)
394+
packagesByHashPath := fmt.Sprintf("%s/binary-%s/by-hash/%s/%s", index.component, index.arch, byHashField.name, packagesGzDigest)
395+
r, err := index.fetch(index.distPath(packagesByHashPath), packagesDigest, field.kind, fetchBulk|fetchGzip)
352396
if err != nil && err != errNotFound {
353397
return err
354398
}
@@ -358,7 +402,7 @@ func (index *ubuntuIndex) fetchIndex() error {
358402
}
359403
}
360404
if reader == nil {
361-
r, err := index.fetch(index.distPath(packagesGzPath), packagesDigest, fetchBulk|fetchGzip)
405+
r, err := index.fetch(index.distPath(packagesGzPath), packagesDigest, field.kind, fetchBulk|fetchGzip)
362406
if err != nil {
363407
return err
364408
}
@@ -399,8 +443,7 @@ func (index *ubuntuIndex) distPath(suffix string) string {
399443
return "dists/" + index.suite + "/" + suffix
400444
}
401445

402-
func (index *ubuntuIndex) fetch(path, digest string, flags fetchFlags) (io.ReadSeekCloser, error) {
403-
const digestKind = cache.SHA256
446+
func (index *ubuntuIndex) fetch(path, digest string, digestKind cache.DigestKind, flags fetchFlags) (io.ReadSeekCloser, error) {
404447
reader, err := index.archive.cache.Open(digestKind, digest)
405448
if err == nil {
406449
return reader, nil

0 commit comments

Comments
 (0)