mirror of
https://github.com/penpot/penpot.git
synced 2026-11-18 16:12:24 +00:00
* 🐛 Check Host and Origin headers in the MCP server The MCP endpoint, the REPL server and the plugin WebSocket accepted requests with any Host and Origin header, so a web page open in the user's browser could drive a local MCP server: connect as the plugin and call its tools. RequestGuard checks both headers against an allowlist and rejects other requests with HTTP 403, or WebSocket close code 1008. The list comes from PENPOT_MCP_ALLOWED_HOSTS, where "*" turns the check off. When it is unset, local mode allows only loopback names and remote mode allows any host, since remote mode relies on user tokens and has no file access. AI-assisted-by: claude-opus-5-5 * 🐛 Keep the Host and Origin check on in single-user remote mode The default allowlist followed remote mode, so a single-user server with PENPOT_MCP_REMOTE_MODE=true accepted any Host and Origin. It requires no user token either, so a web page could still drive it and run code in the user's Penpot session. The default now follows multi-user mode: single-user servers allow only loopback names, and multi-user servers, which rely on user tokens, allow any host. Startup warns when the check is off in single-user mode. PENPOT_MCP_ALLOWED_HOSTS entries with a scheme, port or path never matched, so every request failed with 403. The server now refuses to start and names the bad entry. AI-assisted-by: claude-opus-5-5
117 lines
3.8 KiB
TypeScript
117 lines
3.8 KiB
TypeScript
import express from "express";
|
|
import path from "path";
|
|
import { fileURLToPath } from "url";
|
|
import { PluginBridge } from "./PluginBridge";
|
|
import { ExecuteCodePluginTask } from "./tasks/ExecuteCodePluginTask";
|
|
import { createLogger } from "./logger";
|
|
import type { RequestGuard } from "./RequestGuard";
|
|
|
|
/**
|
|
* Web-based REPL server for executing code through the PluginBridge.
|
|
*
|
|
* Provides a REPL-style HTML interface that allows users to input
|
|
* JavaScript code and execute it via ExecuteCodePluginTask instances.
|
|
* The interface maintains command history, displays logs in <pre> tags,
|
|
* and shows results in visually separated blocks.
|
|
*/
|
|
export class ReplServer {
|
|
private readonly logger = createLogger("ReplServer");
|
|
private readonly app: express.Application;
|
|
private readonly port: number;
|
|
private readonly host: string;
|
|
private server: any;
|
|
|
|
constructor(
|
|
private readonly pluginBridge: PluginBridge,
|
|
private readonly requestGuard: RequestGuard,
|
|
port: number = 4403,
|
|
host: string = "localhost"
|
|
) {
|
|
this.port = port;
|
|
this.host = host;
|
|
this.app = express();
|
|
this.setupMiddleware();
|
|
this.setupRoutes();
|
|
}
|
|
|
|
/**
|
|
* Sets up Express middleware for request parsing and static content.
|
|
*/
|
|
private setupMiddleware(): void {
|
|
this.app.use(this.requestGuard.httpMiddleware);
|
|
this.app.use(express.json());
|
|
}
|
|
|
|
/**
|
|
* Sets up HTTP routes for the REPL interface and API endpoints.
|
|
*/
|
|
private setupRoutes(): void {
|
|
// serve the main REPL interface
|
|
this.app.get("/", (req, res) => {
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
const htmlPath = path.join(__dirname, "static", "repl.html");
|
|
res.sendFile(htmlPath);
|
|
});
|
|
|
|
// API endpoint for executing code
|
|
this.app.post("/execute", async (req, res) => {
|
|
try {
|
|
const { code } = req.body;
|
|
|
|
if (!code || typeof code !== "string") {
|
|
return res.status(400).json({
|
|
error: "Code parameter is required and must be a string",
|
|
});
|
|
}
|
|
|
|
const task = new ExecuteCodePluginTask({ code });
|
|
const result = await this.pluginBridge.executePluginTask(task);
|
|
|
|
// extract the result member from ExecuteCodeTaskResultData
|
|
const executeResult = result.data?.result;
|
|
|
|
res.json({
|
|
success: true,
|
|
result: executeResult,
|
|
log: result.data?.log || "",
|
|
});
|
|
} catch (error) {
|
|
this.logger.error(error, "Failed to execute code in REPL");
|
|
res.status(500).json({
|
|
error: error instanceof Error ? error.message : "Unknown error occurred",
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Starts the REPL web server.
|
|
*
|
|
* Begins listening on the configured port and logs server startup information.
|
|
*/
|
|
public async start(): Promise<void> {
|
|
return new Promise((resolve) => {
|
|
this.server = this.app.listen(this.port, this.host, () => {
|
|
this.logger.info(`REPL server started on port ${this.port}`);
|
|
this.logger.info(`REPL interface URL: http://${this.host}:${this.port}`);
|
|
resolve();
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Stops the REPL web server.
|
|
*/
|
|
public async stop(): Promise<void> {
|
|
if (this.server) {
|
|
return new Promise((resolve) => {
|
|
this.server.close(() => {
|
|
this.logger.info("REPL server stopped");
|
|
resolve();
|
|
});
|
|
});
|
|
}
|
|
}
|
|
}
|