86 lines
3.2 KiB
Go
86 lines
3.2 KiB
Go
package geckorelay
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
)
|
|
|
|
const DistributionPrefix = "/distribution/v0.0.1/"
|
|
|
|
// Updated only after the reproducible source/notice bundle has been reviewed.
|
|
const DistributionManifestSHA256 = "84953114f5e393dd97d9f948a5bc51c95b9355b58461dd3355e068a96cb2e381"
|
|
|
|
type DistributionFile struct {
|
|
File string `json:"file"`
|
|
SHA256 string `json:"sha256"`
|
|
Size int64 `json:"size"`
|
|
Kind string `json:"kind"`
|
|
}
|
|
|
|
var distributionName = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9._+-]{0,150}$`)
|
|
|
|
func VerifyDistribution(dir string) (map[string]DistributionFile, error) {
|
|
return verifyDistribution(dir, DistributionManifestSHA256)
|
|
}
|
|
|
|
func verifyDistribution(dir, expected string) (map[string]DistributionFile, error) {
|
|
path := filepath.Join(dir, "manifest.json")
|
|
info, err := os.Lstat(path)
|
|
if err != nil || !info.Mode().IsRegular() || info.Size() > 1<<20 {
|
|
return nil, fmt.Errorf("Gecko distribution manifest missing or invalid")
|
|
}
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
sum := sha256.Sum256(raw)
|
|
if hex.EncodeToString(sum[:]) != expected {
|
|
return nil, fmt.Errorf("Gecko distribution manifest checksum mismatch")
|
|
}
|
|
var manifest struct {
|
|
Format string `json:"format"`
|
|
Version string `json:"version"`
|
|
Files []DistributionFile `json:"files"`
|
|
}
|
|
if json.Unmarshal(raw, &manifest) != nil || manifest.Format != "pageswiki.gecko-distribution.v1" || manifest.Version != "v0.0.1" || len(manifest.Files) > 512 {
|
|
return nil, fmt.Errorf("invalid Gecko distribution format")
|
|
}
|
|
files := map[string]DistributionFile{}
|
|
for _, entry := range manifest.Files {
|
|
if !distributionName.MatchString(entry.File) || entry.File == "manifest.json" || entry.Size < 1 || entry.Size > 2<<30 || len(entry.SHA256) != 64 {
|
|
return nil, fmt.Errorf("invalid distribution entry")
|
|
}
|
|
if _, exists := files[entry.File]; exists {
|
|
return nil, fmt.Errorf("duplicate distribution entry")
|
|
}
|
|
info, err := os.Lstat(filepath.Join(dir, entry.File))
|
|
if err != nil || !info.Mode().IsRegular() || info.Size() != entry.Size {
|
|
return nil, fmt.Errorf("distribution file missing or invalid: %s", entry.File)
|
|
}
|
|
file, err := os.Open(filepath.Join(dir, entry.File))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
hash := sha256.New()
|
|
_, err = io.Copy(hash, io.LimitReader(file, entry.Size+1))
|
|
file.Close()
|
|
if err != nil || hex.EncodeToString(hash.Sum(nil)) != entry.SHA256 {
|
|
return nil, fmt.Errorf("distribution file checksum mismatch: %s", entry.File)
|
|
}
|
|
files[entry.File] = entry
|
|
}
|
|
for _, name := range []string{"index.html", "README.txt", "gecko.js", "gecko.wasm.zst", "firefox.tar.gz", "firefox.tar.gz.notices.txt", "wrapper.tar.gz", "wrapper-MPL.txt", "emscripten.tar.gz", "emscripten.tar.gz.notices.txt", "rust-src.tar.xz", "rust-src.tar.xz.notices.txt", "zstddec.tgz", "zstddec.tgz.notices.txt", "NotoSansCJKjp-Regular.otf", "NotoSansCJK-LICENSE.txt"} {
|
|
if _, ok := files[name]; !ok {
|
|
return nil, fmt.Errorf("distribution incomplete: %s", name)
|
|
}
|
|
}
|
|
files["manifest.json"] = DistributionFile{File: "manifest.json", SHA256: expected, Size: int64(len(raw)), Kind: "manifest"}
|
|
return files, nil
|
|
}
|