import { test } from 'node:test'; import assert from 'node:assert/strict'; import { mkdtemp, mkdir, writeFile, symlink, rm } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { createServer } from 'node:http'; import { start } from '../../tool/preview/server.mjs'; test('serves guarded assets, deep links, MIME and missing assets; forwards auth routes and artifacts', async t => { const dir=await mkdtemp(join(tmpdir(),'preview-')); await mkdir(join(dir,'assets')); await writeFile(join(dir,'index.html'),'shell'); await writeFile(join(dir,'assets/a.wasm'),'wasm'); const upstream=createServer((req,res)=>{ if(req.url==='/') {res.writeHead(req.headers.cookie==='sid=yes'?200:302,{location:'/login'}).end();return} res.writeHead(200,{'x-forwarded':req.url}).end(req.url) }); await new Promise(ok=>upstream.listen(0,'127.0.0.1',ok)); const preview=await start({root:dir,upstream:`http://127.0.0.1:${upstream.address().port}`,port:0}); t.after(async()=>{await preview.close();upstream.close();await rm(dir,{recursive:true,force:true})}); const base=`http://127.0.0.1:${preview.port}`; assert.equal((await fetch(base+'/assets/a.wasm')).status,401); const headers={cookie:'sid=yes'}; const wasm=await fetch(base+'/assets/a.wasm',{headers}); assert.equal(wasm.headers.get('content-type'),'application/wasm'); assert.equal(wasm.headers.get('cache-control'),'no-store'); assert.equal(await (await fetch(base+'/runs/4',{headers})).text(),'shell'); assert.equal((await fetch(base+'/assets/missing.js',{headers})).status,404); assert.equal((await fetch(base+'/..%2f..%2fetc/passwd',{headers})).status,403); assert.equal((await fetch(base+'/artifacts/1/a',{headers})).status,200); assert.equal((await fetch(base+'/login')).status,200); }); test('rejects a directory index symlink that escapes the preview root', async t => { const dir=await mkdtemp(join(tmpdir(),'preview-')), outside=await mkdtemp(join(tmpdir(),'preview-out-')); await mkdir(join(dir,'linked')); await writeFile(join(outside,'secret.html'),'secret'); await symlink(join(outside,'secret.html'),join(dir,'linked','index.html')); const upstream=createServer((req,res)=>res.end('ok')); await new Promise(ok=>upstream.listen(0,'127.0.0.1',ok)); const preview=await start({root:dir,upstream:`http://127.0.0.1:${upstream.address().port}`,port:0}); t.after(async()=>{await preview.close();upstream.close();await rm(dir,{recursive:true,force:true});await rm(outside,{recursive:true,force:true})}); const response=await fetch(`http://127.0.0.1:${preview.port}/linked/`,{headers:{cookie:'sid=yes'}}); assert.equal(response.status,403); }); test('rejects an SPA fallback index symlink that escapes the preview root', async t => { const dir=await mkdtemp(join(tmpdir(),'preview-')), outside=await mkdtemp(join(tmpdir(),'preview-out-')); await writeFile(join(outside,'secret.html'),'secret'); await symlink(join(outside,'secret.html'),join(dir,'index.html')); const upstream=createServer((req,res)=>res.end('ok')); await new Promise(ok=>upstream.listen(0,'127.0.0.1',ok)); const preview=await start({root:dir,upstream:`http://127.0.0.1:${upstream.address().port}`,port:0}); t.after(async()=>{await preview.close();upstream.close();await rm(dir,{recursive:true,force:true});await rm(outside,{recursive:true,force:true})}); const response=await fetch(`http://127.0.0.1:${preview.port}/missing-route`,{headers:{cookie:'sid=yes'}}); assert.equal(response.status,403); assert.notEqual(await response.text(),'secret'); });