diff --git a/website/node/git-identity.mjs b/website/node/git-identity.mjs index cfc2a5b8..3068ac14 100644 --- a/website/node/git-identity.mjs +++ b/website/node/git-identity.mjs @@ -150,7 +150,8 @@ export function gitProxyIdentity(db,settings={}){ const headers=new Headers();if(req.headers.cookie)headers.set('cookie',req.headers.cookie); const account=await gitSessionIdentity(new Request('https://www.dunerealms.com/git/',{headers}),db); if(!account)return null; - const linked=await provisionGitIdentity(db,account,settings); + // Proxy authentication consumes the verified mapping; page loads do not assign roles. + const linked=await getGitIdentity(db,account.id,settings); return linked?.login||null; }; } diff --git a/website/tests/forgejo-embed-sso.test.mjs b/website/tests/forgejo-embed-sso.test.mjs index ae1c88ed..6d28a012 100644 --- a/website/tests/forgejo-embed-sso.test.mjs +++ b/website/tests/forgejo-embed-sso.test.mjs @@ -1,5 +1,8 @@ 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()=>{ @@ -22,3 +25,12 @@ test('central application mounts existing Git SSO before fallback content and ke }); 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});} +});