Use native URL parsing

Co-authored-by: Corentin THOMASSET <corentin.thomasset74@gmail.com>
This commit is contained in:
sharevb 2024-03-03 11:44:12 +01:00 committed by GitHub
parent 73deeb30e4
commit ec6785e362
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,28 +1,3 @@
export function decodeSafeLinksURL(safeLinksUrl: string) {
// Decode the ATP SafeLinks URL
let originalURL;
try {
// Decode the URL
originalURL = decodeURIComponent(safeLinksUrl);
// Check if it matches the SafeLinks pattern
if (originalURL.match(/\.safelinks\.protection\.outlook\.com\/\?url=.+&data=/)) {
// Extract the original URL
originalURL = originalURL.split('?url=')[1].split('&data=')[0];
}
else if (originalURL.match(/\.safelinks\.protection\.outlook\.com\/\?url=.+&amp;data=/)) {
// Handle the case where "&" is encoded as "&amp;"
originalURL = originalURL.split('?url=')[1].split('&amp;data=')[0];
}
else {
throw new Error('Invalid SafeLinks URL provided');
}
}
catch (error: any) {
// Handle errors
throw new Error(`Failed to decode SafeLinks URL: ${error}`);
}
return originalURL;
return new URL(safeLinksUrl).searchParams.get('url')
}