diff --git a/apps/waterfall-wiki/scripts/auth-cli.ts b/apps/waterfall-wiki/scripts/auth-cli.ts index aa8a769..1b33925 100644 --- a/apps/waterfall-wiki/scripts/auth-cli.ts +++ b/apps/waterfall-wiki/scripts/auth-cli.ts @@ -1,9 +1,9 @@ -import { mkdirSync, writeFileSync, existsSync, renameSync, chmodSync, chownSync } from 'node:fs'; +import { mkdirSync, writeFileSync, existsSync, readFileSync, renameSync, chmodSync, chownSync } from 'node:fs'; import { dirname, resolve } from 'node:path'; import { pathToFileURL } from 'node:url'; import assert from 'node:assert/strict'; import QRCode from 'qrcode'; -import { authConfig, newAuthConfig, signSession, totp } from '../src/lib/auth'; +import { authConfig, newAuthConfig, signSession, totp, isLoginRedirect } from '../src/lib/auth'; import { db } from '../src/lib/db'; async function main() { @@ -46,8 +46,9 @@ async function main() { } if (command === 'verify' || command === 'exercise') { const request = (path:string,init:RequestInit={}) => fetch(base+path,{...init,redirect:'manual'}); - for (const path of ['/','/library/import','/skills/shadow-puppet/egret-example.png','/studio','/_next/data/auth-test/index.json']) { - const response = await request(path); assert.equal(response.status,307,path); assert.match(response.headers.get('location') || response.headers.get('x-nextjs-redirect') || '',/\/login\?/,path+' must redirect to login'); + const buildId = readFileSync('.next/BUILD_ID','utf8').trim(); + for (const path of ['/','/library/import','/skills/shadow-puppet/egret-example.png','/studio',`/_next/data/${buildId}/index.json`]) { + const response = await request(path); assert.equal(response.status,307,path); const destination = response.headers.get('location') || response.headers.get('x-nextjs-redirect'); assert.ok(isLoginRedirect(destination,base),path+' must redirect to login: '+destination); } for (const path of ['/api/feed','/api/upload','/studio/api/export/test','/api/media/missing']) assert.equal((await request(path)).status,401,path); assert.equal((await request('/api/posts',{method:'POST',body:'{}'})).status,401); diff --git a/apps/waterfall-wiki/src/lib/auth.ts b/apps/waterfall-wiki/src/lib/auth.ts index a09e355..9855b0c 100644 --- a/apps/waterfall-wiki/src/lib/auth.ts +++ b/apps/waterfall-wiki/src/lib/auth.ts @@ -56,3 +56,8 @@ export function sessionCookie(token: string, clear = false) { return `${SESSION_COOKIE}=${token}; Path=/; HttpOnly; SameSite=Lax; Max-Age=${clear ? 0 : SESSION_SECONDS}${process.env.AUTH_COOKIE_SECURE === 'false' ? '' : '; Secure'}`; } export function safeReturn(value: unknown) { return typeof value === 'string' && value.startsWith('/') && !value.startsWith('//') && !/[\\\r\n]/.test(value) && !value.startsWith('/login') ? value : '/'; } + +export function isLoginRedirect(value: string | null, base: string) { + if (!value) return false; + try { const target = new URL(value,base); return target.origin === new URL(base).origin && (target.pathname === '/login' || /^\/_next\/data\/[^/]+\/login\.json$/.test(target.pathname)); } catch { return false; } +} diff --git a/apps/waterfall-wiki/tests/auth.test.ts b/apps/waterfall-wiki/tests/auth.test.ts index d8efb1e..dd9a3d2 100644 --- a/apps/waterfall-wiki/tests/auth.test.ts +++ b/apps/waterfall-wiki/tests/auth.test.ts @@ -1,6 +1,6 @@ import test from 'node:test'; import assert from 'node:assert/strict'; -import {base32,totp,matchingStep,newAuthConfig,signSession,validSession,sessionCookie,safeReturn,authConfig} from '../src/lib/auth'; +import {base32,totp,matchingStep,newAuthConfig,signSession,validSession,sessionCookie,safeReturn,authConfig,isLoginRedirect} from '../src/lib/auth'; import {proxy} from '../src/proxy'; import {NextRequest} from 'next/server'; import {mkdtempSync,writeFileSync,rmSync} from 'node:fs'; @@ -44,3 +44,9 @@ test('proxy protects APIs, data routes, static content and Studio; fails closed' writeFileSync(process.env.AUTH_CONFIG_FILE,'{}'); assert.throws(()=>authConfig()); assert.equal(proxy(new NextRequest('http://wiki.test/')).status,503); }finally{if(old===undefined)delete process.env.AUTH_CONFIG_FILE;else process.env.AUTH_CONFIG_FILE=old;rmSync(temp,{recursive:true,force:true});} }); + +test('login redirects recognize browser pages and Next.js data destinations',()=>{ + const base='http://wiki.test'; + for(const target of ['/login?next=%2F','http://wiki.test/login','/_next/data/build-123/login.json?next=%2F']) assert.equal(isLoginRedirect(target,base),true,target); + for(const target of [null,'','https://evil.test/login','//evil.test/login','/api/login.json','/_next/data/build-123/index.json']) assert.equal(isLoginRedirect(target,base),false,String(target)); +});