build: add a license gate for shipped npm packages and Rust crates (P2.3)
scripts/check-licenses.mjs (npm run check:licenses) reads the production dependency closures of the sidecar (@waggle/server minus the packages stage-sidecar-deps.mjs never ships) and the web UI (apps/web) from package-lock.json and fails on GPL, AGPL, SSPL, missing and unknown licenses. It allows permissive licenses plus MPL-2.0 and lists named exceptions with reasons; LGPL libvips and node-webpmux, the OFL fonts and the unlicensed buffers package are marked as pending the licensing review. app/src-tauri/deny.toml applies the same allowlist to the Tauri crates with cargo-deny; the npm check fails when the two lists drift apart. A new CI job runs both, with cargo-deny 0.18.3 installed and cached in CI. scripts/license-inventory.mjs holds the shared closure logic.
This commit is contained in:
287
scripts/check-licenses.mjs
Normal file
287
scripts/check-licenses.mjs
Normal file
@@ -0,0 +1,287 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* License gate for the npm packages that ship in the community edition.
|
||||
*
|
||||
* Reads the production dependency closures of the sidecar and the web UI from
|
||||
* package-lock.json (see scripts/license-inventory.mjs), so it needs no
|
||||
* `npm install`. Every package must declare a license expression that the
|
||||
* allowlist satisfies, or match a named exception below. GPL, AGPL, SSPL,
|
||||
* missing and unknown licenses fail.
|
||||
*
|
||||
* The Rust crates of the desktop shell are checked by cargo-deny with the same
|
||||
* allowlist (app/src-tauri/deny.toml); this script fails when the two lists
|
||||
* drift apart. Node.js and the npm CLI that bundle-node.mjs downloads are not
|
||||
* in package-lock.json and are not checked here (THIRD_PARTY_NOTICES.md lists them).
|
||||
*
|
||||
* Usage: node scripts/check-licenses.mjs [--verbose]
|
||||
*/
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { pathToFileURL } from 'node:url';
|
||||
import { ROOT, npmArtifacts, npmClosure, readLockfilePackages, uniqueVersions } from './license-inventory.mjs';
|
||||
|
||||
/** SPDX identifiers that may ship in an MIT-licensed distribution. Keep in sync with app/src-tauri/deny.toml. */
|
||||
export const ALLOWED_LICENSES = [
|
||||
'0BSD',
|
||||
'Apache-2.0',
|
||||
'BSD-2-Clause',
|
||||
'BSD-3-Clause',
|
||||
'BlueOak-1.0.0',
|
||||
'CC0-1.0',
|
||||
'ISC',
|
||||
'MIT',
|
||||
'MPL-2.0',
|
||||
'Python-2.0',
|
||||
'Unicode-3.0',
|
||||
'Unlicense',
|
||||
'Zlib',
|
||||
];
|
||||
|
||||
/**
|
||||
* Named exceptions. An exception applies only while the package still declares
|
||||
* exactly `license` in package-lock.json (null: no license declared), so a
|
||||
* license change needs a new review. `pending` marks exceptions that wait for
|
||||
* the licensing review (pending decision Q6); the gate prints them on every run.
|
||||
*/
|
||||
export const EXCEPTIONS = [
|
||||
{
|
||||
packages: ['@img/sharp-libvips-*'],
|
||||
license: 'LGPL-3.0-or-later',
|
||||
pending: true,
|
||||
reason: 'Prebuilt libvips for sharp, required by @huggingface/transformers (embeddings). '
|
||||
+ 'Shipped unmodified as a separate shared library in resources/node_modules and loaded '
|
||||
+ 'at runtime; LGPL obligations are listed in THIRD_PARTY_NOTICES.md.',
|
||||
},
|
||||
{
|
||||
packages: ['@img/sharp-win32-x64'],
|
||||
license: 'Apache-2.0 AND LGPL-3.0-or-later',
|
||||
pending: true,
|
||||
reason: 'sharp binding for Windows; the package carries the libvips DLLs (LGPL-3.0-or-later). '
|
||||
+ 'Shipped unmodified in resources/node_modules, same terms as @img/sharp-libvips-*.',
|
||||
},
|
||||
{
|
||||
packages: ['node-webpmux'],
|
||||
license: 'LGPL-3.0-or-later',
|
||||
pending: true,
|
||||
reason: 'Required dependency of whatsapp-web.js (ADR-0005). Kept external to service.js and '
|
||||
+ 'shipped unmodified as a separate package in resources/node_modules.',
|
||||
},
|
||||
{
|
||||
packages: ['@fontsource-variable/hanken-grotesk', '@fontsource-variable/jetbrains-mono'],
|
||||
license: 'OFL-1.1',
|
||||
pending: true,
|
||||
reason: 'Fonts bundled into the web UI. OFL-1.1 allows bundling with software if the '
|
||||
+ 'license ships with the fonts and the fonts are not sold on their own.',
|
||||
},
|
||||
{
|
||||
packages: ['buffers'],
|
||||
license: null,
|
||||
pending: true,
|
||||
reason: 'buffers 0.1.1 (exceljs > unzipper > binary > buffers) has no license field, file or '
|
||||
+ 'README statement. Unlicensed code; needs a decision or a dependency change.',
|
||||
},
|
||||
{
|
||||
packages: ['posthog-js'],
|
||||
license: 'SEE LICENSE IN LICENSE',
|
||||
reason: 'The LICENSE file is Apache-2.0, with MIT for files taken from Sentry, Metro and Expo.',
|
||||
},
|
||||
{
|
||||
packages: ['duck'],
|
||||
license: 'BSD',
|
||||
reason: 'The LICENSE file is the BSD-2-Clause text.',
|
||||
},
|
||||
{
|
||||
packages: ['chainsaw', 'traverse'],
|
||||
license: 'MIT/X11',
|
||||
reason: '"MIT/X11" is the MIT license (traverse ships the MIT text; chainsaw has no license file).',
|
||||
},
|
||||
{
|
||||
packages: ['png-js'],
|
||||
license: null,
|
||||
reason: 'No license field; the LICENSE file is the MIT text.',
|
||||
},
|
||||
{
|
||||
packages: ['async'],
|
||||
license: null,
|
||||
reason: 'async 0.2.10 (whatsapp-web.js > fluent-ffmpeg > async) declares MIT in the legacy '
|
||||
+ '"licenses" field and ships the MIT text.',
|
||||
},
|
||||
];
|
||||
|
||||
const COPYLEFT = /^(?:A?GPL|LGPL|SSPL|EUPL|OSL|CC-BY-SA|CPAL)/i;
|
||||
const LICENSE_ID = /^(?:[A-Za-z0-9][A-Za-z0-9.+-]*|LicenseRef-[A-Za-z0-9.-]+)$/;
|
||||
|
||||
/** Parse an SPDX license expression. Throws on anything that is not one. */
|
||||
export function parseLicenseExpression(expression) {
|
||||
const tokens = expression.replace(/[()]/g, ' $& ').trim().split(/\s+/).filter(Boolean);
|
||||
let position = 0;
|
||||
const peek = () => tokens[position];
|
||||
const isOperator = (token, operator) => token !== undefined && token.toUpperCase() === operator;
|
||||
|
||||
function parseAtom() {
|
||||
const token = tokens[position++];
|
||||
if (token === '(') {
|
||||
const inner = parseOr();
|
||||
if (tokens[position++] !== ')') throw new Error(`missing ")" in "${expression}"`);
|
||||
return inner;
|
||||
}
|
||||
if (token === undefined || !LICENSE_ID.test(token) || ['AND', 'OR', 'WITH'].includes(token.toUpperCase())) {
|
||||
throw new Error(`"${expression}" is not an SPDX expression`);
|
||||
}
|
||||
if (isOperator(peek(), 'WITH')) {
|
||||
position++;
|
||||
const exception = tokens[position++];
|
||||
if (!exception || !LICENSE_ID.test(exception)) throw new Error(`bad WITH clause in "${expression}"`);
|
||||
return { license: token, exception };
|
||||
}
|
||||
return { license: token };
|
||||
}
|
||||
function parseAnd() {
|
||||
let left = parseAtom();
|
||||
while (isOperator(peek(), 'AND')) {
|
||||
position++;
|
||||
left = { and: [left, parseAtom()] };
|
||||
}
|
||||
return left;
|
||||
}
|
||||
function parseOr() {
|
||||
let left = parseAnd();
|
||||
while (isOperator(peek(), 'OR')) {
|
||||
position++;
|
||||
left = { or: [left, parseAnd()] };
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
const tree = parseOr();
|
||||
if (position !== tokens.length) throw new Error(`"${expression}" is not an SPDX expression`);
|
||||
return tree;
|
||||
}
|
||||
|
||||
function satisfied(node, allowed) {
|
||||
if (node.or) return node.or.some((child) => satisfied(child, allowed));
|
||||
if (node.and) return node.and.every((child) => satisfied(child, allowed));
|
||||
// An exception only adds permissions, so an allowed license stays allowed with it.
|
||||
return allowed.has(node.license) || allowed.has(`${node.license} WITH ${node.exception}`);
|
||||
}
|
||||
|
||||
function leaves(node) {
|
||||
if (node.or) return node.or.flatMap(leaves);
|
||||
if (node.and) return node.and.flatMap(leaves);
|
||||
return [node.license];
|
||||
}
|
||||
|
||||
/**
|
||||
* Classify one declared license.
|
||||
* Returns { status: 'allowed' | 'missing' | 'invalid' | 'denied', copyleft, alternatives }.
|
||||
*/
|
||||
export function evaluateLicense(expression, allowed = new Set(ALLOWED_LICENSES)) {
|
||||
if (expression === null || expression === undefined || expression.trim() === '') {
|
||||
return { status: 'missing', copyleft: false, alternatives: [] };
|
||||
}
|
||||
let tree;
|
||||
try {
|
||||
tree = parseLicenseExpression(expression);
|
||||
} catch {
|
||||
return { status: 'invalid', copyleft: false, alternatives: [] };
|
||||
}
|
||||
const ids = leaves(tree);
|
||||
const copyleft = ids.some((id) => COPYLEFT.test(id));
|
||||
if (satisfied(tree, allowed)) {
|
||||
// Dual-licensed packages pass on their permissive option; report the rest.
|
||||
return { status: 'allowed', copyleft, alternatives: ids.filter((id) => !allowed.has(id)) };
|
||||
}
|
||||
return { status: 'denied', copyleft, alternatives: [] };
|
||||
}
|
||||
|
||||
function matchesPattern(name, pattern) {
|
||||
return pattern.endsWith('*') ? name.startsWith(pattern.slice(0, -1)) : name === pattern;
|
||||
}
|
||||
|
||||
export function findException(pkg, exceptions = EXCEPTIONS) {
|
||||
return exceptions.find(
|
||||
(exception) => exception.license === pkg.license
|
||||
&& exception.packages.some((pattern) => matchesPattern(pkg.name, pattern)),
|
||||
);
|
||||
}
|
||||
|
||||
/** The `allow` list of the [licenses] table in a cargo-deny config. */
|
||||
export function readDenyAllowList(denyToml) {
|
||||
const section = /^\[licenses\]\s*$([\s\S]*?)(?=^\[)/m.exec(`${denyToml}\n[`);
|
||||
const allow = section && /^allow\s*=\s*\[([\s\S]*?)\]/m.exec(section[1]);
|
||||
if (!allow) throw new Error('app/src-tauri/deny.toml has no [licenses] allow list');
|
||||
return [...allow[1].replace(/#.*$/gm, '').matchAll(/"([^"]+)"/g)].map((match) => match[1]);
|
||||
}
|
||||
|
||||
function main() {
|
||||
const verbose = process.argv.includes('--verbose');
|
||||
const packages = readLockfilePackages();
|
||||
const failures = [];
|
||||
const usedExceptions = new Map();
|
||||
const dualLicensed = new Map();
|
||||
|
||||
for (const artifact of npmArtifacts()) {
|
||||
const { packages: closure, unresolved } = npmClosure(packages, artifact);
|
||||
for (const missing of unresolved) {
|
||||
failures.push(`${artifact.title}: required dependency is not in package-lock.json: ${missing}`);
|
||||
}
|
||||
const shipped = uniqueVersions(closure.values());
|
||||
for (const pkg of shipped) {
|
||||
const result = evaluateLicense(pkg.license);
|
||||
const id = `${pkg.name}@${pkg.version}`;
|
||||
if (result.status === 'allowed') {
|
||||
if (result.alternatives.length > 0) dualLicensed.set(id, pkg.license);
|
||||
continue;
|
||||
}
|
||||
const exception = findException(pkg);
|
||||
if (exception) {
|
||||
if (!usedExceptions.has(exception)) usedExceptions.set(exception, new Set());
|
||||
usedExceptions.get(exception).add(`${id} (${artifact.title})`);
|
||||
continue;
|
||||
}
|
||||
const label = {
|
||||
missing: 'no license declared',
|
||||
invalid: `not an SPDX expression: "${pkg.license}"`,
|
||||
denied: `${pkg.license}${result.copyleft ? ' (copyleft)' : ''} is not on the allowlist`,
|
||||
}[result.status];
|
||||
failures.push(`${artifact.title}: ${id}: ${label}\n path: ${pkg.via.join(' > ')}`);
|
||||
}
|
||||
if (verbose) console.log(`[check-licenses] ${artifact.title}: ${shipped.length} third-party packages`);
|
||||
}
|
||||
|
||||
const denyPath = path.join(ROOT, 'app', 'src-tauri', 'deny.toml');
|
||||
const denyAllow = readDenyAllowList(fs.readFileSync(denyPath, 'utf8'));
|
||||
const missingInDeny = ALLOWED_LICENSES.filter((id) => !denyAllow.includes(id));
|
||||
const extraInDeny = denyAllow.filter((id) => !ALLOWED_LICENSES.includes(id));
|
||||
if (missingInDeny.length > 0 || extraInDeny.length > 0) {
|
||||
failures.push(
|
||||
'app/src-tauri/deny.toml allow list differs from ALLOWED_LICENSES'
|
||||
+ `${missingInDeny.length ? `; missing: ${missingInDeny.join(', ')}` : ''}`
|
||||
+ `${extraInDeny.length ? `; extra: ${extraInDeny.join(', ')}` : ''}`,
|
||||
);
|
||||
}
|
||||
|
||||
for (const [exception, hits] of usedExceptions) {
|
||||
if (!exception.pending && !verbose) continue;
|
||||
const tag = exception.pending ? 'pending licensing review' : 'exception';
|
||||
console.warn(`[check-licenses] ${tag}: ${exception.license ?? 'no license'}: ${[...hits].sort().join(', ')}`);
|
||||
console.warn(` ${exception.reason}`);
|
||||
}
|
||||
for (const exception of EXCEPTIONS.filter((entry) => !usedExceptions.has(entry))) {
|
||||
console.warn(`[check-licenses] unused exception for ${exception.packages.join(', ')}; remove it`);
|
||||
}
|
||||
if (verbose) {
|
||||
for (const [id, license] of [...dualLicensed].sort()) {
|
||||
console.log(`[check-licenses] dual-licensed, used under the allowed option: ${id} (${license})`);
|
||||
}
|
||||
}
|
||||
|
||||
if (failures.length > 0) {
|
||||
console.error(`[check-licenses] FAILED: ${failures.length} problem(s)`);
|
||||
for (const failure of failures) console.error(` - ${failure}`);
|
||||
process.exit(1);
|
||||
}
|
||||
console.log('[check-licenses] OK: every shipped npm package is allowed or a named exception');
|
||||
}
|
||||
|
||||
if (import.meta.url === pathToFileURL(process.argv[1] ?? '').href) main();
|
||||
Reference in New Issue
Block a user