MCPcopy Create free account
hub / github.com/PerpetualSoftware/pad / setupRouter

Method setupRouter

internal/server/server.go:1685–2739  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1683}
1684
1685func (s *Server) setupRouter() {
1686 r := chi.NewRouter()
1687
1688 // Infrastructure middleware (applies to all routes including /metrics)
1689 // CapturePeerAddr MUST run before TrustedProxyRealIP so downstream code
1690 // that needs to verify the real TCP peer (e.g. the bootstrap loopback
1691 // check) can read the untampered value from request context even on
1692 // deployments with a trusted reverse proxy in front.
1693 // withRequestStart is first so the request clock starts at arrival in this
1694 // process, before anything else can spend time: the account-deletion
1695 // deadline counts from it (TASK-4278).
1696 r.Use(withRequestStart)
1697 r.Use(CapturePeerAddr)
1698 // RealIP is gated on PAD_TRUSTED_PROXIES. When unset (the default), proxy
1699 // headers are ignored and the real TCP peer address is used everywhere.
1700 // This prevents X-Forwarded-For spoofing from bypassing rate limits, the
1701 // bootstrap loopback check, or audit logs on direct-exposed deployments.
1702 r.Use(TrustedProxyRealIP(s.trustedProxyCIDRs))
1703 r.Use(chimiddleware.RequestID)
1704 r.Use(StructuredLogger)
1705 if s.metrics != nil {
1706 r.Use(MetricsMiddleware(s.metrics))
1707 }
1708 r.Use(chimiddleware.Recoverer)
1709 // A rolling deadline on every response write, except routes that bound
1710 // their own (BUG-3821; see middleware_write_deadline.go). It MUST come
1711 // before CompressResponses, so its writer sits beneath the compressor.
1712 r.Use(s.boundResponseWrites)
1713 // gzip what the client accepts, except streams, upgrades, ranges and
1714 // credential responses (TASK-2225; see middleware_compress.go).
1715 r.Use(CompressResponses)
1716
1717 // Security headers (applies to all routes)
1718 r.Use(SecurityHeaders)
1719 if s.secureCookies {
1720 r.Use(StrictTransportSecurity)
1721 }
1722
1723 // One CORS handler, shared. The /api/v1 group mounts it as ordinary
1724 // middleware; ValidatePath serves its REJECTION through the same
1725 // instance, because that rejection short-circuits above the group and
1726 // would otherwise answer a cross-origin caller without the CORS
1727 // headers its siblings carry — see the middleware's doc comment.
1728 corsMW := cors.Handler(cors.Options{
1729 AllowedOrigins: parseCORSOrigins(s.corsOrigins),
1730 AllowedMethods: []string{"GET", "POST", "PATCH", "PUT", "DELETE", "OPTIONS"},
1731 AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token", "X-Share-Password", "X-Bootstrap-Token"},
1732 // Credentials flag is gated on an operator explicitly listing
1733 // PAD_CORS_ORIGINS. The CLI uses Bearer tokens so the default
1734 // "no CORS_ORIGINS set" path doesn't need credential sharing;
1735 // leaving it off by default prevents cross-origin fetches from
1736 // a browser on a different site from piggy-backing cookies
1737 // on the victim's session.
1738 AllowCredentials: corsAllowCredentials(s.corsOrigins),
1739 MaxAge: 300,
1740 })
1741
1742 // Reject a request whose decoded path is not valid UTF-8 (or contains a

Callers 1

ensureRouterMethod · 0.95

Calls 15

registerMCPRoutesMethod · 0.95
registerAppAPIRoutesMethod · 0.95
registerOAuthRoutesMethod · 0.95
itemScopedCommentMethod · 0.95
TrustedProxyRealIPFunction · 0.85
MetricsMiddlewareFunction · 0.85
parseCORSOriginsFunction · 0.85
corsAllowCredentialsFunction · 0.85
ValidatePathFunction · 0.85
ValidateQueryFunction · 0.85
HandlerMethod · 0.80

Tested by

no test coverage detected