football-docs 0.13.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -101,8 +101,11 @@ version. Once a day at most, the server checks the
101
101
  [`data-latest`](https://github.com/withqwerty/football-docs/releases/tag/data-latest)
102
102
  release of this repository for a newer index built from `main`:
103
103
 
104
- - It reads a small manifest (`manifest-v1.json`) and, only when that names a
105
- newer build, downloads the index (about 6 MB).
104
+ - It reads a small signed manifest (`manifest-v1-signed.json`) and, only when that
105
+ names a newer build, downloads the index (about 6 MB).
106
+ - It accepts the manifest only if its ed25519 signature verifies against a public
107
+ key shipped in the package (`src/data-signing.ts`). The manifest carries the
108
+ index's size and SHA-256, so the signature covers the index too.
106
109
  - It checks the download's size and SHA-256, its SQLite integrity, its exact
107
110
  schema and its metadata before using it, and keeps the one it had on any failure.
108
111
  - It stores downloads in `$XDG_DATA_HOME/football-docs/data/` (by default
@@ -295,6 +298,21 @@ every six hours, at most once a day). Merging a doc PR is therefore also shippin
295
298
  which to stop it. To undo a bad doc change, revert it on `main`, which publishes a
296
299
  newer build.
297
300
 
301
+ **What the signature guarantees.** The publish job signs the manifest with a key
302
+ held only in the `data-publish` environment, whose deployment policy allows only
303
+ `main`. A manifest that verifies was therefore produced by `data.yml` running on
304
+ `main`. Replacing release assets by hand, or from a workflow on another branch,
305
+ cannot produce one. It does **not** mean anyone reviewed the change: `main`
306
+ accepts a PR with no approvals, so anyone who can merge to `main` can still ship
307
+ data. Requiring approvals on `main`, or required reviewers on the `data-publish`
308
+ environment (which makes every doc publish wait for a person), would close that
309
+ too.
310
+
311
+ **Rotating the key:** generate a new ed25519 pair, add its public key to
312
+ `TRUSTED_KEYS` in `src/data-signing.ts`, release, then replace the
313
+ `DATA_SIGNING_KEY` environment secret and `DATA_SIGNING_PUBLIC_KEY` in `data.yml`.
314
+ Drop the old key in a later release. A lost private key is handled the same way.
315
+
298
316
  The index carries its own metadata (`meta` table, `src/data-format.ts`): a schema
299
317
  version, the oldest server version that can read it, the build stamp and the
300
318
  provider registry. Two rules follow:
package/data/docs.db CHANGED
Binary file
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Signatures on data-latest manifests.
3
+ *
4
+ * The publish job in .github/workflows/data.yml signs each manifest with an
5
+ * ed25519 key held only in the data-publish environment, which only workflows
6
+ * running on main can use. Servers accept a manifest only if a key listed here
7
+ * verifies it. Replacing release assets by hand, or from a workflow on another
8
+ * branch, therefore cannot put an index on users' machines: without the key it
9
+ * cannot produce a manifest they accept, and the manifest carries the index's
10
+ * size and SHA-256.
11
+ *
12
+ * To rotate: generate a new key pair, add its public key here, release, then
13
+ * replace the DATA_SIGNING_KEY secret and DATA_SIGNING_PUBLIC_KEY in data.yml.
14
+ * Remove the old key in a later release.
15
+ */
16
+ export interface TrustedKey {
17
+ /** First 16 hex characters of the SHA-256 of the raw public key. */
18
+ id: string;
19
+ /** The raw 32-byte ed25519 public key, base64. */
20
+ publicKey: string;
21
+ }
22
+ export declare const TRUSTED_KEYS: readonly TrustedKey[];
23
+ export declare function keyIdFor(publicKey: string): string;
24
+ export declare class SignatureError extends Error {
25
+ }
26
+ /**
27
+ * Check a signed manifest and return the manifest JSON it carries. The payload
28
+ * is verified as the exact string that was signed, before anything parses it.
29
+ */
30
+ export declare function verifySignedManifest(envelope: unknown, keys?: readonly TrustedKey[]): string;
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Signatures on data-latest manifests.
3
+ *
4
+ * The publish job in .github/workflows/data.yml signs each manifest with an
5
+ * ed25519 key held only in the data-publish environment, which only workflows
6
+ * running on main can use. Servers accept a manifest only if a key listed here
7
+ * verifies it. Replacing release assets by hand, or from a workflow on another
8
+ * branch, therefore cannot put an index on users' machines: without the key it
9
+ * cannot produce a manifest they accept, and the manifest carries the index's
10
+ * size and SHA-256.
11
+ *
12
+ * To rotate: generate a new key pair, add its public key here, release, then
13
+ * replace the DATA_SIGNING_KEY secret and DATA_SIGNING_PUBLIC_KEY in data.yml.
14
+ * Remove the old key in a later release.
15
+ */
16
+ import { createHash, createPublicKey, verify } from "node:crypto";
17
+ import { z } from "zod";
18
+ export const TRUSTED_KEYS = [
19
+ { id: "e5a0dcbb72a87e45", publicKey: "1FjljvdILY3mrvrUeK4FqO8KbyYL4iDbTO0nIoNJuO8=" },
20
+ ];
21
+ /** DER prefix that wraps a raw ed25519 public key as SubjectPublicKeyInfo. */
22
+ const ED25519_SPKI_PREFIX = Buffer.from("302a300506032b6570032100", "hex");
23
+ export function keyIdFor(publicKey) {
24
+ return createHash("sha256").update(Buffer.from(publicKey, "base64")).digest("hex").slice(0, 16);
25
+ }
26
+ const envelopeSchema = z.object({
27
+ /** The manifest JSON, exactly as signed. */
28
+ payload: z.string(),
29
+ /** Base64 ed25519 signature over the payload's UTF-8 bytes. */
30
+ signature: z.string(),
31
+ key_id: z.string(),
32
+ });
33
+ export class SignatureError extends Error {
34
+ }
35
+ /**
36
+ * Check a signed manifest and return the manifest JSON it carries. The payload
37
+ * is verified as the exact string that was signed, before anything parses it.
38
+ */
39
+ export function verifySignedManifest(envelope, keys = TRUSTED_KEYS) {
40
+ const parsed = envelopeSchema.safeParse(envelope);
41
+ if (!parsed.success)
42
+ throw new SignatureError("signed manifest is malformed");
43
+ const key = keys.find((candidate) => candidate.id === parsed.data.key_id);
44
+ if (!key)
45
+ throw new SignatureError(`manifest is signed with unknown key ${parsed.data.key_id}`);
46
+ const publicKey = createPublicKey({
47
+ key: Buffer.concat([ED25519_SPKI_PREFIX, Buffer.from(key.publicKey, "base64")]),
48
+ format: "der",
49
+ type: "spki",
50
+ });
51
+ const valid = verify(null, Buffer.from(parsed.data.payload, "utf-8"), publicKey, Buffer.from(parsed.data.signature, "base64"));
52
+ if (!valid)
53
+ throw new SignatureError("manifest signature does not verify");
54
+ return parsed.data.payload;
55
+ }
@@ -11,8 +11,14 @@
11
11
  * which is the MCP channel.
12
12
  */
13
13
  import { z } from "zod";
14
+ import { type TrustedKey } from "./data-signing.js";
14
15
  export declare const DEFAULT_DATA_BASE_URL = "https://github.com/withqwerty/football-docs/releases/download/data-latest/";
15
- export declare const MANIFEST_NAME = "manifest-v1.json";
16
+ /**
17
+ * The signed manifest servers read. Its payload is the manifest-v<schema>.json
18
+ * that data.yml builds; the unsigned file is not published, and a server never
19
+ * falls back to it, so deleting the signed file cannot downgrade the check.
20
+ */
21
+ export declare const MANIFEST_NAME = "manifest-v1-signed.json";
16
22
  export declare const MAX_DOWNLOAD_BYTES: number;
17
23
  declare const manifestSchema: z.ZodObject<{
18
24
  schema_version: z.ZodNumber;
@@ -36,6 +42,8 @@ export interface UpdateOptions {
36
42
  now?: () => number;
37
43
  signal?: AbortSignal;
38
44
  maxBytes?: number;
45
+ /** Keys that may sign manifests; defaults to the keys shipped in the package. */
46
+ trustedKeys?: readonly TrustedKey[];
39
47
  log?: (message: string) => void;
40
48
  }
41
49
  export interface UpdateResult {
@@ -16,9 +16,15 @@ import { open } from "node:fs/promises";
16
16
  import { join } from "node:path";
17
17
  import { z } from "zod";
18
18
  import { compareVersions, DATA_SCHEMA_VERSION, validateDatabase } from "./data-format.js";
19
+ import { SignatureError, TRUSTED_KEYS, verifySignedManifest } from "./data-signing.js";
19
20
  import { cachedFileName } from "./data-source.js";
20
21
  export const DEFAULT_DATA_BASE_URL = "https://github.com/withqwerty/football-docs/releases/download/data-latest/";
21
- export const MANIFEST_NAME = `manifest-v${DATA_SCHEMA_VERSION}.json`;
22
+ /**
23
+ * The signed manifest servers read. Its payload is the manifest-v<schema>.json
24
+ * that data.yml builds; the unsigned file is not published, and a server never
25
+ * falls back to it, so deleting the signed file cannot downgrade the check.
26
+ */
27
+ export const MANIFEST_NAME = `manifest-v${DATA_SCHEMA_VERSION}-signed.json`;
22
28
  const CHECK_INTERVAL_MS = 24 * 60 * 60 * 1000;
23
29
  const MANIFEST_TIMEOUT_MS = 10_000;
24
30
  const DOWNLOAD_TIMEOUT_MS = 60_000;
@@ -76,7 +82,7 @@ class NotPublished extends Error {
76
82
  }
77
83
  class Rejected extends Error {
78
84
  }
79
- async function fetchManifest(fetchImpl, url, parent) {
85
+ async function fetchManifest(fetchImpl, url, keys, parent) {
80
86
  requireHttps(url);
81
87
  const timeout = timeoutSignal(MANIFEST_TIMEOUT_MS, parent);
82
88
  try {
@@ -87,7 +93,9 @@ async function fetchManifest(fetchImpl, url, parent) {
87
93
  throw new NotPublished(`no ${MANIFEST_NAME} published`);
88
94
  if (!response.ok)
89
95
  throw new Error(`manifest request returned HTTP ${response.status}`);
90
- return manifestSchema.parse(await response.json());
96
+ // Verify the signature over the exact signed text before parsing any of it.
97
+ const payload = verifySignedManifest(await response.json(), keys);
98
+ return manifestSchema.parse(JSON.parse(payload));
91
99
  }
92
100
  finally {
93
101
  timeout.done();
@@ -165,7 +173,7 @@ export async function checkForUpdate(options) {
165
173
  const base = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
166
174
  let manifest;
167
175
  try {
168
- manifest = await fetchManifest(fetchImpl, new URL(MANIFEST_NAME, base).toString(), options.signal);
176
+ manifest = await fetchManifest(fetchImpl, new URL(MANIFEST_NAME, base).toString(), options.trustedKeys ?? TRUSTED_KEYS, options.signal);
169
177
  }
170
178
  catch (error) {
171
179
  if (error instanceof NotPublished) {
@@ -174,7 +182,12 @@ export async function checkForUpdate(options) {
174
182
  }
175
183
  const detail = error instanceof Error ? error.message : String(error);
176
184
  // A malformed manifest is a completed check; a network failure is not.
177
- if (error instanceof z.ZodError) {
185
+ if (error instanceof SignatureError) {
186
+ recordCheck(options.dataDir, now());
187
+ log(`football-docs: data manifest rejected: ${detail}`);
188
+ return { outcome: "rejected", detail };
189
+ }
190
+ if (error instanceof z.ZodError || error instanceof SyntaxError) {
178
191
  recordCheck(options.dataDir, now());
179
192
  log(`football-docs: data manifest rejected: ${detail}`);
180
193
  return { outcome: "rejected", detail: "malformed manifest" };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "football-docs",
3
- "version": "0.13.0",
3
+ "version": "0.14.0",
4
4
  "description": "Searchable football data provider documentation for AI coding agents. Like Context7 for football data.",
5
5
  "type": "module",
6
6
  "packageManager": "pnpm@10.34.4",