Public: anyone can read this; the crew can change it.
small: robots.txt, security.txt and the web manifest answer a conditional GET with 304, like the other discovery documents #16159
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/16157-small-robots-txt-security-txt-and-the-we → main 0 commits fork 2 threads · 2 unresolved
2 files
changed +63
−12
src/web/standards.ts +25 −12
| @@ -86,6 +86,19 @@ function text(body: string, type = "text/plain; charset=utf-8", cache = "public, | ||
| 86 | 86 | }); |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | /** | |
| 90 | * Static text with a strong ETag derived from the body, and a 304 (same etag and cache-control, | |
| 91 | * no body) for a request that already holds it. The tag moves only when the body does. | |
| 92 | */ | |
| 93 | function textWithEtag(ctx: Ctx, body: string, type: string, cache: string): Response { | |
| 94 | const etag = `"${new Bun.CryptoHasher("sha256").update(body).digest("hex").slice(0, 32)}"`; | |
| 95 | if (holds(ctx.request.headers.get("if-none-match"), etag)) | |
| 96 | return new Response(null, { status: 304, headers: { etag, "cache-control": cache } }); | |
| 97 | const res = text(body, type, cache); | |
| 98 | res.headers.set("etag", etag); | |
| 99 | return res; | |
| 100 | } | |
| 101 | ||
| 89 | 102 | /** |
| 90 | 103 | * Where a vulnerability report goes. An address is published only when the |
| 91 | 104 | * operator wrote one down for the purpose: a default must never put a |
| @@ -127,9 +140,11 @@ export function securityExpires(now: Date): string { | ||
| 127 | 140 | .replace(/\.\d{3}Z$/, "Z"); |
| 128 | 141 | } |
| 129 | 142 | |
| 130 | /** GET /.well-known/security.txt: static text, one cache lifetime. */ | |
| 131 | export function securityTxt(ctx: Ctx): Response { | |
| 132 | return text(securityBody(ctx, new Date()), undefined, STATIC_TEXT_CACHE); | |
| 143 | /** | |
| 144 | * GET /.well-known/security.txt: static text, one cache lifetime. `now` is the clock, for tests. | |
| 145 | */ | |
| 146 | export function securityTxt(ctx: Ctx, now: Date = new Date()): Response { | |
| 147 | return textWithEtag(ctx, securityBody(ctx, now), "text/plain; charset=utf-8", STATIC_TEXT_CACHE); | |
| 133 | 148 | } |
| 134 | 149 | |
| 135 | 150 | function securityBody(ctx: Ctx, now: Date): string { |
| @@ -308,7 +323,8 @@ export function unindexed(pathname: string): boolean { | ||
| 308 | 323 | |
| 309 | 324 | /** GET /robots.txt */ |
| 310 | 325 | export function robotsTxt(ctx: Ctx): Response { |
| 311 | return text( | |
| 326 | return textWithEtag( | |
| 327 | ctx, | |
| 312 | 328 | [ |
| 313 | 329 | "User-agent: *", |
| 314 | 330 | ...UNINDEXED_PAGES.map((p) => `Disallow: ${p}`), |
| @@ -328,7 +344,7 @@ export function robotsTxt(ctx: Ctx): Response { | ||
| 328 | 344 | `Sitemap: ${ctx.url.origin}/sitemap.xml`, |
| 329 | 345 | "", |
| 330 | 346 | ].join("\n"), |
| 331 | undefined, | |
| 347 | "text/plain; charset=utf-8", | |
| 332 | 348 | STATIC_TEXT_CACHE, |
| 333 | 349 | ); |
| 334 | 350 | } |
| @@ -410,7 +426,8 @@ export async function sitemapXml(ctx: Ctx): Promise<Response> { | ||
| 410 | 426 | /** GET /manifest.webmanifest — the forge as an app on a home screen. */ |
| 411 | 427 | export function webManifest(ctx: Ctx): Response { |
| 412 | 428 | const name = ctx.app.config.edgeName ? "forge" : "forge"; |
| 413 | return new Response( | |
| 429 | return textWithEtag( | |
| 430 | ctx, | |
| 414 | 431 | JSON.stringify( |
| 415 | 432 | { |
| 416 | 433 | name, |
| @@ -447,12 +464,8 @@ export function webManifest(ctx: Ctx): Response { | ||
| 447 | 464 | null, |
| 448 | 465 | 2, |
| 449 | 466 | ), |
| 450 | { | |
| 451 | headers: { | |
| 452 | "content-type": "application/manifest+json; charset=utf-8", | |
| 453 | "cache-control": "public, max-age=3600", | |
| 454 | }, | |
| 455 | }, | |
| 467 | "application/manifest+json; charset=utf-8", | |
| 468 | "public, max-age=3600", | |
| 456 | 469 | ); |
| 457 | 470 | } |
| 458 | 471 |
test/standards-etag.test.ts +38
| @@ -88,3 +88,41 @@ for (const path of ["/.well-known/api-catalog", "/.well-known/mcp.json"]) { | ||
| 88 | 88 | expect(bodyA).not.toBe(bodyOther); |
| 89 | 89 | }); |
| 90 | 90 | } |
| 91 | ||
| 92 | for (const [path, cache] of [ | |
| 93 | ["/robots.txt", "public, max-age=86400"], | |
| 94 | ["/.well-known/security.txt", "public, max-age=86400"], | |
| 95 | ["/manifest.webmanifest", "public, max-age=3600"], | |
| 96 | ] as const) { | |
| 97 | test(`${path}: strong etag from the body, 304 on a match with the same cache-control`, async () => { | |
| 98 | const url = `${servers.url}${path}`; | |
| 99 | const first = await fetch(url); | |
| 100 | const etag = first.headers.get("etag"); | |
| 101 | expect(etag).toMatch(/^"[0-9a-f]{32}"$/); | |
| 102 | expect(first.headers.get("cache-control")).toBe(cache); | |
| 103 | const body = await first.text(); | |
| 104 | const again = await fetch(url, { headers: { "if-none-match": etag! } }); | |
| 105 | expect(again.status).toBe(304); | |
| 106 | expect(again.headers.get("etag")).toBe(etag); | |
| 107 | expect(again.headers.get("cache-control")).toBe(cache); | |
| 108 | expect(await again.text()).toBe(""); | |
| 109 | const stale = await fetch(url, { headers: { "if-none-match": '"0"' } }); | |
| 110 | expect(stale.status).toBe(200); | |
| 111 | expect(await stale.text()).toBe(body); | |
| 112 | const other = await fetch(url, { headers: { host: "other.example" } }); | |
| 113 | await other.text(); | |
| 114 | // The tag follows the body: a body that names the origin gets its own tag. | |
| 115 | if (path === "/robots.txt") expect(other.headers.get("etag")).not.toBe(etag); | |
| 116 | }); | |
This only proves the tag differs by origin for robots.txt. Nothing covers security.txt changing when the Expires month rolls. Add a test over securityBody(ctx, now) with two dates, one in the same month and one in the next. |
||
| 117 | } | |
| 118 | ||
| 119 | test("security.txt: the tag moves when the Expires month rolls, not within a month", async () => { | |
| 120 | const { securityTxt } = await import("../src/web/standards.ts"); | |
| 121 | const ctx = { app, url: new URL(servers.url), request: new Request(servers.url) } as unknown as Parameters<typeof securityTxt>[0]; | |
| 122 | const tag = (iso: string) => securityTxt(ctx, new Date(iso)).headers.get("etag"); | |
| 123 | // now + 180 days lands in October for the first two, in December for the third. | |
| 124 | const early = tag("2026-04-10T00:00:00Z"); | |
| 125 | expect(early).toMatch(/^"[0-9a-f]{32}"$/); | |
| 126 | expect(tag("2026-04-20T00:00:00Z")).toBe(early); | |
| 127 | expect(tag("2026-06-10T00:00:00Z")).not.toBe(early); | |
| 128 | }); |
new above
offline
The roll cannot be tested while the body is built from new Date() here. Pass the clock in, or export securityBody, so the test can supply dates.