|
865 | 865 | * 31368 records them: 「你应该能自主读到所有的安全问题才是合理的」. |
866 | 866 | * |
867 | 867 | * row request the payload carries |
868 | | - * `advisory_list` `GET …/security-advisories?state=S&per_page=100[&after=C]`, and nothing else `public_key` alone |
| 868 | + * `advisory_list` `GET …/security-advisories?state=S&per_page=100[&after=C]`, and at most one `GET /repos/{repo}` `public_key` alone |
869 | 869 | * |
870 | 870 | * The caller names NO state and NO query parameter: the row lists every state |
871 | 871 | * in `ADVISORY_LIST_STATES` (`triage`, `draft` and `closed`, every unpublished |
|
883 | 883 | * origin and path are the relay's own API and this collection, its `state` the |
884 | 884 | * walk's own, and the cursor one value in `ADVISORY_LIST_CURSOR_SHAPE` |
885 | 885 | * (`advisoryListNextCursor`); the next path is the relay's own spelling with |
886 | | - * that cursor, never the link. A link that is none of these, a page that is not |
| 886 | + * that cursor, never the link. The collection is this one under either |
| 887 | + * spelling: `/repos/{repo}/security-advisories`, or the numeric |
| 888 | + * `/repositories/{id}/security-advisories` GitHub's `Link` headers use for a |
| 889 | + * request made on the named path (its pagination guide's example; this |
| 890 | + * repository's own pull-files links, `scripts/pm/check-governed-merges.mjs`) |
| 891 | + * — the numeric one only when `{id}` equals the repository's own id, which the |
| 892 | + * walk reads ONCE, with one `GET /repos/{repo}`, the first time a link names |
| 893 | + * one (`advisoryListRepoIdOf`: 200, its own full name, a positive integer), |
| 894 | + * ⛔ never an id taken from the link; a read that fails is a FAILED action |
| 895 | + * naming its status alone (`advisoryListRepoIdUnreadText`). Other query |
| 896 | + * parameters of the link (`per_page`, a `page` beside the cursor) are never |
| 897 | + * read. A refused link prints its SHAPE beside the fixed sentence |
| 898 | + * (`advisoryListLinkShape`): the next-link count, the origin as the relay's or |
| 899 | + * another, the path with the owner, the name and every id replaced by a |
| 900 | + * placeholder, the query parameter NAMES, and whether `state` and `after` |
| 901 | + * stand as the walk needs — ⛔ never a host, id, state, cursor or any other |
| 902 | + * value, so the next mismatch reads from the job log. A link that is none of these, a page that is not |
887 | 903 | * a list of advisories in the state asked for, an advisory whose `severity` is |
888 | 904 | * not one of `ADVISORY_LIST_SEVERITIES` or null or whose `created_at` or |
889 | 905 | * `updated_at` is neither null nor an ISO-8601 string |
|
919 | 935 | * no byte read-back for it: opening the parts IS its read-back. |
920 | 936 | * |
921 | 937 | * ⛔ What crosses the public run: that the listing landed or failed, the HTTP |
922 | | - * status, `advisoryListCall` (the call, its states in braces) and the padded |
923 | | - * ciphertext. NEVER an id, a count, a state, a severity, a cursor, a page |
924 | | - * number, or a byte or part count of the listing. |
| 938 | + * status, `advisoryListCall` (the call, its states in braces), the padded |
| 939 | + * ciphertext and, on a refused next link, that link's shape. NEVER an id, a |
| 940 | + * count, a state, a severity, a cursor, a page number, or a byte or part count |
| 941 | + * of the listing. |
925 | 942 | * |
926 | 943 | * The grant and the gate are `advisory_read`'s. Its `grant` is `read` on the |
927 | 944 | * stroke-scoped `repository-advisories`, so the validator writes |
928 | 945 | * `permission_repository_advisories=read` for its stroke through the |
929 | 946 | * workflow's existing input; it travels `alone`; and its `senderGate` is |
930 | 947 | * `target-write` from `ADVISORY_READ_SENDER_GATES`: write, maintain or admin on |
931 | 948 | * the target, which every stroke passes before its first request. A row naming |
932 | | - * any other gate is refused before the first GET. The first live run measures |
933 | | - * what the read grant lists and the form of the `Link` cursor. A refusal there |
| 949 | + * any other gate is refused before the first GET. The repository read rides |
| 950 | + * the stroke's literal `metadata: read`. A refusal there |
934 | 951 | * is a FAILED action carrying `ADVISORY_LIST_ACCESS_REMEDY`, ⛔ never a retry |
935 | 952 | * under a wider token. |
936 | 953 | * |
@@ -2480,35 +2497,129 @@ export const ADVISORY_LIST_CURSOR_REFUSED = |
2480 | 2497 | 'collection or this state, or a cursor that is not exactly one value in its declared shape — so nothing was sealed or emitted'; |
2481 | 2498 |
|
2482 | 2499 | /** |
2483 | | - * The next cursor of a full page (header): `{ after }` — `null` when the `Link` header names no `rel="next"`, which ends |
2484 | | - * the walk — or `{ fail }` with `ADVISORY_LIST_CURSOR_REFUSED`. A next link is followed only when there is exactly one, |
2485 | | - * its origin is `api`'s and its path is exactly `repo`'s collection, its `state` is exactly the walk's, and it carries |
2486 | | - * exactly one `after` in `ADVISORY_LIST_CURSOR_SHAPE`. Pure; never throws. |
| 2500 | + * The collection a next link may name by the repository's NUMERIC id (header: "The one row that LISTS advisories"): |
| 2501 | + * `/repositories/{id}/security-advisories`, the spelling GitHub's `Link` headers use for the collection the request |
| 2502 | + * named as `/repos/{owner}/{name}/…`. The id is decimal digits with no sign and no leading zero, at most 20. It is |
| 2503 | + * followed only when it equals the repository's own id, read once by the walk (`advisoryListRepoIdOf`). |
2487 | 2504 | */ |
2488 | | -export function advisoryListNextCursor(link, { api, repo, state }) { |
2489 | | - if (link === null || link === undefined || link === '') return { after: null }; |
2490 | | - if (typeof link !== 'string') return { fail: ADVISORY_LIST_CURSOR_REFUSED }; |
| 2505 | +export const ADVISORY_LIST_NUMERIC_COLLECTION_SHAPE = /^\/repositories\/([1-9][0-9]{0,19})\/security-advisories$/; |
| 2506 | + |
| 2507 | +/** |
| 2508 | + * The repository's own numeric id from the answer of `GET /repos/{repo}`: a positive safe integer, as a decimal string, |
| 2509 | + * when the answer is 200 with an `id` of that shape and a `full_name` equal to `repo` (case-insensitively, as the |
| 2510 | + * platform names a repository); otherwise `null`. Pure; never throws. |
| 2511 | + */ |
| 2512 | +export function advisoryListRepoIdOf(answer, repo) { |
| 2513 | + const json = answer?.json; |
| 2514 | + if (answer?.status !== 200 || json === null || typeof json !== 'object' || Array.isArray(json)) return null; |
| 2515 | + if (typeof json.full_name !== 'string' || json.full_name.toLowerCase() !== String(repo).toLowerCase()) return null; |
| 2516 | + return Number.isSafeInteger(json.id) && json.id > 0 ? String(json.id) : null; |
| 2517 | +} |
| 2518 | + |
| 2519 | +/** What a walk prints when the repository's own id, needed to judge a numeric next link, could not be read: the status alone. */ |
| 2520 | +export function advisoryListRepoIdUnreadText(status) { |
| 2521 | + return `a full page's Link header names the collection by a repository id, and the repository's own id could not be read (GET of the repository answered HTTP ${status}) to compare it with — nothing was sealed or emitted`; |
| 2522 | +} |
| 2523 | + |
| 2524 | +/** |
| 2525 | + * The next cursor of a full page (header): `{ after }` — `null` when the `Link` header names no `rel="next"`, which ends |
| 2526 | + * the walk — or `{ fail }` with `ADVISORY_LIST_CURSOR_REFUSED`, or `{ needsRepoId: true }` when the one next link |
| 2527 | + * names the collection by a numeric id and `repoId` was not given (the walk reads the repository's own id once and |
| 2528 | + * asks again). A next link is followed only when there is exactly one, its origin is `api`'s, its path is exactly |
| 2529 | + * `repo`'s collection — as `/repos/{repo}/security-advisories`, or as `/repositories/{id}/security-advisories` with |
| 2530 | + * `{id}` exactly `repoId`, the repository's own (`ADVISORY_LIST_NUMERIC_COLLECTION_SHAPE`) — its `state` is exactly |
| 2531 | + * the walk's, and it carries exactly one `after` in `ADVISORY_LIST_CURSOR_SHAPE`. Other query parameters (`per_page`, |
| 2532 | + * a `page` beside the cursor, an empty `before`) are never read: the next path is the relay's own spelling. Pure; |
| 2533 | + * never throws. |
| 2534 | + */ |
| 2535 | +export function advisoryListNextCursor(link, { api, repo, state, repoId = null }) { |
| 2536 | + const nexts = advisoryListNextLinks(link); |
| 2537 | + if (nexts === null) return { fail: ADVISORY_LIST_CURSOR_REFUSED }; |
| 2538 | + if (nexts.length === 0) return { after: null }; |
| 2539 | + if (nexts.length > 1) return { fail: ADVISORY_LIST_CURSOR_REFUSED }; |
| 2540 | + let url; |
| 2541 | + let base; |
| 2542 | + try { |
| 2543 | + url = new URL(nexts[0]); |
| 2544 | + base = new URL(api); |
| 2545 | + } catch { |
| 2546 | + return { fail: ADVISORY_LIST_CURSOR_REFUSED }; |
| 2547 | + } |
| 2548 | + if (url.origin !== base.origin) return { fail: ADVISORY_LIST_CURSOR_REFUSED }; |
| 2549 | + const states = url.searchParams.getAll('state'); |
| 2550 | + const afters = url.searchParams.getAll('after'); |
| 2551 | + if (states.length !== 1 || states[0] !== state || afters.length !== 1 || !ADVISORY_LIST_CURSOR_SHAPE.test(afters[0])) return { fail: ADVISORY_LIST_CURSOR_REFUSED }; |
| 2552 | + if (url.pathname === `/repos/${repo}/security-advisories`) return { after: afters[0] }; |
| 2553 | + const numeric = ADVISORY_LIST_NUMERIC_COLLECTION_SHAPE.exec(url.pathname); |
| 2554 | + if (!numeric) return { fail: ADVISORY_LIST_CURSOR_REFUSED }; |
| 2555 | + if (repoId === null || repoId === undefined) return { needsRepoId: true }; |
| 2556 | + return String(repoId) === numeric[1] ? { after: afters[0] } : { fail: ADVISORY_LIST_CURSOR_REFUSED }; |
| 2557 | +} |
| 2558 | + |
| 2559 | +/** The targets of a `Link` header's `rel="next"` links — `[]` for none or an empty header, `null` for one that is no string. */ |
| 2560 | +function advisoryListNextLinks(link) { |
| 2561 | + if (link === null || link === undefined || link === '') return []; |
| 2562 | + if (typeof link !== 'string') return null; |
2491 | 2563 | const nexts = []; |
2492 | 2564 | for (const m of link.matchAll(/<([^>]*)>((?:\s*;\s*[^;,]*)*)/g)) { |
2493 | 2565 | const rel = /;\s*rel\s*=\s*"?([^";]*)"?/i.exec(m[2]); |
2494 | 2566 | // RFC 8288 §3.3: a relation type compares case-insensitively. |
2495 | 2567 | if (rel && rel[1].trim().toLowerCase().split(/\s+/).includes('next')) nexts.push(m[1]); |
2496 | 2568 | } |
2497 | | - if (nexts.length === 0) return { after: null }; |
2498 | | - if (nexts.length > 1) return { fail: ADVISORY_LIST_CURSOR_REFUSED }; |
| 2569 | + return nexts; |
| 2570 | +} |
| 2571 | + |
| 2572 | +/** The path segments a refused link's shape prints as they are; every other segment prints as a placeholder. */ |
| 2573 | +const ADVISORY_LIST_SHAPE_WORDS = Object.freeze(['repos', 'repositories', 'security-advisories']); |
| 2574 | + |
| 2575 | +/** |
| 2576 | + * The diagnostic a refused next link prints in the job log (header): the link's SHAPE, ⛔ never a value — how many |
| 2577 | + * next links, whether the first is on the relay's API origin, its path with the owner, the name and every id or other |
| 2578 | + * segment replaced by a placeholder (and whether they are this repository's), its query parameter NAMES, and of its |
| 2579 | + * `state` and `after` only whether each is the walk's, how many there are and whether a cursor is in its shape. |
| 2580 | + * No host, owner, name, id, state, cursor or other value is ever printed. Pure; never throws. |
| 2581 | + */ |
| 2582 | +export function advisoryListLinkShape(link, { api, repo, state, repoId = null }) { |
| 2583 | + const nexts = advisoryListNextLinks(link); |
| 2584 | + if (nexts === null) return 'link shape: the Link header is no string'; |
| 2585 | + if (nexts.length === 0) return 'link shape: no rel="next" link'; |
| 2586 | + const parts = [`${nexts.length} rel="next" link(s)`]; |
2499 | 2587 | let url; |
2500 | 2588 | let base; |
2501 | 2589 | try { |
2502 | 2590 | url = new URL(nexts[0]); |
2503 | 2591 | base = new URL(api); |
2504 | 2592 | } catch { |
2505 | | - return { fail: ADVISORY_LIST_CURSOR_REFUSED }; |
| 2593 | + return `link shape: ${parts[0]}; the first is no absolute URL`; |
2506 | 2594 | } |
2507 | | - if (url.origin !== base.origin || url.pathname !== `/repos/${repo}/security-advisories`) return { fail: ADVISORY_LIST_CURSOR_REFUSED }; |
| 2595 | + parts.push(url.origin === base.origin ? "on the relay's API origin" : 'on another origin'); |
| 2596 | + const segs = url.pathname.split('/').slice(1); |
| 2597 | + const [own0, own1] = String(repo).split('/'); |
| 2598 | + const shown = []; |
| 2599 | + let note = ''; |
| 2600 | + for (let k = 0; k < segs.length; k++) { |
| 2601 | + const seg = segs[k]; |
| 2602 | + if (k === 1 && segs[0] === 'repos' && segs.length > 2) { |
| 2603 | + shown.push('OWNER', 'NAME'); |
| 2604 | + const same = seg === own0 && segs[2] === own1; |
| 2605 | + const folded = !same && seg.toLowerCase() === String(own0).toLowerCase() && segs[2].toLowerCase() === String(own1).toLowerCase(); |
| 2606 | + note = same ? ' (OWNER/NAME this repository)' : folded ? ' (OWNER/NAME this repository in another letter case)' : ' (OWNER/NAME another repository)'; |
| 2607 | + k++; |
| 2608 | + } else if (k === 1 && segs[0] === 'repositories') { |
| 2609 | + shown.push('ID'); |
| 2610 | + note = !/^[1-9][0-9]{0,19}$/.test(seg) ? ' (ID not a repository id)' : repoId === null || repoId === undefined ? ' (ID not compared: the repository id was not read)' : String(repoId) === seg ? " (ID this repository's)" : ' (ID another repository\'s)'; |
| 2611 | + } else if (ADVISORY_LIST_SHAPE_WORDS.includes(seg) || seg === '') shown.push(seg); |
| 2612 | + else shown.push(/^[0-9]+$/.test(seg) ? 'N' : 'SEG'); |
| 2613 | + } |
| 2614 | + parts.push(`path /${shown.join('/')}${note}`); |
| 2615 | + const names = [...url.searchParams.keys()].map((n) => (/^[a-z_]{1,32}$/.test(n) ? n : 'NAME')); |
| 2616 | + parts.push(`query names [${names.join(',')}]`); |
2508 | 2617 | const states = url.searchParams.getAll('state'); |
| 2618 | + parts.push(`state ${states.length === 0 ? 'absent' : states.length > 1 ? `${states.length} values` : states[0] === state ? "the walk's" : 'not the walk\'s'}`); |
2509 | 2619 | const afters = url.searchParams.getAll('after'); |
2510 | | - if (states.length !== 1 || states[0] !== state || afters.length !== 1 || !ADVISORY_LIST_CURSOR_SHAPE.test(afters[0])) return { fail: ADVISORY_LIST_CURSOR_REFUSED }; |
2511 | | - return { after: afters[0] }; |
| 2620 | + const cursor = (c) => (ADVISORY_LIST_CURSOR_SHAPE.test(c) ? 'in its shape' : c.length > ADVISORY_LIST_CURSOR_MAX_CHARS ? 'over its length cap' : c.length === 0 ? 'empty' : 'with a character outside its alphabet'); |
| 2621 | + parts.push(`after ${afters.length === 0 ? 'absent' : afters.length > 1 ? `${afters.length} values` : cursor(afters[0])}`); |
| 2622 | + return `link shape: ${parts.join('; ')}`; |
2512 | 2623 | } |
2513 | 2624 |
|
2514 | 2625 | /** The severities a listed advisory may carry, beside null (the platform's own enum for the field). */ |
|
0 commit comments