gathergate 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +212 -2
  3. package/client/dist/assets/geist-mono-cyrillic-400-normal-C51Di1Mf.woff2 +0 -0
  4. package/client/dist/assets/geist-mono-cyrillic-400-normal-CPJFfJgk.woff +0 -0
  5. package/client/dist/assets/geist-mono-cyrillic-500-normal-D2cXvDHF.woff2 +0 -0
  6. package/client/dist/assets/geist-mono-cyrillic-500-normal-DfuvdVgn.woff +0 -0
  7. package/client/dist/assets/geist-mono-cyrillic-600-normal-Dfv53e6N.woff2 +0 -0
  8. package/client/dist/assets/geist-mono-cyrillic-600-normal-DzvMIgFZ.woff +0 -0
  9. package/client/dist/assets/geist-mono-latin-400-normal-B2yvC1Cq.woff +0 -0
  10. package/client/dist/assets/geist-mono-latin-400-normal-DTRLJnHl.woff2 +0 -0
  11. package/client/dist/assets/geist-mono-latin-500-normal-D-GG86Jb.woff +0 -0
  12. package/client/dist/assets/geist-mono-latin-500-normal-YINYabwD.woff2 +0 -0
  13. package/client/dist/assets/geist-mono-latin-600-normal-V-KvD_pi.woff2 +0 -0
  14. package/client/dist/assets/geist-mono-latin-600-normal-bZn07FKM.woff +0 -0
  15. package/client/dist/assets/geist-mono-latin-ext-400-normal-CfzLURNc.woff +0 -0
  16. package/client/dist/assets/geist-mono-latin-ext-400-normal-DI-rJ0UV.woff2 +0 -0
  17. package/client/dist/assets/geist-mono-latin-ext-500-normal-Buglb9-a.woff2 +0 -0
  18. package/client/dist/assets/geist-mono-latin-ext-500-normal-CrWEgvU_.woff +0 -0
  19. package/client/dist/assets/geist-mono-latin-ext-600-normal-B7ybHiTt.woff +0 -0
  20. package/client/dist/assets/geist-mono-latin-ext-600-normal-TphwrhF8.woff2 +0 -0
  21. package/client/dist/assets/geist-mono-symbols2-400-normal-DjbiTEEA.woff +0 -0
  22. package/client/dist/assets/geist-mono-symbols2-500-normal-CmnCPGa0.woff +0 -0
  23. package/client/dist/assets/geist-mono-symbols2-600-normal-D7I4D0GJ.woff +0 -0
  24. package/client/dist/assets/geist-mono-vietnamese-400-normal-CZEPLOgu.woff +0 -0
  25. package/client/dist/assets/geist-mono-vietnamese-500-normal-Dx9-epDo.woff +0 -0
  26. package/client/dist/assets/geist-mono-vietnamese-600-normal-BLL01lwa.woff +0 -0
  27. package/client/dist/assets/index-BWbZAfMJ.js +80 -0
  28. package/client/dist/assets/index-ZnG2njjK.css +2 -0
  29. package/client/dist/assets/inter-cyrillic-ext-wght-normal-BOeWTOD4.woff2 +0 -0
  30. package/client/dist/assets/inter-cyrillic-wght-normal-DqGufNeO.woff2 +0 -0
  31. package/client/dist/assets/inter-greek-ext-wght-normal-DlzME5K_.woff2 +0 -0
  32. package/client/dist/assets/inter-greek-wght-normal-CkhJZR-_.woff2 +0 -0
  33. package/client/dist/assets/inter-latin-ext-wght-normal-DO1Apj_S.woff2 +0 -0
  34. package/client/dist/assets/inter-latin-wght-normal-Dx4kXJAl.woff2 +0 -0
  35. package/client/dist/assets/inter-vietnamese-wght-normal-CBcvBZtf.woff2 +0 -0
  36. package/client/dist/favicon.svg +12 -0
  37. package/client/dist/index.html +16 -0
  38. package/client/dist/site.webmanifest +16 -0
  39. package/dist/adapters/email.js +125 -0
  40. package/dist/adapters/storage.js +106 -0
  41. package/dist/app.js +2126 -0
  42. package/dist/config.js +106 -0
  43. package/dist/db/client.js +294 -0
  44. package/dist/db/queries.js +898 -0
  45. package/dist/emails/templates.js +221 -0
  46. package/dist/index.js +77 -0
  47. package/dist/jobs/chaser.js +81 -0
  48. package/dist/jobs/demo.js +329 -0
  49. package/dist/jobs/purge.js +22 -0
  50. package/dist/jobs/webhook.js +153 -0
  51. package/dist/security/auth.js +69 -0
  52. package/dist/security/crypto.js +91 -0
  53. package/dist/security/magic-bytes.js +131 -0
  54. package/dist/security/rate-limit.js +72 -0
  55. package/dist/security/ssrf.js +181 -0
  56. package/dist/templates/presets.js +341 -0
  57. package/dist/validators/index.js +52 -0
  58. package/dist/validators/packs/au.js +108 -0
  59. package/dist/validators/packs/eu.js +85 -0
  60. package/dist/validators/packs/generic.js +57 -0
  61. package/dist/validators/packs/types.js +1 -0
  62. package/dist/validators/packs/uk.js +58 -0
  63. package/dist/validators/packs/us.js +82 -0
  64. package/package.json +55 -4
