const consoleURL = process.env.SEA_CONSOLE_URL?.trim(); const tenantLogin = process.env.SEA_TENANT_LOGIN?.trim(); const tenantPassword = process.env.SEA_TENANT_PASSWORD?.trim(); if (!consoleURL || !tenantLogin || !tenantPassword) { console.error('SEA_CONSOLE_URL, SEA_TENANT_LOGIN and SEA_TENANT_PASSWORD are required.'); process.exit(2); } const consoleBase = new URL(consoleURL); if (!['http:', 'https:'].includes(consoleBase.protocol) || consoleBase.username || consoleBase.password) { console.error('SEA_CONSOLE_URL must be an HTTP(S) URL without embedded credentials.'); process.exit(2); } const checks = []; async function request(pathname, { method = 'GET', body, cookie, origin, expected = [200] } = {}) { const response = await fetch(new URL(pathname, consoleBase), { method, headers: { Accept: 'application/json', ...(body === undefined ? {} : { 'Content-Type': 'application/json' }), ...(cookie ? { Cookie: cookie } : {}), ...(origin ? { Origin: origin } : {}), }, body: body === undefined ? undefined : JSON.stringify(body), redirect: 'manual', signal: AbortSignal.timeout(10_000), }); const text = await response.text(); let payload; try { payload = text ? JSON.parse(text) : undefined; } catch { payload = undefined; } if (!expected.includes(response.status)) { throw new Error(`${method} ${pathname} returned HTTP ${response.status}: ${payload?.error?.message || 'unexpected response'}`); } return { response, payload }; } async function check(name, callback) { await callback(); checks.push(name); } function sessionCookie(response) { const setCookie = typeof response.headers.getSetCookie === 'function' ? response.headers.getSetCookie()[0] : response.headers.get('set-cookie'); if (!setCookie?.startsWith('sea_session=')) throw new Error('login did not return the expected session cookie'); const lower = setCookie.toLowerCase(); if (!lower.includes('httponly') || !lower.includes('samesite=strict')) throw new Error('session cookie security attributes are incomplete'); if (consoleBase.protocol === 'https:' && !lower.includes('secure')) throw new Error('HTTPS console session cookie is missing Secure'); return setCookie.split(';', 1)[0]; } function filePath(projectID, requested) { const url = new URL(`/api/v1/projects/${projectID}/files`, consoleBase); url.searchParams.set('path', requested); return `${url.pathname}${url.search}`; } let cookie; try { await check('unauthenticated API denied', async () => { await request('/api/v1/dashboard', { expected: [401] }); }); await check('cross-origin state request denied', async () => { await request('/api/v1/auth/login', { method: 'POST', origin: 'https://attacker.invalid', expected: [403], body: { identifier: tenantLogin, password: tenantPassword }, }); }); await check('same-host wrong-scheme origin denied', async () => { const wrongScheme = consoleBase.protocol === 'https:' ? 'http:' : 'https:'; await request('/api/v1/auth/login', { method: 'POST', origin: `${wrongScheme}//${consoleBase.host}`, expected: [403], body: { identifier: tenantLogin, password: tenantPassword }, }); }); await check('strict login JSON decoding', async () => { await request('/api/v1/auth/login', { method: 'POST', origin: consoleBase.origin, expected: [400], body: { identifier: tenantLogin, password: tenantPassword, unexpected: true }, }); }); const login = await request('/api/v1/auth/login', { method: 'POST', origin: consoleBase.origin, body: { identifier: tenantLogin, password: tenantPassword }, }); cookie = sessionCookie(login.response); checks.push('session cookie attributes'); const dashboard = await request('/api/v1/dashboard', { cookie }); const project = dashboard.payload?.projects?.[0]; if (!project?.id) throw new Error('tenant has no virtual host for security checks'); await check('tenant cannot use admin API', async () => { await request('/api/v1/admin/plans', { cookie, expected: [403] }); }); await check('tenant project IDOR denied', async () => { const missingProject = '9223372036854775807'; await request(`/api/v1/projects/${missingProject}/credentials`, { cookie, expected: [404] }); await request(`/api/v1/projects/${missingProject}/files`, { cookie, expected: [404] }); }); await check('authenticated CSRF denied before mutation', async () => { await request(`/api/v1/projects/${project.id}/state`, { method: 'POST', cookie, origin: 'https://attacker.invalid', expected: [403], body: { enabled: true }, }); }); await check('file traversal and ambiguous paths denied', async () => { for (const candidate of ['../../etc/passwd', 'public/../outside', 'public//index.html', String.raw`..\..\secret`]) { await request(filePath(project.id, candidate), { cookie, expected: [400] }); } }); await check('internal trash path denied', async () => { await request(filePath(project.id, '.sea-trash'), { cookie, expected: [400] }); await request(filePath(project.id, '.sea-trash/deleted.txt'), { cookie, expected: [400] }); }); await check('strict file-write JSON decoding', async () => { await request(`/api/v1/projects/${project.id}/files`, { method: 'PUT', cookie, origin: consoleBase.origin, expected: [400], body: { path: 'security-probe.txt', content: '', overwrite: false, unexpected: true }, }); }); await check('setup and registration write routes closed', async () => { await request('/api/v1/setup/install', { method: 'POST', origin: consoleBase.origin, body: {}, expected: [404] }); await request('/api/v1/auth/register', { method: 'POST', origin: consoleBase.origin, body: {}, expected: [404] }); }); await check('browser security headers present', async () => { const response = await fetch(consoleBase, { redirect: 'manual', signal: AbortSignal.timeout(10_000) }); if (response.headers.get('x-content-type-options')?.toLowerCase() !== 'nosniff') throw new Error('missing X-Content-Type-Options'); if (response.headers.get('x-frame-options')?.toUpperCase() !== 'DENY') throw new Error('missing X-Frame-Options'); if (!response.headers.get('content-security-policy')) throw new Error('missing Content-Security-Policy'); }); console.log(JSON.stringify({ status: 'passed', checks, count: checks.length }, null, 2)); } finally { if (cookie) { try { await request('/api/v1/auth/logout', { method: 'POST', cookie, origin: consoleBase.origin, expected: [204] }); } catch { /* best-effort session cleanup */ } } }