1
0

Security fixes

Co-Authored-By: dakkar <dakkar@thenautilus.net>
This commit is contained in:
Julia Johannesen
2025-04-27 13:05:09 -04:00
parent 9e13c375c5
commit 0bb4e57b0c
14 changed files with 56 additions and 26 deletions
+10
View File
@@ -0,0 +1,10 @@
/*
* SPDX-FileCopyrightText: dakkar and other Sharkey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
export function clamp(value: number, min: number, max: number) {
if (value > max) return max;
if (value < min) return min;
return value;
}
+17
View File
@@ -26,3 +26,20 @@ export function extractDomain(url: string) {
const match = url.match(/^(?:https?:)?(?:\/\/)?(?:[^@\n]+@)?([^:\/\n]+)/im);
return match ? match[1] : null;
}
export function maybeMakeRelative(urlStr: string, baseStr: string): string {
try {
const baseObj = new URL(baseStr);
const urlObj = new URL(urlStr);
/* in all places where maybeMakeRelative is used, baseStr is the
* instance's public URL, which can't have path components, so the
* relative URL will always have the whole path from the urlStr
*/
if (urlObj.origin === baseObj.origin) {
return urlObj.pathname + urlObj.search + urlObj.hash;
}
return urlStr;
} catch (e) {
return '';
}
}