diff --git a/.github/workflows/install-check.yml b/.github/workflows/install-check.yml new file mode 100644 index 000000000..89944a795 --- /dev/null +++ b/.github/workflows/install-check.yml @@ -0,0 +1,107 @@ +name: Install check + +# Installs the dependencies on Linux, macOS and Windows and checks what the tar overrides in +# package.json are there for (see vendor/README.md): no tar older than 8 is installed, the native +# modules load, and sqlite3 still builds from source. Runs when the dependencies change +on: + push: + paths: + - package.json + - package-lock.json + - vendor/** + - .github/workflows/install-check.yml + pull_request: + paths: + - package.json + - package-lock.json + - vendor/** + - .github/workflows/install-check.yml + workflow_dispatch: + +jobs: + install: + name: Install (${{ matrix.os }}) + runs-on: ${{ matrix.os }} + timeout-minutes: 30 + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + defaults: + run: + shell: bash + + steps: + - uses: actions/checkout@v4 + + - name: Use Node.js + uses: actions/setup-node@v4 + with: + node-version: '24' + cache: 'npm' + + - name: Install dependencies + run: npm ci + + - name: Check the packed shim is the one in vendor/tar-stream-shim + run: | + node -e ' + const fs = require("fs"), path = require("path"), tar = require("tar"); + const out = fs.mkdtempSync(path.join(process.env.RUNNER_TEMP, "shim-")); + tar.extractSync({ file: "vendor/tar-stream-shim-1.0.0.tgz", cwd: out }); + const read = (dir) => Object.fromEntries(fs.readdirSync(dir).sort().map((name) => + [name, fs.readFileSync(path.join(dir, name), "utf8").replace(/\r\n/g, "\n")])); + const source = read("vendor/tar-stream-shim"), packed = read(path.join(out, "package")); + const differ = [...new Set([...Object.keys(source), ...Object.keys(packed)])].filter((name) => source[name] !== packed[name]); + if (differ.length > 0) { console.error("differ: " + differ.join(", ") + " - pack it again (vendor/README.md)"); process.exit(1); } + console.log("the same: " + Object.keys(source).join(", ")); + ' + + - name: Check no tar older than 8 is installed + run: | + node -e ' + const fs = require("fs"), path = require("path"); + const found = []; + (function look(dir) { + for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { + if (!entry.isDirectory()) continue; + const sub = path.join(dir, entry.name); + if (entry.name.startsWith("@")) { look(sub); continue; } + const manifest = path.join(sub, "package.json"); + if (fs.existsSync(manifest)) { + const { name, version } = JSON.parse(fs.readFileSync(manifest, "utf8")); + if (name === "tar") found.push({ version, sub }); + } + if (fs.existsSync(path.join(sub, "node_modules"))) look(path.join(sub, "node_modules")); + } + })("node_modules"); + for (const { version, sub } of found) console.log(`tar ${version} in ${sub}`); + const old = found.filter(({ version }) => parseInt(version, 10) < 8); + if (found.length === 0 || old.length > 0) { console.error("expected tar 8 or later, and nothing older"); process.exit(1); } + ' + + - name: Load the native modules + run: | + node -e ' + const { RoaringBitmap32 } = require("roaring"); + console.log("roaring", new RoaringBitmap32([1, 2, 3]).size); + console.log("better-sqlite3", require("better-sqlite3")(":memory:").prepare("select sqlite_version() v").get().v); + require("bcrypt").hashSync("x", 4); + new (require("sqlite3").Database)(":memory:").get("select sqlite_version() v", (error, row) => { + if (error) throw error; + console.log("sqlite3", row.v); + }); + ' + + # node-gyp is found at node_modules/node-gyp (npm puts it there for sqlite3 and roaring); if a + # dependency change moves it, this step fails at once, and the path needs changing. + # The build unpacks the SQLite source with tar (node_modules/sqlite3/deps/extract.js) and + # then deletes its VERSION file, which on macOS and Windows hides the C++ header + - name: Build sqlite3 from source + working-directory: node_modules/sqlite3 + run: | + node ../node-gyp/bin/node-gyp.js rebuild + left=$(find build -name VERSION | wc -l) + echo "VERSION files left: $left" + [ "$left" -eq 0 ] + node -e 'new (require(".").Database)(":memory:").get("select sqlite_version() v", (error, row) => { if (error) throw error; console.log("sqlite3 built from source", row.v); })' diff --git a/.github/workflows/pr-pipeline.yml b/.github/workflows/pr-pipeline.yml index db853c81b..5100e8dd4 100644 --- a/.github/workflows/pr-pipeline.yml +++ b/.github/workflows/pr-pipeline.yml @@ -70,14 +70,15 @@ jobs: # npm ignores package.json "overrides" when FHIRsmith is installed (npm install -g fhirsmith) # rather than cloned, and the published package has no lock file. So the repo's own audit can # be clean while installed servers get vulnerable transitive versions. This installs the - # package the way users do, into an empty folder, and audits what that actually gets + # package the way users do, into an empty folder, and audits what that actually gets. + # It has its own allowlist: what only an override can fix is accepted there, with the reason run: | npm pack --pack-destination "$RUNNER_TEMP" mkdir "$RUNNER_TEMP/fresh-install" cd "$RUNNER_TEMP/fresh-install" npm init -y > /dev/null npm install --ignore-scripts --no-audit --no-fund "$RUNNER_TEMP"/fhirsmith-*.tgz - node "$GITHUB_WORKSPACE/utilities/audit-check.js" --omit=dev --audit-level=moderate + node "$GITHUB_WORKSPACE/utilities/audit-check.js" --omit=dev --audit-level=moderate --allowlist="$GITHUB_WORKSPACE/audit-allowlist-published.json" dependency-check: name: Dependency Analysis diff --git a/.npmignore b/.npmignore index c98d422a8..a255b9467 100644 --- a/.npmignore +++ b/.npmignore @@ -63,6 +63,7 @@ docs/ # Release and development scripts, and test reports release.sh +vendor/ changelog-template.md tsconfig.eslint.json test-scripts/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 5eaa18e55..569031b6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Security - Packages are read with tar 8 (was 7, which has 16 high advisories, most of them ways for an archive's links to get out of the folder it is extracted into): the package manager (the tx module's packages) and the packages crawler, which read packages from elsewhere. A package is unzipped against the unpacked size limit as before, and a second layer of compression inside one is now refused +- A clone (`npm install`, `npm ci`) and the Docker image no longer install tar 7. sqlite3, node-gyp and @mapbox/node-pre-gyp (under roaring) each ask for tar 7, which they use while installing to unpack a prebuilt binary, the Node headers or the SQLite source, and none has a release on tar 8. package.json `overrides` now gives them tar 8, through a small adapter for the two calls tar 8 changed (vendor/README.md). The tar advisories are gone from audit-allowlist.json. npm doesn't apply a package's `overrides` when the package is installed from the registry, so `npm install fhirsmith` still gets tar 7 under those three until they move to tar 8; the pipeline's audit of a published install accepts that, in audit-allowlist-published.json ### Added @@ -18,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- npm versions that only run the install scripts a project approves (npm 12, and recent npm 11) left sqlite3, better-sqlite3 and roaring without their native binary after `npm install`, with no error. package.json `allowScripts` now approves those three, and bcrypt (which comes with binaries for the usual platforms, and builds from source elsewhere) +- package.json `engines` asks for Node 24, not 24 or later: on Node 26 roaring 2.7.0 has no prebuilt binary and does not compile, and as an optional dependency it is left out without an error, so the sqlite-v2 providers cannot load - tx: the `cache-id` parameter is refused, wherever it is (a parameter, a part, a `$batch-validate-code` validation entry, the query): a 400 (`TX_PARAM_CACHE_ID_WITHDRAWN`) that says to start a cache with `$cache-control` and send its id in the `X-Cache-Id` header. The tx-ecosystem IG withdrew it (servers SHOULD NOT accept it); `$batch-validate-code` ignored it, so a bad cache-id went unnoticed. Clients from before June 2026 that send it already got 404 cache-id-unknown, as the server issues the ids - tx: a media type (`urn:ietf:bcp:13`) is valid only if it follows the grammar of RFC 9110 section 8.3.1 / RFC 2045: a parameter with no name or no value, an unclosed quoted string, a value that isn't a token or a quoted string (an unquoted URI, say), and whitespace around the `/` or `=` or at the ends are invalid, and the issue says where (`MIMETYPE_INVALID`). A `;` in a quoted value is part of the value (it split the parameter, and broke the normal form) - tx: `$subsumes` on a code system whose `hierarchyMeaning` is not is-a (part-of, grouped-by, classified-with) is refused (422, `SUBSUMPTION_CS_HIERARCHY_MEANING`), as core's: its hierarchy doesn't describe subsumption. `$closure` finds no subsumption in it diff --git a/Dockerfile b/Dockerfile index 4278c5485..c9f8585d7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,6 +8,8 @@ RUN apk add --no-cache python3 make g++ # Install app dependencies COPY package*.json ./ +# package.json "overrides" installs this in place of tar 7 (vendor/README.md) +COPY vendor/tar-stream-shim-1.0.0.tgz ./vendor/ RUN npm ci --omit=dev # Stage 2: Runtime image (no build tools) diff --git a/README.md b/README.md index 038589cb6..679832961 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ During development with a cloned repository, the data directory defaults to `[ro is provided by the host — see Deployment below. ### Prerequisites -- Node.js 24+ +- Node.js 24 (not 26 yet: roaring, which the sqlite-v2 terminology providers need, has no build for it) - NPM or Yarn - Java 17+ (for FHIR validator, also for the test cases) diff --git a/audit-allowlist-published.json b/audit-allowlist-published.json new file mode 100644 index 000000000..6dac07ccc --- /dev/null +++ b/audit-allowlist-published.json @@ -0,0 +1,6 @@ +{ + "comment": "Advisories the CI audit of a fresh install of the published package accepts, on top of nothing else (utilities/audit-check.js --allowlist). npm ignores package.json overrides for an installed package, so what FHIRsmith's overrides fix for a clone or the Docker image is still there for npm install fhirsmith.", + "advisories": [{ "package": "tar", "until": "2027-03-01", + "reason": "tar <8: sqlite3 6.0.1, node-gyp 12 and @mapbox/node-pre-gyp 2.0.3 (under roaring) depend on tar 7 and have no release on tar 8. A clone or the Docker image gets tar 8 under them through package.json overrides (vendor/README.md); npm doesn't apply overrides to an installed package, so npm install fhirsmith still gets their tar 7. They use it only while installing, on archives from fixed sources. FHIRsmith's own tar is 8. Remove once they move to tar 8" + }] +} diff --git a/audit-allowlist.json b/audit-allowlist.json index 25e7ce94e..ee1371abd 100644 --- a/audit-allowlist.json +++ b/audit-allowlist.json @@ -1,6 +1,4 @@ { "comment": "Advisories the CI npm audit steps accept (utilities/audit-check.js). Each entry needs a reason, and an expiry date after which it fails again so it gets looked at. Remove an entry once a fixed version is installed - the check warns when an entry no longer matches anything.", - "advisories": [{ "id": "GHSA-25mm-r35j-6hgm", "until": "2027-03-01", - "reason": "tar <8: FHIRsmith's own tar is 8.0.5. The tar 7 copies left are sqlite3 6.0.1's, node-gyp 12's and @mapbox/node-pre-gyp 2.0.3's (under roaring), with no release on tar 8 yet; they use it only while installing, on archives from fixed sources. Remove once they move to tar 8" - }] + "advisories": [] } diff --git a/package-lock.json b/package-lock.json index c023ae168..cd7828b87 100644 --- a/package-lock.json +++ b/package-lock.json @@ -41,7 +41,7 @@ "qrcode-generator": "^2.0.4", "re2js": "^2.8.0", "sqlite3": "^6.0.1", - "tar": "^8.0.5", + "tar": "^8.1.2", "turndown": "^7.2.5", "winston": "^3.19.0", "winston-daily-rotate-file": "^4.7.1", @@ -68,7 +68,7 @@ "tmp": "^0.2.5" }, "engines": { - "node": ">=24" + "node": "^24" }, "optionalDependencies": { "koffi": "^3.3.6", @@ -966,18 +966,6 @@ "url": "https://github.com/chalk/wrap-ansi?sponsor=1" } }, - "node_modules/@isaacs/fs-minipass": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/@isaacs/fs-minipass/-/fs-minipass-4.0.1.tgz", - "integrity": "sha512-wgm9Ehl2jpeqP3zw/7mo3kRHFp5MEDhqAdwy1fTGkHAwnkGOVsgpvQhL8B5n1qlb01jV3n/bI0ZfZp5lWA1k4w==", - "license": "ISC", - "dependencies": { - "minipass": "^7.0.4" - }, - "engines": { - "node": ">=18.0.0" - } - }, "node_modules/@isaacs/nonrecursive-glob-match": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@isaacs/nonrecursive-glob-match/-/nonrecursive-glob-match-1.1.0.tgz", @@ -1947,30 +1935,14 @@ } }, "node_modules/@mapbox/node-pre-gyp/node_modules/tar": { - "version": "7.5.24", - "resolved": "https://registry.npmjs.org/tar/-/tar-7.5.24.tgz", - "integrity": "sha512-to/OxuCk5DeVONcjjODT7aeoRq5NIPPlfPc0tNML+wLlnf6u5BcJz97Uukv3pT3jzorkFhUevylkAiBr+ZmV2w==", - "license": "BlueOak-1.0.0", + "name": "tar-stream-shim", + "version": "1.0.0", + "resolved": "file:vendor/tar-stream-shim-1.0.0.tgz", + "integrity": "sha512-+wZjAAA69cxXKASR20ipmvuWTKoNIQjw/HRdNO8SMQADPx29xcZhO+EMECUOA2EDfACEBliEh7jmK/NZN+IKTw==", + "license": "BSD-3-Clause", "optional": true, "dependencies": { - "@isaacs/fs-minipass": "^4.0.0", - "chownr": "^3.0.0", - "minipass": "^7.1.2", - "minizlib": "^3.1.0", - "yallist": "^5.0.0" - }, - "engines": { - "node": ">=18" - } - }, - "node_modules/@mapbox/node-pre-gyp/node_modules/yallist": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/yallist/-/yallist-5.0.0.tgz", - "integrity": "sha512-YgvUTfwqyc7UXVMrB+SImsVYSmTS8X/tSrtdNZMImM+n7+QTriRXyXim0mBrTXNeqzVF0KWGgHPeiyViFFrNDw==", - "license": "BlueOak-1.0.0", - "optional": true, - "engines": { - "node": ">=18" + "tar8": "npm:tar@^8.1.2" } }, "node_modules/@mixmark-io/domino": { @@ -3938,15 +3910,6 @@ "integrity": "sha512-rddelWYNPRrXq6PtNEN2S3f6t9ILzvqaN5pVgi4kqt9jHQaXIial9PznB5iSPVlQSLNaaH22ItWz3EJtQ10+OA==", "license": "MIT" }, - "node_modules/chownr": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/chownr/-/chownr-3.0.0.tgz", - "integrity": "sha512-+IxzY9BZOQd/XuYPRmrvEVjF/nqj5kgT4kEq7VofrDoM1MxoRjEWkrCC3EtLi59TVawxTAn+orJwFQcrqEN1+g==", - "license": "BlueOak-1.0.0", - "engines": { - "node": ">=18" - } - }, "node_modules/ci-info": { "version": "4.4.0", "resolved": "https://registry.npmjs.org/ci-info/-/ci-info-4.4.0.tgz", @@ -7493,23 +7456,12 @@ "version": "7.1.3", "resolved": "https://registry.npmjs.org/minipass/-/minipass-7.1.3.tgz", "integrity": "sha512-tEBHqDnIoM/1rXME1zgka9g6Q2lcoCkxHLuc7ODJ5BxbP5d4c2Z5cGgtXAku59200Cx7diuHTOYfSBD8n6mm8A==", + "dev": true, "license": "BlueOak-1.0.0", "engines": { "node": ">=16 || 14 >=14.17" } }, - "node_modules/minizlib": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/minizlib/-/minizlib-3.1.0.tgz", - "integrity": "sha512-KZxYo1BUkWD2TVFLr0MQoM8vUUigWD3LlD83a/75BqC+4qE0Hb1Vo5v1FgcfaNXvfXzr+5EhQ6ing/CaBijTlw==", - "license": "MIT", - "dependencies": { - "minipass": "^7.1.2" - }, - "engines": { - "node": ">= 18" - } - }, "node_modules/mkdirp": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz", @@ -7906,20 +7858,14 @@ } }, "node_modules/node-gyp/node_modules/tar": { - "version": "7.5.24", - "resolved": "https://registry.npmjs.org/tar/-/tar-7.5.24.tgz", - "integrity": "sha512-to/OxuCk5DeVONcjjODT7aeoRq5NIPPlfPc0tNML+wLlnf6u5BcJz97Uukv3pT3jzorkFhUevylkAiBr+ZmV2w==", - "license": "BlueOak-1.0.0", + "name": "tar-stream-shim", + "version": "1.0.0", + "resolved": "file:vendor/tar-stream-shim-1.0.0.tgz", + "integrity": "sha512-+wZjAAA69cxXKASR20ipmvuWTKoNIQjw/HRdNO8SMQADPx29xcZhO+EMECUOA2EDfACEBliEh7jmK/NZN+IKTw==", + "license": "BSD-3-Clause", "optional": true, "dependencies": { - "@isaacs/fs-minipass": "^4.0.0", - "chownr": "^3.0.0", - "minipass": "^7.1.2", - "minizlib": "^3.1.0", - "yallist": "^5.0.0" - }, - "engines": { - "node": ">=18" + "tar8": "npm:tar@^8.1.2" } }, "node_modules/node-gyp/node_modules/which": { @@ -7938,16 +7884,6 @@ "node": "^20.17.0 || >=22.9.0" } }, - "node_modules/node-gyp/node_modules/yallist": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/yallist/-/yallist-5.0.0.tgz", - "integrity": "sha512-YgvUTfwqyc7UXVMrB+SImsVYSmTS8X/tSrtdNZMImM+n7+QTriRXyXim0mBrTXNeqzVF0KWGgHPeiyViFFrNDw==", - "license": "BlueOak-1.0.0", - "optional": true, - "engines": { - "node": ">=18" - } - }, "node_modules/node-int64": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/node-int64/-/node-int64-0.4.0.tgz", @@ -9382,28 +9318,13 @@ } }, "node_modules/sqlite3/node_modules/tar": { - "version": "7.5.24", - "resolved": "https://registry.npmjs.org/tar/-/tar-7.5.24.tgz", - "integrity": "sha512-to/OxuCk5DeVONcjjODT7aeoRq5NIPPlfPc0tNML+wLlnf6u5BcJz97Uukv3pT3jzorkFhUevylkAiBr+ZmV2w==", - "license": "BlueOak-1.0.0", + "name": "tar-stream-shim", + "version": "1.0.0", + "resolved": "file:vendor/tar-stream-shim-1.0.0.tgz", + "integrity": "sha512-+wZjAAA69cxXKASR20ipmvuWTKoNIQjw/HRdNO8SMQADPx29xcZhO+EMECUOA2EDfACEBliEh7jmK/NZN+IKTw==", + "license": "BSD-3-Clause", "dependencies": { - "@isaacs/fs-minipass": "^4.0.0", - "chownr": "^3.0.0", - "minipass": "^7.1.2", - "minizlib": "^3.1.0", - "yallist": "^5.0.0" - }, - "engines": { - "node": ">=18" - } - }, - "node_modules/sqlite3/node_modules/yallist": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/yallist/-/yallist-5.0.0.tgz", - "integrity": "sha512-YgvUTfwqyc7UXVMrB+SImsVYSmTS8X/tSrtdNZMImM+n7+QTriRXyXim0mBrTXNeqzVF0KWGgHPeiyViFFrNDw==", - "license": "BlueOak-1.0.0", - "engines": { - "node": ">=18" + "tar8": "npm:tar@^8.1.2" } }, "node_modules/stack-trace": { @@ -9702,9 +9623,9 @@ } }, "node_modules/tar": { - "version": "8.0.5", - "resolved": "https://registry.npmjs.org/tar/-/tar-8.0.5.tgz", - "integrity": "sha512-iYroSHv/iVs5Bfy7uXCZVTye64oecqEETP3jvxksDcJlf8E3hreHYyhT+Qu7b+JgniditLaQbk4ZujNAnPEX9w==", + "version": "8.1.2", + "resolved": "https://registry.npmjs.org/tar/-/tar-8.1.2.tgz", + "integrity": "sha512-yzdgDKPqRFlEYBtTJVP91hT2zyTwadRzAALlGccmGhNYucV2iJyShQLTdSAh/YY9efLmjNjkktsvAwhVg6Sifw==", "license": "BlueOak-1.0.0", "engines": { "node": ">=22" @@ -9744,6 +9665,16 @@ "node": ">=6" } }, + "node_modules/tar8": { + "name": "tar", + "version": "8.1.2", + "resolved": "https://registry.npmjs.org/tar/-/tar-8.1.2.tgz", + "integrity": "sha512-yzdgDKPqRFlEYBtTJVP91hT2zyTwadRzAALlGccmGhNYucV2iJyShQLTdSAh/YY9efLmjNjkktsvAwhVg6Sifw==", + "license": "BlueOak-1.0.0", + "engines": { + "node": ">=22" + } + }, "node_modules/test-exclude": { "version": "7.0.2", "resolved": "https://registry.npmjs.org/test-exclude/-/test-exclude-7.0.2.tgz", diff --git a/package.json b/package.json index be04d6944..cc946e1f0 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "description": "A Node.js server that provides a collection of tools to serve the FHIR ecosystem", "main": "server.js", "engines": { - "node": ">=24" + "node": "^24" }, "scripts": { "start": "node --expose-gc server.js", @@ -66,7 +66,7 @@ "qrcode-generator": "^2.0.4", "re2js": "^2.8.0", "sqlite3": "^6.0.1", - "tar": "^8.0.5", + "tar": "^8.1.2", "turndown": "^7.2.5", "winston": "^3.19.0", "winston-daily-rotate-file": "^4.7.1", @@ -114,6 +114,15 @@ "overrides": { "@istanbuljs/load-nyc-config": { "js-yaml": "^4.1.0" + }, + "sqlite3": { + "tar": "file:./vendor/tar-stream-shim-1.0.0.tgz" + }, + "@mapbox/node-pre-gyp": { + "tar": "file:./vendor/tar-stream-shim-1.0.0.tgz" + }, + "node-gyp": { + "tar": "file:./vendor/tar-stream-shim-1.0.0.tgz" } }, "bin": { @@ -128,5 +137,11 @@ "bugs": { "url": "https://github.com/HealthIntersections/fhirsmith/issues" }, - "homepage": "https://github.com/HealthIntersections/fhirsmith#readme" + "homepage": "https://github.com/HealthIntersections/fhirsmith#readme", + "allowScripts": { + "roaring": true, + "sqlite3": true, + "better-sqlite3": true, + "bcrypt": true + } } diff --git a/tests/utilities/audit-check.test.js b/tests/utilities/audit-check.test.js index 58db1a6f9..2b373dd4b 100644 --- a/tests/utilities/audit-check.test.js +++ b/tests/utilities/audit-check.test.js @@ -44,4 +44,15 @@ describe('audit-check', () => { const allow = { advisories: [{ id: 'GHSA-aaaa-bbbb-cccc' }] }; expect(check(advisories(report), allow, 'high', '2026-11-18').unused).toHaveLength(1); }); + + test('a package entry accepts every advisory against that package, and no other', () => { + const allow = { advisories: [{ package: 'glob', until: '2099-01-01' }, { package: 'tar', until: '2099-01-01' }] }; + const r = check(advisories(report), allow, 'moderate', '2026-11-18'); + expect(r.accepted.map(a => a.id)).toEqual(['GHSA-3g3m-2fjr-6pw4']); + expect(r.failures.map(a => a.id)).toEqual(['GHSA-fc5q-39xc-vjpf']); + expect(r.unused.map(e => e.package)).toEqual(['tar']); + const late = check(advisories(report), allow, 'moderate', '2099-01-02'); + expect(late.accepted).toEqual([]); + expect(late.expired.length).toBe(2); + }); }); diff --git a/utilities/audit-check.js b/utilities/audit-check.js index bbaaa3503..3428ce68e 100644 --- a/utilities/audit-check.js +++ b/utilities/audit-check.js @@ -11,7 +11,10 @@ // defaults to the one at the repository root. // // Allowlist entries: { "id": "GHSA-...", "reason": "...", "until": "YYYY-MM-DD" }. An entry -// past its date no longer applies, so it can't quietly outlive the reason for it. +// past its date no longer applies, so it can't quietly outlive the reason for it. An entry +// with "package" in place of "id" accepts every advisory against that package, for a package +// that can't be updated where it is audited: new advisories against it would fail the check +// with nothing to be done about them. const { execFileSync } = require('child_process'); const fs = require('fs'); @@ -69,6 +72,7 @@ function check(found, allowlist, level, today = new Date().toISOString().substri const min = LEVELS.indexOf(level); const entries = (allowlist && allowlist.advisories) || []; const active = new Map(); + const packages = new Map(); const expired = []; for (const e of entries) { const id = String(e.id || '').toUpperCase(); @@ -76,6 +80,8 @@ function check(found, allowlist, level, today = new Date().toISOString().substri expired.push(e); } else if (id) { active.set(id, e); + } else if (e.package) { + packages.set(e.package, e); } } const failures = []; @@ -84,14 +90,16 @@ function check(found, allowlist, level, today = new Date().toISOString().substri if (LEVELS.indexOf(a.severity) < min) { continue; } - if (active.has(a.id.toUpperCase())) { + if (active.has(a.id.toUpperCase()) || packages.has(a.name)) { accepted.push(a); } else { failures.push(a); } } const seen = new Set(found.map(a => a.id.toUpperCase())); - const unused = [...active.values()].filter(e => !seen.has(String(e.id).toUpperCase())); + const names = new Set(found.map(a => a.name)); + const unused = [...active.values()].filter(e => !seen.has(String(e.id).toUpperCase())) + .concat([...packages.values()].filter(e => !names.has(e.package))); return { failures, accepted, expired, unused }; } @@ -118,13 +126,13 @@ function main() { } const result = check(advisories(report), allowlist, args.level); for (const a of result.accepted) { - console.log(`accepted (audit-allowlist.json): ${a.id} ${a.name} ${a.range} (${a.severity}) - ${a.title}`); + console.log(`accepted (${path.basename(args.allowlist)}): ${a.id} ${a.name} ${a.range} (${a.severity}) - ${a.title}`); } for (const e of result.expired) { - console.log(`::warning::audit-allowlist.json entry ${e.id} expired on ${e.until}: it no longer applies - look at it again`); + console.log(`::warning::${path.basename(args.allowlist)} entry ${e.id || e.package} expired on ${e.until}: it no longer applies - look at it again`); } for (const e of result.unused) { - console.log(`::warning::audit-allowlist.json entry ${e.id} matches nothing any more: remove it`); + console.log(`::warning::${path.basename(args.allowlist)} entry ${e.id || e.package} matches nothing any more: remove it`); } for (const a of result.failures) { console.log(`::error::${a.id} ${a.name} ${a.range} (${a.severity}) - ${a.title} ${a.url || ''}`); diff --git a/vendor/README.md b/vendor/README.md new file mode 100644 index 000000000..fe5d4dfa3 --- /dev/null +++ b/vendor/README.md @@ -0,0 +1,69 @@ +# vendor + +## tar-stream-shim + +`sqlite3`, `node-gyp` and `@mapbox/node-pre-gyp` (the last two come with `roaring`) depend on +tar 7. They use it only while installing: to unpack a prebuilt binary, the Node headers, or the +SQLite source for a build from source. tar 7 has advisories that are fixed only in tar 8, and +none of the three has a release on tar 8. + +tar 8 can't simply be put in its place (`"overrides": { "tar": "$tar" }`), as it changed two +calls they make: + +- `tar.extract(opts)` with no `file` returned a stream to pipe an archive into. In tar 8 the + archive is the `input` option and the call returns a Promise. `node-gyp` and + `@mapbox/node-pre-gyp` pipe their download into it, so `roaring` fails to install. +- `tar.extract({ sync: true, ... })` had finished when it returned. tar 8 ignores `sync` + (the synchronous call is `extractSync`), so `sqlite3/deps/extract.js` goes on before + anything is extracted, and the `VERSION` file it means to delete stays - which breaks a + build of sqlite3 from source on a case-insensitive file system (macOS, Windows). + +`tar-stream-shim` is tar 8 with those two calls as they were. package.json `overrides` installs +it as `tar` under the three packages, so the only tar installed is tar 8. + +This holds for a clone and for the Docker image. npm doesn't apply a package's `overrides` when +the package is itself installed (`npm install fhirsmith`), so an install from the registry still +gets tar 7 under the three; `vendor/` is not in the published package. + +The override names the packed file, `tar-stream-shim-1.0.0.tgz`, not the directory: npm links a +directory named in an override from the wrong place. After changing `tar-stream-shim/`: + + cd vendor/tar-stream-shim && npm pack --pack-destination .. + +and update the three `node_modules/*/node_modules/tar` entries' `integrity` in +package-lock.json (the Install check workflow fails if the `.tgz` and the directory differ). npm (11 and 12) fails to add these entries to an existing package-lock.json +by itself (it looks for the file under the package the override is for); they were written to +a new lock file and copied in. + +### When and how to remove it + +Each of the three overrides can go as soon as its package depends on tar 8 or later. To see what +the latest releases ask for: + + npm view sqlite3 dependencies.tar + npm view node-gyp dependencies.tar + npm view @mapbox/node-pre-gyp dependencies.tar + +An override can also go when its package is no longer installed at all (`npm ls node-gyp +@mapbox/node-pre-gyp sqlite3`): `@mapbox/node-pre-gyp` is there only for `roaring`, and +`node-gyp` only for `roaring` and `sqlite3`. + +For a package that has moved to tar 8: + +1. Update to that release (for `node-gyp` and `@mapbox/node-pre-gyp` that means a release of + `roaring` or `sqlite3` that depends on it, or `npm update`). +2. Delete its entry from `overrides` in package.json and run `npm install`. +3. `npm ls tar` should show no tar older than 8 and, for that package, a `tar` from the registry + rather than `tar-stream-shim`. The Install check workflow checks the same on Linux, macOS and + Windows, and that sqlite3 still builds from source. + +When the last of the three overrides is gone, delete `vendor/tar-stream-shim/`, the `.tgz`, this +section, the `COPY vendor/...` line in the Dockerfile, `vendor/` in `.npmignore`, the "Check the +packed shim" step in `.github/workflows/install-check.yml`, and the `tar` entry in +`audit-allowlist-published.json` (the audit says when it matches nothing any more). That entry +has an expiry date so that this gets looked at again even if nobody is watching the three +packages. + +Until then nothing needs doing when tar itself has a new 8.x release: the shim depends on +`tar ^8`, and passes everything but the two calls straight through. A tar 9 would need the two +calls checked again. diff --git a/vendor/tar-stream-shim-1.0.0.tgz b/vendor/tar-stream-shim-1.0.0.tgz new file mode 100644 index 000000000..923b95f69 Binary files /dev/null and b/vendor/tar-stream-shim-1.0.0.tgz differ diff --git a/vendor/tar-stream-shim/index.js b/vendor/tar-stream-shim/index.js new file mode 100644 index 000000000..3147c1377 --- /dev/null +++ b/vendor/tar-stream-shim/index.js @@ -0,0 +1,42 @@ +// tar 8, with the two tar 7 call forms that the install scripts of our native dependencies +// still use. package.json "overrides" puts this in place of `tar` under node-gyp, +// @mapbox/node-pre-gyp and sqlite3, so that no tar 7 is installed. See vendor/README.md. +// +// 1. extract(opts) with no `file`: tar 7 returned a writable stream to pipe an archive into +// (node-gyp's header download, node-pre-gyp's binary download). tar 8 takes the archive as +// `input` and returns a Promise. +// 2. extract({sync: true, ...}): tar 7 had finished by the time it returned (sqlite3's +// deps/extract.js deletes a file from the extracted tree on the next line). tar 8 ignores +// `sync`; the synchronous form is extractSync(). +const tar = require('tar8'); +const { PassThrough, Writable, Readable } = require('stream'); + +function extract(opts = {}, ...rest) { + if (opts.sync) { + const { sync, ...others } = opts; // eslint-disable-line no-unused-vars + return tar.extractSync(others, ...rest); + } + if (opts.file || opts.f || opts.input) { + return tar.extract(opts, ...rest); + } + const archive = new PassThrough(); + archive.on('error', () => {}); // reported through the returned stream, below + const done = tar.extract({ ...opts, input: Readable.toWeb(archive) }); + const stream = new Writable({ + write: (chunk, encoding, callback) => { + if (archive.destroyed || archive.write(chunk, encoding)) { + callback(); + } else { + archive.once('drain', callback); + } + }, + final: (callback) => { archive.end(); done.then(() => callback(), callback); }, + destroy: (error, callback) => { archive.destroy(error); callback(error); } + }); + // extraction can fail before the archive has all been written (not an archive, a bad entry): + // tar stops reading then, so the writer must be told, or it waits on a drain that never comes + done.catch((error) => stream.destroy(error)); + return stream; +} + +module.exports = { ...tar, extract, x: extract }; diff --git a/vendor/tar-stream-shim/package.json b/vendor/tar-stream-shim/package.json new file mode 100644 index 000000000..23368bcbc --- /dev/null +++ b/vendor/tar-stream-shim/package.json @@ -0,0 +1,11 @@ +{ + "name": "tar-stream-shim", + "version": "1.0.0", + "description": "tar 8 with the two tar 7 call forms that node-gyp, @mapbox/node-pre-gyp and sqlite3 still use at install time", + "main": "index.js", + "license": "BSD-3-Clause", + "private": true, + "dependencies": { + "tar8": "npm:tar@^8.1.2" + } +}