mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
fix(assets): Forward headers from the original request to the internal request to the image (#10775)
This commit is contained in:
parent
01cb41763e
commit
0684312145
2 changed files with 14 additions and 5 deletions
5
.changeset/rich-spoons-fold.md
Normal file
5
.changeset/rich-spoons-fold.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
"astro": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fixes assets endpoint in serverless returning 404 in certain situations where the website might be under a protected route
|
|
@ -7,9 +7,12 @@ import { isRemoteAllowed } from '../utils/remotePattern.js';
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
import { imageConfig } from 'astro:assets';
|
import { imageConfig } from 'astro:assets';
|
||||||
|
|
||||||
async function loadRemoteImage(src: URL) {
|
async function loadRemoteImage(src: URL, headers: Headers) {
|
||||||
try {
|
try {
|
||||||
const res = await fetch(src);
|
const res = await fetch(src, {
|
||||||
|
// Forward all headers from the original request
|
||||||
|
headers,
|
||||||
|
});
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
return undefined;
|
return undefined;
|
||||||
|
@ -41,15 +44,16 @@ export const GET: APIRoute = async ({ request }) => {
|
||||||
|
|
||||||
let inputBuffer: ArrayBuffer | undefined = undefined;
|
let inputBuffer: ArrayBuffer | undefined = undefined;
|
||||||
|
|
||||||
const sourceUrl = isRemotePath(transform.src)
|
const isRemoteImage = isRemotePath(transform.src);
|
||||||
|
const sourceUrl = isRemoteImage
|
||||||
? new URL(transform.src)
|
? new URL(transform.src)
|
||||||
: new URL(transform.src, url.origin);
|
: new URL(transform.src, url.origin);
|
||||||
|
|
||||||
if (isRemotePath(transform.src) && isRemoteAllowed(transform.src, imageConfig) === false) {
|
if (isRemoteImage && isRemoteAllowed(transform.src, imageConfig) === false) {
|
||||||
return new Response('Forbidden', { status: 403 });
|
return new Response('Forbidden', { status: 403 });
|
||||||
}
|
}
|
||||||
|
|
||||||
inputBuffer = await loadRemoteImage(sourceUrl);
|
inputBuffer = await loadRemoteImage(sourceUrl, isRemoteImage ? new Headers() : request.headers);
|
||||||
|
|
||||||
if (!inputBuffer) {
|
if (!inputBuffer) {
|
||||||
return new Response('Not Found', { status: 404 });
|
return new Response('Not Found', { status: 404 });
|
||||||
|
|
Loading…
Reference in a new issue