36 lines
4.7 KiB
JavaScript
36 lines
4.7 KiB
JavaScript
import test from 'node:test';import assert from 'node:assert/strict';import {createServer} from 'node:http';
|
|
import {createForgejoProxy,forgejoIdentityHeader} from '../node/forgejo-proxy.mjs';
|
|
import {mkdtemp,rm} from 'node:fs/promises';
|
|
import {tmpdir} from 'node:os';import {join} from 'node:path';import {DatabaseSync} from 'node:sqlite';
|
|
import {gitProxyIdentity} from '../node/git-identity.mjs';
|
|
import {createApplication} from '../node/server.mjs';
|
|
const listen=s=>new Promise(r=>s.listen(0,'127.0.0.1',r));const close=s=>new Promise(r=>s.close(r));
|
|
test('Forgejo proxy leaves central SSO routes to the application and injects only verified identities',async()=>{
|
|
const upstream=createServer((req,res)=>res.end(JSON.stringify({identity:req.headers['x-dune-test-user'],auth:req.headers.authorization})));await listen(upstream);
|
|
let lookups=0;const proxy=createForgejoProxy({FORGEJO_URL:'http://127.0.0.1:'+upstream.address().port,FORGEJO_AUTH_HEADER:'X-Dune-Test-User'},async()=>{lookups++;return 'verified_dev';});
|
|
const server=createServer((req,res)=>{if(!proxy(req,res)){res.statusCode=404;res.end('central route');}});await listen(server);
|
|
try{const root='http://127.0.0.1:'+server.address().port;
|
|
assert.equal((await fetch(root+'/git/sso/start')).status,404);assert.equal(lookups,0);
|
|
const page=await (await fetch(root+'/git/dev/MC',{headers:{'X-Dune-Test-User':'forged_owner'}})).json();assert.equal(page.identity,'verified_dev');assert.equal(lookups,1);
|
|
const cli=await (await fetch(root+'/git/dev/MC.git/info/refs',{headers:{Authorization:'Basic existing','X-Dune-Test-User':'forged_owner'}})).json();assert.equal(cli.identity,undefined);assert.equal(cli.auth,'Basic existing');assert.equal(lookups,1);
|
|
}finally{await close(server);await close(upstream);}
|
|
});
|
|
test('central application mounts existing Git SSO before fallback content and keeps sign-in state private',async()=>{
|
|
const writes=[];const db={prepare(sql){return {bind(...args){return {async run(){writes.push({sql,args});return {meta:{changes:1}};},async first(){return null;},async all(){return {results:[]}}}}}}};
|
|
const app=createApplication(db,{PUBLIC_ORIGIN:'https://www.dunerealms.com',CP_ORIGIN:'https://cp.dunerealms.com'});
|
|
const response=await app(new Request('https://www.dunerealms.com/git/sso/start?return_to=%2Fgit%2Fdev%2FMC%2Factions'));
|
|
assert.equal(response.status,302);const next=new URL(response.headers.get('location'));assert.equal(next.origin,'https://cp.dunerealms.com');assert.equal(next.pathname,'/sso/authorize');assert.equal(next.searchParams.get('client'),'git');assert.match(next.searchParams.get('state'),/^[A-Za-z0-9_-]{43}$/);
|
|
assert.match(response.headers.get('set-cookie'),/Secure; HttpOnly; SameSite=Lax/);assert.match(response.headers.get('content-security-policy'),/frame-ancestors 'self' https:\/\/cp.dunerealms.com/);assert.equal(writes.length,2);assert.equal(writes[1].args[1],'/git/dev/MC/actions');
|
|
const guest=await app(new Request('https://www.dunerealms.com/api/git/me'));assert.equal(guest.status,401);
|
|
});
|
|
|
|
test('runtime reads the exact configured proxy identity header from the existing Forgejo config',()=>{assert.equal(forgejoIdentityHeader('[security]\r\nREVERSE_PROXY_AUTHENTICATION_USER = X-DuneGit-Test\r\nOTHER = value'),'X-DuneGit-Test');for(const config of ['', '# REVERSE_PROXY_AUTHENTICATION_USER = X-Forged', 'REVERSE_PROXY_AUTHENTICATION_USER = invalid header'])assert.throws(()=>forgejoIdentityHeader(config));});
|
|
|
|
test('proxy sign-in uses a verified native mapping, honours revocation, and performs no role reconciliation',async()=>{
|
|
const folder=await mkdtemp(join(tmpdir(),'dune-git-identity-')),path=join(folder,'forge.db'),native=new DatabaseSync(path);
|
|
native.exec("CREATE TABLE user(id INTEGER,name TEXT,is_active INTEGER,is_admin INTEGER,prohibit_login INTEGER,type INTEGER);CREATE TABLE repository(name TEXT,description TEXT,is_private INTEGER,owner_id INTEGER);INSERT INTO user VALUES(7,'verified_dev',1,0,0,0)");
|
|
const writes=[],db={prepare(sql){return {bind(){return {async first(){if(sql.includes('FROM site_sessions'))return {id:'account-1',username:'dev',role:'user'};if(sql.includes('FROM site_git_identities'))return {forgejo_id:7,verified_at:1};throw Error('Unexpected account query');},async run(){writes.push(sql);return {meta:{changes:1}};}}}}}};
|
|
const lookup=gitProxyIdentity(db,{FORGEJO_DB_PATH:path}),request={headers:{cookie:'__Host-dune_session='+'a'.repeat(43)}};
|
|
try{assert.equal(await lookup(request),'verified_dev');assert.equal(await lookup({headers:{}}),null);native.exec('UPDATE user SET prohibit_login=1');assert.equal(await lookup(request),null);assert.ok(writes.every(sql=>sql.startsWith('UPDATE site_git_identities')||sql.startsWith('DELETE FROM website_badges')));}
|
|
finally{native.close();await rm(folder,{recursive:true,force:true});}
|
|
});
|

Staff