/** * Every gate that is a pure function of the working tree. * * This is most of them. See ./types.ts for why that matters: the registry grades * `trek-plugin.json` and `README.md` at a pinned commit, but those are the same two files * the author has open right now, so there is no reason to make them push a tag to find out. * * Each check answers ONE question and says what to do about it. The `fix` text is the whole * point — a check that reports "README invalid" and stops has told the author nothing they * can act on. */ import { createHash } from 'node:crypto'; import path from 'node:path'; import { validateManifest, isSatisfiableRange, isUnboundedRange, KNOWN_ADDONS, SETTING_FIELD_KEYS } from '../../manifest.js'; // The hosts a plugin can ACTUALLY reach. See `code.egress-reachable` for why this is the // only list that counts — and permissions.ts for why dev's egress guard uses the same one. import { grantedHosts } from '../../permissions.js'; import { LUCIDE_ICON_NAMES } from '../../lucide-icon-names.js'; import { listZipNames } from '../../zip.js'; import { checkSignatureShape } from '../verify-signature.js'; import type { OfflineCheck, CheckContext } from './types.js'; import { pass, fail, skip } from './types.js'; import { REQUIRED_SECTIONS, MIN_PROSE_CHARS, missingSections, placeholders, proseLength, undocumentedPermissions, } from './readme.js'; /** Mirrors the registry's native-binary scan (validate-entry.mjs) and the installer's. */ const NATIVE_RE = /(^|\/)[^/]+\.node$|(^|\/)binding\.gyp$|(^|\/)prebuilds?\//i; const str = (v: unknown): string => (typeof v === 'string' ? v : ''); const arr = (v: unknown): string[] => (Array.isArray(v) ? v.map(String) : []); // ── manifest ──────────────────────────────────────────────────────────────────── const manifestPresent: OfflineCheck = { id: 'manifest.present', blocks: 'artifact', stage: 'manifest', depth: 'offline', severity: 'error', title: 'trek-plugin.json exists and parses', run: (c) => c.manifest ? pass() : fail( c.manifestError ?? 'no trek-plugin.json', 'Every plugin is rooted at a trek-plugin.json manifest. Scaffold one rather than writing it by hand.', 'trek-plugin create', ), }; const manifestValid: OfflineCheck = { id: 'manifest.valid', blocks: 'artifact', stage: 'manifest', depth: 'offline', severity: 'error', title: 'Manifest passes the same rules the TREK installer applies', run: (c) => { if (!c.manifest) return skip(); const r = validateManifest(c.manifest); if (r.ok) return pass(); return fail( `${r.errors.length} problem${r.errors.length === 1 ? '' : 's'}`, r.errors.map((e) => '• ' + e).join('\n'), ); }, }; const manifestIdMatchesDir: OfflineCheck = { id: 'manifest.id-matches-dir', stage: 'manifest', depth: 'offline', severity: 'warn', title: 'Directory name matches the plugin id', run: (c) => { const id = str(c.manifest?.id); if (!id) return skip(); const dirName = path.basename(c.dir); return dirName === id ? pass() : fail( `directory is "${dirName}", id is "${id}"`, 'Not fatal, but the registry files your entry under the id, and every doc assumes the two agree.', ); }, }; const manifestTrekRange: OfflineCheck = { id: 'manifest.trek-range', stage: 'manifest', depth: 'offline', severity: 'warn', title: 'TREK version range is bounded', run: (c) => { const trek = str(c.manifest?.trek); // Unsatisfiable/missing is an ERROR, but manifest.valid already reports it — don't say it twice. if (!isSatisfiableRange(trek)) return skip(); return isUnboundedRange(trek) ? fail( `trek: "${trek}"`, 'This claims support for every TREK version, including ones that do not exist yet. It opts you out of the\n' + 'one mechanism that stops your plugin running on a host it was never tested against. Pin a range like ">=4.0.0 <5.0.0".', ) : pass(trek); }, }; const manifestIcon: OfflineCheck = { id: 'manifest.icon', stage: 'manifest', depth: 'offline', severity: 'error', title: 'Icon is a real lucide name', run: (c) => { if (!c.manifest) return skip(); const icon = str(c.manifest.icon); // Absent is LEGAL — TREK falls back to Blocks and CI accepts it. But every plugin without // an icon is the same grey square in the store, so say so. Warn-shaped, reported as a pass // with a caveat rather than a failure, because refusing to publish over it would be absurd. if (!icon) return pass('none — TREK will show the generic Blocks glyph'); return LUCIDE_ICON_NAMES.has(icon) ? pass(icon) : fail( `"${icon}" is not a lucide icon`, 'TREK resolves the icon against lucide at render time and silently falls back to Blocks, so a typo is\n' + 'invisible locally — but the registry rejects it. Pick a name from https://lucide.dev/icons (PascalCase).', ); }, }; /** * The registry entry's schema, applied to the manifest fields it is built from. * * These are cheap string-length rules, and every one of them is a gate that today fails in CI * — AFTER the release is immutable — because nothing local looks at them. `entry.ts` copies * `description` straight out of the manifest and defaults it to `""`, which trips the schema's * minLength of 5 with an error that names the ENTRY, not the manifest that caused it. */ const manifestEntryFields: OfflineCheck = { id: 'manifest.entry-fields', stage: 'manifest', depth: 'offline', severity: 'error', title: 'name/description/author fit the registry schema', run: (c) => { if (!c.manifest) return skip(); const problems: string[] = []; const name = str(c.manifest.name); const description = str(c.manifest.description); const author = str(c.manifest.author); if (name && (name.length < 2 || name.length > 60)) problems.push(`• name is ${name.length} chars (registry allows 2–60)`); if (!description) problems.push('• description is missing — the registry requires one (5–200 chars); it is your store-card subtitle'); else if (description.length < 5) problems.push(`• description is ${description.length} chars (registry requires at least 5)`); else if (description.length > 200) problems.push(`• description is ${description.length} chars (registry allows at most 200)`); if (!author) problems.push('• author is missing — the entry would be published as "Unknown"'); else if (author.length > 80) problems.push(`• author is ${author.length} chars (registry allows at most 80)`); // Reject a name that mixes Latin with Cyrillic/Greek look-alikes — the registry treats this // as a spoofing attempt (a plugin that looks like someone else's in the store). if (name && /[A-Za-z]/.test(name) && /[Ѐ-ӿͰ-Ͽ]/.test(name)) { problems.push(`• name "${name}" mixes Latin with Cyrillic/Greek characters — the registry reads that as a homoglyph spoof`); } // `tags` and `homepage` are copied VERBATIM out of the manifest into the entry (entry.ts), // and the registry's JSON schema validates both. Nothing local looked at either, so a tag with // a capital or a space — the obvious thing to write — produced an entry CI rejected, after the // release was cut and its bytes pinned. const tags = Array.isArray(c.manifest.tags) ? c.manifest.tags.map(String) : []; for (const t of tags) { if (!/^[a-z0-9-]{2,24}$/.test(t)) { problems.push(`• tag "${t}" is not a lowercase slug of 2–24 chars (the registry's schema rejects it)`); } } if (tags.length > 8) problems.push(`• ${tags.length} tags — the registry allows at most 8`); const homepage = str(c.manifest.homepage); if (homepage) { try { // The schema demands format:uri, which in practice means an absolute URL. const u = new URL(homepage); if (!u.protocol) throw new Error('no protocol'); } catch { problems.push(`• homepage "${homepage}" is not an absolute URL (the registry's schema requires one, e.g. https://…)`); } } return problems.length ? fail(`${problems.length} problem${problems.length === 1 ? '' : 's'}`, problems.join('\n')) : pass(); }, }; const manifestAddonsKnown: OfflineCheck = { id: 'manifest.addons-known', stage: 'manifest', depth: 'offline', severity: 'warn', title: 'requiredAddons are addons TREK has', run: (c) => { const addons = arr(c.manifest?.requiredAddons); if (!addons.length) return skip(); const unknown = addons.filter((a) => !KNOWN_ADDONS.includes(a)); return unknown.length ? fail( unknown.join(', '), `Not necessarily wrong — you may be targeting a newer TREK than this SDK knows about — but an addon\nTREK does not have can never be enabled, so the plugin would never activate. Known: ${KNOWN_ADDONS.join(', ')}.`, ) : pass(addons.join(', ')); }, }; const manifestSettingsKnownKeys: OfflineCheck = { id: 'manifest.settings-known-keys', stage: 'manifest', depth: 'offline', severity: 'warn', title: 'settings fields carry only attributes the SDK knows', run: (c) => { const settings = c.manifest?.settings; const fields = (Array.isArray(settings) ? settings : []).filter( (s): s is Record => !!s && typeof s === 'object', ); if (!fields.length) return skip(); const unknown: string[] = []; for (const f of fields) { for (const k of Object.keys(f)) { if (!(SETTING_FIELD_KEYS as readonly string[]).includes(k)) unknown.push(`${String(f.key ?? '?')}.${k}`); } } return unknown.length ? fail( unknown.join(', '), `TREK stores only the attributes it knows and SILENTLY DROPS the rest at install — a typo ("defalt")\nor an attribute from a newer SDK simply does nothing, with no diagnostic anywhere.\nKnown: ${SETTING_FIELD_KEYS.join(', ')}.`, ) : pass(`${fields.length} field(s) clean`); }, }; const manifestNoEmoji: OfflineCheck = { id: 'manifest.no-emoji', stage: 'manifest', depth: 'offline', severity: 'warn', title: 'No emojis in name/description', run: (c) => { if (!c.manifest) return skip(); const EMOJI_RE = /[\p{Extended_Pictographic}\u{1F1E6}-\u{1F1FF}]/u; const hit = EMOJI_RE.test(str(c.manifest.name)) || EMOJI_RE.test(str(c.manifest.description)); return hit ? fail( 'name/description contains emojis', 'TREK renders with lucide icons and STRIPS emojis from plugin-rendered text and notifications, so they\n' + 'simply vanish at render. Use the `icon` field with a lucide name instead.', ) : pass(); }, }; // ── code ──────────────────────────────────────────────────────────────────────── const codeServerEntry: OfflineCheck = { id: 'code.server-entry', blocks: 'artifact', stage: 'code', depth: 'offline', severity: 'error', title: 'server/index.js exists', run: (c) => c.exists('server/index.js') ? pass() : fail( 'server/index.js is missing', 'Every plugin — even a pure-UI one — is loaded from server/index.js via definePlugin(). If you build from\nTypeScript, build before packing.', ), }; /** * The egress trap, and the reason this check exists at all. * * The manifest has TWO places that look like they control network access, and only one of them * does. `spawnActivated` (server plugin-runtime.service.ts) builds the child's allow-list purely * from `http:outbound:` PERMISSIONS, and the iframe CSP's connect-src comes from the same * place. The manifest's `egress[]` array is never read at runtime at all — it is a declaration * for the consent screen. * * So a plugin with `egress: ["api.example.com"]` and permission `http:outbound` — no host suffix * — installs, activates, consents, and is then SILENTLY BLOCKED from the one host it exists to * call. Nothing in the manifest validator, the packer or the registry catches it. The author * finds out from a production timeout. */ const codeEgressReachable: OfflineCheck = { id: 'code.egress-reachable', stage: 'code', depth: 'offline', severity: 'error', title: 'Every egress host has a matching http:outbound: permission', run: (c) => { if (!c.manifest) return skip(); const egress = arr(c.manifest.egress); const permissions = arr(c.manifest.permissions); if (!egress.length) return skip(); const granted = grantedHosts(permissions); const unreachable = egress.filter((h) => !granted.includes(h)); if (!unreachable.length) return pass(`${egress.length} host${egress.length === 1 ? '' : 's'}`); return fail( unreachable.join(', '), 'TREK opens the network guard and the iframe CSP from the `http:outbound:` PERMISSIONS only — it never\n' + 'reads egress[]. These hosts are declared but unreachable: the plugin will install, activate, and then have\n' + 'every request to them silently blocked.\n' + 'Add to permissions: ' + unreachable.map((h) => `"http:outbound:${h}"`).join(', '), ); }, }; const codeEgressDeclared: OfflineCheck = { id: 'code.egress-declared', stage: 'code', depth: 'offline', severity: 'warn', title: 'Every reachable host is declared in egress[]', run: (c) => { if (!c.manifest) return skip(); const egress = arr(c.manifest.egress); const granted = grantedHosts(arr(c.manifest.permissions)); if (!granted.length) return skip(); const undeclared = granted.filter((h) => !egress.includes(h)); return undeclared.length ? fail( undeclared.join(', '), 'These hosts are reachable (you granted http:outbound:) but absent from egress[]. egress[] is what the\n' + 'admin sees on the consent screen, so the plugin is understating its own network reach. Add them to egress[].', ) : pass(); }, }; const codeDesignKit: OfflineCheck = { id: 'code.design-kit', stage: 'code', depth: 'offline', severity: 'warn', title: 'Client UI inlines the TREK design kit', run: (c) => { if (!c.clientHtml) return skip(); const rawSelect = /]*\bdata-trek-native\b)(\s|>)/i.test(c.clientHtml); const usesKit = c.clientHtml.includes('') || c.clientHtml.includes('data-trek-ui'); return rawSelect && !usesKit ? fail( 'a raw