|
| 1 | +/** |
| 2 | + * Utilities for handling HTTP redirects with validation |
| 3 | + */ |
| 4 | + |
| 5 | +import type { AstroConfig } from '../../types/public/config.js'; |
| 6 | +import { isRemoteAllowed } from '@astrojs/internal-helpers/remote'; |
| 7 | + |
| 8 | +export type RemoteImageConfig = Pick<AstroConfig['image'], 'remotePatterns' | 'domains'>; |
| 9 | + |
| 10 | +export type FetchRedirectOptions = { |
| 11 | + /** |
| 12 | + * URL to fetch (either string or URL object) |
| 13 | + */ |
| 14 | + url: string | URL; |
| 15 | + |
| 16 | + /** |
| 17 | + * Headers to include in the request (optional) |
| 18 | + */ |
| 19 | + headers?: Headers; |
| 20 | + |
| 21 | + /** |
| 22 | + * Image config for validating redirect destinations (optional) |
| 23 | + */ |
| 24 | + imageConfig: RemoteImageConfig; |
| 25 | + |
| 26 | + /** |
| 27 | + * Fetch function to use (default: globalThis.fetch) |
| 28 | + */ |
| 29 | + fetchFn?: typeof fetch; |
| 30 | + |
| 31 | + /** |
| 32 | + * Maximum number of redirects to follow (default: 10) |
| 33 | + */ |
| 34 | + redirectLimit?: number; |
| 35 | + |
| 36 | + /** |
| 37 | + * Error handler for redirect depth exceeded (default: generic Error) |
| 38 | + */ |
| 39 | + onMaxRedirectsExceeded?: (url: string) => Error; |
| 40 | + |
| 41 | + /** |
| 42 | + * Error handler for missing Location header (default: generic Error) |
| 43 | + */ |
| 44 | + onMissingLocationHeader?: (status: number, url: string) => Error; |
| 45 | + |
| 46 | + /** |
| 47 | + * Error handler for disallowed redirect (default: generic Error) |
| 48 | + */ |
| 49 | + onDisallowedRedirect?: (currentUrl: string, targetUrl: string) => Error; |
| 50 | +}; |
| 51 | + |
| 52 | +/** |
| 53 | + * Recursively follows HTTP redirects with validation according to the image configuration. |
| 54 | + * |
| 55 | + * If any of the domains in the redirect chain are not allowed by either `image.remotePatterns` |
| 56 | + * or `image.domains`, this function will throw an error for a disallowed redirect. |
| 57 | + * |
| 58 | + * @param options The options for this fetch call. |
| 59 | + */ |
| 60 | +export async function fetchWithRedirects(options: FetchRedirectOptions): Promise<Response> { |
| 61 | + const { |
| 62 | + url, |
| 63 | + headers, |
| 64 | + imageConfig, |
| 65 | + fetchFn = globalThis.fetch, |
| 66 | + redirectLimit = 10, |
| 67 | + onMaxRedirectsExceeded = (_u) => new Error('Maximum redirect depth exceeded'), |
| 68 | + onMissingLocationHeader = (_s, _u) => |
| 69 | + new Error(`Redirect response ${_s} missing Location header`), |
| 70 | + onDisallowedRedirect = (_current, _target) => |
| 71 | + new Error( |
| 72 | + `The image at ${_current} redirected to ${_target}, which is not an allowed remote location.`, |
| 73 | + ), |
| 74 | + } = options; |
| 75 | + |
| 76 | + if (redirectLimit <= 0) { |
| 77 | + throw onMaxRedirectsExceeded(typeof url === 'string' ? url : url.toString()); |
| 78 | + } |
| 79 | + |
| 80 | + const urlString = typeof url === 'string' ? url : url.toString(); |
| 81 | + const req = new Request(url, { headers }); |
| 82 | + const res = await fetchFn(req, { redirect: 'manual' }); |
| 83 | + |
| 84 | + // Handle redirects (301, 302, 303, 307, 308 are actual redirects, not 304 Not Modified) |
| 85 | + if ([301, 302, 303, 307, 308].includes(res.status)) { |
| 86 | + const location = res.headers.get('Location'); |
| 87 | + if (!location) { |
| 88 | + throw onMissingLocationHeader(res.status, urlString); |
| 89 | + } |
| 90 | + |
| 91 | + // Resolve the redirect URL relative to the current URL |
| 92 | + const redirectUrl = new URL(location, urlString).toString(); |
| 93 | + |
| 94 | + // Validate that the redirect target matches allowed patterns |
| 95 | + if ( |
| 96 | + !isRemoteAllowed(redirectUrl, { |
| 97 | + domains: imageConfig.domains ?? [], |
| 98 | + remotePatterns: imageConfig.remotePatterns ?? [], |
| 99 | + }) |
| 100 | + ) { |
| 101 | + throw onDisallowedRedirect(urlString, redirectUrl); |
| 102 | + } |
| 103 | + |
| 104 | + // Recursively follow the redirect |
| 105 | + return fetchWithRedirects({ |
| 106 | + url: redirectUrl, |
| 107 | + headers, |
| 108 | + imageConfig, |
| 109 | + fetchFn, |
| 110 | + redirectLimit: redirectLimit - 1, |
| 111 | + onMaxRedirectsExceeded, |
| 112 | + onMissingLocationHeader, |
| 113 | + onDisallowedRedirect, |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + return res; |
| 118 | +} |
0 commit comments