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

Merged opened this

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

Merged

scout/forge:scout/13065-well-known-security-txt-s-expires-is-alw → main 3 commits fork

  • Merged. scout/13065-well-known-security-txt-s-expires-is-alw was merged into main as e0f2065.

Review

Sign in to review this change.
2 files changed +36 −7
src/web/standards.ts +18 −6
@@ -92,17 +92,29 @@ function securityContact(ctx: Ctx, own: Repo | undefined): string {
9292 : `${ctx.url.origin}/${admin.username}`;
9393 }
9494
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
95111 /** GET /.well-known/security.txt */
96112 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());
102114 return text(
103115 [
104116 `Contact: ${securityContact(ctx, publicForgeRepo(ctx.app))}`,
105 `Expires: ${expires.toISOString().replace(/\.\d{3}Z$/, "Z")}`,
117 `Expires: ${expires}`,
106118 "Preferred-Languages: en",
107119 `Canonical: ${ctx.url.origin}/.well-known/security.txt`,
108120 "",
test/standards.test.ts +18 −1
@@ -21,6 +21,7 @@ import { forgetListings, listingBuilds } from "../src/core/listings.ts";
2121 import { createOrg, retireOrg } from "../src/core/orgs.ts";
2222 import { getRepo, moveArchived } from "../src/core/repos.ts";
2323 import { SITE_LINE } from "../src/web/site_line.ts";
24 import { securityExpires } from "../src/web/standards.ts";
2425 import { serve, type Servers } from "../src/http/server.ts";
2526
2627 describe("what the open web expects", () => {
@@ -72,7 +73,10 @@ describe("what the open web expects", () => {
7273 expect(body).toContain(`Contact: ${base}/alice`);
7374 expect(body).not.toContain("[email protected]");
7475 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);
7680 expect(body).toContain(`Canonical: ${base}/.well-known/security.txt`);
7781 // An operator who wants an address published writes it down.
7882 process.env["FORGE_SECURITY_CONTACT"] = "mailto:[email protected]";
@@ -84,6 +88,19 @@ describe("what the open web expects", () => {
8488 }
8589 });
8690
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
87104 test("security.txt's contact is the sealed form of the forge's public repository", async () => {
88105 app.config.forgeRepo = "alice/open";
89106 try {

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs