Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion plugins/motrix.filename-template/motrix-plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ allowBuilds:

minimumReleaseAgeExclude:
- '@motrix/plugin-api@2.0.0'
- '@motrix/plugin-manifest-schema@2.0.0'
5 changes: 5 additions & 0 deletions scripts/pack.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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(
Expand Down
72 changes: 72 additions & 0 deletions scripts/validate-manifest.mjs
Original file line number Diff line number Diff line change
@@ -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 ?? '<unknown>') {
let parsed
try {
parsed = ManifestSchema.parse(manifest)
} catch (err) {
const detail = Array.isArray(err?.issues)
? err.issues
.map((i) => `${i.path.join('.') || '<root>'}: ${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
}
17 changes: 17 additions & 0 deletions tests/fixtures/hooks-without-host-permissions.json
Original file line number Diff line number Diff line change
@@ -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" }
}
}
}
14 changes: 14 additions & 0 deletions tests/fixtures/structurally-invalid.json
Original file line number Diff line number Diff line change
@@ -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": {}
}
44 changes: 44 additions & 0 deletions tests/validate-manifest.test.mjs
Original file line number Diff line number Diff line change
@@ -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()
}
})
})