Files
rustpad/static/js/security.js
T

41 lines
1.4 KiB
JavaScript

/*
* Copyright (C) 2026 Mateusz Gruszczyński @linuxiarz.pl
* Source-Available Code / Dual-Licensed.
*
* Free for non-commercial and evaluation use under terms of BSL/GPLv3.
* Commercial or production use requires a valid paid license.
* See LICENSE file in repository root for details.
*/
const RESOURCE_ACCESS_ERROR_PATTERN = /(?:invalid password|password required|authentication required|access expired or revoked)/i;
export function isResourceAccessError(error) {
const status = Number(error?.status);
const message = typeof error === "string" ? error : error?.message;
return status === 401 || RESOURCE_ACCESS_ERROR_PATTERN.test(String(message || ""));
}
export function safeAppUrl(value, fallback = "/") {
try {
const url = new URL(String(value || ""), location.origin);
if (url.origin !== location.origin || !["http:", "https:"].includes(url.protocol)) return fallback;
return `${url.pathname}${url.search}${url.hash}`;
} catch {
return fallback;
}
}
export function safePublicUrl(value, { allowMailto = true } = {}) {
const raw = String(value || "").trim();
if (!raw || raw.startsWith("//")) return "#";
try {
const url = new URL(raw, location.origin);
if (url.protocol === "mailto:" && allowMailto) return url.href;
if (!["http:", "https:"].includes(url.protocol)) return "#";
return url.href;
} catch {
return "#";
}
}