Unrestricted Open-source alternative to AI video platforms — Free AI image & video generation studio with 600+ models (Flux, Midjourney, Kling, Sora, Veo). No content filters. Self-hosted, MIT licensed.
JavaScript ai-art-generatorai-image-generationai-video-generationfal-ai-alternativegenerative-aiimage-to-videoinvideo-alternativekling-ailipsynclitellm-alternativemidjourney-alternativemuapiopen-sourceopenrouter-alternativeseedance2sora-alternativetext-to-imagetext-to-videouncensoredwan-video
fix(security): enable webSecurity, remove cookie auth, add security headers
Anil Matcha7d125445f937
diff --git a/app/api/agents/[[...path]]/route.js b/app/api/agents/[[...path]]/route.jsindex bd308939..80f52b61 100644--- a/app/api/agents/[[...path]]/route.js+++ b/app/api/agents/[[...path]]/route.js@@ -3,13 +3,10 @@ import { NextResponse } from 'next/server';const MUAPI_BASE = 'https://api.muapi.ai';function getApiKey(request) {- // Priority 1: Direct x-api-key header+ // Only accept x-api-key header. Cookie-based auth is removed for security:+ // cookies without HttpOnly flag can be stolen by XSS (CWE-522).const headerKey = request.headers.get('x-api-key');- if (headerKey) return headerKey;-- // Priority 2: muapi_key cookie- const cookieKey = request.cookies.get('muapi_key')?.value;- return cookieKey;+ return headerKey || null;}function cleanHeaders(request) {@@ -37,7 +34,7 @@ export async function GET(request, { params }) {const headers = cleanHeaders(request);const apiKey = getApiKey(request);- console.log(`[agents proxy GET] ${targetUrl} | apiKey: ${apiKey ? apiKey.slice(0,8)+'...' : 'MISSING'}`);+ // NOTE: credential logging removed for security (CWE-200)if (apiKey) headers.set('x-api-key', apiKey);try {@@ -57,7 +54,7 @@ export async function POST(request, { params }) {const headers = cleanHeaders(request);const apiKey = getApiKey(request);- console.log(`[agents proxy POST] ${targetUrl} | apiKey: ${apiKey ? apiKey.slice(0,8)+'...' : 'MISSING'}`);+ // NOTE: credential logging removed for security (CWE-200)if (apiKey) headers.set('x-api-key', apiKey);try {diff --git a/app/api/api/v1/[[...path]]/route.js b/app/api/api/v1/[[...path]]/route.jsindex 9babc32e..26cf4b4c 100644--- a/app/api/api/v1/[[...path]]/route.js+++ b/app/api/api/v1/[[...path]]/route.js@@ -5,8 +5,8 @@ const MUAPI_BASE = 'https://api.muapi.ai';function getApiKey(request) {const headerKey = request.headers.get('x-api-key');if (headerKey) return headerKey;- const cookieKey = request.cookies.get('muapi_key')?.value;- return cookieKey;+ // Cookie-based auth removed for security (CWE-522)+ return null;}function cleanHeaders(request) {@@ -29,8 +29,8 @@ export async function GET(request, { params }) {const headers = cleanHeaders(request);const apiKey = getApiKey(request);-- console.log(`[double-api proxy GET] ${targetUrl} | apiKey: ${apiKey ? apiKey.slice(0,8)+'...' : 'MISSING'}`);++ // NOTE: credential logging removed for security (CWE-200)if (apiKey) headers.set('x-api-key', apiKey);try {diff --git a/app/api/app/[[...path]]/route.js b/app/api/app/[[...path]]/route.jsindex 5b8eb8d3..59b398d8 100644--- a/app/api/app/[[...path]]/route.js+++ b/app/api/app/[[...path]]/route.js@@ -3,13 +3,10 @@ import { NextResponse } from 'next/server';const MUAPI_BASE = 'https://api.muapi.ai';function getApiKey(request) {- // Priority 1: Direct x-api-key header+ // Only accept x-api-key header. Cookie-based auth is removed for security:+ // cookies without HttpOnly flag can be stolen by XSS (CWE-522).const headerKey = request.headers.get('x-api-key');- if (headerKey) return headerKey;-- // Priority 2: muapi_key cookie (used by the fixed builder library)- const cookieKey = request.cookies.get('muapi_key')?.value;- return cookieKey;+ return headerKey || null;}function cleanHeaders(request) {diff --git a/app/api/v1/creative-agent/[[...path]]/route.js b/app/api/v1/creative-agent/[[...path]]/route.jsindex 179999a0..bdc54998 100644--- a/app/api/v1/creative-agent/[[...path]]/route.js+++ b/app/api/v1/creative-agent/[[...path]]/route.js@@ -9,8 +9,8 @@ function getApiKey(request) {}const headerKey = request.headers.get('x-api-key');if (headerKey) return headerKey;- const cookieKey = request.cookies.get('muapi_key')?.value;- return cookieKey;+ // Cookie-based auth removed for security: no HttpOnly flag exposes key to XSS (CWE-522)+ return null;}function cleanHeaders(request) {@@ -33,8 +33,8 @@ export async function GET(request, { params }) {const headers = cleanHeaders(request);const apiKey = getApiKey(request);- console.log(`[creative-agent proxy GET] ${targetUrl} | apiKey: ${apiKey ? apiKey.slice(0,8)+'...' : 'MISSING'}`);-+ // NOTE: credential logging removed for security (CWE-200)+if (apiKey) headers.set('x-api-key', apiKey);try {@@ -57,7 +57,7 @@ export async function POST(request, { params }) {const headers = cleanHeaders(request);const apiKey = getApiKey(request);- console.log(`[creative-agent proxy POST] ${targetUrl} | apiKey: ${apiKey ? apiKey.slice(0,8)+'...' : 'MISSING'}`);+ // NOTE: credential logging removed for security (CWE-200)if (apiKey) headers.set('x-api-key', apiKey);@@ -82,7 +82,7 @@ export async function PATCH(request, { params }) {const headers = cleanHeaders(request);const apiKey = getApiKey(request);- console.log(`[creative-agent proxy PATCH] ${targetUrl} | apiKey: ${apiKey ? apiKey.slice(0,8)+'...' : 'MISSING'}`);+ // NOTE: credential logging removed for security (CWE-200)if (apiKey) headers.set('x-api-key', apiKey);@@ -107,7 +107,7 @@ export async function DELETE(request, { params }) {const headers = cleanHeaders(request);const apiKey = getApiKey(request);- console.log(`[creative-agent proxy DELETE] ${targetUrl} | apiKey: ${apiKey ? apiKey.slice(0,8)+'...' : 'MISSING'}`);+ // NOTE: credential logging removed for security (CWE-200)if (apiKey) headers.set('x-api-key', apiKey);diff --git a/app/api/v1/get_upload_url/route.js b/app/api/v1/get_upload_url/route.jsindex 0c5a361b..21e60c27 100644--- a/app/api/v1/get_upload_url/route.js+++ b/app/api/v1/get_upload_url/route.js@@ -5,8 +5,8 @@ const MUAPI_BASE = 'https://api.muapi.ai';function getApiKey(request) {const headerKey = request.headers.get('x-api-key');if (headerKey) return headerKey;- const cookieKey = request.cookies.get('muapi_key')?.value;- return cookieKey;+ // Cookie-based auth removed for security (CWE-522)+ return null;}function cleanHeaders(request) {diff --git a/app/api/workflow/[[...path]]/route.js b/app/api/workflow/[[...path]]/route.jsindex 527809be..da1e449b 100644--- a/app/api/workflow/[[...path]]/route.js+++ b/app/api/workflow/[[...path]]/route.js@@ -3,13 +3,10 @@ import { NextResponse } from 'next/server';const MUAPI_BASE = 'https://api.muapi.ai';function getApiKey(request) {- // Priority 1: Direct x-api-key header+ // Only accept x-api-key header. Cookie-based auth is removed for security:+ // cookies without HttpOnly flag can be stolen by any XSS (CWE-522).const headerKey = request.headers.get('x-api-key');- if (headerKey) return headerKey;-- // Priority 2: muapi_key cookie (used by the fixed builder library)- const cookieKey = request.cookies.get('muapi_key')?.value;- return cookieKey;+ return headerKey || null;}function cleanHeaders(request) {@@ -31,7 +28,7 @@ export async function GET(request, { params }) {const headers = cleanHeaders(request);const apiKey = getApiKey(request);- console.log(`[proxy GET] ${targetUrl} | apiKey: ${apiKey ? apiKey.slice(0,8)+'...' : 'MISSING'}`);+ // NOTE: apiKey is intentionally NOT logged here to prevent credential leakage (CWE-200)if (apiKey) headers.set('x-api-key', apiKey);try {@@ -60,7 +57,7 @@ export async function POST(request, { params }) {const headers = cleanHeaders(request);const apiKey = getApiKey(request);- console.log(`[proxy POST] ${targetUrl} | apiKey: ${apiKey ? apiKey.slice(0,8)+'...' : 'MISSING'} | cookie: ${request.cookies.get('muapi_key')?.value?.slice(0,8) || 'NONE'} | header: ${request.headers.get('x-api-key')?.slice(0,8) || 'NONE'}`);+ // NOTE: credential logging removed for security (CWE-200)if (apiKey) headers.set('x-api-key', apiKey);try {diff --git a/electron/main.js b/electron/main.jsindex 00a614a6..81292b9c 100644--- a/electron/main.js+++ b/electron/main.js@@ -32,7 +32,7 @@ function createWindow() {minWidth: 1024,minHeight: 640,webPreferences: {- webSecurity: false,+ webSecurity: true,contextIsolation: true,nodeIntegration: false,preload: path.join(__dirname, 'preload.js'),diff --git a/middleware.js b/middleware.jsindex 5317e902..47313fd4 100644--- a/middleware.js+++ b/middleware.js@@ -1,33 +1,54 @@import { NextResponse } from 'next/server';+function addSecurityHeaders(response) {+ // Prevent MIME type sniffing (CWE-693)+ response.headers.set('X-Content-Type-Options', 'nosniff');+ // Prevent clickjacking (CWE-1021)+ response.headers.set('X-Frame-Options', 'DENY');+ // Enable XSS filter in legacy browsers+ response.headers.set('X-XSS-Protection', '1; mode=block');+ // Referrer policy+ response.headers.set('Referrer-Policy', 'strict-origin-when-cross-origin');+ // Content Security Policy - restricts script sources to prevent XSS (CWE-79).+ // connect-src covers *.muapi.ai (not just api.muapi.ai) because generated+ // media, model thumbnails, and other assets are served from cdn.muapi.ai+ // and other muapi subdomains that the renderer fetches directly.+ response.headers.set(+ 'Content-Security-Policy',+ "default-src 'self'; script-src 'self' 'unsafe-eval' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob: https:; media-src 'self' data: blob: https:; connect-src 'self' https://muapi.ai https://*.muapi.ai; font-src 'self' data:;"+ );+ return response;+}+export function middleware(request) {const url = request.nextUrl;-+// Catch requests to /api/workflow, /api/app, and /api/v1- const isMuApi = url.pathname.startsWith('/api/workflow') ||- url.pathname.startsWith('/api/app') ||+ const isMuApi = url.pathname.startsWith('/api/workflow') ||+ url.pathname.startsWith('/api/app') ||url.pathname.startsWith('/api/v1');if (isMuApi) {// Exclude paths that have their own dedicated route handlers with custom logic- const isHandledByRoute = url.pathname.startsWith('/api/v1/creative-agent') ||+ const isHandledByRoute = url.pathname.startsWith('/api/v1/creative-agent') ||url.pathname.startsWith('/api/v1/get_upload_url') ||url.pathname.startsWith('/api/v1/upload-binary');if (url.pathname.startsWith('/api/v1') && !isHandledByRoute) {const targetUrl = new URL(url.pathname + url.search, 'https://api.muapi.ai');- return NextResponse.rewrite(targetUrl);+ const rewriteResponse = NextResponse.rewrite(targetUrl);+ return addSecurityHeaders(rewriteResponse);}}- return NextResponse.next();+ // Add security headers to all responses+ return addSecurityHeaders(NextResponse.next());}-// Match the paths we want to proxy+// Match all paths for security headers. Exclude Next.js internal paths.export const config = {matcher: [- '/api/workflow/:path*',- '/api/app/:path*',- '/api/v1/:path*'+ '/api/:path*',+ '/((?!_next/static|_next/image|favicon.ico|__nextjs_original-stack-frame).*)',],};