Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(assets): Forward headers from the original request to the internal request to the image #10775

Merged
merged 4 commits into from
Apr 15, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: only do it for local images
  • Loading branch information
Princesseuh committed Apr 12, 2024

Verified

This commit was signed with the committer’s verified signature.
commit a0fb9929ba5d78f77a5e77b4e4ed427080e24205
12 changes: 7 additions & 5 deletions packages/astro/src/assets/endpoint/generic.ts
Original file line number Diff line number Diff line change
@@ -7,12 +7,12 @@ import { isRemoteAllowed } from '../utils/remotePattern.js';
// @ts-expect-error
import { imageConfig } from 'astro:assets';

async function loadRemoteImage(src: URL, request: Request) {
async function loadRemoteImage(src: URL, headers: Headers) {
try {
const res = await fetch(src, {
headers: {
// Forward all headers from the original request
...Object.fromEntries(request.headers.entries()),
...Object.fromEntries(headers.entries()),
},
});

@@ -46,15 +46,17 @@ export const GET: APIRoute = async ({ request }) => {

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, url.origin);

if (isRemotePath(transform.src) && isRemoteAllowed(transform.src, imageConfig) === false) {
if (isRemoteImage && isRemoteAllowed(transform.src, imageConfig) === false) {
return new Response('Forbidden', { status: 403 });
}

inputBuffer = await loadRemoteImage(sourceUrl, request);
inputBuffer = await loadRemoteImage(sourceUrl, isRemoteImage ? new Headers() : request.headers);

if (!inputBuffer) {
return new Response('Not Found', { status: 404 });