MC/website/ops/test-cp-v2-preview-nginx.mjs
2026-11-27 23:43:05 +00:00

41 lines
4.5 KiB
JavaScript

import assert from 'node:assert/strict';
import http from 'node:http';
import net from 'node:net';
import {spawn} from 'node:child_process';
import {mkdtemp,mkdir,writeFile,readFile,rm} from 'node:fs/promises';
import path from 'node:path';
import {tmpdir} from 'node:os';
if(process.platform!=='linux'||process.getuid()===0)throw Error('Run this isolated nginx check as an unprivileged Linux user');
const root=await mkdtemp(path.join(tmpdir(),'cpv2-nginx-test-'));
const auth=http.createServer((req,res)=>{if(req.url==='/v2/v2.css'){res.statusCode=req.headers.cookie==='fixture=owner'?200:req.headers.cookie==='fixture=member'?403:401;return res.end('owner check');}res.end('V1 unchanged');});
let child;
try{
await new Promise(resolve=>auth.listen(0,'127.0.0.1',resolve));const authPort=auth.address().port;
const portProbe=net.createServer();await new Promise(resolve=>portProbe.listen(0,'127.0.0.1',resolve));const port=portProbe.address().port;await new Promise(resolve=>portProbe.close(resolve));
const assets=path.join(root,'assets');await mkdir(assets);await writeFile(path.join(assets,'index.html'),'<h1>V2 fixture</h1>');await writeFile(path.join(assets,'v2.js'),'export const fixture=true;');await writeFile(path.join(assets,'revision.json'),'{"revision":"fixture"}');
const fragment=(await readFile(new URL('./cp-v2-preview.nginx.conf',import.meta.url),'utf8')).replace('127.0.0.1:3003','127.0.0.1:'+authPort).replace('/var/lib/dunerealms-cp-v2/current/',assets+'/');await writeFile(path.join(root,'preview.conf'),fragment);
const entry=(await readFile(new URL('./cp-v2-entry.nginx.conf',import.meta.url),'utf8')).replace('127.0.0.1:3103','127.0.0.1:'+authPort).replace('/var/lib/dunerealms-cp-v2/current;',assets+';');await writeFile(path.join(root,'entry.conf'),entry);
const config=`pid ${root}/nginx.pid; error_log ${root}/error.log; events {} http { access_log off; server { listen 127.0.0.1:${port}; server_name cp.dunerealms.com; include ${root}/preview.conf; include ${root}/entry.conf; location / { proxy_pass http://127.0.0.1:${authPort}; } } }`;
await writeFile(path.join(root,'nginx.conf'),config);let errors='';child=spawn('/usr/sbin/nginx',['-p',root+'/', '-c',path.join(root,'nginx.conf'),'-g','daemon off;'],{stdio:['ignore','pipe','pipe']});child.stderr.on('data',chunk=>{errors=(errors+chunk).slice(-4000);});
const base='http://127.0.0.1:'+port;
for(let i=0;i<40;i++){try{await fetch(base);break;}catch{if(child.exitCode!==null)throw Error(errors||'Test nginx exited');await new Promise(resolve=>setTimeout(resolve,50));}}
const read=(url,cookie,method='GET')=>fetch(base+url,{method,redirect:'manual',headers:cookie?{Cookie:'fixture='+cookie}:{},signal:AbortSignal.timeout(3000)});
assert.equal((await read('/v2','owner')).status,308);
assert.equal((await read('/v2/')).status,401);assert.equal((await read('/v2/','member')).status,403);
const page=await read('/v2/','owner');assert.equal(page.status,200);assert.match(await page.text(),/V2 fixture/);
const module=await read('/v2/v2.js','owner');assert.equal(module.status,200);assert.match(module.headers.get('content-type'),/javascript/);assert.equal(await module.text(),'export const fixture=true;');assert.equal(module.headers.get('cache-control'),'private, no-store');
assert.equal((await read('/v2/v2.js','owner','HEAD')).status,200);
assert.equal((await read('/v2/v2.js','owner','POST')).status,405);
assert.equal((await read('/v2/revision.json','owner')).status,200);
assert.equal((await read('/v2/cp-v2-git-admin.mjs','owner')).status,404);
assert.equal((await read('/v2/missing.mjs','owner')).status,404);
assert.equal((await read('/_cp_v2_owner_check','owner')).status,404);
assert.equal(await (await read('/')).text(),'V1 unchanged');
assert.equal(await (await read('/','member')).text(),'V1 unchanged');
const rootPage=await read('/','owner');assert.equal(rootPage.status,200);assert.match(await rootPage.text(),/V2 fixture/);
assert.equal((await read('/?banId=fixture','owner','HEAD')).status,200);
console.log('PASS: isolated nginx owner access, guest/member denial, JS MIME, index, revision, HEAD, GET-only publishing, DuneGit rejection, internal auth isolation and unchanged V1 route');
}finally{
if(child&&child.exitCode===null){const closed=new Promise(resolve=>child.once('exit',resolve));child.kill('SIGQUIT');await Promise.race([closed,new Promise(resolve=>setTimeout(resolve,3000))]);if(child.exitCode===null){child.kill('SIGTERM');await closed;}}
await new Promise(resolve=>auth.close(resolve));if(path.dirname(root)!==tmpdir())throw Error('Unsafe fixture cleanup');await rm(root,{recursive:true,force:true});
}