Files
bwilliams a279405cae Added the HTTP server and serve command, with proxy, CSRF, TLS and security header config; wsserve is now ws serve, alongside a new ws dial
The serve command runs internal/server: the API under /api (health, and
the CSRF token endpoint) and the WebSocket hub under /ws, over HTTP or TLS,
with an optional HTTP to HTTPS redirect, the PROXY protocol from trusted
sources and a graceful shutdown that closes WebSockets first. Its middleware
covers the client IP behind trusted proxies, request IDs, request logging,
panic recovery, global and API rate limits, Host and body size checks,
security response headers, compression, CSRF protection (cross-origin
checks and signed double-submit tokens) and JSON-only API bodies. Middleware
that logs takes a WithLogger option.

New server config sections: proxy, csrf, tls, headers and frontend (not
used yet), plus max_header_bytes, max_body_bytes, global and API rate limits
and HTTP read, write and idle timeouts. Every setting documents what 0 or
empty means, and internal/config/README.md is reorganised by section.
server.timeouts.read_header must be greater than 0, and a header or idle
timeout of 0 means no limit rather than the read timeout.

WebSocket upgrades are protected by the Origin check: the server's own
origin, or one of csrf.cross_origin_protection.trusted_origins, which the
hub now takes with WithTrustedOrigins.

wsserve becomes "ws serve". "ws dial" opens a WebSocket on /ws to test it:
--ping sends a ping and waits for the pong, --message sends messages and
prints the replies, --origin tests the Origin check, and with neither
--ping nor --message each line of stdin is sent until Ctrl+C.
2026-10-15 17:47:40 +01:00

125 lines
3.9 KiB
Go

package config
import (
"reflect"
"time"
)
// JSCKServerCSRF is the server CSRF section's configuration key.
const JSCKServerCSRF string = "csrf"
// JSCServerCSRF configures the server's CSRF protection: its cross-origin
// check and its tokens. ExemptPaths are ServeMux-style patterns skipped by
// both (e.g. "/api/webhooks/"); empty means none are skipped.
type JSCServerCSRF struct {
CrossOriginProtection JSCServerCSRFCrossOrigin `cfg:"cross_origin_protection"`
Tokens JSCServerCSRFTokens `cfg:"tokens"`
ExemptPaths []string `cfg:"exempt_paths"`
}
// ---------- Section methods (see Section) ----------
// Map implements Section.
func (c JSCServerCSRF) Map() map[string]any { return mapOf(c) }
// Flatten implements Section.
func (c JSCServerCSRF) Flatten() map[string]any {
return flattenOf(JSCKServerCSRF, c)
}
// TypeOf implements Section.
func (c JSCServerCSRF) TypeOf(key string) (reflect.Kind, error) {
return typeOf(c, key)
}
// ValueOf implements Section.
func (c JSCServerCSRF) ValueOf(key string) (any, error) {
return valueOf(c, key)
}
// SetValue implements Section.
func (c *JSCServerCSRF) SetValue(key string, value any) error {
return setValue(c, key, value)
}
// ========== Cross-Origin Protection ==========
// JSCKServerCSRFCrossOrigin is the server CSRF cross-origin protection
// section's configuration key.
const JSCKServerCSRFCrossOrigin string = "cross_origin_protection"
// JSCServerCSRFCrossOrigin configures CSRF protection by checking requests'
// origin. TrustedOrigins are extra origins ("https://admin.example.com")
// allowed to make cross-origin unsafe requests; empty means none.
type JSCServerCSRFCrossOrigin struct {
Enabled bool `cfg:"enabled" default:"true"`
TrustedOrigins []string `cfg:"trusted_origins"`
}
// ---------- Section methods (see Section) ----------
// Map implements Section.
func (c JSCServerCSRFCrossOrigin) Map() map[string]any { return mapOf(c) }
// Flatten implements Section.
func (c JSCServerCSRFCrossOrigin) Flatten() map[string]any {
return flattenOf(JSCKServerCSRFCrossOrigin, c)
}
// TypeOf implements Section.
func (c JSCServerCSRFCrossOrigin) TypeOf(key string) (reflect.Kind, error) {
return typeOf(c, key)
}
// ValueOf implements Section.
func (c JSCServerCSRFCrossOrigin) ValueOf(key string) (any, error) {
return valueOf(c, key)
}
// SetValue implements Section.
func (c *JSCServerCSRFCrossOrigin) SetValue(key string, value any) error {
return setValue(c, key, value)
}
// ========== CSRF Tokens ==========
// JSCKServerCSRFTokens is the server CSRF tokens section's configuration key.
const JSCKServerCSRFTokens string = "tokens"
// JSCServerCSRFTokens configures CSRF protection by tokens. Key is the hex
// HMAC key tokens are signed with; empty means a random key for the process,
// so tokens don't survive a restart. An empty CookieName or HeaderName, or a
// TokenTTL of 0 or less, means the default.
type JSCServerCSRFTokens struct {
Enabled bool `cfg:"enabled" default:"true"`
Key string `cfg:"key"`
CookieName string `cfg:"cookie_name" default:"js_csrf"`
HeaderName string `cfg:"header_name" default:"X-CSRF-Token"`
TokenTTL time.Duration `cfg:"token_ttl" default:"12h"`
}
// ---------- Section methods (see Section) ----------
// Map implements Section.
func (c JSCServerCSRFTokens) Map() map[string]any { return mapOf(c) }
// Flatten implements Section.
func (c JSCServerCSRFTokens) Flatten() map[string]any {
return flattenOf(JSCKServerCSRFTokens, c)
}
// TypeOf implements Section.
func (c JSCServerCSRFTokens) TypeOf(key string) (reflect.Kind, error) {
return typeOf(c, key)
}
// ValueOf implements Section.
func (c JSCServerCSRFTokens) ValueOf(key string) (any, error) {
return valueOf(c, key)
}
// SetValue implements Section.
func (c *JSCServerCSRFTokens) SetValue(key string, value any) error {
return setValue(c, key, value)
}