VNOX-Web/tests/server.test.js

16 lines
1.3 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { spawn } from 'node:child_process';
import { once } from 'node:events';
import http from 'node:http';
function fetchRaw(port,path){return new Promise((resolve,reject)=>{http.get({host:'127.0.0.1',port,path},response=>{let body='';response.on('data',chunk=>body+=chunk);response.on('end',()=>resolve({status:response.statusCode,body}));}).on('error',reject);});}
test('preview serves only public/source assets and blocks encoded traversal',async t=>{
const port=24176;
const process=spawn(globalThis.process.execPath,['scripts/serve.mjs'],{env:{...globalThis.process.env,PORT:String(port)},stdio:['ignore','pipe','pipe']});
t.after(()=>process.kill('SIGTERM'));
await Promise.race([once(process.stdout,'data'),once(process,'exit').then(()=>{throw new Error('preview failed to start');})]);
for(const path of ['/','/account','/account/','/src/app.js','/assets/vnox-mark.png']) assert.equal((await fetchRaw(port,path)).status,200,path);
for(const path of ['/src/%2e%2e%2fpackage.json','/src/%2e%2e%2f.env','/src/%2e%2e%2fLICENSE']) assert.equal((await fetchRaw(port,path)).status,403,path);
assert.equal((await fetchRaw(port,'/package.json')).status,404);
assert.equal((await fetchRaw(port,'/v1/auth/session')).status,503);
});