Files
NODEDC_PLATFORM/services/map-gateway/test/egress.test.mjs
T

61 lines
4.3 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {createHash,createHmac,X509Certificate} from 'node:crypto';
import {createServer} from 'node:https';
import {connect} from 'node:tls';
import {execFileSync} from 'node:child_process';
import {mkdtemp,readFile,rm} from 'node:fs/promises';
import {tmpdir} from 'node:os';
import {join} from 'node:path';
import {authorizeEgress,publicIPv4,relayFetch,signingKey} from '../src/egress.mjs';
const secret='a'.repeat(64);
function signed(overrides={}) {
const value={schema:'nodedc.map-egress/v1',timestamp:1000,profile:'cesium',
endpoint:'https://1.1.1.1:19446',fingerprint:'b'.repeat(64),token:'c'.repeat(43),...overrides};
const payload=Buffer.from(JSON.stringify(value)).toString('base64url');
const signature=createHmac('sha256',signingKey(secret)).update(`nodedc.map-egress/v1\nGET\n/api/map/cache\n${payload}`).digest('hex');
return {method:'GET',headers:{'x-nodedc-map-egress':payload,'x-nodedc-map-egress-signature':signature}};
}
test('signed admission rejects tampering, expiry and a subject without delegation',()=>{
assert.equal(authorizeEgress(signed(),secret,'/api/map/cache',1000000).profile,'cesium');
assert.throws(()=>authorizeEgress(signed(),secret,'/api/map/ion',1000000));
assert.throws(()=>authorizeEgress(signed(),secret,'/api/map/cache',1031000));
assert.throws(()=>authorizeEgress(signed(),'wrong','/api/map/cache',1000000));
assert.throws(()=>authorizeEgress(signed({profile:'arbitrary'}),secret,'/api/map/cache',1000000));
assert.equal(authorizeEgress({headers:{'x-nodedc-user-id':'operator'}},secret,'/api/map/cache'),null);
});
test('public relay destination excludes private and special address ranges',()=>{
for(const address of ['127.0.0.1','0.0.0.0','10.1.1.1','100.64.0.1','169.254.169.254','172.22.0.1','192.168.1.2','198.18.0.1','203.0.113.1','224.0.0.1','::1']) assert.equal(publicIPv4(address),false,address);
assert.equal(publicIPv4('1.1.1.1'),true);
});
test('TLS pin checked before secrets; range and cancellation preserve transport boundaries',async()=>{
const folder=await mkdtemp(join(tmpdir(),'egress-test-'));
let server;
try {
execFileSync('openssl',['req','-x509','-newkey','ed25519','-nodes','-days','1','-subj','/CN=test','-keyout',join(folder,'key'),'-out',join(folder,'cert')],{stdio:'ignore'});
const cert=await readFile(join(folder,'cert')),key=await readFile(join(folder,'key'));
const fingerprint=createHash('sha256').update(new X509Certificate(cert).raw).digest('hex');
const received=[];
server=createServer({cert,key,minVersion:'TLSv1.3'},(req,res)=>{
received.push({url:req.url,headers:req.headers});
res.writeHead(206,{'content-type':'application/octet-stream','content-range':'bytes 1-3/4','set-cookie':'secret'});res.end('abc');
});
await new Promise(resolve=>server.listen(0,'127.0.0.1',resolve));
const dependencies={lookup:async()=>[{address:'1.1.1.1'}],connect:options=>connect({...options,host:'127.0.0.1',port:server.address().port})};
const binding={endpoint:'https://1.1.1.1:19446',token:'c'.repeat(43),fingerprint};
await assert.rejects(relayFetch({...binding,fingerprint:'0'.repeat(64)},'https://assets.ion.cesium.com/a',{},dependencies),/identity_changed/);
assert.equal(received.length,0);
const response=await relayFetch(binding,'https://assets.ion.cesium.com/a',{headers:{authorization:'Bearer provider-secret',range:'bytes=1-3',cookie:'no'}},dependencies);
assert.equal(response.status,206);assert.equal(await response.text(),'abc');
assert.equal(response.headers.get('content-range'),'bytes 1-3/4');assert.equal(response.headers.get('set-cookie'),null);
assert.equal(received[0].headers.authorization,'Bearer '+binding.token);
assert.equal(received[0].headers['x-nodedc-upstream-authorization'],'Bearer provider-secret');
assert.equal(received[0].headers.cookie,undefined);
const cancelled=new AbortController();cancelled.abort();
await assert.rejects(relayFetch(binding,'https://assets.ion.cesium.com/a',{signal:cancelled.signal},dependencies));
assert.equal(received.length,1,'A cancelled request must not send credentials');
await assert.rejects(relayFetch(binding,'https://private.example/',{},dependencies));
} finally {if(server)await new Promise(resolve=>server.close(resolve));await rm(folder,{recursive:true,force:true});}
});