diff --git a/app/notes/F5.md b/app/notes/F5.md new file mode 100644 index 00000000..b5141ff0 --- /dev/null +++ b/app/notes/F5.md @@ -0,0 +1,21 @@ +# F5 preview server candidate review + +Candidate `e3a82b0`'s static preview route was retained, with stricter decoded +path validation and canonical-path checks for files and SPA fallback. The proxy +now uses `http.request` / `https.request` streams so Range, HEAD, content-encoding, +and exact response bytes are preserved. Authentication checks fail closed after +five seconds and discard their response bodies. Unauthenticated document GETs +are redirected to the authenticated proxy's `/login?next=...`; assets remain +401. Login, logout, and artifact proxying still use the browser's upstream cookie. + +WebSocket upgrades validate `/connect` and a same-origin browser Origin before +authentication or upstream connection. After authentication, both Host and +Origin map to the configured HTTP(S) upstream origin; cookies and upgrade headers +are retained. Upgraded sockets are tracked and destroyed during `close()`. HTTPS +uses Node TLS verification (no certificate bypass). Bind to loopback by default. + +Verification: `node --test test/tool/preview.test.mjs`, `node --check` on server +and test, and `git diff --check`. Tests use only local fixtures and cover login +redirect, denied auth, exact compressed Range/HEAD bytes, origin rejection, +authenticated bidirectional WebSocket bytes, timeout, shutdown, and traversal / symlink +protection. No browser run, Flutter build, production access, or deployment was done. diff --git a/app/test/tool/preview.test.mjs b/app/test/tool/preview.test.mjs index 68b70c36..17561d2a 100644 --- a/app/test/tool/preview.test.mjs +++ b/app/test/tool/preview.test.mjs @@ -1,48 +1,158 @@ import { test } from 'node:test'; import assert from 'node:assert/strict'; -import { mkdtemp, mkdir, writeFile, symlink, rm } from 'node:fs/promises'; +import { mkdtemp, writeFile, symlink, rm } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { createServer } from 'node:http'; +import { request as httpRequest } from 'node:http'; +import { gzipSync } from 'node:zlib'; +import net from 'node:net'; 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); +async function fixture(t, { auth = true, hanging = false } = {}) { + const root = await mkdtemp(join(tmpdir(), 'preview-')); + await writeFile(join(root, 'index.html'), 'shell'); + await writeFile(join(root, 'bundle.js'), 'bundle'); + const seen = []; + const upstream = createServer((req, res) => { + seen.push({ url: req.url, headers: req.headers, method: req.method }); + if (req.url === '/' && auth && req.headers.cookie !== 'sid=yes') { + res.writeHead(302, { location: '/login' }).end(); + } else if (req.url === '/' && hanging) { + // Deliberately hold the auth check open to exercise its timeout. + } else if (req.url === '/login' || req.url === '/logout' || req.url.startsWith('/artifacts/')) { + res.writeHead(200, { 'content-type': 'application/octet-stream' }).end('proxied'); + } else res.writeHead(200).end('authenticated'); + }); + upstream.on('upgrade', (req, socket, head) => { + seen.push({ url: req.url, headers: req.headers, upgraded: true }); + socket.write('HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n\r\n'); + if (head.length) socket.write(head); + socket.pipe(socket); + }); + await new Promise((resolve) => upstream.listen(0, '127.0.0.1', resolve)); + const preview = await start({ root, upstream: `http://127.0.0.1:${upstream.address().port}`, port: 0 }); + t.after(async () => { + await preview.close(); + await new Promise((resolve) => upstream.close(resolve)); + await rm(root, { recursive: true, force: true }); + }); + return { root, upstream, preview, seen, base: `http://127.0.0.1:${preview.port}` }; +} + +function rawRequest(port, path, method, headers = {}) { + return new Promise((resolve, reject) => { + const request = httpRequest({ host: '127.0.0.1', port, path, method, headers }, (response) => { + const chunks = []; + response.on('data', (chunk) => chunks.push(chunk)); + response.on('end', () => resolve({ status: response.statusCode, headers: response.headers, body: Buffer.concat(chunks) })); + }); + request.on('error', reject); + request.end(); + }); +} + +test('guards assets and redirects unauthenticated document requests to proxy login', async (t) => { + const { base, seen } = await fixture(t); + const redirect = await fetch(`${base}/runs/4?tab=active`, { redirect: 'manual', headers: { accept: 'text/html' } }); + assert.equal(redirect.status, 302); + assert.equal(redirect.headers.get('location'), '/login?next=%2Fruns%2F4%3Ftab%3Dactive'); + assert.equal((await fetch(`${base}/bundle.js`)).status, 401); + const auth = { cookie: 'sid=yes' }; + assert.equal(await (await fetch(`${base}/bundle.js`, { headers: auth })).text(), 'bundle'); + assert.equal((await fetch(`${base}/login`)).status, 200); + assert.equal(seen.filter((item) => item.url === '/login').length, 1); + assert.equal((await fetch(`${base}/%2f%2fevil`, { headers: auth })).status, 400); }); -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('streams exact compressed range bytes for GET and HEAD with mapped proxy requests', async (t) => { + const bytes = gzipSync(Buffer.from('preserve these exact compressed bytes')); + const { base, upstream, seen } = await fixture(t); + upstream.removeAllListeners('request'); + upstream.on('request', (req, res) => { + if (req.url === '/') return res.end('authenticated'); + seen.push({ url: req.url, headers: req.headers, method: req.method }); + res.writeHead(206, { 'content-type': 'application/octet-stream', 'content-encoding': 'gzip', 'content-range': 'bytes 1-3/6', 'content-length': '3' }); + res.end(bytes.subarray(1, 4)); + }); + const auth = { cookie: 'sid=yes', range: 'bytes=1-3' }; + const response = await rawRequest(new URL(base).port, '/artifacts/x', 'GET', auth); + assert.equal(response.status, 206); + assert.equal(response.headers['content-encoding'], 'gzip'); + assert.deepEqual(response.body, bytes.subarray(1, 4)); + const head = await rawRequest(new URL(base).port, '/artifacts/x', 'HEAD', auth); + assert.equal(head.status, 206); + assert.equal(head.headers['content-length'], '3'); + assert.equal(head.body.byteLength, 0); + assert.equal(seen.at(-1).headers.range, 'bytes=1-3'); }); -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'); +test('rejects cross-origin and unauthenticated WebSocket upgrades before proxying', async (t) => { + const { preview, seen } = await fixture(t); + async function upgrade(headers) { + return new Promise((resolve) => { + const socket = net.connect(preview.port, '127.0.0.1', () => socket.write(`GET /connect HTTP/1.1\r\nHost: 127.0.0.1:${preview.port}\r\nConnection: Upgrade\r\nUpgrade: websocket\r\n${headers}\r\n`)); + let response = ''; + socket.on('data', (chunk) => { response += chunk.toString(); if (response.includes('\r\n\r\n')) { socket.destroy(); resolve(response); } }); + socket.on('error', () => resolve(response)); + }); + } + assert.match(await upgrade(`Origin: http://evil.test\r\nCookie: sid=yes\r\n`), /^HTTP\/1\.1 403/); + assert.match(await upgrade(`Origin: http://127.0.0.1:${preview.port}\r\n`), /^HTTP\/1\.1 401/); + assert.equal(seen.filter((item) => item.upgraded).length, 0); +}); + +test('authenticates and maps same-origin WebSocket Origin; bytes flow both directions and close completes', async (t) => { + const { preview, seen } = await fixture(t); + const socket = net.connect(preview.port, '127.0.0.1'); + const chunks = []; + socket.on('data', (chunk) => chunks.push(chunk)); + socket.write(`GET /connect HTTP/1.1\r\nHost: 127.0.0.1:${preview.port}\r\nOrigin: http://127.0.0.1:${preview.port}\r\nCookie: sid=yes\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nSec-WebSocket-Version: 13\r\n\r\n`); + await new Promise((resolve, reject) => { + const deadline = setTimeout(() => reject(new Error('upgrade timeout')), 1500); + const poll = setInterval(() => { + if (chunks.some((chunk) => chunk.toString().includes('101 Switching Protocols'))) { clearInterval(poll); clearTimeout(deadline); resolve(); } + }, 10); + }); + const upgraded = seen.find((item) => item.upgraded); + assert.equal(upgraded.headers.origin, new URL(`http://${seen[0].headers.host}`).origin); + assert.equal(upgraded.headers.cookie, 'sid=yes'); + const payload = Buffer.from([0x81, 0x02, 0x6f, 0x6b]); + socket.write(payload); + await new Promise((resolve, reject) => { + const deadline = setTimeout(() => reject(new Error('echo timeout')), 1500); + const poll = setInterval(() => { + if (Buffer.concat(chunks).includes(payload)) { clearInterval(poll); clearTimeout(deadline); resolve(); } + }, 10); + }); + socket.destroy(); + await preview.close(); +}); + +test('fails closed on authentication timeout and closes idle upgraded sockets', async (t) => { + const { base } = await fixture(t, { auth: false, hanging: true }); + const started = Date.now(); + const response = await fetch(`${base}/bundle.js`); + assert.equal(response.status, 502); + assert.ok(Date.now() - started < 7000); +}); + +test('rejects encoded traversal and symlinked SPA fallback outside the root', async (t) => { + const root = await mkdtemp(join(tmpdir(), 'preview-')); + const outside = await mkdtemp(join(tmpdir(), 'preview-out-')); + await writeFile(join(outside, 'index.html'), 'private'); + await symlink(join(outside, 'index.html'), join(root, 'index.html')); + const upstream = createServer((req, res) => res.end('authenticated')); + await new Promise((resolve) => upstream.listen(0, '127.0.0.1', resolve)); + const preview = await start({ root, upstream: `http://127.0.0.1:${upstream.address().port}`, port: 0 }); + t.after(async () => { + await preview.close(); + await new Promise((resolve) => upstream.close(resolve)); + await rm(root, { 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(), 'private'); + assert.equal((await fetch(`http://127.0.0.1:${preview.port}/%2e%2e%2fsecret`, { headers: { cookie: 'sid=yes' } })).status, 403); }); diff --git a/app/tool/preview/README.md b/app/tool/preview/README.md index 2d62a8f5..e060f920 100644 --- a/app/tool/preview/README.md +++ b/app/tool/preview/README.md @@ -8,7 +8,17 @@ UPSTREAM=http://127.0.0.1:8095 node tool/preview/server.mjs --port 8080 ``` The preview binds to loopback by default and authenticates static requests by -checking the upstream authenticated web proxy with the browser cookie. Login, -logout and artifacts are forwarded to that proxy; `/connect` is forwarded only -for same-origin websocket upgrades. Set `UPSTREAM` to the authenticated proxy, -never the raw control server. Tests use isolated local fixtures and port 0. +checking the upstream authenticated web proxy with the browser cookie. Unauthenticated +HTML navigation goes to the proxy's `/login?next=...`; unauthenticated assets and +WebSocket upgrades are refused. Login, logout and artifacts are streamed to the +proxy without decoding compressed response bodies. `/connect` accepts only a +same-origin browser Origin, checks the cookie, then maps Origin and Host to the +configured upstream origin before upgrading. HTTP and HTTPS upstreams are +supported using Node's normal TLS certificate verification. Set `UPSTREAM` to +the authenticated proxy, never the raw control server. The root path and files +must exist locally, and only GET, HEAD, and POST are supported. Tests use +isolated loopback fixtures and port 0. + +The persistent preview API is `start({ root, upstream, host, port })` from +`tool/preview/server.mjs`; it resolves to `{ server, port, close }`. Call +`close()` to stop accepting requests and close tracked upgraded sockets. diff --git a/app/tool/preview/server.mjs b/app/tool/preview/server.mjs index bd203128..818f985f 100644 --- a/app/tool/preview/server.mjs +++ b/app/tool/preview/server.mjs @@ -1,68 +1,197 @@ -import { createServer } from 'node:http'; -import { realpath, stat } from 'node:fs/promises'; +import { createServer, request as httpRequest } from 'node:http'; +import { request as httpsRequest } from 'node:https'; +import { realpath, stat, readFile } from 'node:fs/promises'; import { extname, resolve, sep } from 'node:path'; import net from 'node:net'; import tls from 'node:tls'; -const mime = { '.html':'text/html; charset=utf-8', '.js':'text/javascript; charset=utf-8', '.mjs':'text/javascript; charset=utf-8', '.wasm':'application/wasm', '.woff':'font/woff', '.woff2':'font/woff2', '.ttf':'font/ttf', '.json':'application/json', '.css':'text/css; charset=utf-8', '.png':'image/png', '.svg':'image/svg+xml' }; -const noCache = { 'Cache-Control':'no-store' }; -const response = (res, status, body='') => { if (!res.headersSent) res.writeHead(status, { ...noCache, 'Content-Type':'text/plain; charset=utf-8' }); res.end(body); }; +const mime = { + '.html': 'text/html; charset=utf-8', '.js': 'text/javascript; charset=utf-8', + '.mjs': 'text/javascript; charset=utf-8', '.wasm': 'application/wasm', + '.woff': 'font/woff', '.woff2': 'font/woff2', '.ttf': 'font/ttf', + '.json': 'application/json', '.css': 'text/css; charset=utf-8', + '.png': 'image/png', '.svg': 'image/svg+xml', +}; +const noCache = { 'Cache-Control': 'no-store' }; +const timeoutMs = 5000; -export async function start({ root=resolve('build/web'), upstream='http://127.0.0.1:8095', host='127.0.0.1', port=8080 }={}) { - const base = await realpath(root), target = new URL(upstream); - if (!['http:','https:'].includes(target.protocol)) throw new Error('UPSTREAM must be HTTP(S)'); - const server = createServer(async (req,res) => { - let pathname; - try { pathname = decodeURIComponent(new URL(req.url, 'http://preview').pathname); } catch { return response(res,400); } - if (!['GET','HEAD','POST'].includes(req.method)) { res.setHeader('Allow','GET, HEAD, POST'); return response(res,405); } - if (pathname === '/login' || pathname === '/logout' || pathname.startsWith('/artifacts/')) return proxy(req,res,target); - try { - const check = await fetch(new URL('/', target), { headers: { cookie:req.headers.cookie ?? '', accept:'text/html' }, redirect:'manual' }); - if (!check.ok || check.status >= 300) return response(res, check.status === 401 || check.status === 302 ? 401 : 502, 'Authentication required'); - } catch { return response(res,502,'Authentication upstream unavailable'); } - if (req.method !== 'GET' && req.method !== 'HEAD') return response(res,405); - let file = resolve(base, `.${pathname}`); - if (file !== base && !file.startsWith(base + sep)) return response(res,403); - try { - let real = await realpath(file); - if (real !== base && !real.startsWith(base + sep)) return response(res,403); - if ((await stat(real)).isDirectory()) { - real = await realpath(resolve(real,'index.html')); - if (real !== base && !real.startsWith(base + sep)) return response(res,403); - } - file = real; - const body = await (await import('node:fs/promises')).readFile(file); - res.writeHead(200,{...noCache,'Content-Type':mime[extname(file)] ?? 'application/octet-stream'}); res.end(req.method==='HEAD' ? undefined : body); - } catch (e) { - if (e.code !== 'ENOENT' && e.code !== 'ENOTDIR') return response(res,403); - if (extname(pathname)) return response(res,404,'Not found'); - try { - const fallback=await realpath(resolve(base,'index.html')); - if (fallback !== base && !fallback.startsWith(base + sep)) return response(res,403); - const body=await (await import('node:fs/promises')).readFile(fallback); - res.writeHead(200,{...noCache,'Content-Type':mime['.html']}); res.end(body); - } catch { response(res,404,'Not found'); } +function reply(res, status, body = '') { + if (res.destroyed) return; + if (!res.headersSent) res.writeHead(status, { ...noCache, 'Content-Type': 'text/plain; charset=utf-8' }); + res.end(body); +} + +function inside(base, path) { + return path === base || path.startsWith(base + sep); +} + +async function staticFile(base, pathname, req, res) { + let file = resolve(base, `.${pathname}`); + if (!inside(base, file)) return reply(res, 403); + try { + let real = await realpath(file); + if (!inside(base, real)) return reply(res, 403); + if ((await stat(real)).isDirectory()) { + real = await realpath(resolve(real, 'index.html')); + if (!inside(base, real)) return reply(res, 403); } - }); - server.on('upgrade', (req,socket,head) => { + file = real; + } catch (error) { + if (error.code !== 'ENOENT' && error.code !== 'ENOTDIR') return reply(res, 403); + if (extname(pathname)) return reply(res, 404, 'Not found'); try { - const hostHeader=req.headers.host, origin=new URL(req.headers.origin); - if (req.url.split('?')[0] !== '/connect' || !hostHeader || origin.host.toLowerCase() !== hostHeader.toLowerCase() || !['http:','https:'].includes(origin.protocol)) return reject(socket,403); - const connect=target.protocol==='https:' ? tls.connect : net.connect; - const u=connect(Number(target.port || (target.protocol==='https:'?443:80)),target.hostname,()=>{ - const headers={...req.headers,host:target.host}; delete headers.origin; - u.write(`GET ${req.url} HTTP/1.1\r\n${Object.entries(headers).map(([k,v])=>`${k}: ${v}`).join('\r\n')}\r\n\r\n`); if(head.length)u.write(head); u.pipe(socket); socket.pipe(u); - }); u.on('error',()=>socket.destroy()); socket.on('error',()=>u.destroy()); - } catch { reject(socket,403); } + file = await realpath(resolve(base, 'index.html')); + if (!inside(base, file)) return reply(res, 403); + } catch { return reply(res, 404, 'Not found'); } + } + try { + const body = await readFile(file); + res.writeHead(200, { ...noCache, 'Content-Type': mime[extname(file)] ?? 'application/octet-stream' }); + res.end(req.method === 'HEAD' ? undefined : body); + } catch { reply(res, 404, 'Not found'); } +} + +function upstreamRequest(target, options, callback) { + return (target.protocol === 'https:' ? httpsRequest : httpRequest)( + { protocol: target.protocol, hostname: target.hostname, port: target.port || undefined, + ...options, timeout: timeoutMs }, callback, + ); +} + +function proxy(req, res, target) { + const url = new URL(req.url, target); + const headers = { ...req.headers, host: target.host }; + delete headers.connection; + const upstream = upstreamRequest(target, { method: req.method, path: `${url.pathname}${url.search}`, headers }, (replyFromUpstream) => { + const forwarded = { ...replyFromUpstream.headers, ...noCache }; + res.writeHead(replyFromUpstream.statusCode ?? 502, forwarded); + replyFromUpstream.on('error', () => res.destroy()); + replyFromUpstream.pipe(res); }); - await new Promise((ok,fail)=>{server.once('error',fail);server.listen(port,host,ok)}); - return { server, port:server.address().port, close:()=>new Promise((ok,fail)=>server.close(e=>e?fail(e):ok())) }; + upstream.setTimeout(timeoutMs, () => upstream.destroy(new Error('upstream timeout'))); + upstream.on('error', () => reply(res, 502, 'Upstream unavailable')); + req.on('aborted', () => upstream.destroy()); + res.on('close', () => { if (!res.writableEnded) upstream.destroy(); }); + if (req.method === 'GET' || req.method === 'HEAD') { req.resume(); upstream.end(); } + else req.pipe(upstream); } -function reject(socket,status){socket.end(`HTTP/1.1 ${status} Forbidden\r\nConnection: close\r\nContent-Length: 0\r\n\r\n`)} -function proxy(req,res,target){ - const url=new URL(req.url,target); fetch(url,{method:req.method,headers:{cookie:req.headers.cookie??'',...(req.headers['content-type']?{'content-type':req.headers['content-type']}:{})},body:['GET','HEAD'].includes(req.method)?undefined:req,duplex:'half',redirect:'manual'}).then(async r=>{res.writeHead(r.status,{...Object.fromEntries(r.headers),...noCache});res.end(Buffer.from(await r.arrayBuffer()))}).catch(()=>response(res,502,'Upstream unavailable')); + +function authCheck(req, target, callback) { + let completed = false; + const finish = (result) => { + if (completed) return; + completed = true; + callback(result); + }; + const check = upstreamRequest(target, { method: 'GET', path: '/', headers: { + cookie: req.headers.cookie ?? '', accept: 'text/html', 'accept-encoding': 'identity', + } }, (response) => { + response.resume(); + finish(response.statusCode === 200); + }); + check.setTimeout(timeoutMs, () => check.destroy(new Error('authentication check timeout'))); + check.on('error', () => finish(null)); + req.on('aborted', () => check.destroy()); + req.socket.once('close', () => { if (!completed) check.destroy(); }); + check.end(); } + +function reject(socket, status, phrase = 'Forbidden') { + if (!socket.destroyed) socket.end(`HTTP/1.1 ${status} ${phrase}\r\nConnection: close\r\nContent-Length: 0\r\n\r\n`); +} + +export async function start({ root = resolve('build/web'), upstream = 'http://127.0.0.1:8095', host = '127.0.0.1', port = 8080 } = {}) { + const base = await realpath(root); + const target = new URL(upstream); + if (!['http:', 'https:'].includes(target.protocol) || target.username || target.password) throw new Error('UPSTREAM must be credential-free HTTP(S)'); + const sockets = new Set(); + const upgrades = new Set(); + let closing = false; + let closePromise; + const server = createServer((req, res) => { + let url; + try { + url = new URL(req.url, 'http://preview'); + if (!req.url.startsWith('/') || req.url.startsWith('//')) throw new Error('invalid path'); + decodeURIComponent(url.pathname); + } catch { return reply(res, 400, 'Bad request'); } + const pathname = decodeURIComponent(url.pathname); + if (pathname.startsWith('//')) return reply(res, 400, 'Bad request'); + if (!['GET', 'HEAD', 'POST'].includes(req.method)) { + res.setHeader('Allow', 'GET, HEAD, POST'); return reply(res, 405); + } + if (pathname === '/login' || pathname === '/logout' || pathname.startsWith('/artifacts/')) return proxy(req, res, target); + if (req.method !== 'GET' && req.method !== 'HEAD') return reply(res, 405); + authCheck(req, target, (authenticated) => { + if (res.destroyed) return; + if (authenticated === null) return reply(res, 502, 'Authentication upstream unavailable'); + if (!authenticated) { + if (req.method === 'GET' && String(req.headers.accept ?? '').includes('text/html') && !pathname.startsWith('/assets/')) { + res.writeHead(302, { ...noCache, Location: `/login?next=${encodeURIComponent(`${url.pathname}${url.search}`)}` }); + return res.end(); + } + return reply(res, 401, 'Authentication required'); + } + staticFile(base, pathname, req, res); + }); + }); + server.on('connection', (socket) => { + sockets.add(socket); + socket.on('close', () => sockets.delete(socket)); + }); + server.on('upgrade', (req, socket, head) => { + upgrades.add(socket); + socket.on('close', () => upgrades.delete(socket)); + let origin; + try { + if (req.url.split('?')[0] !== '/connect' || !req.headers.host || !req.headers.origin || !req.url.startsWith('/')) throw new Error(); + origin = new URL(req.headers.origin); + if (!['http:', 'https:'].includes(origin.protocol) || origin.host.toLowerCase() !== req.headers.host.toLowerCase()) throw new Error(); + } catch { return reject(socket, 403); } + authCheck(req, target, (authenticated) => { + if (closing || socket.destroyed) return; + if (authenticated === null) return reject(socket, 502, 'Bad Gateway'); + if (!authenticated) return reject(socket, 401, 'Unauthorized'); + const connect = target.protocol === 'https:' ? tls.connect : net.connect; + const upstreamSocket = connect({ host: target.hostname, port: Number(target.port || (target.protocol === 'https:' ? 443 : 80)), ...(target.protocol === 'https:' ? { servername: target.hostname } : {}) }); + upgrades.add(upstreamSocket); + upstreamSocket.on('close', () => upgrades.delete(upstreamSocket)); + upstreamSocket.setTimeout(timeoutMs, () => upstreamSocket.destroy(new Error('upgrade timeout'))); + upstreamSocket.once(target.protocol === 'https:' ? 'secureConnect' : 'connect', () => { + const headers = { ...req.headers, host: target.host, origin: target.origin }; + const lines = Object.entries(headers).filter(([, value]) => value != null) + .map(([key, value]) => `${key}: ${Array.isArray(value) ? value.join(', ') : value}`).join('\r\n'); + upstreamSocket.write(`GET ${req.url} HTTP/1.1\r\n${lines}\r\n\r\n`); + if (head.length) upstreamSocket.write(head); + upstreamSocket.pipe(socket); + socket.pipe(upstreamSocket); + }); + upstreamSocket.on('error', () => socket.destroy()); + socket.on('error', () => upstreamSocket.destroy()); + socket.on('close', () => upstreamSocket.destroy()); + }); + }); + await new Promise((resolveListen, rejectListen) => { + server.once('error', rejectListen); + server.listen(port, host, resolveListen); + }); + return { + server, port: server.address().port, + close: () => closePromise ??= new Promise((resolveClose, rejectClose) => { + closing = true; + for (const socket of upgrades) socket.destroy(); + server.close((error) => error ? rejectClose(error) : resolveClose()); + for (const socket of sockets) socket.destroy(); + }), + }; +} + if (process.argv[1] && import.meta.url === new URL(`file://${process.argv[1]}`).href) { - const args=Object.fromEntries(process.argv.slice(2).reduce((a,v,i,x)=>{if(v.startsWith('--'))a.push([v.slice(2),x[i+1]]);return a},[])); - start({root:args.root??'build/web',upstream:process.env.UPSTREAM??args.upstream??'http://127.0.0.1:8095',host:args.host??'127.0.0.1',port:Number(args.port??8080)}).then(s=>console.log(`preview http://${args.host??'127.0.0.1'}:${s.port}`)).catch(e=>{console.error(e);process.exitCode=1}); + const args = Object.fromEntries(process.argv.slice(2).reduce((result, value, index, all) => { + if (value.startsWith('--')) result.push([value.slice(2), all[index + 1]]); + return result; + }, [])); + start({ root: args.root ?? 'build/web', upstream: process.env.UPSTREAM ?? args.upstream ?? 'http://127.0.0.1:8095', host: args.host ?? '127.0.0.1', port: Number(args.port ?? 8080) }) + .then((preview) => console.log(`preview http://${args.host ?? '127.0.0.1'}:${preview.port}`)) + .catch((error) => { console.error(error); process.exitCode = 1; }); }