Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

support wildcard domain pattern in analytics allowed hostnames

Kiran Ka303429b8984
diff --git a/apps/web/lib/analytics/verify-analytics-allowed-hostnames.ts b/apps/web/lib/analytics/verify-analytics-allowed-hostnames.ts
index ea1b2fc71e..50a0d4b064 100644
--- a/apps/web/lib/analytics/verify-analytics-allowed-hostnames.ts
+++ b/apps/web/lib/analytics/verify-analytics-allowed-hostnames.ts
@@ -14,10 +14,36 @@ export const verifyAnalyticsAllowedHostnames = ({
const sourceUrl = source ? new URL(source) : null;
const hostname = sourceUrl?.hostname;
- console.log("allowedHostnames", allowedHostnames);
- console.log("hostname", hostname);
+ if (!hostname) {
+ console.log("No hostname found in request. Denying request ❌", {
+ allowedHostnames,
+ });
+ return false;
+ }
+
+ // Check for exact matches first (including root domain)
+ if (allowedHostnames.includes(hostname)) {
+ return true;
+ }
+
+ // Check for wildcard subdomain matches
+ const wildcardMatches = allowedHostnames
+ .filter((domain) => domain.startsWith("*."))
+ .map((domain) => domain.slice(2)); // Remove the "*.", leaving just the domain
+
+ for (const domain of wildcardMatches) {
+ // Check if the hostname ends with the domain and is not the root domain
+ if (hostname.endsWith(domain) && hostname !== domain) {
+ return true;
+ }
+ }
- return true;
+ console.log(
+ `Hostname ${hostname} does not match any allowed patterns. Denying request ❌`,
+ {
+ allowedHostnames,
+ },
+ );
- // return hostname && allowedHostnames.includes(hostname);
+ return false;
};
diff --git a/apps/web/tests/misc/allowed-hostnames.test.ts b/apps/web/tests/misc/allowed-hostnames.test.ts
new file mode 100644
index 0000000000..4b8374248a
--- /dev/null
+++ b/apps/web/tests/misc/allowed-hostnames.test.ts
@@ -0,0 +1,158 @@
+import { describe, expect, it } from "vitest";
+import { verifyAnalyticsAllowedHostnames } from "../../lib/analytics/verify-analytics-allowed-hostnames";
+
+describe("analytics allowed hostnames", () => {
+ const createMockRequest = (referer: string | null) => {
+ const headers = new Headers();
+ if (referer) {
+ headers.set("referer", referer);
+ }
+ return { headers } as Request;
+ };
+
+ describe("wildcard subdomain pattern (*.example.com)", () => {
+ const allowedHostnames = ["*.example.com"];
+
+ it("should allow subdomain traffic", () => {
+ const testCases = [
+ "https://app.example.com",
+ "https://sub.sub.example.com",
+ ];
+
+ testCases.forEach((referer) => {
+ const req = createMockRequest(referer);
+ const result = verifyAnalyticsAllowedHostnames({
+ allowedHostnames,
+ req,
+ });
+ expect(result).toBe(true);
+ });
+ });
+
+ it("should deny root domain traffic", () => {
+ const testCases = ["https://example.com"];
+
+ testCases.forEach((referer) => {
+ const req = createMockRequest(referer);
+ const result = verifyAnalyticsAllowedHostnames({
+ allowedHostnames,
+ req,
+ });
+ expect(result).toBe(false);
+ });
+ });
+
+ it("should deny traffic from other domains", () => {
+ const testCases = [
+ "https://otherdomain.com",
+ "https://blog.otherdomain.com",
+ "https://example.com.evil.com",
+ ];
+
+ testCases.forEach((referer) => {
+ const req = createMockRequest(referer);
+ const result = verifyAnalyticsAllowedHostnames({
+ allowedHostnames,
+ req,
+ });
+ expect(result).toBe(false);
+ });
+ });
+ });
+
+ describe("root domain pattern (example.com)", () => {
+ const allowedHostnames = ["example.com"];
+
+ it("should allow root domain traffic", () => {
+ const testCases = ["https://example.com"];
+
+ testCases.forEach((referer) => {
+ const req = createMockRequest(referer);
+ const result = verifyAnalyticsAllowedHostnames({
+ allowedHostnames,
+ req,
+ });
+ expect(result).toBe(true);
+ });
+ });
+
+ it("should deny subdomain traffic", () => {
+ const testCases = ["https://app.example.com"];
+
+ testCases.forEach((referer) => {
+ const req = createMockRequest(referer);
+ const result = verifyAnalyticsAllowedHostnames({
+ allowedHostnames,
+ req,
+ });
+ expect(result).toBe(false);
+ });
+ });
+ });
+
+ describe("combined patterns (example.com and *.example.com)", () => {
+ const allowedHostnames = ["example.com", "*.example.com"];
+
+ it("should allow both root domain and subdomain traffic", () => {
+ const testCases = [
+ "https://example.com",
+ "https://app.example.com",
+ "https://sub.sub.example.com",
+ ];
+
+ testCases.forEach((referer) => {
+ const req = createMockRequest(referer);
+ const result = verifyAnalyticsAllowedHostnames({
+ allowedHostnames,
+ req,
+ });
+ expect(result).toBe(true);
+ });
+ });
+
+ it("should deny traffic from other domains", () => {
+ const testCases = [
+ "https://otherdomain.com",
+ "https://blog.otherdomain.com",
+ "https://example.com.evil.com",
+ ];
+
+ testCases.forEach((referer) => {
+ const req = createMockRequest(referer);
+ const result = verifyAnalyticsAllowedHostnames({
+ allowedHostnames,
+ req,
+ });
+ expect(result).toBe(false);
+ });
+ });
+ });
+
+ describe("edge cases", () => {
+ it("should handle requests without referer or origin", () => {
+ const req = createMockRequest(null);
+ const result = verifyAnalyticsAllowedHostnames({
+ allowedHostnames: ["example.com"],
+ req,
+ });
+ expect(result).toBe(false);
+ });
+
+ it("should allow all traffic when no hostnames are specified", () => {
+ const testCases = [
+ "https://example.com",
+ "https://blog.example.com",
+ "https://otherdomain.com",
+ ];
+
+ testCases.forEach((referer) => {
+ const req = createMockRequest(referer);
+ const result = verifyAnalyticsAllowedHostnames({
+ allowedHostnames: [],
+ req,
+ });
+ expect(result).toBe(true);
+ });
+ });
+ });
+});