@appwarden/middleware
Version:
Instantly shut off access your app deployed on Cloudflare or Vercel
85 lines (83 loc) • 2.35 kB
JavaScript
// src/utils/is-cache-url.ts
var getEdgeConfigId = (value = "") => {
if (isValidCacheUrl.edgeConfig(value)) {
const url = new URL(value);
return url.pathname.replace("/", "");
}
return void 0;
};
var isCacheUrl = {
/**
* Checks if a URL is a valid Edge Config URL (hostname check only)
* @param value The URL to check
* @returns True if the URL is a valid Edge Config URL, false otherwise
*/
edgeConfig: (value = "") => {
try {
const url = new URL(value);
return url.hostname === "edge-config.vercel.com";
} catch {
return /^https:\/\/edge-config\.vercel\.com\//.test(value);
}
},
/**
* Checks if a URL is a valid Upstash URL (hostname check only)
* @param value The URL to check
* @returns True if the URL is a valid Upstash URL, false otherwise
*/
upstash: (value = "") => {
try {
const url = new URL(value);
return url.hostname.endsWith(".upstash.io");
} catch {
return /^.*\.upstash\.io$/.test(value || "");
}
}
};
var isValidCacheUrl = {
/**
* Strictly validates an Edge Config URL
* @param value The URL to validate
* @returns True if the URL is a valid Edge Config URL, false otherwise
*/
edgeConfig: (value = "") => {
try {
const url = new URL(value);
return (
// Only allow HTTPS for security
url.protocol === "https:" && // Exact hostname match
url.hostname === "edge-config.vercel.com" && // Path must start with /ecfg_
url.pathname.startsWith("/ecfg_") && // Must have a token parameter
url.searchParams.has("token") && // Token should not be empty
url.searchParams.get("token") !== ""
);
} catch {
return false;
}
},
/**
* Strictly validates an Upstash URL
* @param value The URL to validate
* @returns The password if the URL is valid, false otherwise
*/
upstash: (value = "") => {
try {
const url = new URL(value);
if (
// Only allow redis: or rediss: protocols
["redis:", "rediss:"].includes(url.protocol) && // Hostname must end with .upstash.io
url.hostname.endsWith(".upstash.io")
) {
return url.password || "";
}
return false;
} catch {
return false;
}
}
};
export {
getEdgeConfigId,
isCacheUrl,
isValidCacheUrl
};