@@ -0,0 +1,131 @@
1
+ export const ALLOWED_TYPE_GROUPS = {
2
+ pdf: ["application/pdf"],
3
+ image: ["image/png", "image/jpeg", "image/webp"],
4
+ spreadsheet: [
5
+ "text/csv",
6
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
7
+ ],
8
+ document: [
9
+ "application/pdf",
10
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
11
+ "text/plain",
12
+ ],
13
+ any_safe: [
14
+ "application/pdf",
15
+ "image/png",
16
+ "image/jpeg",
17
+ "image/webp",
18
+ "text/csv",
19
+ "text/plain",
20
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
21
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
22
+ ],
23
+ };
24
+ function startsWithBytes(buf, sig) {
25
+ if (buf.length < sig.length)
26
+ return false;
27
+ for (let i = 0; i < sig.length; i++) {
28
+ if (buf[i] !== sig[i])
29
+ return false;
30
+ }
31
+ return true;
32
+ }
33
+ function isMostlyPrintableUtf8(buf) {
34
+ const sample = buf.subarray(0, Math.min(buf.length, 4096));
35
+ if (sample.length === 0)
36
+ return false;
37
+ // Reject NUL bytes (binary/executable files)
38
+ for (let i = 0; i < sample.length; i++) {
39
+ const b = sample[i];
40
+ if (b === 0x00)
41
+ return false;
42
+ }
43
+ try {
44
+ const decoded = new TextDecoder("utf-8", { fatal: true }).decode(sample);
45
+ // Reject obvious HTML/script injection disguised as CSV/TXT
46
+ if (/<\s*(script|html|svg|iframe)\b/i.test(decoded))
47
+ return false;
48
+ return true;
49
+ }
50
+ catch {
51
+ return false;
52
+ }
53
+ }
54
+ function detectOpenXmlSubType(buf) {
55
+ // Inspect ZIP entry names without external parsers to distinguish DOCX vs XLSX
56
+ const ascii = buf.toString("latin1");
57
+ if (ascii.includes("word/document.xml")) {
58
+ return {
59
+ mime: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
60
+ ext: "docx",
61
+ };
62
+ }
63
+ if (ascii.includes("xl/workbook.xml")) {
64
+ return {
65
+ mime: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
66
+ ext: "xlsx",
67
+ };
68
+ }
69
+ return null;
70
+ }
71
+ export function detectFileSignature(buf, originalName) {
72
+ if (buf.length === 0)
73
+ return null;
74
+ // Reject executable headers explicitly (MZ/PE, ELF, Mach-O, shebang)
75
+ if (startsWithBytes(buf, [0x4d, 0x5a]) ||
76
+ startsWithBytes(buf, [0x7f, 0x45, 0x4c, 0x46]) ||
77
+ startsWithBytes(buf, [0xfe, 0xed, 0xfa, 0xce]) ||
78
+ startsWithBytes(buf, [0xfe, 0xed, 0xfa, 0xcf]) ||
79
+ startsWithBytes(buf, [0xcf, 0xfa, 0xed, 0xfe]) ||
80
+ startsWithBytes(buf, [0x23, 0x21])) {
81
+ return null;
82
+ }
83
+ // PDF: %PDF-
84
+ if (startsWithBytes(buf, [0x25, 0x50, 0x44, 0x46, 0x2d])) {
85
+ return { mime: "application/pdf", ext: "pdf" };
86
+ }
87
+ // PNG
88
+ if (startsWithBytes(buf, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) {
89
+ return { mime: "image/png", ext: "png" };
90
+ }
91
+ // JPEG
92
+ if (startsWithBytes(buf, [0xff, 0xd8, 0xff])) {
93
+ return { mime: "image/jpeg", ext: "jpg" };
94
+ }
95
+ // WebP: RIFF....WEBP
96
+ if (buf.length >= 12 &&
97
+ startsWithBytes(buf, [0x52, 0x49, 0x46, 0x46]) &&
98
+ buf.subarray(8, 12).toString("ascii") === "WEBP") {
99
+ return { mime: "image/webp", ext: "webp" };
100
+ }
101
+ // ZIP container -> allow only DOCX / XLSX (block arbitrary ZIP bombs/archives)
102
+ if (startsWithBytes(buf, [0x50, 0x4b, 0x03, 0x04])) {
103
+ return detectOpenXmlSubType(buf);
104
+ }
105
+ // Plain CSV or TXT
106
+ const lower = originalName.toLowerCase();
107
+ if (isMostlyPrintableUtf8(buf)) {
108
+ if (lower.endsWith(".csv"))
109
+ return { mime: "text/csv", ext: "csv" };
110
+ if (lower.endsWith(".txt"))
111
+ return { mime: "text/plain", ext: "txt" };
112
+ }
113
+ return null;
114
+ }
115
+ export function resolveAllowedMimes(allowedGroups) {
116
+ if (!allowedGroups || allowedGroups.length === 0) {
117
+ return ALLOWED_TYPE_GROUPS.any_safe;
118
+ }
119
+ const mimes = new Set();
120
+ for (const group of allowedGroups) {
121
+ const resolved = ALLOWED_TYPE_GROUPS[group];
122
+ if (resolved) {
123
+ for (const m of resolved)
124
+ mimes.add(m);
125
+ }
126
+ else if (group.includes("/")) {
127
+ mimes.add(group);
128
+ }
129
+ }
130
+ return mimes.size > 0 ? [...mimes] : ALLOWED_TYPE_GROUPS.any_safe;
131
+ }
@@ -0,0 +1,72 @@
1
+ /** Fixed-window in-memory rate limiter suitable for single-instance self-hosted deployments. */
2
+ export class RateLimiter {
3
+ now;
4
+ maxBuckets;
5
+ buckets = new Map();
6
+ constructor(now = () => Date.now(), maxBuckets = 10_000) {
7
+ this.now = now;
8
+ this.maxBuckets = maxBuckets;
9
+ }
10
+ check(key, limit, windowMs) {
11
+ const current = this.now();
12
+ if (this.buckets.size >= this.maxBuckets && !this.buckets.has(key)) {
13
+ for (const [k, entry] of this.buckets) {
14
+ if (entry.resetAt <= current)
15
+ this.buckets.delete(k);
16
+ }
17
+ while (this.buckets.size >= this.maxBuckets) {
18
+ const oldestKey = this.buckets.keys().next().value;
19
+ if (oldestKey === undefined)
20
+ break;
21
+ this.buckets.delete(oldestKey);
22
+ }
23
+ }
24
+ const existing = this.buckets.get(key);
25
+ if (!existing || existing.resetAt <= current) {
26
+ this.buckets.delete(key);
27
+ this.buckets.set(key, { count: 1, resetAt: current + windowMs });
28
+ return {
29
+ allowed: true,
30
+ remaining: Math.max(0, limit - 1),
31
+ retryAfterSeconds: Math.max(1, Math.ceil(windowMs / 1000)),
32
+ };
33
+ }
34
+ if (existing.count >= limit) {
35
+ return {
36
+ allowed: false,
37
+ remaining: 0,
38
+ retryAfterSeconds: Math.max(1, Math.ceil((existing.resetAt - current) / 1000)),
39
+ };
40
+ }
41
+ existing.count += 1;
42
+ return {
43
+ allowed: true,
44
+ remaining: Math.max(0, limit - existing.count),
45
+ retryAfterSeconds: Math.max(1, Math.ceil((existing.resetAt - current) / 1000)),
46
+ };
47
+ }
48
+ }
49
+ export function extractClientIp(c, trustProxy) {
50
+ const incoming = c.env
51
+ ?.incoming;
52
+ const socketIp = incoming?.socket?.remoteAddress?.trim() || "";
53
+ if (trustProxy) {
54
+ const xff = c.req.header("x-forwarded-for");
55
+ if (xff) {
56
+ const parts = xff
57
+ .split(",")
58
+ .map((p) => p.trim())
59
+ .filter(Boolean);
60
+ const rightmost = parts[parts.length - 1];
61
+ if (rightmost)
62
+ return rightmost.slice(0, 64);
63
+ }
64
+ const realIp = c.req.header("x-real-ip")?.trim();
65
+ if (realIp)
66
+ return realIp.slice(0, 64);
67
+ const cfIp = c.req.header("cf-connecting-ip")?.trim();
68
+ if (cfIp)
69
+ return cfIp.slice(0, 64);
70
+ }
71
+ return socketIp ? socketIp.slice(0, 64) : "unknown";
72
+ }
@@ -0,0 +1,181 @@
1
+ import dns from "node:dns";
2
+ import net from "node:net";
3
+ function parseIpv4Parts(ip) {
4
+ const m = ip.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
5
+ if (!m)
6
+ return null;
7
+ const parts = [Number(m[1]), Number(m[2]), Number(m[3]), Number(m[4])];
8
+ if (parts.some((n) => n < 0 || n > 255))
9
+ return null;
10
+ return parts;
11
+ }
12
+ export function isNonPublicIpv4(ip) {
13
+ const parts = parseIpv4Parts(ip);
14
+ if (!parts)
15
+ return false;
16
+ const [a, b, c] = parts;
17
+ return (a === 0 || // 0.0.0.0/8 "this network"
18
+ a === 10 || // 10.0.0.0/8 RFC 1918
19
+ a === 127 || // 127.0.0.0/8 loopback
20
+ (a === 100 && b >= 64 && b <= 127) || // 100.64.0.0/10 CGNAT
21
+ (a === 169 && b === 254) || // 169.254.0.0/16 link-local / cloud metadata
22
+ (a === 172 && b >= 16 && b <= 31) || // 172.16.0.0/12 RFC 1918
23
+ (a === 192 && b === 0 && c === 0) || // 192.0.0.0/24 IETF protocol assignments
24
+ (a === 192 && b === 0 && c === 2) || // 192.0.2.0/24 TEST-NET-1
25
+ (a === 192 && b === 168) || // 192.168.0.0/16 RFC 1918
26
+ (a === 198 && (b === 18 || b === 19)) || // 198.18.0.0/15 benchmarking
27
+ (a === 198 && b === 51 && c === 100) || // 198.51.100.0/24 TEST-NET-2
28
+ (a === 203 && b === 0 && c === 113) || // 203.0.113.0/24 TEST-NET-3
29
+ a >= 224 // 224.0.0.0/4 multicast + 240.0.0.0/4 reserved + 255.255.255.255 broadcast
30
+ );
31
+ }
32
+ function expandIpv6(ip) {
33
+ const clean = ip.toLowerCase().split("%")[0];
34
+ if (!clean.includes(":"))
35
+ return null;
36
+ let work = clean;
37
+ // Convert trailing embedded IPv4 (e.g. ::ffff:127.0.0.1 or ::127.0.0.1 or 64:ff9b::10.0.0.1)
38
+ const lastColon = work.lastIndexOf(":");
39
+ const tail = work.slice(lastColon + 1);
40
+ if (tail.includes(".")) {
41
+ const v4 = parseIpv4Parts(tail);
42
+ if (!v4)
43
+ return null;
44
+ const hi = ((v4[0] << 8) | v4[1]).toString(16);
45
+ const lo = ((v4[2] << 8) | v4[3]).toString(16);
46
+ work = `${work.slice(0, lastColon)}:${hi}:${lo}`;
47
+ }
48
+ const halves = work.split("::");
49
+ if (halves.length > 2)
50
+ return null;
51
+ const left = halves[0] ? halves[0].split(":").filter(Boolean) : [];
52
+ const right = halves[1] ? halves[1].split(":").filter(Boolean) : [];
53
+ const missing = 8 - (left.length + right.length);
54
+ if (halves.length === 1 && missing !== 0)
55
+ return null;
56
+ if (halves.length === 2 && missing < 1)
57
+ return null;
58
+ const full = [...left, ...Array(Math.max(0, missing)).fill("0"), ...right];
59
+ if (full.length !== 8)
60
+ return null;
61
+ const groups = full.map((g) => (/^[0-9a-f]{1,4}$/.test(g) ? parseInt(g, 16) : NaN));
62
+ return groups.some(Number.isNaN) ? null : groups;
63
+ }
64
+ export function isNonPublicIp(rawIp) {
65
+ const ip = rawIp.replace(/^\[|\]$/g, "").trim();
66
+ const kind = net.isIP(ip);
67
+ if (kind === 4)
68
+ return isNonPublicIpv4(ip);
69
+ if (kind === 6) {
70
+ const g = expandIpv6(ip);
71
+ if (!g)
72
+ return true;
73
+ const isZeroPrefix6 = g[0] === 0 && g[1] === 0 && g[2] === 0 && g[3] === 0 && g[4] === 0;
74
+ // :: and ::1
75
+ if (isZeroPrefix6 && g[5] === 0 && g[6] === 0 && (g[7] === 0 || g[7] === 1))
76
+ return true;
77
+ // ::ffff:0:0/96 (IPv4-mapped) or ::/96 (IPv4-compatible)
78
+ if (isZeroPrefix6 && (g[5] === 0xffff || g[5] === 0)) {
79
+ const embeddedV4 = `${g[6] >> 8}.${g[6] & 0xff}.${g[7] >> 8}.${g[7] & 0xff}`;
80
+ if (isNonPublicIpv4(embeddedV4))
81
+ return true;
82
+ }
83
+ // ::ffff:0:0:0/96 (IPv4-translated)
84
+ if (g[0] === 0 && g[1] === 0 && g[2] === 0 && g[3] === 0 && g[4] === 0xffff && g[5] === 0) {
85
+ const embeddedV4 = `${g[6] >> 8}.${g[6] & 0xff}.${g[7] >> 8}.${g[7] & 0xff}`;
86
+ if (isNonPublicIpv4(embeddedV4))
87
+ return true;
88
+ }
89
+ // 64:ff9b::/96 (NAT64 well-known) and 64:ff9b:1::/48 (NAT64 local-use)
90
+ if (g[0] === 0x0064 && g[1] === 0xff9b) {
91
+ if (g[2] === 0 && g[3] === 0 && g[4] === 0 && g[5] === 0) {
92
+ const embeddedV4 = `${g[6] >> 8}.${g[6] & 0xff}.${g[7] >> 8}.${g[7] & 0xff}`;
93
+ if (isNonPublicIpv4(embeddedV4))
94
+ return true;
95
+ }
96
+ if (g[2] === 0x0001)
97
+ return true;
98
+ }
99
+ // fc00::/7 ULA, fe80::/10 link-local, ff00::/8 multicast, 2001:db8::/32 documentation
100
+ if ((g[0] & 0xfe00) === 0xfc00)
101
+ return true;
102
+ if ((g[0] & 0xffc0) === 0xfe80)
103
+ return true;
104
+ if ((g[0] & 0xff00) === 0xff00)
105
+ return true;
106
+ if (g[0] === 0x2001 && g[1] === 0x0db8)
107
+ return true;
108
+ return false;
109
+ }
110
+ return false;
111
+ }
112
+ export function isPrivateOrLocalHost(hostname) {
113
+ const host = hostname.replace(/^\[|\]$/g, "").toLowerCase().replace(/\.$/, "").trim();
114
+ if (!host)
115
+ return true;
116
+ if (host === "localhost" ||
117
+ host.endsWith(".localhost") ||
118
+ host.endsWith(".local") ||
119
+ host.endsWith(".internal")) {
120
+ return true;
121
+ }
122
+ if (net.isIP(host))
123
+ return isNonPublicIp(host);
124
+ // Reject single-label internal service names (e.g. "redis", "metadata", "db") in multi-tenant mode
125
+ if (!host.includes("."))
126
+ return true;
127
+ return false;
128
+ }
129
+ export function validateWebhookEndpoint(rawUrl, multiTenant) {
130
+ try {
131
+ const parsed = new URL(rawUrl);
132
+ if (multiTenant) {
133
+ if (parsed.protocol !== "https:" || isPrivateOrLocalHost(parsed.hostname)) {
134
+ return "Webhook URL must target a public https endpoint in multi-tenant mode.";
135
+ }
136
+ }
137
+ else if (parsed.protocol !== "https:" && parsed.protocol !== "http:") {
138
+ return "Webhook URL must be valid http or https.";
139
+ }
140
+ return null;
141
+ }
142
+ catch {
143
+ return "Webhook URL must be valid http or https.";
144
+ }
145
+ }
146
+ export async function assertPublicHostname(hostname, lookupImpl) {
147
+ const host = hostname.replace(/^\[|\]$/g, "").toLowerCase().replace(/\.$/, "").trim();
148
+ if (isPrivateOrLocalHost(host)) {
149
+ return "Target host must be a public hostname.";
150
+ }
151
+ if (net.isIP(host)) {
152
+ return isNonPublicIp(host) ? "Target IP must be a public address." : null;
153
+ }
154
+ try {
155
+ const records = lookupImpl
156
+ ? await lookupImpl(host)
157
+ : await dns.promises.lookup(host, { all: true });
158
+ if (!records || records.length === 0) {
159
+ return "Target hostname could not be resolved.";
160
+ }
161
+ for (const rec of records) {
162
+ if (isNonPublicIp(rec.address)) {
163
+ return "Target hostname resolves to a private or non-public address.";
164
+ }
165
+ }
166
+ return null;
167
+ }
168
+ catch {
169
+ return "Target hostname could not be resolved.";
170
+ }
171
+ }
172
+ export async function validateWebhookEndpointRuntime(rawUrl, multiTenant, options) {
173
+ const basicErr = validateWebhookEndpoint(rawUrl, multiTenant);
174
+ if (basicErr)
175
+ return basicErr;
176
+ if (!multiTenant || options?.skipDns)
177
+ return null;
178
+ const parsed = new URL(rawUrl);
179
+ const dnsErr = await assertPublicHostname(parsed.hostname, options?.lookupImpl);
180
+ return dnsErr ? "Webhook URL must target a public https endpoint in multi-tenant mode." : null;
181
+ }