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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/forge:mason/16157-small-robots-txt-security-txt-and-the-we → main 0 commits fork 2 threads · 2 unresolved

  • Merged. mason/16157-small-robots-txt-security-txt-and-the-we was merged into main as c87afca.

Review

Sign in to review this change.
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,
8686 });
8787 }
8888
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
89102 /**
90103 * Where a vulnerability report goes. An address is published only when the
91104 * operator wrote one down for the purpose: a default must never put a
@@ -127,9 +140,11 @@ export function securityExpires(now: Date): string {
127140 .replace(/\.\d{3}Z$/, "Z");
128141 }
129142
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 /**
outdated

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.

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);
133148 }
134149
135150 function securityBody(ctx: Ctx, now: Date): string {
@@ -308,7 +323,8 @@ export function unindexed(pathname: string): boolean {
308323
309324 /** GET /robots.txt */
310325 export function robotsTxt(ctx: Ctx): Response {
311 return text(
326 return textWithEtag(
327 ctx,
312328 [
313329 "User-agent: *",
314330 ...UNINDEXED_PAGES.map((p) => `Disallow: ${p}`),
@@ -328,7 +344,7 @@ export function robotsTxt(ctx: Ctx): Response {
328344 `Sitemap: ${ctx.url.origin}/sitemap.xml`,
329345 "",
330346 ].join("\n"),
331 undefined,
347 "text/plain; charset=utf-8",
332348 STATIC_TEXT_CACHE,
333349 );
334350 }
@@ -410,7 +426,8 @@ export async function sitemapXml(ctx: Ctx): Promise<Response> {
410426 /** GET /manifest.webmanifest — the forge as an app on a home screen. */
411427 export function webManifest(ctx: Ctx): Response {
412428 const name = ctx.app.config.edgeName ? "forge" : "forge";
413 return new Response(
429 return textWithEtag(
430 ctx,
414431 JSON.stringify(
415432 {
416433 name,
@@ -447,12 +464,8 @@ export function webManifest(ctx: Ctx): Response {
447464 null,
448465 2,
449466 ),
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",
456469 );
457470 }
458471
test/standards-etag.test.ts +38
@@ -88,3 +88,41 @@ for (const path of ["/.well-known/api-catalog", "/.well-known/mcp.json"]) {
8888 expect(bodyA).not.toBe(bodyOther);
8989 });
9090 }
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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs