Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

Fix Local API CSRF Vulnerability

dorengstearns6e1a09a685cc
diff --git a/src/api/backend.ts b/src/api/backend.ts
index 39dfe92f..6528b019 100644
--- a/src/api/backend.ts
+++ b/src/api/backend.ts
@@ -168,16 +168,18 @@ export async function backendCall<T = any>(
const method = options?.method || endpoint.method || "GET";
const fetchOptions: RequestInit = { method };
+ const headers: Record<string, string> = { "x-locally-uncensored": "true" };
if (options?.body) {
fetchOptions.body = options.body;
if (options.headers) {
- fetchOptions.headers = options.headers;
+ Object.assign(headers, options.headers);
}
} else if (args && method !== "GET") {
- fetchOptions.headers = { "Content-Type": "application/json" };
+ headers["Content-Type"] = "application/json";
fetchOptions.body = JSON.stringify(args);
}
+ fetchOptions.headers = headers;
// For GET with args, append as query params
let url = endpoint.path;
diff --git a/vite.config.ts b/vite.config.ts
index d64fedd4..ffed3987 100644
--- a/vite.config.ts
+++ b/vite.config.ts
@@ -141,6 +141,44 @@ function comfyLauncher(): Plugin {
return {
name: 'comfy-launcher',
configureServer(server) {
+ // --- Security Middleware ---
+ server.middlewares.use('/local-api', (req, res, next) => {
+ // Exclude GET proxy-image/download from strict header checks (used in <img> tags and simple fetches)
+ if (req.method === 'GET' && (req.url?.startsWith('/proxy-image') || req.url?.startsWith('/proxy-download'))) {
+ return next();
+ }
+
+ // 1. Strict Content-Type enforcement for POST requests
+ if (req.method === 'POST') {
+ const contentType = req.headers['content-type'] || '';
+ if (!contentType.includes('application/json')) {
+ res.writeHead(415, { 'Content-Type': 'text/plain' });
+ res.end('Unsupported Media Type: Must be application/json');
+ return;
+ }
+ }
+
+ // 2. Custom Header Requirement (CSRF Protection)
+ if (req.headers['x-locally-uncensored'] !== 'true') {
+ res.writeHead(403, { 'Content-Type': 'text/plain' });
+ res.end('Forbidden: Missing x-locally-uncensored header (CSRF Protection)');
+ return;
+ }
+
+ // 3. Strict Origin Validation (Defense in Depth)
+ const origin = req.headers.origin;
+ if (origin) {
+ const allowedOrigins = ['http://localhost:5173', 'http://127.0.0.1:5173', 'tauri://localhost', 'http://tauri.localhost'];
+ if (!allowedOrigins.includes(origin)) {
+ res.writeHead(403, { 'Content-Type': 'text/plain' });
+ res.end('Forbidden: Invalid Origin (CSRF Protection)');
+ return;
+ }
+ }
+
+ next();
+ });
+
// Auto-start Ollama when dev server starts
try {
execSync('tasklist /FI "IMAGENAME eq ollama.exe" | find /I "ollama.exe"', { stdio: 'ignore' })