Validate the configured Forgejo proxy header using actual INI line syntax

This commit is contained in:
Dev 2026-11-13 00:35:52 +01:00
commit 6fa811a393
3 changed files with 6 additions and 3 deletions

View file

@ -1,4 +1,5 @@
import {request as httpRequest} from 'node:http';
export function forgejoIdentityHeader(config){const value=String(config).match(/^REVERSE_PROXY_AUTHENTICATION_USER[ \t]*=[ \t]*([A-Za-z0-9-]+)[ \t\r]*$/mi)?.[1];if(!value)throw Error('DuneGit proxy identity header is not configured.');return value;}
export function createForgejoProxy(settings,identityFor=async()=>null){
if(!settings.FORGEJO_URL)return ()=>false;
const upstream=new URL(settings.FORGEJO_URL),origin=new URL(settings.PUBLIC_ORIGIN||'https://dunerealms.com');

View file

@ -10,7 +10,7 @@ import {openDatabase} from './mysql.mjs';
import {createAuth,identity,safeReturn} from './auth.mjs';
import {createWorldStatus} from './world-status.mjs';
import {createPublicStaffRoster,initializePublicRoster} from './public-staff-roster.mjs';
import {createForgejoProxy} from './forgejo-proxy.mjs';
import {createForgejoProxy,forgejoIdentityHeader} from './forgejo-proxy.mjs';
import {createContact} from './contact.mjs';
import {createGameLink} from './game-link.mjs';
import {createCommunityRewards} from './community-rewards.mjs';
@ -80,7 +80,7 @@ export function createApplication(db,settings={}) {
export async function start(settings=process.env) {
const db=await openDatabase(settings);await initializeRanks(db);await initializeGitIdentities(db);await initializePublicRoster(db);await initializeCompanyRegistry(db);await initializeShareLedger(db);const handle=createApplication(db,settings),origin=new URL(settings.PUBLIC_ORIGIN||'https://dunerealms.com').origin;
let forgeHeader=settings.FORGEJO_AUTH_HEADER;
if(!forgeHeader&&settings.FORGEJO_URL){const config=await readFile(settings.FORGEJO_CONFIG_FILE||'/home/dunerealms/apps/forgejo/custom/conf/app.ini','utf8');forgeHeader=config.match(/^REVERSE_PROXY_AUTHENTICATION_USER\s*=\s*(.+)$/mi)?.[1]?.trim();}
if(!forgeHeader&&settings.FORGEJO_URL){const config=await readFile(settings.FORGEJO_CONFIG_FILE||'/home/dunerealms/apps/forgejo/custom/conf/app.ini','utf8');forgeHeader=forgejoIdentityHeader(config);}
if(settings.FORGEJO_URL&&!/^[A-Za-z0-9-]+$/.test(forgeHeader||''))throw Error('DuneGit proxy identity header is not configured.');
const forgejo=createForgejoProxy({...settings,FORGEJO_AUTH_HEADER:forgeHeader},gitProxyIdentity(db,settings));
const server=createServer(async(req,res)=>{

View file

@ -1,5 +1,5 @@
import test from 'node:test';import assert from 'node:assert/strict';import {createServer} from 'node:http';
import {createForgejoProxy} from '../node/forgejo-proxy.mjs';
import {createForgejoProxy,forgejoIdentityHeader} from '../node/forgejo-proxy.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()=>{
@ -20,3 +20,5 @@ test('central application mounts existing Git SSO before fallback content and ke
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));});