mirror of
https://iceshrimp.dev/crimekillz/trashposs
synced 2024-11-22 08:53:48 +01:00
[backend] Permit redirects for AP object lookups
This commit is contained in:
parent
695528bed7
commit
8d7d95fd23
@ -57,6 +57,7 @@ export async function getResponse(args: {
|
|||||||
headers: Record<string, string>;
|
headers: Record<string, string>;
|
||||||
timeout?: number;
|
timeout?: number;
|
||||||
size?: number;
|
size?: number;
|
||||||
|
redirect?: RequestRedirect;
|
||||||
}) {
|
}) {
|
||||||
const timeout = args.timeout || 10 * 1000;
|
const timeout = args.timeout || 10 * 1000;
|
||||||
|
|
||||||
@ -73,8 +74,13 @@ export async function getResponse(args: {
|
|||||||
size: args.size || 10 * 1024 * 1024,
|
size: args.size || 10 * 1024 * 1024,
|
||||||
agent: getAgentByUrl,
|
agent: getAgentByUrl,
|
||||||
signal: controller.signal,
|
signal: controller.signal,
|
||||||
|
redirect: args.redirect
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (args.redirect === "manual" && [301,302,307,308].includes(res.status)) {
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
throw new StatusError(
|
throw new StatusError(
|
||||||
`${res.status} ${res.statusText}`,
|
`${res.status} ${res.statusText}`,
|
||||||
|
@ -34,8 +34,9 @@ export default async (user: { id: User["id"] }, url: string, object: any) => {
|
|||||||
* Get AP object with http-signature
|
* Get AP object with http-signature
|
||||||
* @param user http-signature user
|
* @param user http-signature user
|
||||||
* @param url URL to fetch
|
* @param url URL to fetch
|
||||||
|
* @param redirects whether or not to accept redirects
|
||||||
*/
|
*/
|
||||||
export async function signedGet(url: string, user: { id: User["id"] }) {
|
export async function signedGet(url: string, user: { id: User["id"] }, redirects: boolean = true) {
|
||||||
apLogger.debug(`Running signedGet on url: ${url}`);
|
apLogger.debug(`Running signedGet on url: ${url}`);
|
||||||
const keypair = await getUserKeypair(user.id);
|
const keypair = await getUserKeypair(user.id);
|
||||||
|
|
||||||
@ -54,7 +55,15 @@ export async function signedGet(url: string, user: { id: User["id"] }) {
|
|||||||
url,
|
url,
|
||||||
method: req.request.method,
|
method: req.request.method,
|
||||||
headers: req.request.headers,
|
headers: req.request.headers,
|
||||||
|
redirect: redirects ? "manual" : "error"
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (redirects && [301,302,307,308].includes(res.status)) {
|
||||||
|
const newUrl = res.headers.get('location');
|
||||||
|
if (!newUrl) throw new Error('signedGet got redirect but no target location');
|
||||||
|
apLogger.debug(`signedGet is redirecting to ${newUrl}`);
|
||||||
|
return signedGet(newUrl, user, false);
|
||||||
|
}
|
||||||
|
|
||||||
return await res.json();
|
return await res.json();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user