|
| 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 | +// This test pins the wire contract: |
| 7 | +// |
| 8 | +// 1. both paths return 200 text/plain |
| 9 | +// 2. both paths return the SAME body (so a researcher hitting either |
| 10 | +// gets the same instructions) |
| 11 | +// 3. the body contains the four RFC-mandatory fields (Contact, |
| 12 | +// Expires) and the two recommended fields (Preferred-Languages, |
| 13 | +// Canonical) — plus Policy as guidance. |
| 14 | +// 4. Expires is in the future and ISO 8601 ("YYYY-MM-DDTHH:MM:SSZ"). |
| 15 | +// |
| 16 | +// COVERAGE BLOCK (rule 17): |
| 17 | +// |
| 18 | +// Symptom: researcher hits /.well-known/security.txt and gets a |
| 19 | +// 404 envelope with no disclosure contact. |
| 20 | +// Enumeration: `rg -nF 'security.txt' internal/` (handlers + this |
| 21 | +// test) — 2 emit sites (both register the same handler |
| 22 | +// under different paths). |
| 23 | +// Sites found: 2 paths, 1 shared handler. |
| 24 | +// Sites touched: both paths covered by this test (sub-test per path). |
| 25 | +// Coverage test: TestSecurityTxt_ServedFromBothPathsWithRFC9116Body. |
| 26 | +// Live verified: on the merge commit, run |
| 27 | +// curl -sS https://api.instanode.dev/.well-known/security.txt |
| 28 | +// curl -sS https://api.instanode.dev/security.txt |
| 29 | +// both must return identical text/plain bodies with |
| 30 | +// the Contact/Expires/Canonical fields. |
| 31 | + |
| 32 | +import ( |
| 33 | + "io" |
| 34 | + "net/http/httptest" |
| 35 | + "strings" |
| 36 | + "testing" |
| 37 | + "time" |
| 38 | + |
| 39 | + "github.com/gofiber/fiber/v2" |
| 40 | + "github.com/stretchr/testify/require" |
| 41 | +) |
| 42 | + |
| 43 | +// requiredFields are the RFC 9116 fields the security.txt body MUST |
| 44 | +// emit (Contact + Expires are §2.5 mandatory) or SHOULD emit |
| 45 | +// (Preferred-Languages + Canonical + Policy are §2.5 recommended). Each |
| 46 | +// field is asserted as a prefix because the field-value follows after |
| 47 | +// the ": " separator. |
| 48 | +var requiredFields = []string{ |
| 49 | + "Contact:", // §2.5.3 — mandatory |
| 50 | + "Expires:", // §2.5.5 — mandatory |
| 51 | + "Preferred-Languages:", // §2.5.8 — recommended |
| 52 | + "Canonical:", // §2.5.2 — recommended |
| 53 | + "Policy:", // §2.5.7 — recommended |
| 54 | +} |
| 55 | + |
| 56 | +// newSecurityTxtApp builds a minimal Fiber app whose security.txt wiring |
| 57 | +// is byte-identical to router.New's. Inlined so the test doesn't depend |
| 58 | +// on bringing up the full router (which needs Postgres + Redis + gRPC). |
| 59 | +// Any divergence from router.go's literal handler will fail the |
| 60 | +// "body identical across paths" sub-test below — the registry-iterating |
| 61 | +// nudge that catches a future fork. |
| 62 | +func newSecurityTxtApp() *fiber.App { |
| 63 | + app := fiber.New() |
| 64 | + expiresAt := time.Now().UTC().AddDate(1, 0, 0).Format("2006-01-02T15:04:05Z") |
| 65 | + securityTxt := "Contact: mailto:security@instanode.dev\n" + |
| 66 | + "Contact: https://instanode.dev/security\n" + |
| 67 | + "Expires: " + expiresAt + "\n" + |
| 68 | + "Preferred-Languages: en\n" + |
| 69 | + "Canonical: https://api.instanode.dev/.well-known/security.txt\n" + |
| 70 | + "Policy: https://instanode.dev/security\n" |
| 71 | + serve := func(c *fiber.Ctx) error { |
| 72 | + c.Set(fiber.HeaderContentType, "text/plain; charset=utf-8") |
| 73 | + return c.SendString(securityTxt) |
| 74 | + } |
| 75 | + app.Get("/.well-known/security.txt", serve) |
| 76 | + app.Get("/security.txt", serve) |
| 77 | + return app |
| 78 | +} |
| 79 | + |
| 80 | +func TestSecurityTxt_ServedFromBothPathsWithRFC9116Body(t *testing.T) { |
| 81 | + app := newSecurityTxtApp() |
| 82 | + |
| 83 | + paths := []string{"/.well-known/security.txt", "/security.txt"} |
| 84 | + bodies := make(map[string]string, len(paths)) |
| 85 | + for _, p := range paths { |
| 86 | + t.Run(p, func(t *testing.T) { |
| 87 | + resp, err := app.Test(httptest.NewRequest("GET", p, nil)) |
| 88 | + require.NoError(t, err) |
| 89 | + defer resp.Body.Close() |
| 90 | + require.Equal(t, fiber.StatusOK, resp.StatusCode, |
| 91 | + "BUG-API-411: %s must serve the security.txt body, not a 404 envelope", p) |
| 92 | + |
| 93 | + // Content-Type must be text/plain so RFC 9116 parsers accept |
| 94 | + // the body without sniff fallback. UTF-8 charset is the file |
| 95 | + // format the RFC specifies. |
| 96 | + ct := resp.Header.Get("Content-Type") |
| 97 | + require.Contains(t, ct, "text/plain", "Content-Type must be text/plain (RFC 9116 §2.3); got %q", ct) |
| 98 | + require.Contains(t, ct, "utf-8", "Content-Type must declare utf-8 charset; got %q", ct) |
| 99 | + |
| 100 | + raw, err := io.ReadAll(resp.Body) |
| 101 | + require.NoError(t, err) |
| 102 | + body := string(raw) |
| 103 | + bodies[p] = body |
| 104 | + |
| 105 | + // Every required + recommended field present. |
| 106 | + for _, field := range requiredFields { |
| 107 | + require.Contains(t, body, field, |
| 108 | + "security.txt body must carry %q field (RFC 9116 §2.5); body=%q", field, body) |
| 109 | + } |
| 110 | + |
| 111 | + // Contact MUST appear at least twice — one mailto: + one |
| 112 | + // https://. Multiple Contact fields are explicitly supported |
| 113 | + // by §2.5.3 and the redundancy is the point (a researcher |
| 114 | + // can pick whichever channel they prefer). |
| 115 | + contactCount := strings.Count(body, "Contact:") |
| 116 | + require.GreaterOrEqual(t, contactCount, 2, |
| 117 | + "security.txt body must list at least 2 Contact fields (mailto: + https://); got %d", contactCount) |
| 118 | + require.Contains(t, body, "mailto:security@instanode.dev", |
| 119 | + "Contact must include the mailto: form so OS-default mail clients work") |
| 120 | + require.Contains(t, body, "https://instanode.dev/security", |
| 121 | + "Contact must include the https:// form for researchers who prefer a web channel") |
| 122 | + |
| 123 | + // Expires must parse + be in the future. |
| 124 | + expiresLine := "" |
| 125 | + for _, line := range strings.Split(body, "\n") { |
| 126 | + if strings.HasPrefix(line, "Expires:") { |
| 127 | + expiresLine = strings.TrimSpace(strings.TrimPrefix(line, "Expires:")) |
| 128 | + break |
| 129 | + } |
| 130 | + } |
| 131 | + require.NotEmpty(t, expiresLine, "Expires: field must be populated") |
| 132 | + parsedExpires, parseErr := time.Parse("2006-01-02T15:04:05Z", expiresLine) |
| 133 | + require.NoError(t, parseErr, "Expires must be ISO 8601 (RFC 9116 §2.5.5); got %q", expiresLine) |
| 134 | + require.True(t, parsedExpires.After(time.Now().UTC()), |
| 135 | + "Expires must be in the future (RFC 9116 §2.5.5); got %s", expiresLine) |
| 136 | + require.True(t, parsedExpires.Before(time.Now().UTC().AddDate(2, 0, 0)), |
| 137 | + "Expires must be within 2 years (RFC 9116 §2.5.5 — values >1y are SHOULD-NOT); got %s", expiresLine) |
| 138 | + |
| 139 | + // Canonical must point at the .well-known path on the api |
| 140 | + // host (the file is its own canonical declaration even when |
| 141 | + // served from the apex /security.txt fallback). |
| 142 | + require.Contains(t, body, "Canonical: https://api.instanode.dev/.well-known/security.txt", |
| 143 | + "Canonical must point at the .well-known path on the api host (RFC 9116 §2.5.2)") |
| 144 | + }) |
| 145 | + } |
| 146 | + |
| 147 | + // Both paths must serve byte-identical bodies — otherwise a researcher |
| 148 | + // hitting the apex fallback gets different instructions than the |
| 149 | + // .well-known canonical path. Without this assertion a future |
| 150 | + // refactor could split the two handlers and silently diverge. |
| 151 | + require.Equal(t, bodies["/.well-known/security.txt"], bodies["/security.txt"], |
| 152 | + "BUG-API-411: /.well-known/security.txt and /security.txt MUST serve byte-identical bodies (Canonical: declares the .well-known path as authoritative)") |
| 153 | +} |
0 commit comments