Skip to content

Commit fd960f3

Browse files
committed
feat: allow wildcard domain
1 parent 159e36b commit fd960f3

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
const version = "0.0.3"
22

3-
const allowedDomains = process?.env?.ALLOWED_REMOTE_DOMAINS?.split(",") || ["*"];
3+
let allowedDomains = process?.env?.ALLOWED_REMOTE_DOMAINS?.split(",") || ["*"];
44
let imgproxyUrl = process?.env?.IMGPROXY_URL || "http://imgproxy:8080";
55
if (process.env.NODE_ENV === "development") {
66
imgproxyUrl = "http://localhost:8888"
77
}
8+
allowedDomains = allowedDomains.map(d => d.trim());
89

910
Bun.serve({
1011
port: 3000,
@@ -30,7 +31,13 @@ async function resize(url) {
3031
const preset = "pr:sharp"
3132
const src = url.pathname.split("/").slice(2).join("/");
3233
const origin = new URL(src).hostname;
33-
if (!allowedDomains.includes("*") && !allowedDomains.includes(origin)) {
34+
const allowed = allowedDomains.filter(domain => {
35+
if (domain === "*") return true;
36+
if (domain === origin) return true;
37+
if (domain.startsWith("*.") && origin.endsWith(domain.split("*.").pop())) return true;
38+
return false;
39+
})
40+
if (allowed.length === 0) {
3441
return new Response(`Domain (${origin}) not allowed. More details here: https://github.com/coollabsio/next-image-transformation`, { status: 403 });
3542
}
3643
const width = url.searchParams.get("width") || 0;

0 commit comments

Comments
 (0)