()
| 1734 | } |
| 1735 | |
| 1736 | func (s *Server) setupRouter() { |
| 1737 | r := chi.NewRouter() |
| 1738 | |
| 1739 | // Infrastructure middleware (applies to all routes including /metrics) |
| 1740 | // CapturePeerAddr MUST run before TrustedProxyRealIP so downstream code |
| 1741 | // that needs to verify the real TCP peer (e.g. the bootstrap loopback |
| 1742 | // check) can read the untampered value from request context even on |
| 1743 | // deployments with a trusted reverse proxy in front. |
| 1744 | // withRequestStart is first so the request clock starts at arrival in this |
| 1745 | // process, before anything else can spend time: the account-deletion |
| 1746 | // deadline counts from it (TASK-4278). |
| 1747 | r.Use(withRequestStart) |
| 1748 | r.Use(CapturePeerAddr) |
| 1749 | // RealIP is gated on PAD_TRUSTED_PROXIES. When unset (the default), proxy |
| 1750 | // headers are ignored and the real TCP peer address is used everywhere. |
| 1751 | // This prevents X-Forwarded-For spoofing from bypassing rate limits, the |
| 1752 | // bootstrap loopback check, or audit logs on direct-exposed deployments. |
| 1753 | r.Use(TrustedProxyRealIP(s.trustedProxyCIDRs)) |
| 1754 | r.Use(chimiddleware.RequestID) |
| 1755 | r.Use(StructuredLogger) |
| 1756 | if s.metrics != nil { |
| 1757 | r.Use(MetricsMiddleware(s.metrics)) |
| 1758 | } |
| 1759 | r.Use(chimiddleware.Recoverer) |
| 1760 | // A rolling deadline on every response write, except routes that bound |
| 1761 | // their own (BUG-3821; see middleware_write_deadline.go). It MUST come |
| 1762 | // before CompressResponses, so its writer sits beneath the compressor. |
| 1763 | r.Use(s.boundResponseWrites) |
| 1764 | // gzip what the client accepts, except streams, upgrades, ranges and |
| 1765 | // credential responses (TASK-2225; see middleware_compress.go). |
| 1766 | r.Use(CompressResponses) |
| 1767 | |
| 1768 | // Security headers (applies to all routes) |
| 1769 | r.Use(SecurityHeaders) |
| 1770 | if s.secureCookies { |
| 1771 | r.Use(StrictTransportSecurity) |
| 1772 | } |
| 1773 | |
| 1774 | // One CORS handler, shared. The /api/v1 group mounts it as ordinary |
| 1775 | // middleware; ValidatePath serves its REJECTION through the same |
| 1776 | // instance, because that rejection short-circuits above the group and |
| 1777 | // would otherwise answer a cross-origin caller without the CORS |
| 1778 | // headers its siblings carry — see the middleware's doc comment. |
| 1779 | corsMW := cors.Handler(cors.Options{ |
| 1780 | AllowedOrigins: parseCORSOrigins(s.corsOrigins), |
| 1781 | AllowedMethods: []string{"GET", "POST", "PATCH", "PUT", "DELETE", "OPTIONS"}, |
| 1782 | AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token", "X-Share-Password", "X-Bootstrap-Token"}, |
| 1783 | // Credentials flag is gated on an operator explicitly listing |
| 1784 | // PAD_CORS_ORIGINS. The CLI uses Bearer tokens so the default |
| 1785 | // "no CORS_ORIGINS set" path doesn't need credential sharing; |
| 1786 | // leaving it off by default prevents cross-origin fetches from |
| 1787 | // a browser on a different site from piggy-backing cookies |
| 1788 | // on the victim's session. |
| 1789 | AllowCredentials: corsAllowCredentials(s.corsOrigins), |
| 1790 | MaxAge: 300, |
| 1791 | }) |
| 1792 | |
| 1793 | // Reject a request whose decoded path is not valid UTF-8 (or contains a |
no test coverage detected