Files
OmniRoute/tests/unit/route-guard-plugins-local-only.test.ts
Diego Rodrigues de Sa e Souza 20c31493af feat(plugins): plugins framework + per-API-key disable-non-public-models (#3041)
Integrates two community contributions into release/v3.8.8 with security hardening and conflict resolution.

- **Plugins framework** (#2913 — thanks @oyi77): hooks + registry unification, plugin SDK (`definePlugin`), worker-thread sandbox, per-plugin hook rate limiting, SHA-256 integrity verification, semver-gated upgrade, and execution analytics. Plugin routes are loopback-only (`isLocalOnlyPath`); `child_process` exec is opt-in via `OMNIROUTE_PLUGINS_ALLOW_EXEC` (default off).
- **API key option: disable non-published models** (#3017 — thanks @androw): a per-key flag restricting the key to discovered public models (combos / `auto/*` / `qtSd/*` routing still allowed).

Hardening applied during integration: migration renumber (089/090/091), `/api/plugins` LOCAL_ONLY route-guard classification (closes the plugin-RCE vector), atomic install/upgrade with path containment, `O_EXCL` tmp-file creation (TOCTOU), rate-limit-map eviction, `validatePluginConfig` on configure, `buildErrorBody` on all plugin error paths. 246/246 tests; typecheck / cycles / docs-sync clean.

Co-authored-by: oyi77 <14921983+oyi77@users.noreply.github.com>
Co-authored-by: Nicolas Lorin <androw95220@gmail.com>
2026-06-01 15:43:55 -03:00

57 lines
2.5 KiB
TypeScript

/**
* Security regression: /api/plugins/* routes must be classified as LOCAL_ONLY
* so loopback enforcement runs unconditionally before any auth check.
*
* These routes trigger plugin loading via worker_threads + child_process:
* POST /api/plugins — install (loads plugin file via worker)
* GET /api/plugins — list (read-only, but same prefix must be gated
* to avoid auth-bypass leaking installed plugin names)
* GET/DELETE /api/plugins/[name] — inspect / uninstall
* POST /api/plugins/[name]/activate — loads + executes the plugin worker
* POST /api/plugins/[name]/deactivate — stops the plugin worker
* GET/PUT /api/plugins/[name]/config — configure the plugin
* POST /api/plugins/scan — filesystem scan (spawns child_process)
*
* Classifying the whole prefix as LOCAL_ONLY closes the remote-RCE vector:
* a leaked JWT over a Cloudflared/Ngrok tunnel cannot trigger process spawning.
* Hard Rules #15 + #17. See docs/security/ROUTE_GUARD_TIERS.md.
*/
import test from "node:test";
import assert from "node:assert/strict";
import { isLocalOnlyPath } from "../../src/server/authz/routeGuard.ts";
test("/api/plugins prefix (trailing slash) is LOCAL_ONLY", () => {
assert.equal(isLocalOnlyPath("/api/plugins/"), true);
});
test("/api/plugins (bare, no trailing slash) is LOCAL_ONLY", () => {
// The GET-list + POST-install route lives at exactly /api/plugins — must also be gated.
assert.equal(isLocalOnlyPath("/api/plugins"), true);
});
test("/api/plugins/[name]/activate is LOCAL_ONLY (worker_threads execution)", () => {
assert.equal(isLocalOnlyPath("/api/plugins/my-plugin/activate"), true);
});
test("/api/plugins/[name]/deactivate is LOCAL_ONLY", () => {
assert.equal(isLocalOnlyPath("/api/plugins/my-plugin/deactivate"), true);
});
test("/api/plugins/[name]/config is LOCAL_ONLY", () => {
assert.equal(isLocalOnlyPath("/api/plugins/my-plugin/config"), true);
});
test("/api/plugins/[name] (GET/DELETE) is LOCAL_ONLY", () => {
assert.equal(isLocalOnlyPath("/api/plugins/my-plugin"), true);
});
test("/api/plugins/scan is LOCAL_ONLY (spawns child_process)", () => {
assert.equal(isLocalOnlyPath("/api/plugins/scan"), true);
});
test("non-plugin paths are NOT LOCAL_ONLY (no over-match)", () => {
assert.equal(isLocalOnlyPath("/api/combos"), false);
assert.equal(isLocalOnlyPath("/api/providers"), false);
assert.equal(isLocalOnlyPath("/api/keys"), false);
});