fix: handle requests to URI IDs correctly

This commit is contained in:
Opliko
2024-04-25 17:16:30 +02:00
parent 844d1402ba
commit 1ecbf02a58

View File

@@ -37,9 +37,12 @@ Helpers.isUri = (value) => {
}; };
Helpers.query = async (id) => { Helpers.query = async (id) => {
const [username, hostname] = id.split('@');
const isUri = Helpers.isUri(id); const isUri = Helpers.isUri(id);
if ((!username || !hostname) && !isUri) { // username@host ids use acct: URI schema
const uri = isUri ? new URL(id) : new URL(`acct:${id}`);
// JS doesn't parse anything other than protocol and pathname from acct: URIs, so we need to just split id manually
const [username, hostname] = isUri ? [uri.pathname || uri.href, uri.host] : id.split('@');
if (!username || !hostname) {
return false; return false;
} }
@@ -47,8 +50,7 @@ Helpers.query = async (id) => {
return webfingerCache.get(id); return webfingerCache.get(id);
} }
// only add acct: schema if id is not an URI already const query = new URLSearchParams({ resource: uri });
const query = new URLSearchParams({ resource: `${isUri ? '' : 'acct:'}${id}` });
// Make a webfinger query to retrieve routing information // Make a webfinger query to retrieve routing information
let response; let response;
@@ -73,7 +75,7 @@ Helpers.query = async (id) => {
const { subject, publicKey } = body; const { subject, publicKey } = body;
const payload = { subject, username, hostname, actorUri, publicKey }; const payload = { subject, username, hostname, actorUri, publicKey };
const claimedId = subject.slice(5); const claimedId = new URL(subject).pathname;
webfingerCache.set(claimedId, payload); webfingerCache.set(claimedId, payload);
if (claimedId !== id) { if (claimedId !== id) {
webfingerCache.set(id, payload); webfingerCache.set(id, payload);