|
| 1 | +// Package catalogue loads the release-signed app-store catalogue and exposes |
| 2 | +// the per-app publisher "pins" the daemon uses as its trust anchor. |
| 3 | +// |
| 4 | +// The catalogue (signed by the embedded catalogue key, see internal/catalogtrust) |
| 5 | +// is the root of trust: it declares, per app id, the ed25519 publisher key that |
| 6 | +// app's manifest must be signed by. The app-store supervisor confirms each |
| 7 | +// non-sideloaded app's manifest.Store.Publisher matches this pin before spawning |
| 8 | +// (manifest.VerifyTrustAnchor). This package is what feeds those pins to the |
| 9 | +// supervisor via appstore.Config.CataloguePublisher. |
| 10 | +package catalogue |
| 11 | + |
| 12 | +import ( |
| 13 | + "encoding/base64" |
| 14 | + "encoding/json" |
| 15 | + "fmt" |
| 16 | + "io" |
| 17 | + "net/http" |
| 18 | + "net/url" |
| 19 | + "os" |
| 20 | + "strings" |
| 21 | + "sync" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/pilot-protocol/pilotprotocol/internal/catalogtrust" |
| 25 | +) |
| 26 | + |
| 27 | +// DefaultURL is the production catalogue location; override with |
| 28 | +// $PILOT_APPSTORE_CATALOG_URL (kept identical to pilotctl's default). |
| 29 | +const DefaultURL = "https://raw.githubusercontent.com/pilot-protocol/pilotprotocol/main/catalogue/catalogue.json" |
| 30 | + |
| 31 | +// URL returns the catalogue URL the daemon should load — env override wins. |
| 32 | +func URL() string { |
| 33 | + if u := strings.TrimSpace(os.Getenv("PILOT_APPSTORE_CATALOG_URL")); u != "" { |
| 34 | + return u |
| 35 | + } |
| 36 | + return DefaultURL |
| 37 | +} |
| 38 | + |
| 39 | +// entry is the minimal slice of a catalogue entry this package needs: the app |
| 40 | +// id and the publisher pin. All other catalogue fields are ignored. |
| 41 | +type entry struct { |
| 42 | + ID string `json:"id"` |
| 43 | + Publisher string `json:"publisher"` |
| 44 | +} |
| 45 | + |
| 46 | +type doc struct { |
| 47 | + Version int `json:"version"` |
| 48 | + Apps []entry `json:"apps"` |
| 49 | +} |
| 50 | + |
| 51 | +// LoadPublishers fetches the catalogue at url (and its detached <url>.sig), |
| 52 | +// verifies the signature against the embedded catalogue key (fail-closed), and |
| 53 | +// returns appID -> publisher pin ("ed25519:<base64>") for every entry that |
| 54 | +// declares a publisher. The signature check is the same gate pilotctl uses at |
| 55 | +// install time — a substituted catalogue cannot change the pins. |
| 56 | +func LoadPublishers(url string) (map[string]string, error) { |
| 57 | + data, err := fetch(url) |
| 58 | + if err != nil { |
| 59 | + return nil, fmt.Errorf("fetch catalogue from %s: %w", url, err) |
| 60 | + } |
| 61 | + sigRaw, err := fetch(url + ".sig") |
| 62 | + if err != nil { |
| 63 | + return nil, fmt.Errorf("fetch catalogue signature %s.sig: %w", url, err) |
| 64 | + } |
| 65 | + sig, err := base64.StdEncoding.DecodeString(strings.TrimSpace(string(sigRaw))) |
| 66 | + if err != nil { |
| 67 | + return nil, fmt.Errorf("decode catalogue signature: %w", err) |
| 68 | + } |
| 69 | + if err := catalogtrust.Verify(data, sig); err != nil { |
| 70 | + return nil, fmt.Errorf("catalogue signature: %w", err) |
| 71 | + } |
| 72 | + var d doc |
| 73 | + if err := json.Unmarshal(data, &d); err != nil { |
| 74 | + return nil, fmt.Errorf("parse catalogue: %w", err) |
| 75 | + } |
| 76 | + pins := make(map[string]string, len(d.Apps)) |
| 77 | + for _, e := range d.Apps { |
| 78 | + if e.ID != "" && strings.TrimSpace(e.Publisher) != "" { |
| 79 | + pins[e.ID] = e.Publisher |
| 80 | + } |
| 81 | + } |
| 82 | + return pins, nil |
| 83 | +} |
| 84 | + |
| 85 | +// fetch reads up to 1 MiB from a file://, https://, or http://localhost URL. |
| 86 | +// Mirrors pilotctl's openURL: plaintext http is refused for non-loopback hosts. |
| 87 | +func fetch(raw string) ([]byte, error) { |
| 88 | + u, err := url.Parse(raw) |
| 89 | + if err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + var body io.ReadCloser |
| 93 | + switch u.Scheme { |
| 94 | + case "file": |
| 95 | + f, err := os.Open(u.Path) |
| 96 | + if err != nil { |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + body = f |
| 100 | + case "https": |
| 101 | + body, err = httpGet(raw) |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + case "http": |
| 106 | + if h := u.Hostname(); h != "localhost" && h != "127.0.0.1" && h != "::1" { |
| 107 | + return nil, fmt.Errorf("refusing plaintext http for non-localhost host %q (use https)", h) |
| 108 | + } |
| 109 | + body, err = httpGet(raw) |
| 110 | + if err != nil { |
| 111 | + return nil, err |
| 112 | + } |
| 113 | + default: |
| 114 | + return nil, fmt.Errorf("unsupported url scheme %q", u.Scheme) |
| 115 | + } |
| 116 | + defer body.Close() |
| 117 | + data, err := io.ReadAll(io.LimitReader(body, 1<<20)) |
| 118 | + if err != nil { |
| 119 | + return nil, fmt.Errorf("read body: %w", err) |
| 120 | + } |
| 121 | + return data, nil |
| 122 | +} |
| 123 | + |
| 124 | +func httpGet(raw string) (io.ReadCloser, error) { |
| 125 | + client := &http.Client{Timeout: 15 * time.Second} |
| 126 | + resp, err := client.Get(raw) //nolint:noctx // short-lived, bounded by client.Timeout |
| 127 | + if err != nil { |
| 128 | + return nil, err |
| 129 | + } |
| 130 | + if resp.StatusCode != http.StatusOK { |
| 131 | + resp.Body.Close() |
| 132 | + return nil, fmt.Errorf("GET %s: status %d", raw, resp.StatusCode) |
| 133 | + } |
| 134 | + return resp.Body, nil |
| 135 | +} |
| 136 | + |
| 137 | +// Provider serves catalogue publisher pins to the app-store supervisor and |
| 138 | +// refreshes them from the signed catalogue. Safe for concurrent use: the |
| 139 | +// supervisor reads via Publisher on every scan while a background loop writes |
| 140 | +// via Refresh. A disk cache lets the daemon survive a transient catalogue |
| 141 | +// outage on restart (fail-closed only when there is neither a live catalogue |
| 142 | +// nor a cache). |
| 143 | +type Provider struct { |
| 144 | + url string |
| 145 | + cachePath string |
| 146 | + |
| 147 | + mu sync.RWMutex |
| 148 | + pins map[string]string |
| 149 | +} |
| 150 | + |
| 151 | +// NewProvider builds a Provider for the catalogue at url, caching the last |
| 152 | +// verified pin set at cachePath (empty disables the cache). |
| 153 | +func NewProvider(url, cachePath string) *Provider { |
| 154 | + return &Provider{url: url, cachePath: cachePath, pins: map[string]string{}} |
| 155 | +} |
| 156 | + |
| 157 | +// Publisher implements appstore.Config.CataloguePublisher: it returns the |
| 158 | +// catalogue-pinned publisher for appID and whether appID is pinned. |
| 159 | +func (p *Provider) Publisher(appID string) (string, bool) { |
| 160 | + p.mu.RLock() |
| 161 | + defer p.mu.RUnlock() |
| 162 | + pub, ok := p.pins[appID] |
| 163 | + return pub, ok |
| 164 | +} |
| 165 | + |
| 166 | +// Refresh fetches + verifies the catalogue and atomically swaps in the new pin |
| 167 | +// set. On success it also writes the disk cache. On failure the previous pins |
| 168 | +// are kept (so a transient outage doesn't suddenly fail-close running apps). |
| 169 | +func (p *Provider) Refresh() error { |
| 170 | + pins, err := LoadPublishers(p.url) |
| 171 | + if err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + p.mu.Lock() |
| 175 | + p.pins = pins |
| 176 | + p.mu.Unlock() |
| 177 | + p.writeCache(pins) |
| 178 | + return nil |
| 179 | +} |
| 180 | + |
| 181 | +// LoadCache populates the pin set from the disk cache. Best-effort: used at |
| 182 | +// startup when the initial Refresh fails (e.g. the daemon booted offline). |
| 183 | +// Returns true if any pins were loaded. |
| 184 | +func (p *Provider) LoadCache() bool { |
| 185 | + if p.cachePath == "" { |
| 186 | + return false |
| 187 | + } |
| 188 | + data, err := os.ReadFile(p.cachePath) |
| 189 | + if err != nil { |
| 190 | + return false |
| 191 | + } |
| 192 | + var pins map[string]string |
| 193 | + if err := json.Unmarshal(data, &pins); err != nil || len(pins) == 0 { |
| 194 | + return false |
| 195 | + } |
| 196 | + p.mu.Lock() |
| 197 | + p.pins = pins |
| 198 | + p.mu.Unlock() |
| 199 | + return true |
| 200 | +} |
| 201 | + |
| 202 | +func (p *Provider) writeCache(pins map[string]string) { |
| 203 | + if p.cachePath == "" { |
| 204 | + return |
| 205 | + } |
| 206 | + data, err := json.Marshal(pins) |
| 207 | + if err != nil { |
| 208 | + return |
| 209 | + } |
| 210 | + tmp := p.cachePath + ".tmp" |
| 211 | + if err := os.WriteFile(tmp, data, 0o600); err != nil { |
| 212 | + return |
| 213 | + } |
| 214 | + _ = os.Rename(tmp, p.cachePath) // atomic replace; best-effort |
| 215 | +} |
| 216 | + |
| 217 | +// Count returns how many apps are currently pinned (for startup logging). |
| 218 | +func (p *Provider) Count() int { |
| 219 | + p.mu.RLock() |
| 220 | + defer p.mu.RUnlock() |
| 221 | + return len(p.pins) |
| 222 | +} |
0 commit comments