Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

Forward request in image proxy (#1666)

Bogdan Chadkin2ee5be960bd7
diff --git a/apps/builder/app/routes/asset.image.$name.ts b/apps/builder/app/routes/asset.image.$name.ts
index 7125f50ef0..f5cefbe641 100644
--- a/apps/builder/app/routes/asset.image.$name.ts
+++ b/apps/builder/app/routes/asset.image.$name.ts
@@ -16,15 +16,29 @@ export const loader = async ({ params, request }: LoaderArgs) => {
const width = url.searchParams.get("width");
const quality = url.searchParams.get("quality");
const format = url.searchParams.get("format");
+
if (width === null || quality === null || format === null) {
throw Error("Options are invalid");
}
+
const options = `width=${width},quality=${quality},format=${format}`;
+ // Allow direct image access, and from the same origin
+ const refererRawUrl = request.headers.get("referer");
+ const refererUrl = refererRawUrl === null ? url : new URL(refererRawUrl);
+ if (refererUrl.origin !== url.origin) {
+ throw new Response("Forbidden", {
+ status: 403,
+ });
+ }
+
if (env.RESIZE_ORIGIN !== undefined) {
const assetUrl = `${env.ASSET_BASE_URL}${name}`;
// @todo add secret ti avoid exploiting our server
- return fetch(`${env.RESIZE_ORIGIN}/cdn-cgi/image/${options}/${assetUrl}`);
+ const imageUrl = `${env.RESIZE_ORIGIN}/cdn-cgi/image/${options}/${assetUrl}`;
+ const response = await fetch(imageUrl, request);
+ response.headers.set("Access-Control-Allow-Origin", url.origin);
+ return response;
}
if (env.FILE_UPLOAD_PATH === undefined) {