Skip to content

Commit

Permalink
fix (backend): verify object id host matches final URL when fetching …
Browse files Browse the repository at this point in the history
  • Loading branch information
2 people authored and atsu1125 committed Mar 31, 2024
1 parent 0f5dec0 commit 68d3e25
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/remote/activitypub/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export async function signedGet(url: string, user: ILocalUser) {
export async function apGet(
url: string,
user?: ILocalUser,
redirects: boolean = true
): Promise<IObject> {
redirects: boolean = true,
): Promise<{ finalUrl: string; content: IObject }> {
if (!isValidUrl(url)) {
throw new StatusError('Invalid URL', 400);
}
Expand Down Expand Up @@ -136,7 +136,10 @@ export async function apGet(
const text = await res.text();
if (text.length > 65536) throw new Error("too big result");

return JSON.parse(text) as IObject;
return {
finalUrl: res.url,
content: JSON.parse(text) as IObject,
};
}

function validateContentType(contentType: string): boolean {
Expand Down
9 changes: 8 additions & 1 deletion src/remote/activitypub/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default class Resolver {
this.user = await getInstanceActor();
}

const object = await apGet(value, this.user);
const { finalUrl, content: object } = await apGet(value, this.user);

if (object == null || (
Array.isArray(object['@context']) ?
Expand All @@ -92,6 +92,13 @@ export default class Resolver {
throw new Error('invalid response');
}

if (
object.id != null &&
new URL(finalUrl).host != new URL(object.id).host
) {
throw new Error("Object ID host doesn't match final url host");
}

return object;
}

Expand Down

0 comments on commit 68d3e25

Please sign in to comment.