|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +import { appDataSchema as databuddySchema } from "./databuddy/zod"; |
| 4 | +import { appDataSchema as fathomSchema } from "./fathom/zod"; |
| 5 | +import { appDataSchema as ga4Schema } from "./ga4/zod"; |
| 6 | +import { appDataSchema as gtmSchema } from "./gtm/zod"; |
| 7 | +import { appDataSchema as insihtsSchema } from "./insihts/zod"; |
| 8 | +import { appDataSchema as matomoSchema } from "./matomo/zod"; |
| 9 | +import { appDataSchema as metapixelSchema } from "./metapixel/zod"; |
| 10 | +import { appDataSchema as plausibleSchema } from "./plausible/zod"; |
| 11 | +import { appDataSchema as posthogSchema } from "./posthog/zod"; |
| 12 | +import { appDataSchema as twiplaSchema } from "./twipla/zod"; |
| 13 | +import { appDataSchema as umamiSchema } from "./umami/zod"; |
| 14 | + |
| 15 | +// Common XSS payloads that should be rejected by all schemas |
| 16 | +const xssPayloads = [ |
| 17 | + "';alert(1)//", |
| 18 | + '"><script>alert(1)</script>', |
| 19 | + "javascript:alert(1)", |
| 20 | + "<img src=x onerror=alert(1)>", |
| 21 | + "' onclick=alert(1) data-x='", |
| 22 | +]; |
| 23 | + |
| 24 | +describe("Analytics Apps - Input Validation", () => { |
| 25 | + describe("GTM", () => { |
| 26 | + it("accepts valid GTM container IDs", () => { |
| 27 | + expect(gtmSchema.parse({ trackingId: "GTM-ABC123" }).trackingId).toBe("GTM-ABC123"); |
| 28 | + expect(gtmSchema.parse({ trackingId: "abc123" }).trackingId).toBe("GTM-ABC123"); |
| 29 | + expect(gtmSchema.parse({ trackingId: "gtm-xyz789" }).trackingId).toBe("GTM-XYZ789"); |
| 30 | + }); |
| 31 | + |
| 32 | + it("rejects XSS payloads", () => { |
| 33 | + for (const payload of xssPayloads) { |
| 34 | + expect(() => gtmSchema.parse({ trackingId: payload })).toThrow(); |
| 35 | + } |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe("GA4", () => { |
| 40 | + it("accepts valid GA4 measurement IDs", () => { |
| 41 | + expect(ga4Schema.parse({ trackingId: "G-ABC1234567" }).trackingId).toBe("G-ABC1234567"); |
| 42 | + expect(ga4Schema.parse({ trackingId: "g-abc1234567" }).trackingId).toBe("G-ABC1234567"); |
| 43 | + expect(ga4Schema.parse({ trackingId: "" }).trackingId).toBe(""); |
| 44 | + }); |
| 45 | + |
| 46 | + it("rejects XSS payloads", () => { |
| 47 | + for (const payload of xssPayloads) { |
| 48 | + expect(() => ga4Schema.parse({ trackingId: payload })).toThrow(); |
| 49 | + } |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe("Meta Pixel", () => { |
| 54 | + it("accepts valid pixel IDs (numeric)", () => { |
| 55 | + expect(metapixelSchema.parse({ trackingId: "1234567890123456" }).trackingId).toBe("1234567890123456"); |
| 56 | + expect(metapixelSchema.parse({ trackingId: "" }).trackingId).toBe(""); |
| 57 | + }); |
| 58 | + |
| 59 | + it("rejects non-numeric values", () => { |
| 60 | + expect(() => metapixelSchema.parse({ trackingId: "abc123" })).toThrow(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("rejects XSS payloads", () => { |
| 64 | + for (const payload of xssPayloads) { |
| 65 | + expect(() => metapixelSchema.parse({ trackingId: payload })).toThrow(); |
| 66 | + } |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe("PostHog", () => { |
| 71 | + it("accepts valid PostHog credentials", () => { |
| 72 | + const result = posthogSchema.parse({ |
| 73 | + TRACKING_ID: "phc_abc123XYZ", |
| 74 | + API_HOST: "https://app.posthog.com", |
| 75 | + }); |
| 76 | + expect(result.TRACKING_ID).toBe("phc_abc123XYZ"); |
| 77 | + expect(result.API_HOST).toBe("https://app.posthog.com"); |
| 78 | + }); |
| 79 | + |
| 80 | + it("accepts legacy alphanumeric TRACKING_IDs", () => { |
| 81 | + expect(posthogSchema.parse({ TRACKING_ID: "legacy_key_123" }).TRACKING_ID).toBe("legacy_key_123"); |
| 82 | + }); |
| 83 | + |
| 84 | + it("rejects javascript: URLs", () => { |
| 85 | + expect(() => posthogSchema.parse({ API_HOST: "javascript:alert(1)" })).toThrow(); |
| 86 | + }); |
| 87 | + |
| 88 | + it("rejects XSS payloads", () => { |
| 89 | + for (const payload of xssPayloads) { |
| 90 | + expect(() => posthogSchema.parse({ TRACKING_ID: payload })).toThrow(); |
| 91 | + expect(() => posthogSchema.parse({ API_HOST: payload })).toThrow(); |
| 92 | + } |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe("Fathom", () => { |
| 97 | + it("accepts valid site IDs", () => { |
| 98 | + expect(fathomSchema.parse({ trackingId: "ABCDEFG" }).trackingId).toBe("ABCDEFG"); |
| 99 | + expect(fathomSchema.parse({ trackingId: "abcdefg" }).trackingId).toBe("ABCDEFG"); |
| 100 | + }); |
| 101 | + |
| 102 | + it("rejects XSS payloads", () => { |
| 103 | + for (const payload of xssPayloads) { |
| 104 | + expect(() => fathomSchema.parse({ trackingId: payload })).toThrow(); |
| 105 | + } |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe("Plausible", () => { |
| 110 | + it("accepts valid domain and URL", () => { |
| 111 | + const result = plausibleSchema.parse({ |
| 112 | + trackingId: "example.com", |
| 113 | + PLAUSIBLE_URL: "https://plausible.io/js/script.js", |
| 114 | + }); |
| 115 | + expect(result.trackingId).toBe("example.com"); |
| 116 | + expect(result.PLAUSIBLE_URL).toBe("https://plausible.io/js/script.js"); |
| 117 | + }); |
| 118 | + |
| 119 | + it("accepts valid subdomains", () => { |
| 120 | + expect(plausibleSchema.parse({ trackingId: "sub.example.com" }).trackingId).toBe("sub.example.com"); |
| 121 | + expect(plausibleSchema.parse({ trackingId: "deep.sub.example.com" }).trackingId).toBe( |
| 122 | + "deep.sub.example.com" |
| 123 | + ); |
| 124 | + }); |
| 125 | + |
| 126 | + it("accepts single-label domains", () => { |
| 127 | + expect(plausibleSchema.parse({ trackingId: "localhost" }).trackingId).toBe("localhost"); |
| 128 | + }); |
| 129 | + |
| 130 | + it("accepts domains with hyphens in labels", () => { |
| 131 | + expect(plausibleSchema.parse({ trackingId: "my-site.example.com" }).trackingId).toBe( |
| 132 | + "my-site.example.com" |
| 133 | + ); |
| 134 | + }); |
| 135 | + |
| 136 | + it("rejects consecutive dots", () => { |
| 137 | + expect(() => plausibleSchema.parse({ trackingId: "example..com" })).toThrow(); |
| 138 | + }); |
| 139 | + |
| 140 | + it("rejects hyphens at label boundaries", () => { |
| 141 | + expect(() => plausibleSchema.parse({ trackingId: "-example.com" })).toThrow(); |
| 142 | + expect(() => plausibleSchema.parse({ trackingId: "example-.com" })).toThrow(); |
| 143 | + expect(() => plausibleSchema.parse({ trackingId: "example.-com" })).toThrow(); |
| 144 | + }); |
| 145 | + |
| 146 | + it("rejects XSS payloads", () => { |
| 147 | + for (const payload of xssPayloads) { |
| 148 | + expect(() => plausibleSchema.parse({ trackingId: payload })).toThrow(); |
| 149 | + expect(() => plausibleSchema.parse({ PLAUSIBLE_URL: payload })).toThrow(); |
| 150 | + } |
| 151 | + }); |
| 152 | + }); |
| 153 | + |
| 154 | + describe("Matomo", () => { |
| 155 | + it("accepts valid URL and numeric site ID", () => { |
| 156 | + const result = matomoSchema.parse({ |
| 157 | + MATOMO_URL: "https://matomo.example.com", |
| 158 | + SITE_ID: "42", |
| 159 | + }); |
| 160 | + expect(result.MATOMO_URL).toBe("https://matomo.example.com"); |
| 161 | + expect(result.SITE_ID).toBe("42"); |
| 162 | + }); |
| 163 | + |
| 164 | + it("rejects non-numeric SITE_ID", () => { |
| 165 | + expect(() => matomoSchema.parse({ SITE_ID: "abc" })).toThrow(); |
| 166 | + }); |
| 167 | + |
| 168 | + it("rejects XSS payloads", () => { |
| 169 | + for (const payload of xssPayloads) { |
| 170 | + expect(() => matomoSchema.parse({ MATOMO_URL: payload })).toThrow(); |
| 171 | + expect(() => matomoSchema.parse({ SITE_ID: payload })).toThrow(); |
| 172 | + } |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + describe("Umami", () => { |
| 177 | + it("accepts UUID (v2) and URL", () => { |
| 178 | + const result = umamiSchema.parse({ |
| 179 | + SITE_ID: "4fb7fa4c-5b46-438d-94b3-3a8fb9bc2e8b", |
| 180 | + SCRIPT_URL: "https://umami.example.com/script.js", |
| 181 | + }); |
| 182 | + expect(result.SITE_ID).toBe("4fb7fa4c-5b46-438d-94b3-3a8fb9bc2e8b"); |
| 183 | + expect(result.SCRIPT_URL).toBe("https://umami.example.com/script.js"); |
| 184 | + }); |
| 185 | + |
| 186 | + it("accepts numeric ID (v1)", () => { |
| 187 | + expect(umamiSchema.parse({ SITE_ID: "12345" }).SITE_ID).toBe("12345"); |
| 188 | + }); |
| 189 | + |
| 190 | + it("rejects invalid format", () => { |
| 191 | + expect(() => umamiSchema.parse({ SITE_ID: "not-a-valid-id!" })).toThrow(); |
| 192 | + }); |
| 193 | + |
| 194 | + it("rejects XSS payloads", () => { |
| 195 | + for (const payload of xssPayloads) { |
| 196 | + expect(() => umamiSchema.parse({ SITE_ID: payload })).toThrow(); |
| 197 | + expect(() => umamiSchema.parse({ SCRIPT_URL: payload })).toThrow(); |
| 198 | + } |
| 199 | + }); |
| 200 | + }); |
| 201 | + |
| 202 | + describe("Twipla", () => { |
| 203 | + it("accepts valid site IDs", () => { |
| 204 | + expect(twiplaSchema.parse({ SITE_ID: "abc123" }).SITE_ID).toBe("abc123"); |
| 205 | + expect(twiplaSchema.parse({ SITE_ID: "4fb7fa4c-5b46-438d-94b3-3a8fb9bc2e8b" }).SITE_ID).toBe( |
| 206 | + "4fb7fa4c-5b46-438d-94b3-3a8fb9bc2e8b" |
| 207 | + ); |
| 208 | + }); |
| 209 | + |
| 210 | + it("rejects XSS payloads", () => { |
| 211 | + for (const payload of xssPayloads) { |
| 212 | + expect(() => twiplaSchema.parse({ SITE_ID: payload })).toThrow(); |
| 213 | + } |
| 214 | + }); |
| 215 | + }); |
| 216 | + |
| 217 | + describe("Insihts", () => { |
| 218 | + it("accepts valid site ID and URL", () => { |
| 219 | + const result = insihtsSchema.parse({ |
| 220 | + SITE_ID: "site_abc123", |
| 221 | + SCRIPT_URL: "https://collector.insihts.com/script.js", |
| 222 | + }); |
| 223 | + expect(result.SITE_ID).toBe("site_abc123"); |
| 224 | + expect(result.SCRIPT_URL).toBe("https://collector.insihts.com/script.js"); |
| 225 | + }); |
| 226 | + |
| 227 | + it("rejects XSS payloads", () => { |
| 228 | + for (const payload of xssPayloads) { |
| 229 | + expect(() => insihtsSchema.parse({ SITE_ID: payload })).toThrow(); |
| 230 | + expect(() => insihtsSchema.parse({ SCRIPT_URL: payload })).toThrow(); |
| 231 | + } |
| 232 | + }); |
| 233 | + }); |
| 234 | + |
| 235 | + describe("Databuddy", () => { |
| 236 | + it("accepts valid client ID and URLs", () => { |
| 237 | + const result = databuddySchema.parse({ |
| 238 | + CLIENT_ID: "client_abc123", |
| 239 | + DATABUDDY_SCRIPT_URL: "https://cdn.databuddy.cc/databuddy.js", |
| 240 | + DATABUDDY_API_URL: "https://basket.databuddy.cc", |
| 241 | + }); |
| 242 | + expect(result.CLIENT_ID).toBe("client_abc123"); |
| 243 | + expect(result.DATABUDDY_SCRIPT_URL).toBe("https://cdn.databuddy.cc/databuddy.js"); |
| 244 | + expect(result.DATABUDDY_API_URL).toBe("https://basket.databuddy.cc"); |
| 245 | + }); |
| 246 | + |
| 247 | + it("rejects XSS payloads", () => { |
| 248 | + for (const payload of xssPayloads) { |
| 249 | + expect(() => databuddySchema.parse({ CLIENT_ID: payload })).toThrow(); |
| 250 | + expect(() => databuddySchema.parse({ DATABUDDY_SCRIPT_URL: payload })).toThrow(); |
| 251 | + expect(() => databuddySchema.parse({ DATABUDDY_API_URL: payload })).toThrow(); |
| 252 | + } |
| 253 | + }); |
| 254 | + }); |
| 255 | +}); |
0 commit comments