-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
1550 lines (1436 loc) · 54.5 KB
/
Copy pathindex.ts
File metadata and controls
1550 lines (1436 loc) · 54.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as Sentry from "@sentry/cloudflare";
import { type Context, Hono } from "hono";
import { cors } from "hono/cors";
import { csrf } from "hono/csrf";
import { streamSSE } from "hono/streaming";
import { dispatchPendingAlerts } from "./alerts/dispatcher.js";
import { validateUnsubscribeToken } from "./alerts/unsubscribe.js";
import type {
BimiResult,
DaneResult,
DkimResult,
DmarcResult,
DnssecResult,
MtaStsResult,
MxResult,
ScanResult,
SecurityTxtResult,
SpfResult,
TlsRptResult,
} from "./analyzers/types.js";
import {
getAgentSkillsIndexJson,
SCAN_DOMAIN_SKILL_MD,
} from "./api/agent-skills.js";
import { isValidGrade, renderBadgeSvg } from "./api/badge.js";
import {
BULK_IN_BAND_CAP,
isCapExceeded,
processBulkScan,
} from "./api/bulk-scan.js";
import { API_CATALOG_JSON, CANONICAL_ORIGIN } from "./api/catalog.js";
import { clampHistoryLimit, fetchDomainHistory } from "./api/history.js";
import { LLMS_TXT } from "./api/llms-txt.js";
import { OPENAPI_JSON } from "./api/openapi.js";
import { accessJwtMiddleware } from "./auth/access-jwt.js";
import { type BearerIdentity, resolveBearer } from "./auth/api-key.js";
import { authRoutes } from "./auth/routes.js";
import { stripeWebhookRoutes } from "./billing/routes.js";
import { getCachedScan, setCachedScan } from "./cache.js";
import { runDueRescans } from "./cron/rescan.js";
import { generateCsv } from "./csv.js";
import { dashboardRoutes } from "./dashboard/routes.js";
import { getDomainByUserAndName } from "./db/domains.js";
import { recordScan } from "./db/scans.js";
import { getPlanForUser } from "./db/subscriptions.js";
import {
getMaxDomainsOverrideForUser,
setEmailAlertsEnabled,
} from "./db/users.js";
import type { Env } from "./env.js";
import { handleMcpRequest, MCP_SERVER_CARD } from "./mcp/handler.js";
import type { ProtocolId, ProtocolResult } from "./orchestrator.js";
import { scan, scanStreaming } from "./orchestrator.js";
import {
checkRateLimit,
getRateLimitConfig,
type RateLimitResult,
rateLimitHeaders,
} from "./rate-limit.js";
import { scrubSentryEvent } from "./sentry-scrub.js";
import { normalizeDomain } from "./shared/domain.js";
import { listIndexableScanDomains } from "./shared/indexable-domains.js";
import { watchlistCapFor } from "./shared/limits.js";
import { parseScoringConfig } from "./shared/scoring-config.js";
import { CSS_PATH, JS_PATH } from "./views/assets.js";
import {
APPLE_TOUCH_ICON_BASE64,
FAVICON_ICO_BASE64,
FAVICON_SVG,
ICON_192_BASE64,
ICON_512_BASE64,
OG_IMAGE_PNG_BASE64,
webManifest,
} from "./views/favicon.js";
import {
renderApiDocs,
renderBimiCard,
renderDaneCard,
renderDkimCard,
renderDmarcCard,
renderDnssecCard,
renderError,
renderLandingPage,
renderMtaStsCard,
renderMxCard,
renderReport,
renderReportFooter,
renderReportHeader,
renderScoreBreakdown,
renderScoringRubric,
renderSecurityTxtCard,
renderSpfCard,
renderStreamingLoading,
renderTlsRptCard,
} from "./views/html.js";
import {
renderLearnBimi,
renderLearnDane,
renderLearnDkim,
renderLearnDmarc,
renderLearnDnssec,
renderLearnHub,
renderLearnMtaSts,
renderLearnSecurityTxt,
renderLearnSpf,
renderLearnTlsRpt,
} from "./views/learn.js";
import { renderPrivacyPage } from "./views/legal.js";
import {
renderApiDocsMarkdown,
renderErrorMarkdown,
renderLandingMarkdown,
renderLearnHubMarkdown,
renderMxHubMarkdown,
renderMxProviderMarkdown,
renderPricingMarkdown,
renderPrivacyMarkdown,
renderReportMarkdown,
renderScoringRubricMarkdown,
} from "./views/markdown.js";
import { renderMxHub, renderMxProviderPage } from "./views/mx.js";
import { renderPricingPage } from "./views/pricing.js";
import { JS } from "./views/scripts.js";
import { CSS } from "./views/styles.js";
import { fireBulkScanWebhooks } from "./webhooks/triggers.js";
// Durable Object class for the atomic rate limiter (GHSA-v7qc-7qh8-h69g).
// Must be re-exported from the Worker entry module so the `RATE_LIMITER`
// binding in wrangler.toml can resolve its `class_name`.
export { RateLimiterDO } from "./rate-limit-do.js";
// The Hono app is exported for tests (which call `app.request(...)`).
// Runtime Workers use the Sentry-wrapped default export below, which adds
// cron (`scheduled`) alongside `fetch`.
export const app = new Hono<{ Bindings: Env }>();
// Set Sentry scope context for every request
app.use("*", async (c, next) => {
const scope = Sentry.getCurrentScope();
const domain = c.req.query("domain")?.trim().toLowerCase() || undefined;
const format =
c.req.query("format") ||
(c.req.header("Accept")?.includes("application/json") ? "json" : "html");
const selectors = c.req.query("selectors") || undefined;
// Raw user input (not normalizeDomain) — shows what was actually typed, even for rejected requests
if (domain) scope.setTag("domain", domain);
scope.setTag("format", format);
scope.setTag("path", c.req.path);
scope.setContext("request", {
selectors,
method: c.req.method,
path: c.req.path,
});
scope.setUser({
ip_address: c.req.header("CF-Connecting-IP") || undefined,
});
await next();
});
// Cloudflare Access JWT enforcement for `*.workers.dev` preview-branch
// deploys. No-ops on the production custom domain (dmarc.mx). See
// src/auth/access-jwt.ts for the protected-host predicate and fail-CLOSED
// posture when ACCESS_AUD / ACCESS_TEAM_DOMAIN are missing.
app.use("*", accessJwtMiddleware());
// HSTS: 2 years + includeSubDomains. The 2-year max-age satisfies the
// hstspreload.org submission requirement, but `preload` is intentionally
// omitted — adding it is a one-way commitment that locks every current and
// future subdomain (including any short-lived `*.workers.dev` previews
// proxied behind a custom domain) into HTTPS forever. Submit to the preload
// list as a separate, deliberate change once we're confident.
// Content types that should be hidden from search engines. HTML is the opposite:
// it's the whole point of the site and must stay crawlable. Images/CSS/JS are
// skipped because noindex on subresources is a no-op for how Googlebot renders
// pages. XML (sitemap) and text/plain (robots.txt) need to stay crawlable.
const NOINDEX_CONTENT_TYPES = [
"application/json",
"application/manifest+json",
"application/linkset+json",
"application/openapi+json",
"text/csv",
"text/event-stream",
"text/markdown",
];
// Link header (RFC 8288) pointing agents to discovery resources.
// Attached to HTML responses only — JSON/CSV/SSE consumers are already
// using the API directly.
const AGENT_DISCOVERY_LINK_HEADER = [
'</.well-known/api-catalog>; rel="api-catalog"; type="application/linkset+json"',
'</.well-known/agent-skills/index.json>; rel="https://agentskills.io/rel/index"; type="application/json"',
'</openapi.json>; rel="service-desc"; type="application/openapi+json"',
'</docs/api>; rel="service-doc"; type="text/html"',
'</health>; rel="status"',
].join(", ");
// Origins permitted to embed the HTML report in an iframe. Anything not listed
// here (including subdomains) is blocked by the `frame-ancestors` directive
// below. X-Frame-Options is intentionally NOT set — older browsers honor it
// over `frame-ancestors`, which would defeat this allowlist.
const EMBED_ALLOWED_ORIGINS = ["https://cortech.online"];
// Paths that skip Cloudflare Web Analytics beacon injection. Dashboard and
// auth pages can expose user-specific URL patterns (e.g. domain names in
// the path); we deliberately keep those out of analytics even though the
// beacon itself is cookieless.
const ANALYTICS_SKIP_PATH_PREFIXES = ["/dashboard", "/auth", "/webhooks"];
// Cloudflare Web Analytics tokens are 32-char lowercase hex. Guard against
// a misconfigured env var injecting arbitrary strings into HTML.
const CF_ANALYTICS_TOKEN_RE = /^[a-f0-9]{32}$/;
app.use("*", async (c, next) => {
await next();
c.res.headers.set("X-Content-Type-Options", "nosniff");
c.res.headers.set("Referrer-Policy", "strict-origin-when-cross-origin");
c.res.headers.set(
"Permissions-Policy",
"camera=(), microphone=(), geolocation=()",
);
c.res.headers.set(
"Strict-Transport-Security",
"max-age=63072000; includeSubDomains",
);
const contentType = c.res.headers.get("content-type") ?? "";
const isHtml = contentType.includes("text/html");
if (isHtml) {
const frameAncestors = ["'self'", ...EMBED_ALLOWED_ORIGINS].join(" ");
// Per-request nonce eliminates 'unsafe-inline' from script-src. Scripts
// with a matching nonce attribute execute; all others are blocked.
const nonce = btoa(
String.fromCharCode(...crypto.getRandomValues(new Uint8Array(16))),
);
c.res.headers.set(
"Content-Security-Policy",
`default-src 'none'; script-src 'nonce-${nonce}' 'strict-dynamic'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; manifest-src 'self'; form-action 'self'; base-uri 'none'; frame-ancestors ${frameAncestors}`,
);
if (!c.res.headers.has("Link")) {
c.res.headers.set("Link", AGENT_DISCOVERY_LINK_HEADER);
}
// Short edge cache so Cloudflare can absorb landing/scoring/report traffic
// without hitting the Worker on every request. Browsers still revalidate.
if (!c.res.headers.has("Cache-Control")) {
c.res.headers.set(
"Cache-Control",
"public, max-age=0, s-maxage=300, stale-while-revalidate=600",
);
}
// Inject nonce into all <script> tags (excluding JSON-LD data blocks which
// are not executable JS and don't fall under script-src). Combine with the
// optional Cloudflare Analytics beacon injection to avoid reading the body
// twice — HTML responses are buffered strings, never true streams.
const token = (c.env as Env | undefined)?.CF_ANALYTICS_TOKEN;
const path = c.req.path;
const isAnalyticsEligible =
token &&
CF_ANALYTICS_TOKEN_RE.test(token) &&
!ANALYTICS_SKIP_PATH_PREFIXES.some((p) => path.startsWith(p));
let body = (await c.res.text()).replace(
/<script(?!\s+type=["']application\/ld\+json)/g,
`<script nonce="${nonce}"`,
);
if (isAnalyticsEligible) {
const beacon = `<script defer nonce="${nonce}" src="https://static.cloudflareinsights.com/beacon.min.js" data-cf-beacon='{"token":"${token}"}'></script>`;
body = body.replace("</body>", `${beacon}</body>`);
}
c.res = new Response(body, {
status: c.res.status,
statusText: c.res.statusText,
headers: c.res.headers,
});
} else {
// `frame-ancestors` does not inherit from `default-src`, so it must be
// declared explicitly to keep JSON/CSV/SSE responses unframable.
c.res.headers.set(
"Content-Security-Policy",
"default-src 'none'; frame-ancestors 'none'",
);
}
// Keep the JSON API, CSV exports, the SSE stream, and the PWA manifest out
// of Google's index. These showed up in Search Console as "Crawled - currently
// not indexed" noise — no reason to spend crawl budget on them.
if (NOINDEX_CONTENT_TYPES.some((t) => contentType.includes(t))) {
c.res.headers.set("X-Robots-Tag", "noindex");
}
});
// Safety net: capture any unhandled errors that bypass route catch blocks
app.onError((err, c) => {
Sentry.captureException(err);
const message = err instanceof Error ? err.message : "Internal error";
const wantsJson =
c.req.header("Accept")?.includes("application/json") ||
c.req.query("format") === "json";
if (wantsJson) {
return c.json({ error: message }, 500);
}
return c.html(renderError(message), 500);
});
app.use("/api/*", cors());
// Auth routes (public) — login, WorkOS callback, logout
app.route("/auth", authRoutes);
// Dashboard routes (auth enforced inside dashboardRoutes via requireAuth)
app.use("/dashboard/*", csrf());
app.route("/dashboard", dashboardRoutes);
// Local-only dashboard fixture preview. Lets a developer eyeball every
// scenario (current / fire / allGreen / firstRun / free / zero) without
// going through WorkOS. Self-gated on the absence of WORKOS_API_KEY:
// production always has it set, `wrangler dev` (without a .dev.vars file)
// does not. If a self-host operator does set up local secrets, they can
// still hit the route via .dev.vars omission of this single key.
app.get("/_dev/dashboard", async (c) => {
const apiKey = (c.env as { WORKOS_API_KEY?: string } | undefined)
?.WORKOS_API_KEY;
if (apiKey && apiKey.length > 0) return c.text("Not Found", 404);
const {
renderDashboardFixture,
renderDashboardFixtureIndex,
DASHBOARD_FIXTURE_NAMES,
} = await import("./views/dashboard.js");
const fixture = c.req.query("fixture");
if (!fixture) return c.html(renderDashboardFixtureIndex());
if (!DASHBOARD_FIXTURE_NAMES.includes(fixture as never)) {
return c.text(
`Unknown fixture. Pick one of: ${DASHBOARD_FIXTURE_NAMES.join(", ")}`,
404,
);
}
return c.html(renderDashboardFixture(fixture as never));
});
// Stripe webhook (public — signature-verified). Self-gates on
// isBillingEnabled so self-host deploys without Stripe env still boot.
app.route("/webhooks", stripeWebhookRoutes);
function markdownResponse(c: Context, body: string, status = 200) {
return c.body(body, status as 200, {
"Content-Type": "text/markdown; charset=utf-8",
});
}
// Returns true when the client explicitly asked for markdown (via `?format=md`
// or an `Accept` header that lists `text/markdown` before `text/html`). HTML
// stays the default for browsers that send wildcards like `*/*`.
function wantsMarkdown(c: Context): boolean {
const format = c.req.query("format");
if (format === "md" || format === "markdown") return true;
const accept = c.req.header("Accept");
if (!accept) return false;
const types = accept.toLowerCase().split(",");
const mdIndex = types.findIndex((t) => t.trim().startsWith("text/markdown"));
if (mdIndex === -1) return false;
const htmlIndex = types.findIndex((t) => t.trim().startsWith("text/html"));
// Agents that send `Accept: text/markdown` (and nothing else, or markdown
// first) get markdown. Browsers that prefer HTML keep getting HTML.
return htmlIndex === -1 || mdIndex < htmlIndex;
}
function getClientIp(c: Context): string {
const cfIp = c.req.header("CF-Connecting-IP");
if (cfIp) return cfIp;
return "unknown";
}
// Resolves rate-limit identity + config for a request. Pro-authed bearers
// lift to the per-user bucket (60/hour). Everyone else — anonymous callers,
// bearers whose subscription isn't active, free-plan bearers — falls through
// to the per-IP anon bucket (10/60s). Free-authed keeps on IP on purpose: a
// free bearer hitting from two IPs gets two anon buckets, which matches what
// anonymous scanners already see and avoids making a free account worse than
// no account. Bearer identity is stashed on context so downstream handlers
// (/api/check scan-history persistence) can read it without re-verifying.
export async function resolveRateLimitScope(c: Context): Promise<{
identity: string;
config: ReturnType<typeof getRateLimitConfig>;
}> {
const bearer = await resolveBearer(c);
if (bearer) {
c.set("bearer" as never, bearer);
const db = (c.env as { DB?: D1Database }).DB;
if (db) {
const plan = await getPlanForUser(db, bearer.userId);
if (plan === "pro") {
return {
identity: `user:${bearer.userId}`,
config: getRateLimitConfig("pro"),
};
}
}
}
return {
identity: `ip:${getClientIp(c)}`,
config: getRateLimitConfig("free"),
};
}
type RateLimitBlockedResponder = (
c: Context,
result: RateLimitResult,
headers: Record<string, string>,
) => Response | Promise<Response>;
export function rateLimitMiddleware(onBlocked: RateLimitBlockedResponder) {
return async (c: Context, next: () => Promise<void>) => {
const { identity, config } = await resolveRateLimitScope(c);
// The Durable Object RPC is awaited end-to-end, so the counter is durably
// updated before the decision is used — no deferred write to drain.
// `c.env` is always present at runtime; the optional chain keeps the
// limiter working in lightweight unit tests that call `app.request(path)`
// without an env (falls back to the in-memory limiter).
const result = await checkRateLimit(identity, config, c.env?.RATE_LIMITER);
const headers = rateLimitHeaders(result);
if (!result.allowed) {
return onBlocked(c, result, headers);
}
await next();
// ⚡ Bolt Optimization: Use for...in instead of Object.entries() on hot paths.
// Avoids allocating an array of key-value tuples for headers on every request,
// reducing GC pressure for high-traffic middleware.
for (const key in headers) {
c.res.headers.set(key, headers[key]);
}
};
}
function blockedMessage(result: RateLimitResult): string {
const waitSec = Math.max(1, result.resetAt - Math.floor(Date.now() / 1000));
return `Rate limit exceeded. Try again in ${waitSec} seconds.`;
}
// Rate limit scan endpoints (not the landing page)
app.use(
"/check",
rateLimitMiddleware((c, result, headers) => {
const format = c.req.query("format");
const wantsJson =
format === "json" || c.req.header("Accept")?.includes("application/json");
if (wantsJson || format === "csv") {
return c.json(
{ error: blockedMessage(result) },
{ status: 429, headers },
);
}
return c.html(
renderError(
"Rate limit exceeded. Please wait a minute before scanning again.",
),
{ status: 429, headers },
);
}),
);
app.use(
"/check/score",
rateLimitMiddleware((_c, _result, headers) =>
_c.html(
renderError(
"Rate limit exceeded. Please wait a minute before scanning again.",
),
{ status: 429, headers },
),
),
);
app.use(
"/api/check",
rateLimitMiddleware((c, result, headers) =>
c.json({ error: blockedMessage(result) }, { status: 429, headers }),
),
);
// Bulk scan also runs N analyzers in-band per request — same rate-limit
// posture as /api/check (Pro bearer → user bucket; everyone else → IP).
// TODO(phase-4-pr3-followup): once per-plan limits expose a "weight" knob,
// charge bulk requests proportionally to the in-band scan count instead of
// counting as a single request.
app.use(
"/api/bulk-scan",
rateLimitMiddleware((c, result, headers) =>
c.json({ error: blockedMessage(result) }, { status: 429, headers }),
),
);
// Per-domain API endpoints (currently only /api/domain/:name/history). Path
// prefix instead of exact-match so future per-domain endpoints inherit the
// same limiter without re-wiring. Hono matches `/api/domain/*` after the
// exact-match routes above, so /api/check and /api/bulk-scan aren't affected.
// This middleware is what populates `c.get("bearer")` via resolveRateLimitScope.
app.use(
"/api/domain/*",
rateLimitMiddleware((c, result, headers) =>
c.json({ error: blockedMessage(result) }, { status: 429, headers }),
),
);
// The SSE streaming endpoint fans out ~50 DNS lookups per request and is
// bypassed by the `/api/check` middleware above (Hono matches exact paths).
// Give it its own limiter so it cannot be used as a DNS amplification vector.
app.use(
"/api/check/stream",
rateLimitMiddleware((c, result, headers) =>
c.json({ error: blockedMessage(result) }, { status: 429, headers }),
),
);
// Badge endpoint runs a full scan on cache miss, so it gets the same
// per-IP limiter as /api/check. Cloudflare edge caches the SVG response
// (1h max-age), so README-embedded badges collapse to a small number of
// origin hits regardless of view volume.
app.use(
"/badge",
rateLimitMiddleware((c, _result, headers) =>
// Even rate-limit responses must be SVG so embeds don't render a JSON
// blob in place of the badge. "rate limited" is a fallback grade.
c.body(renderBadgeSvg({ grade: "rate limited", color: "#737373" }), {
status: 429,
headers: { ...headers, "Content-Type": "image/svg+xml; charset=utf-8" },
}),
),
);
// MCP endpoint runs a full scan on cache miss — same per-IP budget as /api/check.
app.use(
"/mcp",
rateLimitMiddleware((c, result, headers) =>
c.json({ error: blockedMessage(result) }, { status: 429, headers }),
),
);
const protocolRenderers: Record<
ProtocolId,
(result: ProtocolResult) => string
> = {
mx: (r) => renderMxCard(r as MxResult),
dmarc: (r) => renderDmarcCard(r as DmarcResult),
spf: (r) => renderSpfCard(r as SpfResult),
dkim: (r) => renderDkimCard(r as DkimResult),
bimi: (r) => renderBimiCard(r as BimiResult),
mta_sts: (r) => renderMtaStsCard(r as MtaStsResult),
security_txt: (r) => renderSecurityTxtCard(r as SecurityTxtResult),
tls_rpt: (r) => renderTlsRptCard(r as TlsRptResult),
dnssec: (r) => renderDnssecCard(r as DnssecResult),
dane: (r) => renderDaneCard(r as DaneResult),
};
function tagScanResult(result: ScanResult): void {
const scope = Sentry.getCurrentScope();
scope.setTag("grade", result.grade);
scope.setTag("dmarc.status", result.protocols.dmarc.status);
scope.setTag("spf.status", result.protocols.spf.status);
scope.setTag("dkim.status", result.protocols.dkim.status);
scope.setTag("bimi.status", result.protocols.bimi.status);
scope.setTag("mta_sts.status", result.protocols.mta_sts.status);
// Optional access — older cached scans (pre-#40) may not include
// security_txt. Drop the tag rather than crash the SSE replay.
if (result.protocols.security_txt) {
scope.setTag("security_txt.status", result.protocols.security_txt.status);
}
}
app.get("/api/check/stream", async (c) => {
const domain = normalizeDomain(c.req.query("domain"));
if (!domain) {
return c.json({ error: "Missing or invalid domain parameter" }, 400);
}
const selectors = parseSelectors(c.req.query("selectors"));
const bearer =
(c.get("bearer" as never) as BearerIdentity | undefined) ?? null;
return streamSSE(c, async (stream) => {
Sentry.addBreadcrumb({
category: "scan.start",
message: domain,
data: { domain, selectors },
level: "info",
});
const cached = await getCachedScan(domain, selectors);
Sentry.addBreadcrumb({
category: cached ? "cache.hit" : "cache.miss",
message: domain,
data: { domain },
level: "info",
});
if (cached) {
tagScanResult(cached);
// Derive the replay set from `protocolRenderers` keys, not a hand-listed
// literal. `protocolRenderers` is `Record<ProtocolId, …>`, so adding a
// protocol to the union forces a renderer key, which is iterated here —
// no protocol can be silently dropped from the cache-hit path (#455).
// Object insertion order matches the previous literal, so `done` still
// comes last with no duplicate or reordered events.
const protocolIds = Object.keys(protocolRenderers) as ProtocolId[];
for (const id of protocolIds) {
const protocolResult = cached.protocols[id];
// Older cached scans (pre-#40) may lack security_txt; skip rather
// than crashing the replay. Fresh scans always populate it.
if (!protocolResult) continue;
const html = protocolRenderers[id](protocolResult);
await stream.writeSSE({
event: "protocol",
data: JSON.stringify({ id, html }),
});
}
await stream.writeSSE({
event: "done",
data: JSON.stringify({
grade: cached.grade,
headerHtml: renderReportHeader(cached),
footerHtml: renderReportFooter(cached),
}),
});
return;
}
const protocolWrites: Promise<unknown>[] = [];
const result = await scanStreaming(
domain,
selectors,
(id: ProtocolId, protocolResult: ProtocolResult) => {
const html = protocolRenderers[id](protocolResult);
const pending = stream.writeSSE({
event: "protocol",
data: JSON.stringify({ id, html }),
});
if (pending) protocolWrites.push(pending);
},
parseScoringConfig(c.env?.SCORING_CONFIG),
);
await Promise.all(protocolWrites);
tagScanResult(result);
const pendingCacheWrite = setCachedScan(domain, selectors, result);
if (pendingCacheWrite) {
c.executionCtx.waitUntil(pendingCacheWrite.catch(() => {}));
}
if (bearer) {
persistBearerScanIfWatched(c, bearer.userId, domain, result);
}
await stream.writeSSE({
event: "done",
data: JSON.stringify({
grade: result.grade,
headerHtml: renderReportHeader(result),
footerHtml: renderReportFooter(result),
}),
});
});
});
app.get("/logo.svg", (c) => {
const svg = `<svg xmlns="http://www.w3.org/2000/svg" version="1.2" baseProfile="tiny-ps" viewBox="0 0 512 512">
<title>dmarcheck</title>
<rect width="512" height="512" rx="64" fill="#0a0a0a"/>
<text x="256" y="310" font-family="monospace" font-size="220" fill="#f97316" text-anchor="middle">@</text>
<circle cx="210" cy="210" r="28" fill="white"/>
<circle cx="302" cy="210" r="28" fill="white"/>
<circle cx="216" cy="218" r="14" fill="#0a0a0f"/>
<circle cx="308" cy="218" r="14" fill="#0a0a0f"/>
<rect x="196" y="380" width="20" height="40" rx="8" fill="#ea580c"/>
<rect x="246" y="380" width="20" height="32" rx="8" fill="#ea580c"/>
<rect x="296" y="380" width="20" height="40" rx="8" fill="#ea580c"/>
</svg>`;
return c.body(svg, 200, {
"Content-Type": "image/svg+xml",
"Cache-Control": "public, max-age=86400",
});
});
app.get("/og-image.svg", (c) => {
const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1200 630">
<rect width="1200" height="630" fill="#0a0a0f"/>
<!-- Creature -->
<text x="340" y="310" font-family="monospace" font-size="180" fill="#f97316" text-anchor="middle">@</text>
<circle cx="300" cy="220" r="22" fill="white"/>
<circle cx="375" cy="220" r="22" fill="white"/>
<circle cx="305" cy="226" r="11" fill="#0a0a0f"/>
<circle cx="380" cy="226" r="11" fill="#0a0a0f"/>
<rect x="290" y="370" width="16" height="32" rx="6" fill="#ea580c"/>
<rect x="330" y="370" width="16" height="26" rx="6" fill="#ea580c"/>
<rect x="370" y="370" width="16" height="32" rx="6" fill="#ea580c"/>
<!-- Wordmark -->
<text x="500" y="300" font-family="-apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif" font-weight="800" font-size="72">
<tspan fill="#e4e4e7">dmar</tspan><tspan fill="#f97316">check</tspan>
</text>
<!-- Tagline -->
<text x="500" y="350" font-family="-apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif" font-size="24" fill="#71717a">DNS Email Security Analyzer</text>
<!-- BIMI badge -->
<text x="500" y="400" font-family="-apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif" font-size="18" fill="#f97316">Meet DMarcus — your email security sidekick</text>
</svg>`;
return c.body(svg, 200, {
"Content-Type": "image/svg+xml",
"Cache-Control": "public, max-age=86400",
});
});
// Embeddable email-security badge for READMEs and dashboards. Always
// returns a 200 SVG (even for invalid input or scan errors) so a badge
// embed never renders as a broken image — error states are encoded into
// the badge text instead.
app.get("/badge", async (c) => {
const domain = normalizeDomain(c.req.query("domain"));
const svgHeaders = (): Record<string, string> => ({
"Content-Type": "image/svg+xml; charset=utf-8",
// 1h browser, 1h edge, generous SWR. Badges live on README pages —
// they need to render fast and stay fresh-ish without re-scanning per
// viewer. The scan itself is also cached for 5 minutes inside getCachedScan,
// but that's a different layer; this header controls what GitHub
// (and downstream image proxies like camo) see.
"Cache-Control":
"public, max-age=3600, s-maxage=3600, stale-while-revalidate=86400",
// GitHub's image proxy (camo) won't show user-supplied SVGs unless
// the response is a clean SVG with no embedded scripts. Our generator
// emits no <script>, but reinforce with CSP.
"Content-Security-Policy": "default-src 'none'; style-src 'unsafe-inline'",
});
if (!domain) {
return c.body(renderBadgeSvg({ grade: "invalid", color: "#737373" }), 400, {
...svgHeaders(),
});
}
try {
const cached = await getCachedScan(domain, []);
const result =
cached ??
(await scan(domain, [], parseScoringConfig(c.env?.SCORING_CONFIG)));
if (!cached) {
const pendingCacheWrite = setCachedScan(domain, [], result);
if (pendingCacheWrite) {
c.executionCtx.waitUntil(pendingCacheWrite.catch(() => {}));
}
}
const grade = isValidGrade(result.grade) ? result.grade : "unknown";
return c.body(renderBadgeSvg({ grade }), 200, svgHeaders());
} catch (err) {
Sentry.captureException(err);
return c.body(renderBadgeSvg({ grade: "error", color: "#737373" }), 200, {
...svgHeaders(),
// Shorter cache on errors so a transient DNS failure doesn't lock
// a domain into the error badge for an hour.
"Cache-Control": "public, max-age=60",
});
}
});
app.get("/og-image.png", (c) => {
const buf = Uint8Array.from(atob(OG_IMAGE_PNG_BASE64), (ch) =>
ch.charCodeAt(0),
);
return c.body(buf, 200, {
"Content-Type": "image/png",
"Cache-Control": "public, max-age=86400",
});
});
// Content-hashed static assets with immutable caching
app.get(CSS_PATH, (c) => {
return c.body(CSS, 200, {
"Content-Type": "text/css; charset=utf-8",
"Cache-Control": "public, max-age=31536000, immutable",
});
});
app.get(JS_PATH, (c) => {
return c.body(JS, 200, {
"Content-Type": "application/javascript; charset=utf-8",
"Cache-Control": "public, max-age=31536000, immutable",
});
});
app.get("/favicon.svg", (c) => {
return c.body(FAVICON_SVG, 200, {
"Content-Type": "image/svg+xml",
"Cache-Control": "public, max-age=86400",
});
});
app.get("/manifest.webmanifest", (c) => {
return c.body(webManifest(), 200, {
"Content-Type": "application/manifest+json",
"Cache-Control": "public, max-age=86400",
});
});
app.get("/favicon.ico", (c) => {
const buf = Uint8Array.from(atob(FAVICON_ICO_BASE64), (ch) =>
ch.charCodeAt(0),
);
return c.body(buf, 200, {
"Content-Type": "image/x-icon",
"Cache-Control": "public, max-age=86400",
});
});
app.get("/apple-touch-icon.png", (c) => {
const buf = Uint8Array.from(atob(APPLE_TOUCH_ICON_BASE64), (ch) =>
ch.charCodeAt(0),
);
return c.body(buf, 200, {
"Content-Type": "image/png",
"Cache-Control": "public, max-age=86400",
});
});
app.get("/icon-192.png", (c) => {
const buf = Uint8Array.from(atob(ICON_192_BASE64), (ch) => ch.charCodeAt(0));
return c.body(buf, 200, {
"Content-Type": "image/png",
"Cache-Control": "public, max-age=86400",
});
});
app.get("/icon-512.png", (c) => {
const buf = Uint8Array.from(atob(ICON_512_BASE64), (ch) => ch.charCodeAt(0));
return c.body(buf, 200, {
"Content-Type": "image/png",
"Cache-Control": "public, max-age=86400",
});
});
app.get("/health", (c) => {
return c.json({ status: "ok", timestamp: new Date().toISOString() });
});
// RFC 9727 API catalog — agents discover this via the Link header on HTML
// pages or by fetching a well-known URI directly.
app.get("/.well-known/api-catalog", (c) => {
return c.body(API_CATALOG_JSON, 200, {
"Content-Type": "application/linkset+json",
"Cache-Control": "public, max-age=3600",
});
});
// Agent Skills discovery index — Cloudflare RFC v0.2.0.
// https://github.com/cloudflare/agent-skills-discovery-rfc
app.get("/.well-known/agent-skills/index.json", async (c) => {
const json = await getAgentSkillsIndexJson();
return c.body(json, 200, {
"Content-Type": "application/json; charset=utf-8",
"Cache-Control": "public, max-age=3600",
});
});
app.get("/.well-known/agent-skills/scan-domain/SKILL.md", (c) => {
return c.body(SCAN_DOMAIN_SKILL_MD, 200, {
"Content-Type": "text/markdown; charset=utf-8",
"Cache-Control": "public, max-age=3600",
});
});
// Remote MCP server — streamable-HTTP transport (POST only; stateless).
// Agents discover this endpoint via /.well-known/mcp/server-card.json and
// the agent-skills index.
app.post("/mcp", async (c) => {
let body: unknown;
try {
body = await c.req.json();
} catch {
return c.json(
{
jsonrpc: "2.0",
id: null,
error: { code: -32700, message: "Parse error" },
},
400,
);
}
return handleMcpRequest(body, {
executionCtx: c.executionCtx,
scoringConfig: parseScoringConfig(c.env?.SCORING_CONFIG),
});
});
// SEP-1649 MCP server card — minimal shape, served before the RFC finalises.
app.get("/.well-known/mcp/server-card.json", (c) => {
return c.body(MCP_SERVER_CARD, 200, {
"Content-Type": "application/json; charset=utf-8",
"Cache-Control": "public, max-age=3600",
});
});
app.get("/openapi.json", (c) => {
return c.body(OPENAPI_JSON, 200, {
"Content-Type": "application/openapi+json; charset=utf-8",
"Cache-Control": "public, max-age=3600",
});
});
app.get("/docs/api", (c) => {
if (wantsMarkdown(c)) return markdownResponse(c, renderApiDocsMarkdown());
return c.html(renderApiDocs());
});
// llmstxt.org — vendor-neutral pointer to the canonical markdown URLs LLM
// clients should pull instead of scraping rendered HTML. See src/api/llms-txt.ts.
app.get("/llms.txt", (c) => {
return c.body(LLMS_TXT, 200, {
"Content-Type": "text/plain; charset=utf-8",
"Cache-Control": "public, max-age=3600",
});
});
// Crawl guidance for search engines. Block the API namespace (Google was
// logging `/api/check?domain=dmarc.mx` as "Crawled - currently not indexed"
// noise) and CSV export URLs (each crawl triggers a full live DNS scan; the
// X-Robots-Tag: noindex on text/csv stops indexing but not crawling, #521),
// and point to the sitemap. `/*format=csv` uses only Google-supported
// wildcards and cannot match plain /check?domain=X pages because `=` is
// rejected by normalizeDomain.
app.get("/robots.txt", (c) => {
const body = `User-agent: *
Allow: /
Disallow: /api/
Disallow: /*format=csv
Sitemap: https://dmarc.mx/sitemap.xml
`;
return c.body(body, 200, {
"Content-Type": "text/plain; charset=utf-8",
"Cache-Control": "public, max-age=86400",
});
});
// Static URLs worth reinforcing to search engines. The /check entries are
// generated from the curated allowlist in src/shared/indexable-domains.ts —
// every domain listed there is also marked indexable on its scan page, so
// the sitemap and the per-page robots meta stay in sync.
const STATIC_SITEMAP_URLS: Array<{ loc: string; priority: string }> = [
{ loc: "https://dmarc.mx/", priority: "1.0" },
{ loc: "https://dmarc.mx/pricing", priority: "0.9" },
{ loc: "https://dmarc.mx/scoring", priority: "0.8" },
{ loc: "https://dmarc.mx/legal/privacy", priority: "0.3" },
{ loc: "https://dmarc.mx/learn", priority: "0.7" },
{ loc: "https://dmarc.mx/learn/dmarc", priority: "0.8" },
{ loc: "https://dmarc.mx/learn/spf", priority: "0.8" },
{ loc: "https://dmarc.mx/learn/dkim", priority: "0.7" },
{ loc: "https://dmarc.mx/learn/bimi", priority: "0.6" },
{ loc: "https://dmarc.mx/learn/mta-sts", priority: "0.7" },
{ loc: "https://dmarc.mx/learn/security-txt", priority: "0.6" },
{ loc: "https://dmarc.mx/learn/tls-rpt", priority: "0.6" },
{ loc: "https://dmarc.mx/learn/dnssec", priority: "0.7" },
{ loc: "https://dmarc.mx/learn/dane", priority: "0.6" },
{ loc: "https://dmarc.mx/mx", priority: "0.7" },
{ loc: "https://dmarc.mx/mx/outlook", priority: "0.8" },
{ loc: "https://dmarc.mx/mx/google", priority: "0.8" },
{ loc: "https://dmarc.mx/mx/mimecast", priority: "0.7" },
{ loc: "https://dmarc.mx/mx/proofpoint", priority: "0.7" },
{ loc: "https://dmarc.mx/mx/fastmail", priority: "0.6" },
{ loc: "https://dmarc.mx/mx/zoho", priority: "0.6" },
{ loc: "https://dmarc.mx/mx/amazon-ses", priority: "0.6" },
{ loc: "https://dmarc.mx/mx/cloudflare", priority: "0.6" },
{ loc: "https://dmarc.mx/llms.txt", priority: "0.2" },
];
const SITEMAP_LASTMOD = "2026-05-24";
function buildSitemapUrls(): Array<{ loc: string; priority: string }> {
const scanUrls = listIndexableScanDomains().map((domain) => ({
loc: `https://dmarc.mx/check?domain=${encodeURIComponent(domain)}`,
priority: "0.6",
}));
return [...STATIC_SITEMAP_URLS, ...scanUrls];
}
app.get("/sitemap.xml", (c) => {
const urls = buildSitemapUrls()
.map(
({ loc, priority }) =>
` <url><loc>${loc}</loc><lastmod>${SITEMAP_LASTMOD}</lastmod><priority>${priority}</priority></url>`,
)
.join("\n");
const body = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${urls}
</urlset>
`;
return c.body(body, 200, {
"Content-Type": "application/xml; charset=utf-8",
"Cache-Control": "public, max-age=86400",
});
});