Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
61 lines
2.8 KiB
JavaScript
61 lines
2.8 KiB
JavaScript
import dns from 'dns';
|
|
import net from 'net';
|
|
import { GitError } from './errors.js';
|
|
|
|
// URL- und Host-Prüfung (ADR 0003 §4, SSRF-Schutz).
|
|
|
|
export const parseRepoUrl = (value, { allowHttp = false } = {})=>{
|
|
let u;
|
|
try { u = new URL(String(value)); } catch { throw new GitError('G08', 'Die Repository-URL ist ungültig.'); }
|
|
const okProtocol = u.protocol === 'https:' || (allowHttp && u.protocol === 'http:');
|
|
if(!okProtocol) throw new GitError('G08', 'Nur https://-URLs sind erlaubt.');
|
|
if(u.username || u.password) throw new GitError('G08', 'Die URL darf keine Zugangsdaten enthalten.');
|
|
if(u.search || u.hash) throw new GitError('G08', 'Die URL darf keine Query oder kein Fragment enthalten.');
|
|
if(!u.hostname || u.pathname === '/' || u.pathname === '') throw new GitError('G08', 'Die URL muss einen Repository-Pfad enthalten.');
|
|
if(String(value).length > 500) throw new GitError('G08', 'Die URL ist zu lang.');
|
|
return u;
|
|
};
|
|
|
|
const hostMatches = (host, pattern)=>pattern.startsWith('*.') ? host.endsWith(pattern.slice(1)) && host.length > pattern.length - 1 : host === pattern;
|
|
|
|
const blocked = new net.BlockList();
|
|
[
|
|
['0.0.0.0', 8], ['10.0.0.0', 8], ['100.64.0.0', 10], ['127.0.0.0', 8], ['169.254.0.0', 16],
|
|
['172.16.0.0', 12], ['192.0.0.0', 24], ['192.168.0.0', 16], ['198.18.0.0', 15], ['224.0.0.0', 3]
|
|
].forEach(([n, p])=>blocked.addSubnet(n, p, 'ipv4'));
|
|
[
|
|
['::', 128], ['::1', 128], ['fc00::', 7], ['fe80::', 10], ['ff00::', 8],
|
|
// IPv4-gemappte Adressen (::ffff:a.b.c.d / ::ffff:a9fe:a9fe) prüft BlockList selbst gegen die IPv4-Regeln
|
|
['64:ff9b::', 96], // NAT64
|
|
['2002::', 16] // 6to4
|
|
].forEach(([n, p])=>blocked.addSubnet(n, p, 'ipv6'));
|
|
|
|
export const isPrivateAddress = (address)=>{
|
|
const ip = String(address).toLowerCase().replace(/^\[|\]$/g, '').split('%')[0];
|
|
if(net.isIPv4(ip)) return blocked.check(ip, 'ipv4');
|
|
if(net.isIPv6(ip)) return blocked.check(ip, 'ipv6');
|
|
return true;
|
|
};
|
|
|
|
/**
|
|
* Wirft G08, wenn die URL nicht erlaubt ist. `lookup` ist für Tests austauschbar.
|
|
*/
|
|
export const assertUrlAllowed = async (value, cfg, { lookup = dns.promises.lookup } = {})=>{
|
|
const u = parseRepoUrl(value, cfg);
|
|
const host = u.hostname.toLowerCase().replace(/^\[|\]$/g, '');
|
|
if(cfg.allowedHosts?.length) {
|
|
if(!cfg.allowedHosts.some((p)=>hostMatches(host, p)))
|
|
throw new GitError('G08', 'Dieser Git-Host ist auf dieser Instanz nicht erlaubt.');
|
|
return u;
|
|
}
|
|
if(cfg.allowPrivate) return u;
|
|
let addrs;
|
|
if(net.isIP(host)) addrs = [{ address: host }];
|
|
else {
|
|
try { addrs = await lookup(host, { all: true }); } catch { throw new GitError('G04', 'Der Git-Host konnte nicht aufgelöst werden.'); }
|
|
}
|
|
if(!addrs.length || addrs.some((a)=>isPrivateAddress(a.address)))
|
|
throw new GitError('G08', 'Interne oder private Adressen sind als Git-Ziel nicht erlaubt.');
|
|
return u;
|
|
};
|