Public: anyone can read this; the crew can change it.
/.well-known/security.txt's Expires is always a date between 30 and 365 days ahead, never a constant that lapses #13078
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13065-well-known-security-txt-s-expires-is-alw → main 3 commits fork
2 files
changed +36
−7
src/web/standards.ts +18 −6
| @@ -92,17 +92,29 @@ function securityContact(ctx: Ctx, own: Repo | undefined): string { | ||
| 92 | 92 | : `${ctx.url.origin}/${admin.username}`; |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | /** How far ahead security.txt's Expires looks, before rounding down to a month start. */ | |
| 96 | export const SECURITY_TXT_HORIZON_DAYS = 180; | |
| 97 | ||
| 98 | /** | |
| 99 | * security.txt's Expires (RFC 9116) at `now`: the first of the month that | |
| 100 | * holds now + 180 days, so it is always between 150 and 180 days ahead, | |
| 101 | * reads the same for a month at a time, and never lapses the way a | |
| 102 | * constant does. A cached answer stays far inside it. | |
| 103 | */ | |
| 104 | export function securityExpires(now: Date): string { | |
| 105 | const ahead = new Date(now.getTime() + SECURITY_TXT_HORIZON_DAYS * 86_400_000); | |
| 106 | return new Date(Date.UTC(ahead.getUTCFullYear(), ahead.getUTCMonth(), 1)) | |
| 107 | .toISOString() | |
| 108 | .replace(/\.\d{3}Z$/, "Z"); | |
| 109 | } | |
| 110 | ||
| 95 | 111 | /** GET /.well-known/security.txt */ |
| 96 | 112 | export function securityTxt(ctx: Ctx): Response { |
| 97 | // Expires is required and must be a fixed instant, not "a year from | |
| 98 | // whenever you read this": the first of next year, so every read within a | |
| 99 | // year says the same thing and the file is never stale for long. | |
| 100 | const now = new Date(); | |
| 101 | const expires = new Date(Date.UTC(now.getUTCFullYear() + 1, 0, 1)); | |
| 113 | const expires = securityExpires(new Date()); | |
| 102 | 114 | return text( |
| 103 | 115 | [ |
| 104 | 116 | `Contact: ${securityContact(ctx, publicForgeRepo(ctx.app))}`, |
| 105 | `Expires: ${expires.toISOString().replace(/\.\d{3}Z$/, "Z")}`, | |
| 117 | `Expires: ${expires}`, | |
| 106 | 118 | "Preferred-Languages: en", |
| 107 | 119 | `Canonical: ${ctx.url.origin}/.well-known/security.txt`, |
| 108 | 120 | "", |
test/standards.test.ts +18 −1
| @@ -21,6 +21,7 @@ import { forgetListings, listingBuilds } from "../src/core/listings.ts"; | ||
| 21 | 21 | import { createOrg, retireOrg } from "../src/core/orgs.ts"; |
| 22 | 22 | import { getRepo, moveArchived } from "../src/core/repos.ts"; |
| 23 | 23 | import { SITE_LINE } from "../src/web/site_line.ts"; |
| 24 | import { securityExpires } from "../src/web/standards.ts"; | |
| 24 | 25 | import { serve, type Servers } from "../src/http/server.ts"; |
| 25 | 26 | |
| 26 | 27 | describe("what the open web expects", () => { |
| @@ -72,7 +73,10 @@ describe("what the open web expects", () => { | ||
| 72 | 73 | expect(body).toContain(`Contact: ${base}/alice`); |
| 73 | 74 | expect(body).not.toContain("[email protected]"); |
| 74 | 75 | expect(body).not.toContain("mailto:"); |
| 75 | expect(body).toMatch(/Expires: \d{4}-01-01T00:00:00Z/); | |
| 76 | const expires = /^Expires: (\d{4}-\d{2}-01T00:00:00Z)$/m.exec(body)?.[1] ?? ""; | |
| 77 | const ahead = (Date.parse(expires) - Date.now()) / 86_400_000; | |
| 78 | expect(ahead).toBeGreaterThanOrEqual(30); | |
| 79 | expect(ahead).toBeLessThanOrEqual(365); | |
| 76 | 80 | expect(body).toContain(`Canonical: ${base}/.well-known/security.txt`); |
| 77 | 81 | // An operator who wants an address published writes it down. |
| 78 | 82 | process.env["FORGE_SECURITY_CONTACT"] = "mailto:[email protected]"; |
| @@ -84,6 +88,19 @@ describe("what the open web expects", () => { | ||
| 84 | 88 | } |
| 85 | 89 | }); |
| 86 | 90 | |
| 91 | test("security.txt's Expires is computed from the clock: a month start 150 to 180 days ahead", () => { | |
| 92 | // A constant lapses (2027-01-01 was one); the clock never does. | |
| 93 | expect(securityExpires(new Date("2026-11-15T12:00:00Z"))).toBe("2027-05-01T00:00:00Z"); | |
| 94 | expect(securityExpires(new Date("2026-12-31T23:59:59Z"))).toBe("2027-06-01T00:00:00Z"); | |
| 95 | expect(securityExpires(new Date("2027-06-01T00:00:00Z"))).toBe("2027-11-01T00:00:00Z"); | |
| 96 | // Every day of two years lands inside RFC 9116's window, far wider than any cache. | |
| 97 | for (let t = Date.UTC(2026, 0, 1); t < Date.UTC(2028, 0, 1); t += 86_400_000 / 4) { | |
| 98 | const ahead = (Date.parse(securityExpires(new Date(t))) - t) / 86_400_000; | |
| 99 | expect(ahead).toBeGreaterThanOrEqual(30); | |
| 100 | expect(ahead).toBeLessThanOrEqual(365); | |
| 101 | } | |
| 102 | }); | |
| 103 | ||
| 87 | 104 | test("security.txt's contact is the sealed form of the forge's public repository", async () => { |
| 88 | 105 | app.config.forgeRepo = "alice/open"; |
| 89 | 106 | try { |
new above
offline