Skip to content

Commit

Permalink
Secure image proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
soruly committed Mar 23, 2024
1 parent b072896 commit ec3e36e
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion image-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ const errorResponse = (errorMessage) =>
});

const handleRequest = async (originalRequest) => {
if (originalRequest.headers.get("referrer")) {
return new Response("Forbidden", {
status: 403,
statusText: "Forbidden",
});
}

let originalURL = new URL(originalRequest.url);
if (!originalURL.searchParams.get("url")) {
return errorResponse("Error: Cannot get url from param");
Expand Down Expand Up @@ -88,9 +95,11 @@ const handleRequest = async (originalRequest) => {
return errorResponse("Error: Content-Type is not image or video or application/octet-stream");
}

return new Response(response.body, {
const res = new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: response.headers,
});
res.headers.set("Access-Control-Allow-Origin", "https://trace.moe");
return res;
};

0 comments on commit ec3e36e

Please sign in to comment.