fix plugin loading with bad names

This commit is contained in:
KingKDot
2026-05-07 20:37:29 -05:00
parent dd6007fb74
commit 9f1ecd3931
2 changed files with 35 additions and 10 deletions
@@ -398,6 +398,29 @@ export async function dispatchAutoLoadPlugins(
}
}
export function detectPluginIdFromZip(zip: AdmZip): string | null {
const entries = zip.getEntries();
for (const entry of entries) {
if (entry.isDirectory) continue;
const base = path.basename(entry.entryName);
if (base.toLowerCase().endsWith(".html")) {
return base.slice(0, -5);
}
}
for (const entry of entries) {
if (entry.isDirectory) continue;
const base = path.basename(entry.entryName);
if (base.toLowerCase() === "config.json") {
try {
const cfg = JSON.parse(entry.getData().toString("utf-8"));
if (typeof cfg?.id === "string" && cfg.id.length > 0) return cfg.id;
} catch {}
break;
}
}
return null;
}
function derivePlatformKey(filename: string): string {
const match = filename.match(/-(linux|darwin|windows|freebsd)-(amd64|arm64|arm|386)\.(so|dll|dylib)$/i);
if (match) {
@@ -12,7 +12,7 @@ import { encodeMessage, type PluginSignatureInfo } from "../../protocol";
import { getConfig, updatePluginsConfig } from "../../config";
import { getOrVerifySignature, BUILTIN_TRUSTED_KEYS } from "../plugin-signature";
import type { PluginRuntime } from "../plugin-runtime/runtime";
import { getPluginPull, deletePluginPull } from "../plugin-state-bundle";
import { getPluginPull, deletePluginPull, detectPluginIdFromZip } from "../plugin-state-bundle";
import { isAuthorizedAgentRequest } from "../agent-auth";
import { logger } from "../../logger";
@@ -253,21 +253,14 @@ export async function handlePluginRoutes(
return new Response("Plugin zip exceeds size limit", { status: 413 });
}
const base = path.basename(filename, path.extname(filename));
let pluginId = "";
try {
pluginId = deps.sanitizePluginId(base);
} catch {
return new Response("Invalid plugin name", { status: 400 });
}
const data = new Uint8Array(await file.arrayBuffer());
if (data.byteLength > MAX_PLUGIN_ZIP_BYTES) {
return new Response("Plugin zip exceeds size limit", { status: 413 });
}
let probe: AdmZip;
try {
const probe = new AdmZip(Buffer.from(data));
probe = new AdmZip(Buffer.from(data));
let totalUncompressed = 0;
for (const entry of probe.getEntries() as any[]) {
const sz = Number(entry?.header?.size ?? 0);
@@ -283,6 +276,15 @@ export async function handlePluginRoutes(
return new Response("Invalid plugin zip", { status: 400 });
}
const internalId = detectPluginIdFromZip(probe);
const idCandidate = internalId || path.basename(filename, path.extname(filename));
let pluginId = "";
try {
pluginId = deps.sanitizePluginId(idCandidate);
} catch {
return new Response("Invalid plugin name", { status: 400 });
}
await fs.mkdir(deps.PLUGIN_ROOT, { recursive: true });
const zipPath = path.join(deps.PLUGIN_ROOT, `${pluginId}.zip`);
await fs.writeFile(zipPath, data);