Repository navigation
ci: check newly locked versions against the age gate before installing - #103
Merged
Merged
Conversation
Yarn applies npmMinimalAgeGate only while it picks a version for a range or tag from registry metadata; a version already pinned in yarn.lock installs without the check, so a pull request whose lockfile pins a fresh version passes CI's immutable install untouched. test/lockfile-age.js compares yarn.lock with a base revision and judges each npm version the head newly locks as Yarn would: younger than the gate, or without a publish time, is too young unless npmPreapprovedPackages names that exact version. It reads the gate, the preapprovals and the registry through `yarn config get`, so .yarnrc.yml stays the one source, and it imports only Node's built-ins: it is meant to run before anything is installed, where an installed young package could otherwise change its verdict. Preapprovals broader than one exact name@version are refused rather than matched. Yarn's own matching needs a semver range parser and glob matching, which would mean a dependency or a reimplementation, and an exemption is meant to cover one version anyway. Each test failed when the rule it pins was broken: the boundary at exactly the gate's age, the base lockfile, preapprovals, unknown publish times and broad preapprovals. Against develop before PR #100 the check fails on astro@7.4.8, which clears the gate at 2026-11-21T14:51:52Z, and passes with that version preapproved.
The output checks now fetch two levels of history and run test/lockfile-age.js against HEAD^1 before `yarn install --immutable`. For a pull_request run the checked-out commit is GitHub's merge commit, whose first parent is the commit the pull request merges into, so the check judges exactly what merging would add, and a version younger than the age gate fails the run before any of its code is installed or run. Ferit chose this on 2026-11-20, after Astro 7.4.8 had been adopted a day early by resolving it once with an exemption that was never committed: CI could not tell. An early adoption now keeps its exact name@version under npmPreapprovedPackages in .yarnrc.yml until it clears the gate, where reviewers see it. README.md says so where it describes .yarnrc.yml, and ARCHITECTURE.md gives the reason.
The check found the versions a lockfile pins with a pattern for the resolution lines Yarn 4 writes. If a later Yarn wrote them differently, the head lockfile would yield no versions, nothing would count as newly locked, and every pull request would pass. A head lockfile without a single npm version now fails the check instead; the new test failed until the check refused it.
The check turned whatever the registry listed as a version's publish time into a Date and compared its age with the gate. A listed value that is not a date string slipped through: "not-a-date" gives an invalid date whose age is NaN, and `NaN < gate` is false, so the version passed; null gives 1970, so it passed as very old. Yarn's own gate compares the same way and shares the first gap, but this check promises to fail closed whenever a version's age cannot be established. The judgement now takes the listed value as the registry gives it and reads a publish time only from a string that parses to a valid date; anything else counts as unknown, which is too young. The test failed for "not-a-date" and null until then, and fails again when either half of the parse is loosened. Found by an independent review of PR #103.
The check fetched each package's metadata itself, from npmRegistryServer with a path it built by hand. Yarn picks the registry per scope: npmScopes can send a scope elsewhere, and Yarn 4.10.3 sends the jsr scope to npm.jsr.io by default. A scoped package served from its own registry was therefore judged by another registry's publish time, or by none. CodeQL also flagged the hand-built path, which escaped only the first "/" of a name. `yarn npm info <packages> --fields name,time --json` now supplies the publish times, so Yarn's own routing, path encoding, authentication and proxy settings apply, and the check builds no URL. The judgement takes a batch: one Yarn call per run, for the unique packages among the versions it judges, and none when nothing new needs judging. Rejected: copying Yarn's routing into the check, which would duplicate Yarn internals such as the built-in jsr registry and still leave out authentication; and asking Yarn for name@version, because `yarn npm info astro@99.0.0` exits 0 and reports 7.4.13. Only each package's full time map is requested, and the check looks the locked version up in it. Yarn fetches one package at a time, about half a second each. Past lockfile changes newly locked 4, 39 and 262 packages: a few seconds for an upgrade, about two minutes for a migration of the whole tree. Checked: against develop before PR #100 the check still fails on astro@7.4.8 and passes with it preapproved. With the astrojs scope routed to https://registry.invalid in a throwaway worktree, Yarn reports ENOTFOUND for that host and the check fails; without the route it passes. Found by an independent review of PR #103 and by CodeQL.
Yarn writes `name@npm:1.0.0::__archiveUrl=<encoded url>` as the resolution when a registry serves a version's tarball from an address other than the conventional one. The check took everything after `npm:` as the version, so it looked up a publish time for "1.0.0::__archiveUrl=..." and found none, and a preapproval of name@1.0.0 could never match: an old, approved version failed for good. The version is now the reference up to its first `::`, as Yarn's own range parser reads it. Anything else left in the reference still counts as part of the version, so an odd reference still fails closed. The test failed until the parameters were dropped; the current yarn.lock carries none. Found by an independent review of PR #103.
The check compares a version's age in minutes with npmMinimalAgeGate as `yarn config get` prints it. Yarn 4.10.3 types the setting as a number of minutes, but Yarn's main branch has made it a duration (unit minutes, default "1d"), and 4.18.1 is already out. If an upgrade made the printed value a string such as "12d", `age < "12d"` would be false for every version and the check would pass everything without a word, from the upgrade's own pull request onwards. The judgement now refuses a gate that is not a finite number, so a change in Yarn's output fails the pull request that brings it. The test failed until then.
`yarn npm info` was the wrong source. Its output is
`{...packument, ...selectedVersionManifest, version, versions}`
(plugin-npm-cli/sources/commands/npm/info.ts#L131-L144 at
@yarnpkg/cli/4.10.3), so a field in the selected version's own
package.json replaces the registry's. A young version published with
`"time": {"1.0.0": "2020-01-01T00:00:00Z"}` in its manifest would have
looked six years old and passed: the very release the age gate exists
to hold back.
The check now fetches each package's metadata document itself and
reads only its top-level `time`, which the registry writes at publish.
It fetches from the address Yarn's resolver uses, and so the times
Yarn's own gate judges: the registry `npmScopes` gives the scope, else
Yarn's built-in registry for `jsr`, else `npmRegistryServer`, with one
trailing slash dropped, at the path `@scope%2fname`. `metadataUrlOf`
mirrors Yarn's getScopeRegistry, getScopeConfiguration and getIdentUrl
and links them; a test pins each rule.
Given up with Yarn: its authentication and proxy settings. A registry
that needs credentials answers 401 or 403 and fails the check, which
then passes only for preapproved versions. No registry here needs them.
Rejected: resolving the new versions with Yarn itself in a scratch
project, which applies Yarn's exact gate but resolves the whole
transitive tree around them. Fetching in parallel brings back speed:
the 262 packages of the Astro migration took 4.7 s against about two
minutes through Yarn. A failed fetch now names the address it tried.
Checked: each routing rule broken in turn fails the new test. Against
develop before PR #100 the check fails on astro@7.4.8 and passes with
it preapproved. With the astrojs scope routed to
https://registry.invalid in a throwaway worktree it fails with "Could
not read https://registry.invalid/@astrojs%2finternal-helpers"; without
the route it passes.
Found by the second independent review of PR #103.
CI failed on 4b7e6c8 with `Unexpected token 'u', "undefined" is not valid JSON`. `yarn config get <name> --json` prints a setting without a value as the bare word `undefined`, and npmScopes has none when no scope is configured: on CI there is no home .yarnrc.yml. Local runs had passed only because Yarn reads every .yarnrc.yml above the project too, and the one in the home directory configures a scope. yarnSetting now returns undefined for that output, and no npmScopes means no scope configurations. `yarn config --json` was no way out: it leaves `effective` out for an empty map and prints `{}` for a non-empty one, since a Map serializes to `{}`. Checked in a worktree outside the home directory, with HOME empty and Yarn 4.10.3 run from its bundle as corepack runs it: against HEAD^1 the check passes with nothing newly locked; against develop before PR #100 it fails on astro@7.4.8 and passes with it preapproved through YARN_NPM_PREAPPROVED_PACKAGES; with the astrojs scope routed to https://registry.invalid it fails reading that address.
The check accepts a preapproval only as an exact name@version and is meant to refuse anything broader, but its name pattern took any character except `@`, so `*@7.4.8` and `@*/telemetry@3.3.1` passed the validation. Such a glob matched no locked version, so it granted nothing; it was silently ignored where the rule says to refuse it. The scope and the name now admit only letters, digits, `.`, `_` and `-`, the characters an npm package name may use. A legacy name with one of `~'!()*`, which npm no longer accepts for new packages, cannot be preapproved and keeps failing the check until it clears the gate. The test failed for both globs until then, and fails again when either part of the pattern is loosened. Found by the third independent review of PR #103.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Output Checks workflow now judges every version a pull request newly locks against Yarn's age gate, before installing anything.
node test/lockfile-age.js HEAD^1fails the run when such a version is younger thannpmMinimalAgeGateor has no readable publish time, unlessnpmPreapprovedPackagesnames that exact version. Yarn applies the gate only while it picks a version, so a lockfile that already pins a fresh one, as #100's did forastro@7.4.8, passedyarn install --immutableunchecked.Where publish times come from
The check reads the top-level
timemap of each package's metadata document; the registry writes that map at publish. The document is fetched from the address Yarn's resolver uses, so the check judges the same times as Yarn's own gate. That address is on the registry thatnpmScopesgives the package's scope, else on Yarn's built-in registry forjsr, else onnpmRegistryServer.metadataUrlOflinks the Yarn 4.10.3 functions it mirrors, and a test pins each rule.yarn npm infois not a safe source: its output spreads the selected version's manifest over the registry's metadata. A young version that ships"time": {"1.0.0": "2020-01-01T00:00:00Z"}in itspackage.jsonwould pass as published in 2020. Fetching the document directly gives up Yarn's authentication and proxy settings, so a registry that needs credentials fails the check. No registry this site uses needs them.All packages are fetched at once: the 262 packages of the Astro migration took 4.7 seconds.
What it relies on
HEAD^1is the base. Onpull_requestruns, the checkout is GitHub's merge commit, whose first parent is the commit the pull request merges into;fetch-depth: 2fetches it.name@version;npmMinimalAgeGatea duration.It catches a young version that is pinned unnoticed. Only review catches a pull request that edits the check, the workflow or
.yarnrc.yml.After merging
An early adoption like #100 keeps its exemption committed in
.yarnrc.yml, where a reviewer sees it, until the version clears the gate; a later change then removes it. Yarn's broader preapproval forms (bare names, ranges and globs) make the check fail rather than being matched. Matching them would need a semver and glob implementation, and an exemption should name one version.Against develop before #100, the check fails on
astro@7.4.8until2026-11-21T14:51:52Zand passes with that version preapproved.