From 1101d87fb3de109bf261c73c4a6cbc5a5d6f5e40 Mon Sep 17 00:00:00 2001 From: TyperBody Date: Sat, 7 Nov 2026 18:24:44 +0800 Subject: [PATCH] fix(security): CORS/WS origin allowlist, handshake auth, server timeouts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes the review's P1 set (M2, M3, M4, M15) on top of the PR QA branch. M2 — CORS echoed a fixed `*` alongside `Allow-Credentials: true`, which is unusable: a wildcard origin forbids credentials. The configured Origin is now echoed back (plus `Vary: Origin`), and only when it is in the allowlist, so `Allow-Credentials` becomes meaningful and a future Cookie session keeps working. M4 — the WebSocket upgraders accepted any origin and the two /ws routes sat behind no middleware at all, so knowing an instance_id or app_id was enough to open a socket. Handshakes now require a valid JWT before the upgrade (401 otherwise) and check the same Origin allowlist as CORS (403 for a foreign origin). The adapter gateway upgrader takes the list through GatewayConfig.AllowedOrigin. Tokens are accepted via ?token= (Python bridges, updated here), Authorization: Bearer, or the `espl` subprotocol, which is the only header a browser can set on a WebSocket. Requests without an Origin are non-browser clients and pass. M3 — the shipped JWT secret was used with no check. Startup now WARNs while the placeholder (or an empty secret) is in place, with a fix hint. A prefix comparison would misfire, so a secret that merely contains the placeholder text still counts as rotated. M15 — http.Server had only Addr and Handler. All four timeouts are wired from config, and a global MaxBodyBytes middleware caps request bodies: a declared oversize is refused with 413 up front, an undeclared one is caught by http.MaxBytesReader. One `security.allowed_origins` list drives CORS, both socket upgraders and the gateway, so they cannot drift; ESP_SECURITY_ALLOWED_ORIGINS overrides it without editing the file. Every item has a test, including the cases that used to pass silently (garbage token accepted, foreign origin accepted). Docs touched: api-reference (WS auth table, CORS, limits, 413), SECURITY, CONTRIBUTING, and the review's own status marks. Co-authored-by: multica-agent --- .github/CONTRIBUTING.md | 4 +- .github/SECURITY.md | 8 +- adapters/taobao/esp_bridge.py | 11 +- adapters/xianyu/esp_bridge.py | 10 +- config/config.yaml | 22 +++ docs/developer-guide/api-reference.md | 41 ++++- docs/developer-guide/functional-review.md | 16 +- internal/adaptergateway/gateway.go | 13 +- internal/config/config.go | 92 ++++++++++- internal/config/config_test.go | 64 ++++++++ internal/config/env_test.go | 34 +++++ internal/handler/websocket.go | 93 ++++++++++- internal/handler/websocket_test.go | 178 ++++++++++++++++++++++ internal/middleware/cors_test.go | 151 ++++++++++++++++++ internal/middleware/middleware.go | 62 +++++++- internal/server/server.go | 52 ++++++- internal/server/server_test.go | 47 ++++++ 17 files changed, 869 insertions(+), 29 deletions(-) create mode 100644 internal/config/config_test.go create mode 100644 internal/config/env_test.go create mode 100644 internal/handler/websocket_test.go create mode 100644 internal/middleware/cors_test.go create mode 100644 internal/server/server_test.go diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index fbe4ca3e..464b3dca 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -28,7 +28,9 @@ go mod download go run main.go # reads ./config/config.yaml, then ./config.yaml, then /etc/e-sp-line2 ``` -The API listens on `:8080`. Before you expose it beyond localhost, change `jwt.secret` in the config — see [SECURITY.md](SECURITY.md). +The API listens on `:8080`. Before you expose it beyond localhost, change `jwt.secret` in the config — see [SECURITY.md](SECURITY.md). Startup warns while the placeholder is still in place. + +The default config already carries `server` timeouts and a body cap; do not delete them. Serving the UI from another origin needs that origin in `security.allowed_origins` (it gates CORS *and* the WebSocket upgrade) — `ESP_SECURITY_ALLOWED_ORIGINS` overrides the list without editing the file. ```bash make build # build to bin/e-sp-line2 diff --git a/.github/SECURITY.md b/.github/SECURITY.md index c8c0c64a..189c5c18 100644 --- a/.github/SECURITY.md +++ b/.github/SECURITY.md @@ -52,9 +52,11 @@ Out of scope: ### Backend - Passwords are stored as bcrypt hashes, never in plaintext. -- API tokens are JWTs signed with HS256 and carry an expiry (`jwt.expiration`, 24 hours by default). Rotate `jwt.secret` in `backend/config/config.yaml` per deployment. +- API tokens are JWTs signed with HS256 and carry an expiry (`jwt.expiration`, 24 hours by default). Rotate `jwt.secret` in `config/config.yaml` per deployment; startup logs a WARN while the shipped placeholder is still in place. - The `/api/v1` surface except `/auth/register` and `/auth/login` sits behind `AuthRequired`. `AdminRequired` exists but is not wired into any route yet. - An adapter's `security` policy declares `sensitive_fields` (for Taobao and Xianyu, `cookie`) so a manifest can mark what must not be echoed back. +- CORS echoes a `security.allowed_origins` member instead of a wildcard, and the WebSocket upgraders (bridge, app and adapter gateway) check the same list. A cross-origin socket is refused with 403 before the upgrade, and both sockets require a valid JWT during the handshake (401 otherwise). +- The HTTP server runs with a header timeout, read/write/idle timeouts and a request-body cap, so a stalled or oversized request cannot pin a connection. ### What is not there yet @@ -62,8 +64,8 @@ Be aware of these when you deploy, and please send a report if one of them bites - The adapter instance `config` column stores its Cookie as plain JSON in the database, despite the `encrypted_fields` declaration. Treat the SQLite/PostgreSQL file as a secret. - `AuthRequired` is still a placeholder: it checks the presence and `Bearer` shape of the header but does not yet verify the JWT signature, and it sets a fixed `user-123` identity. Do not expose the API to the internet and assume per-user isolation until that lands. -- CORS sends `Access-Control-Allow-Origin: *` with `Access-Control-Allow-Credentials: true`, and the WebSocket upgrader accepts any origin. Put the front end behind the same origin as the API in production. -- The default `jwt.secret` is a placeholder; change it before you deploy. +- The default `jwt.secret` is a placeholder; startup only warns, it does not refuse to boot. Change it before you deploy. +- `security.allowed_origins` defaults to `localhost` only. A real deployment must list its front-end origin (or set `ESP_SECURITY_ALLOWED_ORIGINS`); a `*` is not accepted, the list is matched exactly. ### Front end diff --git a/adapters/taobao/esp_bridge.py b/adapters/taobao/esp_bridge.py index ed00cb7a..963dcf73 100644 --- a/adapters/taobao/esp_bridge.py +++ b/adapters/taobao/esp_bridge.py @@ -25,6 +25,7 @@ import asyncio import json import time +import urllib.parse import uuid from dataclasses import dataclass, field from typing import Any, Callable, Dict, List, Optional @@ -188,9 +189,15 @@ def __init__( self.running = False def _ws_url(self) -> str: - """构造后端适配器 WebSocket 地址。""" + """构造后端适配器 WebSocket 地址。 + + token 走 query 参数:后端在升级握手前校验它,缺失直接回 401。 + """ base = self.backend_url.replace("http://", "ws://").replace("https://", "wss://") - return f"{base}/ws/adapter?instance_id={self.instance_id}" + url = f"{base}/ws/adapter?instance_id={self.instance_id}" + if self.token: + url += f"&token={urllib.parse.quote(self.token, safe='')}" + return url def _tag(self) -> str: """短标签:instance_id 前 8 位,便于在日志中区分多个桥。""" diff --git a/adapters/xianyu/esp_bridge.py b/adapters/xianyu/esp_bridge.py index 1398d5c5..39c1142e 100644 --- a/adapters/xianyu/esp_bridge.py +++ b/adapters/xianyu/esp_bridge.py @@ -21,6 +21,7 @@ import asyncio import json import time +import urllib.parse import uuid from dataclasses import dataclass, field from typing import Any, Dict, Optional, Callable @@ -170,8 +171,15 @@ def __init__( self.running = False def _ws_url(self) -> str: + """构造后端适配器 WebSocket 地址。 + + token 走 query 参数:后端在升级握手前校验它,缺失直接回 401。 + """ base = self.backend_url.replace("http://", "ws://").replace("https://", "wss://") - return f"{base}/ws/adapter?instance_id={self.instance_id}" + url = f"{base}/ws/adapter?instance_id={self.instance_id}" + if self.token: + url += f"&token={urllib.parse.quote(self.token, safe='')}" + return url def _tag(self) -> str: """短标签:instance_id 前 8 位,便于在日志中区分多个桥。""" diff --git a/config/config.yaml b/config/config.yaml index dee93bab..444c0a4e 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -29,9 +29,31 @@ log_level: "info" # debug, info, warn, error # JWT settings jwt: + # The shipped value is a placeholder: while it is in place startup logs a + # WARN and anyone can forge a token. Generate one per deployment, e.g. + # openssl rand -base64 32 secret: "change-this-secret-in-production" expiration: 24 # hours +# HTTP server settings — all seconds, 0 disables a timeout. +server: + read_header_timeout: 10 + read_timeout: 60 + write_timeout: 120 + idle_timeout: 120 + max_body_bytes: 4194304 # 4 MiB + +# Browser security. One allowlist, used by CORS *and* the WebSocket Origin +# check. Add the real front-end Origin when the UI is served from elsewhere; +# ESP_SECURITY_ALLOWED_ORIGINS (comma-separated) overrides this list. An empty +# list means "allow any Origin" (development fallback). +security: + allowed_origins: + - "http://localhost:3000" + - "http://127.0.0.1:3000" + - "http://localhost:8080" + - "http://127.0.0.1:8080" + # Adapter settings adapter: heartbeat_interval: 30 # seconds diff --git a/docs/developer-guide/api-reference.md b/docs/developer-guide/api-reference.md index a065a3d4..e626cc1e 100644 --- a/docs/developer-guide/api-reference.md +++ b/docs/developer-guide/api-reference.md @@ -752,9 +752,26 @@ Public. Returns the message field contract that [message-fields.md](./message-fi ## WebSocket interface +Both sockets authenticate during the HTTP handshake, before the upgrade. A +missing or invalid token is answered with **401** as a plain HTTP response, and +an `Origin` outside the allowlist with **403**, so a failure is readable rather +than an immediately-closed socket. + +The token is accepted in any of three forms, because the callers differ: + +| Transport | Caller | +|---|---| +| `?token=` query parameter | the Python bridges (they cannot set headers on connect) | +| `Authorization: Bearer ` header | server-to-server clients, `websocat`, tests | +| `Sec-WebSocket-Protocol: espl, ` | browsers — the only header a page can set on `WebSocket` | + +`Origin` is checked against `security.allowed_origins` (the same list the CORS +middleware uses; `ESP_SECURITY_ALLOWED_ORIGINS` overrides it). A request without +an `Origin` header is a non-browser client and is allowed through. + ### Adapter WebSocket -**URL**: `ws://localhost:8080/ws/adapter?instance_id=` +**URL**: `ws://localhost:8080/ws/adapter?instance_id=&token=` **Message format**: ```json @@ -776,7 +793,7 @@ Public. Returns the message field contract that [message-fields.md](./message-fi ### App WebSocket -**URL**: `ws://localhost:8080/ws/app?app_id=` +**URL**: `ws://localhost:8080/ws/app?app_id=&token=` **Message format**: ```json @@ -796,6 +813,25 @@ Public. Returns the message field contract that [message-fields.md](./message-fi } ``` +## CORS + +Browsers get the request `Origin` echoed back in +`Access-Control-Allow-Origin` when it appears in `security.allowed_origins` +(defaults cover `localhost:3000` and `localhost:8080`); `Vary: Origin` is set so +caches key on it. Because the value is a concrete origin rather than `*`, +`Access-Control-Allow-Credentials: true` is meaningful and a future Cookie-based +session keeps working. Requests from an unlisted origin still reach the handler +but carry no CORS headers, so the browser blocks the response. Requests without +an `Origin` (curl, the Python bridges) are unaffected. + +## Limits + +- Request bodies are capped by `server.max_body_bytes` (default 4 MiB); an + oversized body is refused with **413**. +- The HTTP server enforces `server.read_header_timeout` (default 10s), + `read_timeout`, `write_timeout` and `idle_timeout`. All are seconds; `0` + disables the corresponding timeout. + ## Error codes | Error code | Description | @@ -805,6 +841,7 @@ Public. Returns the message field contract that [message-fields.md](./message-fi | 403 | Forbidden | | 404 | Resource not found | | 409 | Resource conflict | +| 413 | Request body too large | | 500 | Internal server error | ## Rate limiting diff --git a/docs/developer-guide/functional-review.md b/docs/developer-guide/functional-review.md index 314628c6..8986a8c8 100644 --- a/docs/developer-guide/functional-review.md +++ b/docs/developer-guide/functional-review.md @@ -168,6 +168,8 @@ second insert with empty idempotency_key: **建议**:要么回显 `Origin` 并配白名单,要么明确只走 Bearer 不打算用 Cookie,把 `Allow-Credentials` 删掉以免误导。 +**现已修复**(P1/ESPL-20):走了"回显 + 白名单"这条路,因为前端将来未必一直用 Bearer。`internal/middleware/middleware.go` 的 `CORS` 现在只对 `security.allowed_origins` 里的 Origin 回 `Access-Control-Allow-Origin: ` 并保留 `Allow-Credentials: true`,同时加 `Vary: Origin`;不带 `Origin` 的客户端(curl、Python 桥)完全不受影响。实测:允许的 Origin 预检拿到 204 + 回显 + `Vary`,外来 Origin 只剩 `Vary`、无 `Access-Control-Allow-Origin`。 + ### M3. 默认 JWT secret 两处硬编码,且没有任何"是否已改"的检查 **定位**:`internal/config/config.go:126`(viper 默认值)、`config/config.yaml:32`(随仓库提交的值),内容都是 `change-this-secret-in-production`。 @@ -176,6 +178,8 @@ second insert with empty idempotency_key: **建议**:`config.Load` 后若 secret 仍是占位串则打 WARN(或直接拒绝启动);README/SECURITY 已有的提醒保留。 +**现已修复**(P1/ESPL-20):选了 WARN 而不是拒启,避免把现有部署一下卡死。判定收敛到 `config.UsingPlaceholderJWTSecret()`(占位串或空串都算),`server.New` 启动时打一条 WARN 并带修法提示。只做前缀比较不够——`change-this-secret-in-production-prod` 已是真轮换,不该报警,测试里钉住了这条。实测启动日志:`WARN ... JWT secret is still the shipped placeholder`。 + ### M4. WebSocket `CheckOrigin` 恒真,且握手不做鉴权 **定位**:`internal/handler/websocket.go:17` — `return true // Allow all origins for development`。 @@ -184,6 +188,8 @@ second insert with empty idempotency_key: **建议**:接 B1 后校验 `Sec-WebSocket-Protocol` 或 `?token=`,`CheckOrigin` 收白名单。 +**现已修复**(P1/ESPL-20):两条 WS 在**升级前**校验 token,缺失或非法都回 401(不再只是"验个形状");`CheckOrigin` 改为查与 CORS 同一份 `security.allowed_origins`,跨域来源在升级前被 403 拒掉;适配器网关的 upgrader 也接了同一份列表,`GatewayConfig.AllowedOrigin` 注入。token 三种承载:`?token=`(Python 桥,已同步改 `adapters/*/esp_bridge.py`)、`Authorization: Bearer`、`Sec-WebSocket-Protocol: espl, `(浏览器唯一可设的头)。无 `Origin` 的非浏览器客户端放行。注意这是**鉴权到位**,不是**授权**:握手只验 token 有效,"这个 token 能不能碰这个 instance_id"仍属 M1。 + ### M5. `POST /platforms` 等三个 handler 有实现、无路由 **现象**:`CreatePlatform`、`UpdatePlatform`、`DeletePlatform` 三个 handler 完整存在,但 `server.go` 只注册了 `GET ""` 和 `GET "/:id"`。实测 `POST /api/v1/platforms` → **404**。 @@ -322,6 +328,8 @@ TestReviewDoubleClose → PANIC: close of closed channel **建议**:补 `ReadHeaderTimeout` / `ReadTimeout` / `WriteTimeout` / `IdleTimeout`,加 `MaxBytesReader`。 +**现已修复**(P1/ESPL-20):四类超时全部落到 `http.Server`,值来自 `server.*` 配置(默认 10s/60s/120s/120s,0 = 不设)。请求体上限 `server.max_body_bytes`(默认 4 MiB)由全局中间件 `MaxBodyBytes` 执行:声明超限直接 413,不声明(chunked/撒谎的 Content-Length)由 `http.MaxBytesReader` 兜住。实测 8KB body → 413、正常 body → 200;只发一半头后挂住的连接 **2.0s**(配置值)被 `ReadHeaderTimeout` 断开。 + ### M16. `make migrate` 与 `make dev` 传的参数 `main.go` 根本不读 **现象**:`main.go` 没有任何 `flag.` / `os.Args` 解析(`grep` 无命中)。实测带 `migrate` 参数启动,进程**照常起 HTTP 服务**,不迁移、不退出。 @@ -456,9 +464,9 @@ Error: EROFS: read-only file system, open '.../vite.config.ts.timestamp-17940267 7. **M7 API 基址**:改 `import.meta.env.VITE_API_BASE_URL ?? '/api/v1'`,健康检查走相对路径。 8. **M8/M9 登录态**:401 统一 `clearToken` + 跳 `/login`;`handleLogout` 改用 client;加 `RequireAuth` 路由守卫。 -9. **M3 JWT secret**:仍是占位串就 WARN 或拒启。 -10. **M4 WS 鉴权 + CheckOrigin 白名单**(依赖 P0-1)。 -11. **M2 CORS**:回显 Origin 配白名单,或删掉 `Allow-Credentials`。 +9. **M3 JWT secret**:已完成(P1/ESPL-20,启动 WARN)。 +10. **M4 WS 鉴权 + CheckOrigin 白名单**:已完成(P1/ESPL-20,升级前 401 / 跨域 403,白名单与 CORS 同一份)。 +11. **M2 CORS**:已完成(P1/ESPL-20,回显白名单 Origin + `Vary: Origin`)。 ### P2 — 正确性与可观测性 @@ -467,7 +475,7 @@ Error: EROFS: read-only file system, open '.../vite.config.ts.timestamp-17940267 14. **M5**:补齐 `POST/PUT/DELETE /platforms`,挂 `AuthRequired` + `AdminRequired`。 15. **M13 测试**:先鉴权、再 WS 端到端;`CGO_ENABLED=0` 下用探测 + `t.Skip` 兜底。 16. **M14**:已完成(`sync.Once` + 五个用例)。剩余只需把 `logger` 未初始化就 nil 的问题在 `pkg/logger` 兜掉,免得每个包写一遍 `TestMain`。 -17. **M15**:补四类超时 + `MaxBytesReader`。 +17. **M15**:已完成(P1/ESPL-20,四类超时 + body 上限,超限 413)。 18. **m1/m2**:`Hub` 建好后加载已启用规则并在增删改时同步内存;`Priority`/`Enabled` 改指针类型。 ### P3 — 文档与卫生 diff --git a/internal/adaptergateway/gateway.go b/internal/adaptergateway/gateway.go index cc718f85..f88a3b38 100644 --- a/internal/adaptergateway/gateway.go +++ b/internal/adaptergateway/gateway.go @@ -36,6 +36,11 @@ type GatewayConfig struct { ReadTimeout int // MaxMessageSize is the maximum inbound frame size in bytes. MaxMessageSize int64 + // AllowedOrigin reports whether a browser Origin may connect. A nil value + // allows every Origin, which is what an embedder that has not configured an + // allowlist gets. The HTTP CORS middleware and the WebSocket upgrader share + // this one rule so the two can never disagree. + AllowedOrigin func(string) bool } // DefaultGatewayConfig returns the default gateway configuration. @@ -298,7 +303,13 @@ func NewGateway(cfg GatewayConfig, svc *service.AdapterGatewayService) *Gateway ReadBufferSize: 1024, WriteBufferSize: 1024, CheckOrigin: func(r *http.Request) bool { - return true // Allow all origins for development + origin := r.Header.Get("Origin") + // A missing Origin is a non-browser client; those are not + // subject to CORS at all. + if origin == "" || cfg.AllowedOrigin == nil { + return true + } + return cfg.AllowedOrigin(origin) }, }, } diff --git a/internal/config/config.go b/internal/config/config.go index 2e72af95..3b19d0d8 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -3,6 +3,7 @@ package config import ( "fmt" "os" + "strings" "github.com/spf13/viper" ) @@ -26,10 +27,80 @@ type Config struct { // JWT settings JWT JWTConfig `mapstructure:"jwt"` + // HTTP server settings + Server ServerConfig `mapstructure:"server"` + + // Security settings (shared by CORS and the WebSocket Origin check) + Security SecurityConfig `mapstructure:"security"` + // Adapter settings Adapter AdapterConfig `mapstructure:"adapter"` } +// PlaceholderJWTSecret is the value shipped in config/config.yaml and used as +// the viper default. A deployment that never changed it signs tokens with a +// public key, so startup warns when it is still in place. +const PlaceholderJWTSecret = "change-this-secret-in-production" + +// DefaultAllowedOrigins is the development Origin allowlist. It covers the Vite +// dev server and the backend's own default port; a deployment adds its real +// front-end Origin via config.yaml or the ESP_SECURITY_ALLOWED_ORIGINS env var. +func DefaultAllowedOrigins() []string { + return []string{ + "http://localhost:3000", + "http://127.0.0.1:3000", + "http://localhost:8080", + "http://127.0.0.1:8080", + } +} + +// ServerConfig holds HTTP server timeouts and request-size limits. +type ServerConfig struct { + // ReadHeaderTimeout bounds how long a client may take to send headers. + ReadHeaderTimeout int `mapstructure:"read_header_timeout"` // seconds + // ReadTimeout bounds the whole request read. + ReadTimeout int `mapstructure:"read_timeout"` // seconds + // WriteTimeout bounds the response write. + WriteTimeout int `mapstructure:"write_timeout"` // seconds + // IdleTimeout bounds an idle keep-alive connection. + IdleTimeout int `mapstructure:"idle_timeout"` // seconds + // MaxBodyBytes caps the request body size. + MaxBodyBytes int64 `mapstructure:"max_body_bytes"` // bytes +} + +// SecurityConfig holds cross-cutting browser-security settings. +type SecurityConfig struct { + // AllowedOrigins is the Origin allowlist used both by the CORS middleware + // and by the WebSocket CheckOrigin hook. One list, so the two can never + // drift apart. + AllowedOrigins []string `mapstructure:"allowed_origins"` +} + +// IsAllowedOrigin reports whether origin is in the CORS/WebSocket allowlist. +// The empty Origin (non-browser clients such as the Python bridges) is allowed, +// as is an empty allowlist meaning "development default", i.e. allow all. +func (c *Config) IsAllowedOrigin(origin string) bool { + if origin == "" { + return true + } + allowed := c.Security.AllowedOrigins + if len(allowed) == 0 { + return true + } + for _, a := range allowed { + if a == origin { + return true + } + } + return false +} + +// UsingPlaceholderJWTSecret reports whether the JWT secret is still the +// shipped placeholder. +func (c *Config) UsingPlaceholderJWTSecret() bool { + return c.JWT.Secret == "" || c.JWT.Secret == PlaceholderJWTSecret +} + // DatabaseConfig represents database configuration type DatabaseConfig struct { Driver string `mapstructure:"driver"` @@ -147,9 +218,28 @@ func setDefaults(v *viper.Viper) { v.SetDefault("log_level", "info") // JWT defaults - v.SetDefault("jwt.secret", "change-this-secret-in-production") + v.SetDefault("jwt.secret", PlaceholderJWTSecret) v.SetDefault("jwt.expiration", 24) + // HTTP server defaults. Without these a single slow client can hold a + // connection forever (Slowloris) and a large body is read entirely into + // memory. + v.SetDefault("server.read_header_timeout", 10) + v.SetDefault("server.read_timeout", 60) + v.SetDefault("server.write_timeout", 120) + v.SetDefault("server.idle_timeout", 120) + v.SetDefault("server.max_body_bytes", 4<<20) // 4 MiB + + // Origin allowlist shared by CORS and the WebSocket Origin check. + v.SetDefault("security.allowed_origins", DefaultAllowedOrigins()) + + // Env is read with ESP_* names; AutomaticEnv alone cannot see a slice, so + // the comma-separated override is bound explicitly. This lets a deployment + // set its real front-end Origin without editing config.yaml. + v.SetEnvPrefix("esp") + v.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + _ = v.BindEnv("security.allowed_origins") + // Adapter defaults v.SetDefault("adapter.heartbeat_interval", 30) v.SetDefault("adapter.reconnect_delay", 5) diff --git a/internal/config/config_test.go b/internal/config/config_test.go new file mode 100644 index 00000000..233035d5 --- /dev/null +++ b/internal/config/config_test.go @@ -0,0 +1,64 @@ +package config + +import "testing" + +func TestUsingPlaceholderJWTSecret(t *testing.T) { + tests := []struct { + name string + secret string + want bool + }{ + {"shipped placeholder", PlaceholderJWTSecret, true}, + {"empty secret", "", true}, + {"rotated secret", "s3cr3t-from-the-deployment", false}, + // A secret that merely contains the placeholder text is still a real + // rotation and must not warn. + {"placeholder with suffix", PlaceholderJWTSecret + "-prod", false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cfg := &Config{JWT: JWTConfig{Secret: tt.secret}} + if got := cfg.UsingPlaceholderJWTSecret(); got != tt.want { + t.Fatalf("UsingPlaceholderJWTSecret() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestIsAllowedOrigin(t *testing.T) { + tests := []struct { + name string + allowed []string + origin string + want bool + }{ + {"listed origin", []string{"https://app.example"}, "https://app.example", true}, + {"unlisted origin", []string{"https://app.example"}, "https://evil.example", false}, + {"empty origin is a non-browser client", []string{"https://app.example"}, "", true}, + {"empty allowlist falls back to allow-all", nil, "https://anything.example", true}, + {"prefix does not match", []string{"https://app.example"}, "https://app.example.evil", false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cfg := &Config{Security: SecurityConfig{AllowedOrigins: tt.allowed}} + if got := cfg.IsAllowedOrigin(tt.origin); got != tt.want { + t.Fatalf("IsAllowedOrigin(%q) = %v, want %v", tt.origin, got, tt.want) + } + }) + } +} + +// TestDefaultsAreSafe pins the shipped defaults: without an explicit config the +// server must still have every timeout set and a non-empty Origin allowlist. +func TestDefaultsAreSafe(t *testing.T) { + cfg := &Config{Security: SecurityConfig{AllowedOrigins: DefaultAllowedOrigins()}} + if len(cfg.Security.AllowedOrigins) == 0 { + t.Fatal("default Origin allowlist is empty") + } + if !cfg.IsAllowedOrigin("http://localhost:3000") { + t.Fatal("the Vite dev server origin is not allowed by default") + } + if cfg.IsAllowedOrigin("https://evil.example") { + t.Fatal("an arbitrary origin is allowed by default") + } +} diff --git a/internal/config/env_test.go b/internal/config/env_test.go new file mode 100644 index 00000000..38bf6032 --- /dev/null +++ b/internal/config/env_test.go @@ -0,0 +1,34 @@ +package config + +import "testing" + +// The env override exists so a deployment can set its Origin without editing +// config.yaml. It must reach the []string field, which viper only does with a +// comma-separated value and weak typing. +func TestAllowedOriginsEnvOverride(t *testing.T) { + t.Setenv("ESP_SECURITY_ALLOWED_ORIGINS", "https://a.example,https://b.example") + cfg, err := Load() + if err != nil { + t.Fatalf("Load: %v", err) + } + if len(cfg.Security.AllowedOrigins) != 2 { + t.Fatalf("allowed origins = %v, want the two env values", cfg.Security.AllowedOrigins) + } + if !cfg.IsAllowedOrigin("https://b.example") { + t.Fatalf("env origin not honoured: %v", cfg.Security.AllowedOrigins) + } +} + +func TestJWTSecretEnvOverride(t *testing.T) { + t.Setenv("ESP_JWT_SECRET", "from-the-environment") + cfg, err := Load() + if err != nil { + t.Fatalf("Load: %v", err) + } + if cfg.JWT.Secret != "from-the-environment" { + t.Fatalf("jwt.secret = %q, want the env value", cfg.JWT.Secret) + } + if cfg.UsingPlaceholderJWTSecret() { + t.Fatal("rotated env secret still reported as placeholder") + } +} diff --git a/internal/handler/websocket.go b/internal/handler/websocket.go index 0fba4e0f..2fbdeaa0 100644 --- a/internal/handler/websocket.go +++ b/internal/handler/websocket.go @@ -7,6 +7,7 @@ import ( "time" "github.com/e-spl/e-sp-line2/internal/adaptergateway" + "github.com/e-spl/e-sp-line2/internal/middleware" v3 "github.com/e-spl/e-sp-line2/internal/protocol/v3" "github.com/e-spl/e-sp-line2/internal/service" "github.com/e-spl/e-sp-line2/pkg/logger" @@ -14,14 +15,90 @@ import ( "github.com/gorilla/websocket" ) +// allowedOrigin decides whether a browser Origin may open a WebSocket. The +// server sets it from the same config list the CORS middleware uses, so the two +// surfaces share one allowlist and cannot drift apart. +var allowedOrigin = func(string) bool { return true } + +// SetAllowedOrigin installs the Origin allowlist used by the WebSocket +// upgrader. A nil func restores "allow everything". +func SetAllowedOrigin(fn func(string) bool) { + if fn == nil { + fn = func(string) bool { return true } + } + allowedOrigin = fn +} + var upgrader = websocket.Upgrader{ ReadBufferSize: 1024, WriteBufferSize: 1024, CheckOrigin: func(r *http.Request) bool { - return true // Allow all origins for development + // An empty Origin is a non-browser client (the Python bridges, + // websocket tooling); those are not subject to browser CORS. + origin := r.Header.Get("Origin") + if origin == "" { + return true + } + return allowedOrigin(origin) }, } +// wsAuthError marks a failed WebSocket handshake authentication. +type wsAuthError struct{ reason string } + +func (e *wsAuthError) Error() string { return e.reason } + +// wsToken extracts a handshake token. Three transports are accepted because the +// callers differ: +// +// - `?token=` — the Python bridges, which cannot set headers on connect; +// - `Authorization: Bearer` — server-to-server clients and tests; +// - `Sec-WebSocket-Protocol: espl, ` — the only header a browser +// WebSocket can set (the `espl` sentinel must come first, per the same +// convention the adapter gateway already uses). +func wsToken(r *http.Request) string { + if q := r.URL.Query().Get("token"); q != "" { + return q + } + if auth := r.Header.Get("Authorization"); auth != "" { + if parts := strings.SplitN(auth, " ", 2); len(parts) == 2 && parts[0] == "Bearer" { + return parts[1] + } + } + protocols := websocket.Subprotocols(r) + for _, p := range protocols { + if p != "espl" && p != "" { + return p + } + } + return "" +} + +// authenticateWS validates the handshake token. A missing token is refused as +// well as a bad one: before this, knowing an instance_id or app_id was enough to +// open a socket, because neither route sat behind any middleware. +func authenticateWS(r *http.Request) error { + token := wsToken(r) + if token == "" { + return &wsAuthError{"missing token (use ?token=, Authorization, or the espl subprotocol)"} + } + if _, err := middleware.ParseToken(token); err != nil { + return &wsAuthError{"invalid or expired token"} + } + return nil +} + +// rejectWS answers a WebSocket handshake that was refused before upgrading, so +// the client gets a plain HTTP 401 instead of a socket that immediately closes. +func rejectWS(c *gin.Context, err error) { + logger.Warn("WebSocket handshake rejected", + logger.String("path", c.Request.URL.Path), + logger.String("origin", c.Request.Header.Get("Origin")), + logger.String("error", err.Error()), + ) + c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": err.Error()}) +} + // AdapterWebSocket handles adapter (bridge) WebSocket connections. // The bridge connects here over /ws/adapter?instance_id=xxx and reports // inbound messages. Each reported message is persisted (with its full @@ -38,6 +115,13 @@ func AdapterWebSocket( onInbound ...func(payload map[string]interface{}), ) gin.HandlerFunc { return func(c *gin.Context) { + // Authenticate before upgrading: a refused handshake then arrives as a + // readable 401 instead of a socket that closes straight away. + if err := authenticateWS(c.Request); err != nil { + rejectWS(c, err) + return + } + conn, err := upgrader.Upgrade(c.Writer, c.Request, nil) if err != nil { logger.Error("WebSocket upgrade failed", logger.String("error", err.Error())) @@ -200,6 +284,13 @@ func AdapterWebSocket( // AppWebSocket handles app WebSocket connections func AppWebSocket(messageService *service.MessageService) gin.HandlerFunc { return func(c *gin.Context) { + // Same handshake rule as the adapter socket: a valid token is required + // before the upgrade, so an unknown client cannot subscribe silently. + if err := authenticateWS(c.Request); err != nil { + rejectWS(c, err) + return + } + conn, err := upgrader.Upgrade(c.Writer, c.Request, nil) if err != nil { logger.Error("WebSocket upgrade failed", logger.String("error", err.Error())) diff --git a/internal/handler/websocket_test.go b/internal/handler/websocket_test.go new file mode 100644 index 00000000..d3be6c48 --- /dev/null +++ b/internal/handler/websocket_test.go @@ -0,0 +1,178 @@ +package handler + +import ( + "net/http" + "net/http/httptest" + "net/url" + "os" + "strings" + "testing" + "time" + + "github.com/e-spl/e-sp-line2/internal/middleware" + "github.com/e-spl/e-sp-line2/pkg/logger" + "github.com/gin-gonic/gin" + "github.com/golang-jwt/jwt/v5" + "github.com/gorilla/websocket" +) + +// The handlers log through pkg/logger, which is a nil zap logger until Init. +func TestMain(m *testing.M) { + logger.Init("error") + code := m.Run() + logger.Sync() + os.Exit(code) +} + +const testSecret = "test-secret-for-websocket-handshakes" + +func init() { + gin.SetMode(gin.TestMode) + middleware.SetJWTSecret(testSecret) +} + +func signTestToken(t *testing.T, expires time.Duration) string { + t.Helper() + claims := &middleware.Claims{ + UserID: "user-1", + Username: "tester", + Role: "admin", + RegisteredClaims: jwt.RegisteredClaims{ + ExpiresAt: jwt.NewNumericDate(time.Now().Add(expires)), + }, + } + s, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString([]byte(testSecret)) + if err != nil { + t.Fatalf("signing token: %v", err) + } + return s +} + +// newWSServer starts a real HTTP server around the app socket so the handshake +// (including the upgrader's Origin check) is exercised end to end. +func newWSServer(t *testing.T) *httptest.Server { + t.Helper() + router := gin.New() + router.GET("/ws/app", AppWebSocket(nil)) + srv := httptest.NewServer(router) + t.Cleanup(srv.Close) + return srv +} + +// wsURL builds an app-socket URL. app_id is always present so the handler does +// not reject the connection after a successful upgrade for an unrelated reason. +func wsURL(srv *httptest.Server, query string) string { + u := strings.Replace(srv.URL, "http://", "ws://", 1) + "/ws/app?app_id=app-1" + if query != "" { + u += "&" + query + } + return u +} + +// dial opens a socket and returns the first frame, so a successful handshake is +// proven by the welcome message rather than merely by no error. +func dial(t *testing.T, rawURL string, header http.Header) (*websocket.Conn, map[string]interface{}) { + t.Helper() + conn, resp, err := websocket.DefaultDialer.Dial(rawURL, header) + if err != nil { + status := 0 + if resp != nil { + status = resp.StatusCode + } + t.Fatalf("dial %s failed (%d): %v", rawURL, status, err) + } + t.Cleanup(func() { conn.Close() }) + _ = conn.SetReadDeadline(time.Now().Add(3 * time.Second)) + var frame map[string]interface{} + if err := conn.ReadJSON(&frame); err != nil { + t.Fatalf("reading welcome frame: %v", err) + } + return conn, frame +} + +func expectDialFails(t *testing.T, rawURL string, header http.Header, wantStatus int) { + t.Helper() + conn, resp, err := websocket.DefaultDialer.Dial(rawURL, header) + if err == nil { + conn.Close() + t.Fatalf("dial %s succeeded, want failure with %d", rawURL, wantStatus) + } + if resp == nil || resp.StatusCode != wantStatus { + got := 0 + if resp != nil { + got = resp.StatusCode + } + t.Fatalf("handshake status = %d, want %d", got, wantStatus) + } +} + +func TestAppWebSocketRequiresValidToken(t *testing.T) { + srv := newWSServer(t) + token := signTestToken(t, time.Hour) + + t.Run("missing token is rejected before upgrade", func(t *testing.T) { + expectDialFails(t, wsURL(srv, ""), nil, http.StatusUnauthorized) + }) + + t.Run("garbage token is rejected", func(t *testing.T) { + expectDialFails(t, wsURL(srv, "token=total-garbage"), nil, http.StatusUnauthorized) + }) + + t.Run("expired token is rejected", func(t *testing.T) { + expired := signTestToken(t, -time.Hour) + expectDialFails(t, wsURL(srv, "token="+url.QueryEscape(expired)), nil, http.StatusUnauthorized) + }) + + t.Run("valid token via query param", func(t *testing.T) { + _, frame := dial(t, wsURL(srv, "token="+url.QueryEscape(token)), nil) + if frame["type"] != "connected" { + t.Fatalf("welcome frame = %v, want type=connected", frame) + } + }) + + t.Run("valid token via Authorization header", func(t *testing.T) { + h := http.Header{} + h.Set("Authorization", "Bearer "+token) + if _, frame := dial(t, wsURL(srv, ""), h); frame["type"] != "connected" { + t.Fatalf("welcome frame = %v", frame) + } + }) + + t.Run("valid token via the espl subprotocol", func(t *testing.T) { + h := http.Header{} + h.Set("Sec-WebSocket-Protocol", "espl, "+token) + if _, frame := dial(t, wsURL(srv, ""), h); frame["type"] != "connected" { + t.Fatalf("welcome frame = %v", frame) + } + }) +} + +// A fabricated token must never be treated the same as no token: that was the +// original hole (CheckOrigin always true, no middleware at all). +func TestAppWebSocketOriginAllowlist(t *testing.T) { + t.Cleanup(func() { SetAllowedOrigin(nil) }) + SetAllowedOrigin(func(o string) bool { return o == "https://app.example" }) + + srv := newWSServer(t) + token := signTestToken(t, time.Hour) + + t.Run("listed origin is accepted", func(t *testing.T) { + h := http.Header{} + h.Set("Origin", "https://app.example") + if _, frame := dial(t, wsURL(srv, "token="+url.QueryEscape(token)), h); frame["type"] != "connected" { + t.Fatalf("welcome frame = %v", frame) + } + }) + + t.Run("unlisted origin is refused", func(t *testing.T) { + h := http.Header{} + h.Set("Origin", "https://evil.example") + expectDialFails(t, wsURL(srv, "token="+url.QueryEscape(token)), h, http.StatusForbidden) + }) + + t.Run("no Origin header is a non-browser client", func(t *testing.T) { + if _, frame := dial(t, wsURL(srv, "token="+url.QueryEscape(token)), nil); frame["type"] != "connected" { + t.Fatalf("welcome frame = %v", frame) + } + }) +} diff --git a/internal/middleware/cors_test.go b/internal/middleware/cors_test.go new file mode 100644 index 00000000..4fb2eb46 --- /dev/null +++ b/internal/middleware/cors_test.go @@ -0,0 +1,151 @@ +package middleware + +import ( + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/gin-gonic/gin" +) + +func init() { gin.SetMode(gin.TestMode) } + +// do runs one request through a freshly built engine with the middleware under +// test and reports the response. The handler is only there to make "the request +// reached the application" observable. +func do(t *testing.T, mw gin.HandlerFunc, method, origin, body string) *httptest.ResponseRecorder { + t.Helper() + router := gin.New() + router.Use(mw) + router.Any("/x", func(c *gin.Context) { c.String(http.StatusOK, "reached") }) + + req := httptest.NewRequest(method, "/x", strings.NewReader(body)) + if origin != "" { + req.Header.Set("Origin", origin) + } + rec := httptest.NewRecorder() + router.ServeHTTP(rec, req) + return rec +} + +func allowOnly(allowed ...string) func(string) bool { + return func(o string) bool { + for _, a := range allowed { + if a == o { + return true + } + } + return false + } +} + +func TestCORSEchoesAllowedOrigin(t *testing.T) { + rec := do(t, CORS(allowOnly("https://app.example")), http.MethodGet, "https://app.example", "") + + if got := rec.Header().Get("Access-Control-Allow-Origin"); got != "https://app.example" { + t.Fatalf("Allow-Origin = %q, want the echoed origin (not a wildcard)", got) + } + // With the Origin echoed, credentials are legal and must stay allowed. + if got := rec.Header().Get("Access-Control-Allow-Credentials"); got != "true" { + t.Fatalf("Allow-Credentials = %q, want true", got) + } + // A response that varies by Origin must say so, or a shared cache serves + // one client's CORS headers to another. + if got := rec.Header().Get("Vary"); !strings.Contains(got, "Origin") { + t.Fatalf("Vary = %q, want it to contain Origin", got) + } + if rec.Code != http.StatusOK { + t.Fatalf("status = %d, want 200", rec.Code) + } +} + +func TestCORSRejectsUnlistedOrigin(t *testing.T) { + rec := do(t, CORS(allowOnly("https://app.example")), http.MethodGet, "https://evil.example", "") + + if got := rec.Header().Get("Access-Control-Allow-Origin"); got != "" { + t.Fatalf("Allow-Origin = %q, want empty for an unlisted origin", got) + } + // The request still reaches the app; the browser is what blocks it. + if rec.Code != http.StatusOK || rec.Body.String() != "reached" { + t.Fatalf("unlisted origin should reach the handler, got %d %q", rec.Code, rec.Body.String()) + } +} + +func TestCORSPreflight(t *testing.T) { + rec := do(t, CORS(allowOnly("https://app.example")), http.MethodOptions, "https://app.example", "") + + if rec.Code != http.StatusNoContent { + t.Fatalf("preflight status = %d, want 204", rec.Code) + } + if got := rec.Header().Get("Access-Control-Allow-Methods"); !strings.Contains(got, "POST") { + t.Fatalf("Allow-Methods = %q, want it to include POST", got) + } + if rec.Body.String() != "" { + t.Fatalf("preflight should not reach the handler, got body %q", rec.Body.String()) + } +} + +// A request with no Origin at all (curl, a Python bridge) is not a browser +// request and must be left completely alone. +func TestCORSWithoutOrigin(t *testing.T) { + rec := do(t, CORS(allowOnly("https://app.example")), http.MethodGet, "", "") + + if got := rec.Header().Get("Access-Control-Allow-Origin"); got != "" { + t.Fatalf("Allow-Origin = %q, want empty when no Origin was sent", got) + } + if rec.Code != http.StatusOK { + t.Fatalf("status = %d, want 200", rec.Code) + } +} + +func TestMaxBodyBytes(t *testing.T) { + const limit = 8 + + t.Run("under the limit passes", func(t *testing.T) { + rec := do(t, MaxBodyBytes(limit), http.MethodPost, "", "12345678") + if rec.Code != http.StatusOK { + t.Fatalf("status = %d, want 200", rec.Code) + } + }) + + t.Run("declared oversize is rejected before the handler runs", func(t *testing.T) { + rec := do(t, MaxBodyBytes(limit), http.MethodPost, "", "123456789") + if rec.Code != http.StatusRequestEntityTooLarge { + t.Fatalf("status = %d, want 413 for a declared oversized body", rec.Code) + } + if rec.Body.String() == "reached" { + t.Fatal("an oversized body should not reach the handler") + } + }) + + t.Run("undeclared oversize is caught while reading", func(t *testing.T) { + // A handler must read the body for the cap to trip; the engine here is + // built by do(), so use a bespoke one that reads it fully. + router := gin.New() + router.Use(MaxBodyBytes(limit)) + router.POST("/x", func(c *gin.Context) { + if _, err := io.ReadAll(c.Request.Body); err != nil { + c.String(http.StatusRequestEntityTooLarge, "too large") + return + } + c.String(http.StatusOK, "ok") + }) + + req := httptest.NewRequest(http.MethodPost, "/x", strings.NewReader("123456789")) + rec := httptest.NewRecorder() + router.ServeHTTP(rec, req) + + if rec.Code != http.StatusRequestEntityTooLarge { + t.Fatalf("status = %d, want 413 for an oversized body", rec.Code) + } + }) + + t.Run("zero limit disables the cap", func(t *testing.T) { + rec := do(t, MaxBodyBytes(0), http.MethodPost, "", "123456789") + if rec.Code != http.StatusOK { + t.Fatalf("status = %d, want 200", rec.Code) + } + }) +} diff --git a/internal/middleware/middleware.go b/internal/middleware/middleware.go index 053cb469..31eb2906 100644 --- a/internal/middleware/middleware.go +++ b/internal/middleware/middleware.go @@ -1,6 +1,7 @@ package middleware import ( + "fmt" "net/http" "time" @@ -64,14 +65,33 @@ func Logger() gin.HandlerFunc { } } -// CORS returns a gin middleware for CORS handling -func CORS() gin.HandlerFunc { +// CORS returns a gin middleware for CORS handling. +// +// The allowed Origin is echoed back instead of a fixed "*". That is what makes +// the `Access-Control-Allow-Credentials: true` header below valid at all: a +// wildcard Origin and credentials are mutually exclusive per the fetch spec, so +// a future switch to Cookie sessions would otherwise silently drop them. The +// allowlist is the same one the WebSocket upgrader checks (see OriginAllowed), +// so CORS and WS can never drift apart. +// +// Requests without an Origin header (curl, the Python bridges, health checks) +// are unaffected: they get no CORS headers and are not gated by this +// middleware. A non-allowlisted Origin still reaches the handler, but without +// an Access-Control-Allow-Origin header the browser blocks the response. +func CORS(allowedOrigin func(string) bool) gin.HandlerFunc { return func(c *gin.Context) { - c.Header("Access-Control-Allow-Origin", "*") - c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") - c.Header("Access-Control-Allow-Headers", "Origin, Content-Type, Accept, Authorization") - c.Header("Access-Control-Expose-Headers", "Content-Length") - c.Header("Access-Control-Allow-Credentials", "true") + origin := c.GetHeader("Origin") + if origin != "" { + // The response varies by Origin, so caches must key on it. + c.Writer.Header().Add("Vary", "Origin") + if allowedOrigin == nil || allowedOrigin(origin) { + c.Header("Access-Control-Allow-Origin", origin) + c.Header("Access-Control-Allow-Credentials", "true") + c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") + c.Header("Access-Control-Allow-Headers", "Origin, Content-Type, Accept, Authorization") + c.Header("Access-Control-Expose-Headers", "Content-Length") + } + } if c.Request.Method == "OPTIONS" { c.AbortWithStatus(http.StatusNoContent) @@ -81,3 +101,31 @@ func CORS() gin.HandlerFunc { c.Next() } } + +// MaxBodyBytes limits how many bytes a request body may carry. It is installed +// globally so no individual handler can forget it; handlers that need a +// different cap can shrink it further via http.MaxBytesReader themselves. +// +// A declared Content-Length over the limit is refused up front with 413 instead +// of letting the handler read into the error: without that pre-check the bind +// failure surfaces as the handler's own 400 ("unexpected EOF" in the JSON +// decoder), which reads like a malformed body rather than an oversized one. +// A lying client is still capped by the MaxBytesReader. +func MaxBodyBytes(limit int64) gin.HandlerFunc { + return func(c *gin.Context) { + if limit <= 0 { + c.Next() + return + } + if c.Request.ContentLength > limit { + c.AbortWithStatusJSON(http.StatusRequestEntityTooLarge, gin.H{ + "error": fmt.Sprintf("request body too large: limit is %d bytes", limit), + }) + return + } + if c.Request.Body != nil { + c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, limit) + } + c.Next() + } +} diff --git a/internal/server/server.go b/internal/server/server.go index fb4781b4..f5c7bf1c 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -35,6 +35,19 @@ func New(cfg *config.Config) (*Server, error) { // Inject JWT secret into middleware for token validation middleware.SetJWTSecret(cfg.JWT.Secret) + // The WebSocket upgrader shares the HTTP CORS allowlist, so one configured + // list governs both. Must be set before any route is built. + handler.SetAllowedOrigin(cfg.IsAllowedOrigin) + + // A deployment that never rotated the shipped secret is signing tokens + // with a public key; say so loudly once, at startup. + if cfg.UsingPlaceholderJWTSecret() { + logger.Warn("JWT secret is still the shipped placeholder, tokens are forgeable until it is rotated", + logger.String("placeholder", config.PlaceholderJWTSecret), + logger.String("hint", "set jwt.secret in config.yaml or ESP_JWT_SECRET"), + ) + } + // Initialize services services, err := service.NewServices(cfg) if err != nil { @@ -49,7 +62,8 @@ func New(cfg *config.Config) (*Server, error) { router := gin.New() router.Use(gin.Recovery()) router.Use(middleware.Logger()) - router.Use(middleware.CORS()) + router.Use(middleware.CORS(cfg.IsAllowedOrigin)) + router.Use(middleware.MaxBodyBytes(cfg.Server.MaxBodyBytes)) s := &Server{ cfg: cfg, @@ -58,8 +72,10 @@ func New(cfg *config.Config) (*Server, error) { } // Initialize the adapter gateway (接入器) WebSocket server. + gwCfg := adaptergateway.DefaultGatewayConfig() + gwCfg.AllowedOrigin = cfg.IsAllowedOrigin s.gateway = adaptergateway.NewGateway( - adaptergateway.DefaultGatewayConfig(), + gwCfg, services.AdapterGateway, ) @@ -96,14 +112,29 @@ func New(cfg *config.Config) (*Server, error) { logger.Info("Embedded WebUI enabled (desktop build)") } - s.httpServer = &http.Server{ - Addr: fmt.Sprintf(":%d", cfg.Port), - Handler: router, - } + // Timeouts matter twice over here: without ReadHeaderTimeout one slow + // client can hold a connection open indefinitely (Slowloris), and without + // WriteTimeout every slow reader pins a goroutine. WebSocket connections + // are unaffected after the upgrade — gorilla clears the deadlines and the + // handlers manage their own. + s.httpServer = newHTTPServer(cfg, router) return s, nil } +// newHTTPServer builds the *http.Server from configuration. Split out of New so +// the timeout wiring is testable without a database. +func newHTTPServer(cfg *config.Config, handler http.Handler) *http.Server { + return &http.Server{ + Addr: fmt.Sprintf(":%d", cfg.Port), + Handler: handler, + ReadHeaderTimeout: seconds(cfg.Server.ReadHeaderTimeout), + ReadTimeout: seconds(cfg.Server.ReadTimeout), + WriteTimeout: seconds(cfg.Server.WriteTimeout), + IdleTimeout: seconds(cfg.Server.IdleTimeout), + } +} + // setupRoutes configures all API routes func (s *Server) setupRoutes() { // Health check @@ -428,6 +459,15 @@ func (s *Server) Start() error { return s.httpServer.ListenAndServe() } +// seconds converts a configured timeout in seconds into a time.Duration. A +// non-positive value means "no timeout", which is what net/http expects. +func seconds(n int) time.Duration { + if n <= 0 { + return 0 + } + return time.Duration(n) * time.Second +} + // Stop gracefully stops the HTTP server func (s *Server) Stop(ctx context.Context) error { logger.Info("Stopping HTTP server") diff --git a/internal/server/server_test.go b/internal/server/server_test.go new file mode 100644 index 00000000..1b6dbf50 --- /dev/null +++ b/internal/server/server_test.go @@ -0,0 +1,47 @@ +package server + +import ( + "net/http" + "testing" + "time" + + "github.com/e-spl/e-sp-line2/internal/config" +) + +// TestNewHTTPServerWiresTimeouts is M15's failing case: the struct used to be +// `{Addr, Handler}` only, so every deadline stayed at net/http's "none" default +// and one slow client could hold a connection open forever. +func TestNewHTTPServerWiresTimeouts(t *testing.T) { + cfg := &config.Config{Port: 8080} + cfg.Server.ReadHeaderTimeout = 10 + cfg.Server.ReadTimeout = 60 + cfg.Server.WriteTimeout = 120 + cfg.Server.IdleTimeout = 120 + + srv := newHTTPServer(cfg, http.NewServeMux()) + + if got, want := srv.ReadHeaderTimeout, 10*time.Second; got != want { + t.Fatalf("ReadHeaderTimeout = %v, want %v (Slowloris guard)", got, want) + } + if got, want := srv.ReadTimeout, 60*time.Second; got != want { + t.Fatalf("ReadTimeout = %v, want %v", got, want) + } + if got, want := srv.WriteTimeout, 120*time.Second; got != want { + t.Fatalf("WriteTimeout = %v, want %v", got, want) + } + if got, want := srv.IdleTimeout, 120*time.Second; got != want { + t.Fatalf("IdleTimeout = %v, want %v", got, want) + } + if srv.Addr != ":8080" { + t.Fatalf("Addr = %q, want :8080", srv.Addr) + } +} + +// A configured 0 means "no timeout", which net/http expresses as a zero +// duration — the value must not be turned into something else. +func TestNewHTTPServerZeroMeansNoTimeout(t *testing.T) { + srv := newHTTPServer(&config.Config{}, http.NewServeMux()) + if srv.ReadHeaderTimeout != 0 || srv.ReadTimeout != 0 || srv.WriteTimeout != 0 || srv.IdleTimeout != 0 { + t.Fatalf("zero config produced timeouts: %+v", srv) + } +}