|
| 1 | +package router_test |
| 2 | + |
| 3 | +// security_txt_test.go — BUG-API-411 (QA 2026-05-29). RFC 9116 |
| 4 | +// /.well-known/security.txt + the apex /security.txt fallback both used |
| 5 | +// to 404, leaving security researchers no documented disclosure path. |
| 6 | +// |
| 7 | +// COVERAGE BLOCK (rule 17): |
| 8 | +// |
| 9 | +// Symptom: researcher hits /.well-known/security.txt and gets a |
| 10 | +// 404 envelope with no disclosure contact. |
| 11 | +// Enumeration: `rg -nF 'security.txt' internal/` (router.go inline |
| 12 | +// wiring + security_txt.go builder + this test). |
| 13 | +// Sites found: 2 paths, 1 shared builder. |
| 14 | +// Sites touched: both paths covered by sub-tests; the shared builder |
| 15 | +// is unit-tested via buildSecurityTxtBody so a future |
| 16 | +// divergence between the wiring and the body fails. |
| 17 | +// Coverage test: TestSecurityTxt_ServedFromBothPathsWithRFC9116Body |
| 18 | +// + TestBuildSecurityTxtBody_RFC9116Fields below. |
| 19 | +// Live verified: on the merge commit, run |
| 20 | +// curl -sS https://api.instanode.dev/.well-known/security.txt |
| 21 | +// curl -sS https://api.instanode.dev/security.txt |
| 22 | +// both must return identical text/plain bodies with |
| 23 | +// the Contact/Expires/Canonical fields. |
| 24 | + |
| 25 | +import ( |
| 26 | + "io" |
| 27 | + "net/http/httptest" |
| 28 | + "strings" |
| 29 | + "testing" |
| 30 | + "time" |
| 31 | + |
| 32 | + "github.com/gofiber/fiber/v2" |
| 33 | + "github.com/stretchr/testify/require" |
| 34 | + |
| 35 | + "instant.dev/internal/router" |
| 36 | +) |
| 37 | + |
| 38 | +// requiredFields are the RFC 9116 fields the security.txt body MUST |
| 39 | +// emit (Contact + Expires are §2.5 mandatory) or SHOULD emit |
| 40 | +// (Preferred-Languages + Canonical + Policy are §2.5 recommended). |
| 41 | +var requiredFields = []string{ |
| 42 | + "Contact:", // §2.5.3 — mandatory |
| 43 | + "Expires:", // §2.5.5 — mandatory |
| 44 | + "Preferred-Languages:", // §2.5.8 — recommended |
| 45 | + "Canonical:", // §2.5.2 — recommended |
| 46 | + "Policy:", // §2.5.7 — recommended |
| 47 | +} |
| 48 | + |
| 49 | +// newSecurityTxtApp wires the exported handler builder against a |
| 50 | +// minimal fiber app. The handler is the literal one router.New |
| 51 | +// installs (extracted to its own file in security_txt.go specifically |
| 52 | +// so the unit test can call it directly without standing up the full |
| 53 | +// router — which needs Postgres + Redis + gRPC). |
| 54 | +func newSecurityTxtApp(t *testing.T) *fiber.App { |
| 55 | + t.Helper() |
| 56 | + app := fiber.New() |
| 57 | + h := router.ExportedMakeSecurityTxtHandler(time.Now()) |
| 58 | + app.Get("/.well-known/security.txt", h) |
| 59 | + app.Get("/security.txt", h) |
| 60 | + return app |
| 61 | +} |
| 62 | + |
| 63 | +func TestSecurityTxt_ServedFromBothPathsWithRFC9116Body(t *testing.T) { |
| 64 | + app := newSecurityTxtApp(t) |
| 65 | + |
| 66 | + paths := []string{"/.well-known/security.txt", "/security.txt"} |
| 67 | + bodies := make(map[string]string, len(paths)) |
| 68 | + for _, p := range paths { |
| 69 | + t.Run(p, func(t *testing.T) { |
| 70 | + resp, err := app.Test(httptest.NewRequest("GET", p, nil)) |
| 71 | + require.NoError(t, err) |
| 72 | + defer resp.Body.Close() |
| 73 | + require.Equal(t, fiber.StatusOK, resp.StatusCode, |
| 74 | + "BUG-API-411: %s must serve the security.txt body, not a 404 envelope", p) |
| 75 | + |
| 76 | + // Content-Type must be text/plain so RFC 9116 parsers accept |
| 77 | + // the body without sniff fallback. UTF-8 charset is the file |
| 78 | + // format the RFC specifies. |
| 79 | + ct := resp.Header.Get("Content-Type") |
| 80 | + require.Contains(t, ct, "text/plain", "Content-Type must be text/plain (RFC 9116 §2.3); got %q", ct) |
| 81 | + require.Contains(t, ct, "utf-8", "Content-Type must declare utf-8 charset; got %q", ct) |
| 82 | + |
| 83 | + raw, err := io.ReadAll(resp.Body) |
| 84 | + require.NoError(t, err) |
| 85 | + body := string(raw) |
| 86 | + bodies[p] = body |
| 87 | + |
| 88 | + // Every required + recommended field present. |
| 89 | + for _, field := range requiredFields { |
| 90 | + require.Contains(t, body, field, |
| 91 | + "security.txt body must carry %q field (RFC 9116 §2.5); body=%q", field, body) |
| 92 | + } |
| 93 | + |
| 94 | + // Contact MUST appear at least twice — one mailto: + one |
| 95 | + // https://. Multiple Contact fields are explicitly supported |
| 96 | + // by §2.5.3 and the redundancy is the point. |
| 97 | + contactCount := strings.Count(body, "Contact:") |
| 98 | + require.GreaterOrEqual(t, contactCount, 2, |
| 99 | + "security.txt body must list at least 2 Contact fields (mailto: + https://); got %d", contactCount) |
| 100 | + require.Contains(t, body, "mailto:security@instanode.dev", |
| 101 | + "Contact must include the mailto: form so OS-default mail clients work") |
| 102 | + require.Contains(t, body, "https://instanode.dev/security", |
| 103 | + "Contact must include the https:// form for researchers who prefer a web channel") |
| 104 | + |
| 105 | + // Canonical must point at the .well-known path on the api |
| 106 | + // host (the file is its own canonical declaration even when |
| 107 | + // served from the apex /security.txt fallback). |
| 108 | + require.Contains(t, body, "Canonical: https://api.instanode.dev/.well-known/security.txt", |
| 109 | + "Canonical must point at the .well-known path on the api host (RFC 9116 §2.5.2)") |
| 110 | + }) |
| 111 | + } |
| 112 | + |
| 113 | + // Both paths must serve byte-identical bodies — otherwise a researcher |
| 114 | + // hitting the apex fallback gets different instructions than the |
| 115 | + // .well-known canonical path. |
| 116 | + require.Equal(t, bodies["/.well-known/security.txt"], bodies["/security.txt"], |
| 117 | + "BUG-API-411: both paths MUST serve byte-identical bodies") |
| 118 | +} |
| 119 | + |
| 120 | +// TestBuildSecurityTxtBody_ExpiresFieldIsOneYearAfterNow pins the |
| 121 | +// Expires-window rule: §2.5.5 SHOULD-NOT exceed 1 year, our policy is |
| 122 | +// exactly 1y from build time. Failing this gate means a future edit |
| 123 | +// that bumps the window past 1y would let researchers see a long-stale |
| 124 | +// file long after the operator stopped maintaining it. |
| 125 | +func TestBuildSecurityTxtBody_ExpiresFieldIsOneYearAfterNow(t *testing.T) { |
| 126 | + now := time.Date(2026, 5, 30, 12, 0, 0, 0, time.UTC) |
| 127 | + h := router.ExportedMakeSecurityTxtHandler(now) |
| 128 | + |
| 129 | + app := fiber.New() |
| 130 | + app.Get("/.well-known/security.txt", h) |
| 131 | + resp, err := app.Test(httptest.NewRequest("GET", "/.well-known/security.txt", nil)) |
| 132 | + require.NoError(t, err) |
| 133 | + defer resp.Body.Close() |
| 134 | + |
| 135 | + raw, _ := io.ReadAll(resp.Body) |
| 136 | + body := string(raw) |
| 137 | + |
| 138 | + expectedExpires := "Expires: 2027-05-30T12:00:00Z" |
| 139 | + require.Contains(t, body, expectedExpires, |
| 140 | + "Expires must be exactly 1 year after the builder's `now` (got body=%q, want substring=%q)", body, expectedExpires) |
| 141 | +} |
0 commit comments