50 lines
2.2 KiB
JavaScript
50 lines
2.2 KiB
JavaScript
export function publicBillingSettings(value = {}) {
|
|
// Safe for the PUBLIC customer portal. Never return stripe_sk or other keys.
|
|
return Object.fromEntries(['iban', 'bank_name', 'account_holder', 'observations', 'stripe_enabled']
|
|
.filter(k => Object.hasOwn(value, k)).map(k => [k, value[k]]));
|
|
}
|
|
|
|
const secretKey = /password|secret|token|api[_-]?key|stripe[_-]?[sp]k|signature|pdf_base64|contract_pdf_url|pdf_raw_data/i;
|
|
export function redact(value) {
|
|
if (Array.isArray(value)) return value.map(redact);
|
|
if (value && typeof value === 'object') return Object.fromEntries(Object.entries(value)
|
|
.filter(([key]) => !secretKey.test(key)).map(([key, val]) => [key, redact(val)]));
|
|
return value;
|
|
}
|
|
|
|
export function normalizeOrigin(value) {
|
|
const url = new URL(value);
|
|
if (url.protocol !== 'https:' || url.username || url.password || url.pathname !== '/' || url.search || url.hash)
|
|
throw new Error('INTEGRA_MCP_PUBLIC_ORIGIN debe ser un origen HTTPS sin ruta, por ejemplo https://tu-backend.es');
|
|
return url.origin;
|
|
}
|
|
|
|
export function safeRedirect(value) {
|
|
try {
|
|
if (typeof value !== 'string') return false;
|
|
const u = new URL(value);
|
|
return !u.username && !u.password && !u.hash && (u.protocol === 'https:' || isLoopbackRedirect(value));
|
|
} catch { return false; }
|
|
}
|
|
|
|
export function isLoopbackRedirect(value) {
|
|
try {
|
|
if (typeof value !== 'string' || !/^http:\/\/127\.0\.0\.1(?::[0-9]+)?(?:\/|$)/.test(value)) return false;
|
|
const u = new URL(value);
|
|
return u.protocol === 'http:' && u.hostname === '127.0.0.1' && !u.username && !u.password && !u.hash;
|
|
} catch { return false; }
|
|
}
|
|
|
|
export function redirectAllowed(requested, registeredUris) {
|
|
if (!safeRedirect(requested)) return false;
|
|
return registeredUris.some(registered => {
|
|
if (!safeRedirect(registered)) return false;
|
|
if (requested === registered) return true;
|
|
if (!isLoopbackRedirect(requested) || !isLoopbackRedirect(registered)) return false;
|
|
const a = new URL(requested), b = new URL(registered);
|
|
// RFC 8252: a native client chooses a free local port at login. Only that
|
|
// port may vary; the registered host, path and query must still match.
|
|
return a.pathname === b.pathname && a.search === b.search;
|
|
});
|
|
}
|