Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr SMS Gateway8 * File : backend/src/security/ssrf.ts9 * Module : security10 * Repository : https://git.spboucher.ai/maclustr-sms11 * Project Website : https://sms.spboucher.ai12 * Created : 2026-11-3013 * Last Modified : 2026-11-3014 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142import { lookup } from "node:dns/promises"43import { BlockList, isIP } from "node:net"44import { config } from "../config.ts"45import { unprocessable } from "../util.ts"4647const blocked = new BlockList()48for (const [net, bits] of [["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],49 ["192.0.0.0", 24], ["192.0.2.0", 24], ["192.168.0.0", 16], ["198.18.0.0", 15], ["198.51.100.0", 24], ["203.0.113.0", 24], ["224.0.0.0", 3]] as const)50 blocked.addSubnet(net, bits, "ipv4")51for (const [net, bits] of [["::", 127], ["fc00::", 7], ["fe80::", 10], ["ff00::", 8], ["64:ff9b::", 96], ["2001:db8::", 32]] as const)52 blocked.addSubnet(net, bits, "ipv6")5354export function isPrivateAddress(ip: string): boolean {55 const fam = isIP(ip)56 if (!fam) return true57 if (fam === 6) {58 const m = ip.toLowerCase().match(/^::ffff:(\d+\.\d+\.\d+\.\d+)$/)59 if (m) return blocked.check(m[1], "ipv4")60 return blocked.check(ip, "ipv6")61 }62 return blocked.check(ip, "ipv4")63}6465/** Validation syntaxique d'une URL de webhook (à la création). */66export function checkWebhookUrl(raw: string): URL {67 let u: URL68 try { u = new URL(raw) } catch { throw unprocessable("invalid_webhook_url", "URL de webhook invalide") }69 const host = u.hostname.toLowerCase()70 const allowed = config.webhookAllowHosts.includes(host)71 if (u.username || u.password) throw unprocessable("invalid_webhook_url", "l'URL ne doit pas contenir d'identifiants")72 if (u.protocol !== "https:" && !(u.protocol === "http:" && allowed)) throw unprocessable("invalid_webhook_url", "seules les URL https sont acceptées")73 if (!allowed && (isIP(host.replace(/^\[|\]$/g, "")) || host === "localhost" || host.endsWith(".local") || host.endsWith(".internal") || !host.includes(".")))74 throw unprocessable("invalid_webhook_url", "cette destination n'est pas autorisée")75 return u76}7778/** Résolution au moment de l'envoi : toutes les adresses doivent être publiques ; l'adresse retenue est épinglée pour la connexion. */79export async function resolvePublic(u: URL): Promise<{ address: string; family: number }> {80 const host = u.hostname.toLowerCase()81 const addrs = await lookup(host, { all: true })82 if (!addrs.length) throw new Error("résolution DNS vide")83 if (!config.webhookAllowHosts.includes(host)) for (const a of addrs) if (isPrivateAddress(a.address)) throw new Error("destination privée refusée (protection SSRF)")84 return addrs[0]85}86