diff --git a/package.json b/package.json index 1660a28..ca42e12 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "devDependencies": { "@biomejs/biome": "^2.5.4", "@motrix/plugin-api": "^2.0.0", + "@motrix/plugin-manifest-schema": "^2.0.0", "esbuild": "^0.24.2", "vitest": "^3", "yauzl": "^3", diff --git a/plugins/motrix.filename-template/motrix-plugin.json b/plugins/motrix.filename-template/motrix-plugin.json index 1566a7b..dfda85f 100644 --- a/plugins/motrix.filename-template/motrix-plugin.json +++ b/plugins/motrix.filename-template/motrix-plugin.json @@ -3,13 +3,14 @@ "manifestVersion": 1, "id": "motrix.filename-template", "name": "%name%", - "version": "1.1.0", + "version": "1.1.1", "description": "%description%", "categories": ["post-action"], "engines": { "motrix": ">=2.0.0 <3.0.0" }, "main": "dist/plugin.js", "requestedHeapMB": 32, "permissions": ["fs.task.write"], + "hostPermissions": ["*://*/*"], "activationEvents": ["onTaskType:http", "onTaskType:ftp", "onTaskType:bt"], "contributes": { "commands": [ diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7fbff65..2772435 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: '@motrix/plugin-api': specifier: ^2.0.0 version: 2.0.0 + '@motrix/plugin-manifest-schema': + specifier: ^2.0.0 + version: 2.0.0 esbuild: specifier: ^0.24.2 version: 0.24.2 @@ -434,6 +437,9 @@ packages: '@motrix/plugin-api@2.0.0': resolution: {integrity: sha512-S4YsMvAo9ehDN8R6jwQAzcTHBstEFH1kPw8CRePMVLSNT0pb+5jlaQdBXd1NfsxY9O4FFX4e63uuAYWlkD9Urw==} + '@motrix/plugin-manifest-schema@2.0.0': + resolution: {integrity: sha512-//RCfj9K5V3moqRUWsDzEEiC+6TSayJFCXPe2s9uj7PkdwC/Z3nvBN21SlxPfzvBI/VVr1kiRilhd28ldYfUHA==} + '@rollup/rollup-android-arm-eabi@4.62.2': resolution: {integrity: sha512-6o7ZLZK+BeenkZCFNDXqpbjw9bD6nuWonvS/lwQJp7NoVVxm6p3qE7qQ5jGuBjiFsgvqjD8mZAU5oWxTmbOeOg==} cpu: [arm] @@ -847,6 +853,9 @@ packages: yazl@2.5.1: resolution: {integrity: sha512-phENi2PLiHnHb6QBVot+dJnaAZ0xosj7p3fWl+znIjBDlnMI2PsZCJZ306BPTFOaHf5qdDEI8x5qFrSOBN5vrw==} + zod@4.4.3: + resolution: {integrity: sha512-ytENFjIJFl2UwYglde2jchW2Hwm4GJFLDiSXWdTrJQBIN9Fcyp7n4DhxJEiWNAJMV1/BqWfW/kkg71UDcHJyTQ==} + snapshots: '@biomejs/biome@2.5.4': @@ -1041,6 +1050,10 @@ snapshots: '@motrix/plugin-api@2.0.0': {} + '@motrix/plugin-manifest-schema@2.0.0': + dependencies: + zod: 4.4.3 + '@rollup/rollup-android-arm-eabi@4.62.2': optional: true @@ -1432,3 +1445,5 @@ snapshots: yazl@2.5.1: dependencies: buffer-crc32: 0.2.13 + + zod@4.4.3: {} diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 4834e25..b931c32 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -7,3 +7,4 @@ allowBuilds: minimumReleaseAgeExclude: - '@motrix/plugin-api@2.0.0' + - '@motrix/plugin-manifest-schema@2.0.0' diff --git a/scripts/pack.mjs b/scripts/pack.mjs index 93e35a7..63dc2a2 100644 --- a/scripts/pack.mjs +++ b/scripts/pack.mjs @@ -26,6 +26,7 @@ import { import path from 'node:path' import { fileURLToPath } from 'node:url' import yazl from 'yazl' +import { validateManifest } from './validate-manifest.mjs' const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..') const PLUGINS = path.join(ROOT, 'plugins') @@ -108,6 +109,10 @@ export async function packOne(id) { if (manifest.id !== id) { throw new Error(`${id}: manifest.id "${manifest.id}" != directory name`) } + // Full manifest validation BEFORE any artifact bytes are written, so an + // invalid manifest (e.g. hooks without hostPermissions, which the host + // rejects at install) can never be packed, signed, or released. + validateManifest(manifest, id) const bundle = await stat(path.join(staged, 'dist', 'plugin.js')) if (bundle.size > BUNDLE_MAX) { throw new Error( diff --git a/scripts/validate-manifest.mjs b/scripts/validate-manifest.mjs new file mode 100644 index 0000000..8751772 --- /dev/null +++ b/scripts/validate-manifest.mjs @@ -0,0 +1,72 @@ +// Pack-time manifest gate. +// +// motrix-turbo's host rejects a plugin at manifest-parse time if the manifest +// violates the manifest schema OR the cross-field invariant "a manifest that +// declares contributes.hooks must declare at least one hostPermissions entry". +// That invariant escaped to a signed release once (motrix.filename-template@ +// 1.1.0 shipped hooks with no hostPermissions and was uninstallable on every +// host) because pack.mjs only checked manifest.id vs the directory name and ran +// no real validation. This module is the gate pack.mjs now runs before any +// .moext is produced, so an uninstallable manifest can never be packed, signed, +// or released again. +// +// Validation is composed from the published plugin SDK (@^2.0.0): +// +// 1. @motrix/plugin-manifest-schema's ManifestSchema.parse() — the single +// source of truth for manifest structure (field shapes, id/version +// grammar, hostPermissions match-pattern grammar, bounded config schemas, +// hook roles, ...). Note it validates hook *roles* only; pre-resolve is a +// valid role here because these are origin: 'builtin' plugins (the +// builtin-only eligibility of pre-resolve is a host-side rule, not a +// schema rule, so schema parse accepts it). +// +// 2. the hooks => hostPermissions invariant, which the schema deliberately +// does NOT encode — it is a cross-field rule the host applies in its own +// parse.ts (see the HookContributionSchema comment in +// @motrix/plugin-manifest-schema, which states cross-field hook rules live +// in parse.ts, not the schema). We replicate the exact rule and canonical +// error code from @motrix/plugin-cli's `validate-host-permissions` +// command. That check cannot be imported: @motrix/plugin-cli publishes a +// bin-only bundle (its commands are inlined into dist/bin, and the package +// exposes no main/exports), so the logic is reproduced here rather than +// linked. + +import { ManifestSchema } from '@motrix/plugin-manifest-schema' + +export const HOST_PERMISSIONS_REQUIRED_FOR_HOOKS = + 'plugin.manifest.host_permissions_required_for_hooks' + +/** + * Validate a parsed motrix-plugin.json object. Throws an Error with a + * human-readable, id-prefixed message on the first violation; returns the + * schema-parsed manifest on success. + * + * @param {unknown} manifest parsed motrix-plugin.json contents + * @param {string} [id] plugin id, used only to prefix error messages + */ +export function validateManifest(manifest, id = manifest?.id ?? '') { + let parsed + try { + parsed = ManifestSchema.parse(manifest) + } catch (err) { + const detail = Array.isArray(err?.issues) + ? err.issues + .map((i) => `${i.path.join('.') || ''}: ${i.message}`) + .join('; ') + : (err?.message ?? String(err)) + throw new Error(`${id}: invalid motrix-plugin.json — ${detail}`) + } + + const hooks = parsed.contributes?.hooks + const hasHooks = hooks != null && Object.keys(hooks).length > 0 + const hostPermissions = Array.isArray(parsed.hostPermissions) + ? parsed.hostPermissions + : [] + if (hasHooks && hostPermissions.length === 0) { + throw new Error( + `${id}: ${HOST_PERMISSIONS_REQUIRED_FOR_HOOKS}: declared hooks require at least one hostPermissions entry` + ) + } + + return parsed +} diff --git a/tests/fixtures/hooks-without-host-permissions.json b/tests/fixtures/hooks-without-host-permissions.json new file mode 100644 index 0000000..1279ac0 --- /dev/null +++ b/tests/fixtures/hooks-without-host-permissions.json @@ -0,0 +1,17 @@ +{ + "manifestVersion": 1, + "id": "motrix.fixture-hook", + "name": "Fixture Hook Plugin", + "version": "1.0.0", + "description": "Fixture: declares a hook but no hostPermissions. Otherwise a fully valid manifest, so the only violation is the hooks => hostPermissions invariant.", + "categories": ["site-resolver"], + "engines": { "motrix": ">=2.0.0 <3.0.0" }, + "main": "dist/plugin.js", + "permissions": ["http"], + "activationEvents": ["onTaskType:http"], + "contributes": { + "hooks": { + "beforeCreate": { "role": "pre-resolve" } + } + } +} diff --git a/tests/fixtures/structurally-invalid.json b/tests/fixtures/structurally-invalid.json new file mode 100644 index 0000000..03784e2 --- /dev/null +++ b/tests/fixtures/structurally-invalid.json @@ -0,0 +1,14 @@ +{ + "manifestVersion": 1, + "id": "motrix.fixture-bad-version", + "name": "Fixture Bad Version", + "version": "not-a-semver", + "description": "Fixture: version does not match the manifest schema's semver grammar, so ManifestSchema.parse must reject it before the invariant check ever runs.", + "categories": ["site-resolver"], + "engines": { "motrix": ">=2.0.0 <3.0.0" }, + "main": "dist/plugin.js", + "permissions": ["http"], + "hostPermissions": ["*://*/*"], + "activationEvents": ["onTaskType:http"], + "contributes": {} +} diff --git a/tests/validate-manifest.test.mjs b/tests/validate-manifest.test.mjs new file mode 100644 index 0000000..f2a271d --- /dev/null +++ b/tests/validate-manifest.test.mjs @@ -0,0 +1,44 @@ +import { readFileSync } from 'node:fs' +import path from 'node:path' +import { describe, expect, it } from 'vitest' +import { + HOST_PERMISSIONS_REQUIRED_FOR_HOOKS, + validateManifest, +} from '../scripts/validate-manifest.mjs' + +const ROOT = path.resolve(import.meta.dirname, '..') +const FIXTURES = path.join(import.meta.dirname, 'fixtures') +const PLUGINS = path.join(ROOT, 'plugins') + +function readJson(file) { + return JSON.parse(readFileSync(file, 'utf8')) +} + +describe('validateManifest (pack-time gate)', () => { + it('rejects a manifest that declares hooks but no hostPermissions', () => { + const manifest = readJson( + path.join(FIXTURES, 'hooks-without-host-permissions.json') + ) + expect(() => validateManifest(manifest, manifest.id)).toThrow( + new RegExp(HOST_PERMISSIONS_REQUIRED_FOR_HOOKS.replace(/\./g, '\\.')) + ) + }) + + it('rejects a structurally invalid manifest before the invariant check', () => { + const manifest = readJson(path.join(FIXTURES, 'structurally-invalid.json')) + expect(() => validateManifest(manifest, manifest.id)).toThrow( + /invalid motrix-plugin\.json/ + ) + }) + + it('accepts all three real builtin manifests', () => { + for (const id of [ + 'motrix.filename-template', + 'motrix.scraper-hook', + 'motrix.url-resolver', + ]) { + const manifest = readJson(path.join(PLUGINS, id, 'motrix-plugin.json')) + expect(() => validateManifest(manifest, id)).not.toThrow() + } + }) +})