1
0
Fork 0
mirror of https://github.com/cheeaun/phanpy.git synced 2025-03-30 01:21:36 +01:00

Guard against invalid URLs

This commit is contained in:
Lim Chee Aun 2023-12-03 20:40:00 +08:00
parent 012e944a53
commit cbb7378601
3 changed files with 28 additions and 17 deletions

View file

@ -22,6 +22,7 @@ const Link = forwardRef((props, ref) => {
// Handle encodeURIComponent of searchParams values
if (!!hash && hash !== '/' && hash.includes('?')) {
try {
const parsedHash = new URL(hash, location.origin); // Fake base URL
if (parsedHash.searchParams.size) {
const searchParamsStr = Array.from(parsedHash.searchParams.entries())
@ -29,6 +30,7 @@ const Link = forwardRef((props, ref) => {
.join('&');
hash = parsedHash.pathname + '?' + searchParamsStr;
}
} catch (e) {}
}
const isActive = hash === to || decodeURIComponent(hash) === to;

View file

@ -2269,7 +2269,12 @@ function _unfurlMastodonLink(instance, url) {
theURL = `https://${finalURL}`;
}
const urlObj = new URL(theURL);
let urlObj;
try {
urlObj = new URL(theURL);
} catch (e) {
return;
}
const domain = urlObj.hostname;
const path = urlObj.pathname;
// Regex /:username/:id, where username = @username or @username@domain, id = number

View file

@ -1,4 +1,5 @@
export default function isMastodonLinkMaybe(url) {
try {
const { pathname, hash } = new URL(url);
return (
/^\/.*\/\d+$/i.test(pathname) ||
@ -8,4 +9,7 @@ export default function isMastodonLinkMaybe(url) {
/^\/(notice|objects)\/[a-z0-9-]+$/i.test(pathname) || // Pleroma
/#\/[^\/]+\.[^\/]+\/s\/.+/i.test(hash) // Phanpy 🫣
);
} catch (e) {
return false;
}
}