From db6f53d4cd1e8ae168873404f550be578e1824fe Mon Sep 17 00:00:00 2001 From: TyperBody Date: Sat, 7 Nov 2026 20:09:38 +0800 Subject: [PATCH 1/3] config: add the origin allow-list and HTTP hardening knobs `server.allowed_origins` is the single list CORS and the WebSocket handshake will both read, so the two cannot drift apart. Matching is exact (scheme + host + port): a prefix match would accept `http://localhost:3000.evil.example` for `http://localhost:3000`. `server.read_header_timeout` / `read_timeout` / `write_timeout` / `idle_timeout` / `max_body_bytes` carry defaults, so a deployment that never edits config.yaml is also hardened rather than only the one that does. `PlaceholderJWTSecret` names the shipped secret in one place, and `JWTSecretWarning` returns the startup warning for it. It is a pure function rather than a log call inside Load because Load runs before logger.Init; a nil zap logger there would drop the warning silently. The empty-string case warns too: exporting an empty JWT.SECRET signs with an empty key, which is not the placeholder but is just as forgeable. Co-authored-by: multica-agent --- internal/config/config.go | 84 ++++++++++++++- internal/config/config_test.go | 182 +++++++++++++++++++++++++++++++++ 2 files changed, 265 insertions(+), 1 deletion(-) create mode 100644 internal/config/config_test.go diff --git a/internal/config/config.go b/internal/config/config.go index 2e72af95..c964d984 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -7,6 +7,11 @@ import ( "github.com/spf13/viper" ) +// PlaceholderJWTSecret is the value shipped in config/config.yaml and used as +// the viper default. A deployment that still runs with it signs tokens with a +// secret anybody can read in the repository, so startup warns about it. +const PlaceholderJWTSecret = "change-this-secret-in-production" + // Config represents the application configuration type Config struct { // Server settings @@ -14,6 +19,10 @@ type Config struct { Port int `mapstructure:"port"` Version string `mapstructure:"version"` + // HTTP hardening, plus the browser origins allowed to call the API and the + // WebSocket handshake. See ServerConfig and CORSConfig. + Server ServerConfig `mapstructure:"server"` + // Database settings Database DatabaseConfig `mapstructure:"database"` @@ -30,6 +39,40 @@ type Config struct { Adapter AdapterConfig `mapstructure:"adapter"` } +// ServerConfig holds the HTTP server hardening knobs and the browser origin +// allow-list. +// +// The timeouts are seconds, matching the rest of the config file, and are +// converted to time.Duration in server.New. A zero value disables the +// corresponding net/http timeout, which is what the standard library does too. +type ServerConfig struct { + // AllowedOrigins is the allow-list shared by CORS and the WebSocket + // handshake, so the two never drift apart. An entry of "*" allows any + // origin; otherwise the Origin header is echoed only when it matches an + // entry exactly (scheme + host + port, no trailing slash). + AllowedOrigins []string `mapstructure:"allowed_origins"` + + ReadHeaderTimeout int `mapstructure:"read_header_timeout"` // seconds + ReadTimeout int `mapstructure:"read_timeout"` // seconds + WriteTimeout int `mapstructure:"write_timeout"` // seconds + IdleTimeout int `mapstructure:"idle_timeout"` // seconds + + // MaxBodyBytes caps an HTTP request body. Uploads are JSON blobs (adapter + // configs, cookies), so this stays far below a file-transfer size. + MaxBodyBytes int64 `mapstructure:"max_body_bytes"` +} + +// OriginAllowed reports whether origin is in the allow-list. A "*" entry +// allows everything. +func (c *ServerConfig) OriginAllowed(origin string) bool { + for _, allowed := range c.AllowedOrigins { + if allowed == "*" || allowed == origin { + return true + } + } + return false +} + // DatabaseConfig represents database configuration type DatabaseConfig struct { Driver string `mapstructure:"driver"` @@ -120,6 +163,27 @@ func Load() (*Config, error) { return &cfg, nil } +// JWTSecretWarning returns the warning a caller should log when secret is +// still the shipped placeholder (or empty), and "" when it was changed. +// +// The check is a pure function, not a log call inside Load, because Load runs +// before logger.Init and a nil zap logger would silently drop it. +func JWTSecretWarning(secret string) string { + switch secret { + case PlaceholderJWTSecret: + return "jwt.secret is still the shipped placeholder " + PlaceholderJWTSecret + + "; anyone with the repository can forge a token for this deployment. " + + "Set jwt.secret in config/config.yaml, or export JWT.SECRET to override it." + case "": + // An empty key still signs, with nothing. Reached by exporting + // JWT.SECRET= or blanking the key, not by the shipped config. + return "jwt.secret is empty; tokens are signed with an empty HS256 key. " + + "Set jwt.secret in config/config.yaml." + default: + return "" + } +} + // setDefaults sets default configuration values func setDefaults(v *viper.Viper) { // Server defaults @@ -127,6 +191,24 @@ func setDefaults(v *viper.Viper) { v.SetDefault("port", 8080) v.SetDefault("version", "1.0.0") + // HTTP hardening. 5s is enough for every header block a browser sends and + // still ends a Slowloris connection quickly; the read/write budgets cover a + // large JSON body; idle covers keep-alive reuse. + v.SetDefault("server.read_header_timeout", 5) + v.SetDefault("server.read_timeout", 60) + v.SetDefault("server.write_timeout", 60) + v.SetDefault("server.idle_timeout", 120) + v.SetDefault("server.max_body_bytes", 8<<20) // 8 MiB + + // Origins allowed to call the API and open a WebSocket. The Vite dev server + // is the only cross-origin caller out of the box; a deployment behind a + // reverse proxy is same-origin and needs no entry. Replace/add entries + // rather than widening to "*", see docs/developer-guide/api-reference.md. + v.SetDefault("server.allowed_origins", []string{ + "http://localhost:3000", + "http://127.0.0.1:3000", + }) + // Database defaults v.SetDefault("database.driver", "sqlite") v.SetDefault("database.dsn", "data/e-sp-line2.db") @@ -147,7 +229,7 @@ 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) // Adapter defaults diff --git a/internal/config/config_test.go b/internal/config/config_test.go new file mode 100644 index 00000000..31371eed --- /dev/null +++ b/internal/config/config_test.go @@ -0,0 +1,182 @@ +package config + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +// writeConfig drops a config.yaml into a temp dir laid out the way the loader +// searches for it, and points the working directory at it. +func writeConfig(t *testing.T, body string) { + t.Helper() + dir := t.TempDir() + if err := os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(body), 0o600); err != nil { + t.Fatalf("write config: %v", err) + } + chdir(t, dir) +} + +// chdir moves into dir for the duration of the test. testing.Chdir needs +// go1.24 while go.mod still declares 1.22, so this does it by hand. +func chdir(t *testing.T, dir string) { + t.Helper() + old, err := os.Getwd() + if err != nil { + t.Fatalf("getwd: %v", err) + } + if err := os.Chdir(dir); err != nil { + t.Fatalf("chdir %s: %v", dir, err) + } + t.Cleanup(func() { + if err := os.Chdir(old); err != nil { + t.Fatalf("restore wd: %v", err) + } + }) +} + +// TestJWTSecretWarning is the acceptance test for M3: the shipped placeholder +// secret must be reported at startup, and a real secret must not be. +func TestJWTSecretWarning(t *testing.T) { + tests := []struct { + name string + secret string + wantWarn bool + }{ + {"shipped placeholder warns", PlaceholderJWTSecret, true}, + {"empty secret warns", "", true}, + {"a changed secret is silent", "s3cr3t-from-the-operator", false}, + {"a secret that only looks similar is silent", "change-this-secret-in-production-2", false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := JWTSecretWarning(tt.secret) + if tt.wantWarn && got == "" { + t.Fatalf("JWTSecretWarning(%q) = \"\", want a warning", tt.secret) + } + if !tt.wantWarn && got != "" { + t.Fatalf("JWTSecretWarning(%q) = %q, want no warning", tt.secret, got) + } + }) + } +} + +// TestLoadKeepsPlaceholderSecret proves the loader really returns the shipped +// value when the config file leaves it alone — otherwise the warning above +// would never fire in a real deployment. +func TestLoadKeepsPlaceholderSecret(t *testing.T) { + writeConfig(t, "port: 8080\n") + + cfg, err := Load() + if err != nil { + t.Fatalf("Load: %v", err) + } + if cfg.JWT.Secret != PlaceholderJWTSecret { + t.Fatalf("JWT.Secret = %q, want the placeholder %q", cfg.JWT.Secret, PlaceholderJWTSecret) + } + if warning := JWTSecretWarning(cfg.JWT.Secret); warning == "" { + t.Fatal("default config produced no placeholder warning") + } +} + +// TestLoadChangedSecretIsSilent is the other half: setting a secret in the file +// must suppress the warning. +func TestLoadChangedSecretIsSilent(t *testing.T) { + writeConfig(t, "jwt:\n secret: \"a-real-deployment-secret\"\n") + + cfg, err := Load() + if err != nil { + t.Fatalf("Load: %v", err) + } + if cfg.JWT.Secret != "a-real-deployment-secret" { + t.Fatalf("JWT.Secret = %q, want the configured value", cfg.JWT.Secret) + } + if warning := JWTSecretWarning(cfg.JWT.Secret); warning != "" { + t.Fatalf("configured secret still warned: %s", warning) + } +} + +// TestLoadServerDefaults pins the hardening defaults: an operator who never +// touches config/config.yaml must still get timeouts and a body cap rather than +// the pre-M15 zero server. +func TestLoadServerDefaults(t *testing.T) { + writeConfig(t, "port: 8080\n") + + cfg, err := Load() + if err != nil { + t.Fatalf("Load: %v", err) + } + if cfg.Server.ReadHeaderTimeout <= 0 { + t.Error("ReadHeaderTimeout default is not positive") + } + if cfg.Server.ReadTimeout <= 0 { + t.Error("ReadTimeout default is not positive") + } + if cfg.Server.WriteTimeout <= 0 { + t.Error("WriteTimeout default is not positive") + } + if cfg.Server.IdleTimeout <= 0 { + t.Error("IdleTimeout default is not positive") + } + if cfg.Server.MaxBodyBytes <= 0 { + t.Error("MaxBodyBytes default is not positive") + } + if len(cfg.Server.AllowedOrigins) == 0 { + t.Error("AllowedOrigins default is empty; the Vite dev server would be rejected") + } +} + +// TestOriginAllowed checks the allow-list predicate the CORS middleware and the +// WebSocket upgrader share. +func TestOriginAllowed(t *testing.T) { + cfg := &ServerConfig{AllowedOrigins: []string{"http://localhost:3000"}} + + if !cfg.OriginAllowed("http://localhost:3000") { + t.Error("listed origin rejected") + } + // A prefix match would wrongly accept this one. + if cfg.OriginAllowed("http://localhost:3000.evil.example") { + t.Error("suffix-extended origin accepted") + } + if cfg.OriginAllowed("https://localhost:3000") { + t.Error("different scheme accepted") + } + if cfg.OriginAllowed("http://localhost:3001") { + t.Error("different port accepted") + } + + wildcard := &ServerConfig{AllowedOrigins: []string{"*"}} + if !wildcard.OriginAllowed("https://evil.example") { + t.Error("wildcard did not allow an unlisted origin") + } +} + +// TestAllowedOriginsStringForm covers the `server.allowed_origins: "..."` form +// somebody will inevitably write (a single string rather than a list). +func TestAllowedOriginsStringForm(t *testing.T) { + writeConfig(t, "server:\n allowed_origins: \"https://ui.example\"\n") + + cfg, err := Load() + if err != nil { + t.Fatalf("Load: %v", err) + } + if !cfg.Server.OriginAllowed("https://ui.example") { + t.Fatalf("single-string allowed_origins not loaded: %v", cfg.Server.AllowedOrigins) + } + if cfg.Server.OriginAllowed("http://localhost:3000") { + t.Fatalf("file value did not replace the default list: %v", cfg.Server.AllowedOrigins) + } +} + +// TestWarningNamesTheFix keeps the message actionable; a bare "change me" sends +// the operator looking for the key. +func TestWarningNamesTheFix(t *testing.T) { + warning := JWTSecretWarning(PlaceholderJWTSecret) + for _, want := range []string{"jwt.secret", "config/config.yaml", "JWT.SECRET"} { + if !strings.Contains(warning, want) { + t.Errorf("warning does not mention %q: %s", want, warning) + } + } +} From 99449729c52fabeb35c1c172536d069e7d2634f9 Mon Sep 17 00:00:00 2001 From: TyperBody Date: Sat, 7 Nov 2026 20:09:54 +0800 Subject: [PATCH 2/3] middleware, server: enforce the origin allow-list, WS auth and timeouts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit M2 — CORS echoed a literal `*` together with `Access-Control-Allow-Credentials: true`. A browser ignores the credentials flag when the origin is a star, so the pair was incoherent even though the current Bearer-header session does not depend on it. The allowed origin is now echoed, which makes the pair true — and keeps the front door open for a later Cookie-carried session instead of quietly breaking it. M4 — `/ws/adapter` and `/ws/app` hang no middleware at all and their upgrader returned `true` from CheckOrigin. Either route therefore accepted any site for whatever `instance_id` / `app_id` the caller typed. Both now sit behind WSTokenRequired (JWT from `?token=`, or an Authorization header for non-browser clients) and share the CORS allow-list through one OriginPolicy; a bad token answers 401 and a bad origin answers 403 before the upgrade, so a client can tell those apart from a network failure. Browsers send Origin on *every* handshake, same-origin included, so CheckOrigin also accepts a request whose Origin host equals its Host. Without that the embedded desktop build, which serves UI and API from one port, would have to list its own URL. CORS reads the same predicate. M15 — the http.Server had a bare Addr and Handler: no header deadline, no read/write/idle budget, and no cap on the body `ShouldBindJSON` slurps into memory. All four timeouts are set, and MaxBodyBytes wraps the body in http.MaxBytesReader; handlers answer 413 instead of the misleading 400 for "unexpected end of JSON input". WebSockets are unaffected because the upgrade hijacks the conn and clears the deadlines. M3 — main logs the placeholder-secret warning after logger.Init. Co-authored-by: multica-agent --- internal/adaptergateway/gateway.go | 8 +- internal/handler/websocket.go | 25 ++- internal/middleware/auth.go | 44 ++++ internal/middleware/body.go | 54 +++++ internal/middleware/helpers_test.go | 23 ++ internal/middleware/middleware.go | 36 ++- internal/middleware/middleware_test.go | 294 +++++++++++++++++++++++++ internal/middleware/origin.go | 94 ++++++++ internal/server/main_test.go | 17 ++ internal/server/security_test.go | 191 ++++++++++++++++ internal/server/server.go | 68 ++++-- internal/server/server_test.go | 145 ++++++++++++ 12 files changed, 967 insertions(+), 32 deletions(-) create mode 100644 internal/middleware/body.go create mode 100644 internal/middleware/helpers_test.go create mode 100644 internal/middleware/middleware_test.go create mode 100644 internal/middleware/origin.go create mode 100644 internal/server/main_test.go create mode 100644 internal/server/security_test.go create mode 100644 internal/server/server_test.go diff --git a/internal/adaptergateway/gateway.go b/internal/adaptergateway/gateway.go index cc718f85..4c0f10d7 100644 --- a/internal/adaptergateway/gateway.go +++ b/internal/adaptergateway/gateway.go @@ -36,6 +36,10 @@ type GatewayConfig struct { ReadTimeout int // MaxMessageSize is the maximum inbound frame size in bytes. MaxMessageSize int64 + // CheckOrigin decides which browser origins may open a gateway socket, + // including the custom per-adapter listen paths. A nil value allows every + // origin, which is what the gateway did before the allow-list existed. + CheckOrigin func(*http.Request) bool } // DefaultGatewayConfig returns the default gateway configuration. @@ -297,9 +301,7 @@ func NewGateway(cfg GatewayConfig, svc *service.AdapterGatewayService) *Gateway upgrader: websocket.Upgrader{ ReadBufferSize: 1024, WriteBufferSize: 1024, - CheckOrigin: func(r *http.Request) bool { - return true // Allow all origins for development - }, + CheckOrigin: cfg.CheckOrigin, }, } diff --git a/internal/handler/websocket.go b/internal/handler/websocket.go index 0fba4e0f..4ac4e83f 100644 --- a/internal/handler/websocket.go +++ b/internal/handler/websocket.go @@ -2,11 +2,11 @@ package handler import ( "encoding/json" - "net/http" "strings" "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,12 +14,18 @@ import ( "github.com/gorilla/websocket" ) -var upgrader = websocket.Upgrader{ - ReadBufferSize: 1024, - WriteBufferSize: 1024, - CheckOrigin: func(r *http.Request) bool { - return true // Allow all origins for development - }, +// wsUpgrader builds the upgrader for this server's origin policy. It is a +// constructor, not a package-level var, because the allow-list comes from +// config; both WS handlers take the result. +// +// CheckOrigin runs before the handshake completes, so a cross-site socket is +// closed with 403 and never reaches the read loop. +func wsUpgrader(policy *middleware.OriginPolicy) websocket.Upgrader { + return websocket.Upgrader{ + ReadBufferSize: 1024, + WriteBufferSize: 1024, + CheckOrigin: policy.CheckOrigin, + } } // AdapterWebSocket handles adapter (bridge) WebSocket connections. @@ -35,8 +41,10 @@ func AdapterWebSocket( adapterService *service.AdapterService, messageService *service.MessageService, gateway *adaptergateway.Gateway, + policy *middleware.OriginPolicy, onInbound ...func(payload map[string]interface{}), ) gin.HandlerFunc { + upgrader := wsUpgrader(policy) return func(c *gin.Context) { conn, err := upgrader.Upgrade(c.Writer, c.Request, nil) if err != nil { @@ -198,7 +206,8 @@ func AdapterWebSocket( } // AppWebSocket handles app WebSocket connections -func AppWebSocket(messageService *service.MessageService) gin.HandlerFunc { +func AppWebSocket(messageService *service.MessageService, policy *middleware.OriginPolicy) gin.HandlerFunc { + upgrader := wsUpgrader(policy) return func(c *gin.Context) { conn, err := upgrader.Upgrade(c.Writer, c.Request, nil) if err != nil { diff --git a/internal/middleware/auth.go b/internal/middleware/auth.go index 7a0f46d2..1e549cc0 100644 --- a/internal/middleware/auth.go +++ b/internal/middleware/auth.go @@ -94,3 +94,47 @@ func AdminRequired() gin.HandlerFunc { c.Next() } } + +// WSTokenRequired authenticates a WebSocket handshake before the upgrade. +// +// The token is read from `?token=`, because the browser WebSocket API cannot +// set request headers, falling back to a normal `Authorization: Bearer` header +// for non-browser clients. Rejecting here — rather than inside the read loop — +// gives the client a plain HTTP 401 it can distinguish from a network failure, +// and keeps unauthenticated sockets from reaching the hub at all. +// +// This is the handshake-level check only: it proves the caller holds a valid +// token, not that it owns the instance_id in the query. Ownership is the +// caller's business (see the instance routes). +func WSTokenRequired() gin.HandlerFunc { + return func(c *gin.Context) { + token := c.Query("token") + if token == "" { + header := c.GetHeader("Authorization") + if parts := strings.SplitN(header, " ", 2); len(parts) == 2 && parts[0] == "Bearer" { + token = parts[1] + } + } + if token == "" { + c.JSON(http.StatusUnauthorized, gin.H{ + "error": "websocket token is required: connect with ?token=", + }) + c.Abort() + return + } + + claims, err := ParseToken(token) + if err != nil { + c.JSON(http.StatusUnauthorized, gin.H{ + "error": "invalid or expired token", + }) + c.Abort() + return + } + + c.Set("userID", claims.UserID) + c.Set("username", claims.Username) + c.Set("role", claims.Role) + c.Next() + } +} diff --git a/internal/middleware/body.go b/internal/middleware/body.go new file mode 100644 index 00000000..fb39bce1 --- /dev/null +++ b/internal/middleware/body.go @@ -0,0 +1,54 @@ +package middleware + +import ( + "errors" + "net/http" + "strings" + + "github.com/gin-gonic/gin" +) + +// MaxBodyBytes caps the request body before it is read. +// +// Without it, `ShouldBindJSON` slurps whatever it is sent into memory, so a +// single client can drive the process out of memory. http.MaxBytesReader makes +// the underlying reader return *http.MaxBytesError once the limit is crossed, +// which the encoder renders as a 413. +// +// Only requests that can carry a body are wrapped: a GET with a bounded body is +// harmless, but wrapping also changes nothing for it, so the method check keeps +// the hot path untouched. +func MaxBodyBytes(limit int64) gin.HandlerFunc { + return func(c *gin.Context) { + if limit > 0 && methodHasBody(c.Request.Method) && c.Request.Body != nil { + c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, limit) + } + c.Next() + } +} + +// methodHasBody reports whether an HTTP method normally carries a payload. +func methodHasBody(method string) bool { + switch strings.ToUpper(method) { + case http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete: + return true + } + return false +} + +// BodyTooLarge inspects a bind error and answers 413 itself when the cause was +// the size cap, reporting whether it handled the response. +// +// A handler that just returns 400 for every bind error reports "unexpected end +// of JSON input" for an over-size body, which sends the client hunting for a +// syntax problem instead of shrinking the payload. +func BodyTooLarge(c *gin.Context, err error) bool { + var maxErr *http.MaxBytesError + if !errors.As(err, &maxErr) { + return false + } + c.AbortWithStatusJSON(http.StatusRequestEntityTooLarge, gin.H{ + "error": "request body too large", + }) + return true +} diff --git a/internal/middleware/helpers_test.go b/internal/middleware/helpers_test.go new file mode 100644 index 00000000..96fe1a80 --- /dev/null +++ b/internal/middleware/helpers_test.go @@ -0,0 +1,23 @@ +package middleware + +import ( + "time" + + "github.com/golang-jwt/jwt/v5" +) + +// signForTest mints a token with the package's current secret. It goes through +// jwt directly rather than the auth service so the test stays in this package; +// the claims mirror service.Claims, which is what ParseToken decodes. +func signForTest(userID string) (string, error) { + claims := Claims{ + UserID: userID, + Username: "tester", + Role: "user", + RegisteredClaims: jwt.RegisteredClaims{ + ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour)), + IssuedAt: jwt.NewNumericDate(time.Now()), + }, + } + return jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString(jwtSecret) +} diff --git a/internal/middleware/middleware.go b/internal/middleware/middleware.go index 053cb469..da7ff2ca 100644 --- a/internal/middleware/middleware.go +++ b/internal/middleware/middleware.go @@ -64,14 +64,42 @@ 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 blanket "*". That is what +// gives `Access-Control-Allow-Credentials: true` any effect — paired with "*" +// a browser ignores the credentials flag entirely, which is exactly the +// incoherence this replaced. With an allow-list the pair is consistent, so a +// future move to Cookie-carried sessions does not silently lose the Cookie. +// +// The session itself still travels in the Authorization header, not a Cookie, +// so a same-origin deployment does not need CORS at all — but an operator +// running the Vite dev server (or a separate UI host) does, and this is where +// that is decided. policy is shared with the WebSocket upgrader. +func CORS(policy *OriginPolicy) gin.HandlerFunc { return func(c *gin.Context) { - c.Header("Access-Control-Allow-Origin", "*") + origin := c.GetHeader("Origin") + allowed := origin != "" && policy.AllowsRequest(c.Request) + + // Always Vary: Origin, otherwise a cache that served a response to one + // origin keeps serving it to the next. + c.Header("Vary", "Origin") + + switch { + case allowed: + // Echo, even under a "*" policy: the echoed form keeps the + // credentials flag meaningful. + c.Header("Access-Control-Allow-Origin", origin) + c.Header("Access-Control-Allow-Credentials", "true") + case origin == "" && policy.AllowsAny(): + // No Origin to echo (curl, a native client) — send the literal. + c.Header("Access-Control-Allow-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") - c.Header("Access-Control-Allow-Credentials", "true") if c.Request.Method == "OPTIONS" { c.AbortWithStatus(http.StatusNoContent) diff --git a/internal/middleware/middleware_test.go b/internal/middleware/middleware_test.go new file mode 100644 index 00000000..c4a22b12 --- /dev/null +++ b/internal/middleware/middleware_test.go @@ -0,0 +1,294 @@ +package middleware + +import ( + "encoding/json" + nethttp "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/gin-gonic/gin" +) + +func init() { gin.SetMode(gin.TestMode) } + +// TestCORSAllowedOriginEchoed is the M2 regression test: a listed origin gets +// its own value echoed back, not "*", which is what makes the paired +// Allow-Credentials: true actually mean something. +func TestCORSAllowedOriginEchoed(t *testing.T) { + policy := NewOriginPolicy([]string{"http://localhost:3000"}) + + req := httptest.NewRequest(nethttp.MethodGet, "/api/v1/adapters", nil) + req.Header.Set("Origin", "http://localhost:3000") + rec := httptest.NewRecorder() + + router := gin.New() + router.Use(CORS(policy)) + router.GET("/api/v1/adapters", func(c *gin.Context) { c.Status(nethttp.StatusOK) }) + router.ServeHTTP(rec, req) + + if got := rec.Header().Get("Access-Control-Allow-Origin"); got != "http://localhost:3000" { + t.Fatalf("Allow-Origin = %q, want the echoed origin", got) + } + if got := rec.Header().Get("Access-Control-Allow-Credentials"); got != "true" { + t.Fatalf("Allow-Credentials = %q, want true on an echoed origin", got) + } + if got := rec.Header().Get("Vary"); !strings.Contains(got, "Origin") { + t.Fatalf("Vary = %q, want it to include Origin", got) + } +} + +// TestCORSUnlistedOriginHasNoAllowOrigin is the other half of M2: an origin +// nobody listed must not receive an allow header at all. +func TestCORSUnlistedOriginHasNoAllowOrigin(t *testing.T) { + policy := NewOriginPolicy([]string{"http://localhost:3000"}) + + req := httptest.NewRequest(nethttp.MethodGet, "/api/v1/adapters", nil) + req.Header.Set("Origin", "https://evil.example") + rec := httptest.NewRecorder() + + router := gin.New() + router.Use(CORS(policy)) + router.GET("/api/v1/adapters", func(c *gin.Context) { c.Status(nethttp.StatusOK) }) + router.ServeHTTP(rec, req) + + if got := rec.Header().Get("Access-Control-Allow-Origin"); got != "" { + t.Fatalf("Allow-Origin = %q, want empty for an unlisted origin", got) + } + if got := rec.Header().Get("Access-Control-Allow-Credentials"); got != "" { + t.Fatalf("Allow-Credentials = %q, want empty without an allowed origin", got) + } +} + +// TestCORSWildcard covers the escape hatch. Even under "*" the origin is +// echoed rather than sent as a literal star, because the echoed form keeps +// Allow-Credentials meaningful; the wildcard only skips the membership check. +func TestCORSWildcard(t *testing.T) { + policy := NewOriginPolicy([]string{"*"}) + + req := httptest.NewRequest(nethttp.MethodGet, "/x", nil) + req.Header.Set("Origin", "https://anything.example") + rec := httptest.NewRecorder() + + router := gin.New() + router.Use(CORS(policy)) + router.GET("/x", func(c *gin.Context) { c.Status(nethttp.StatusOK) }) + router.ServeHTTP(rec, req) + + if got := rec.Header().Get("Access-Control-Allow-Origin"); got != "https://anything.example" { + t.Fatalf("Allow-Origin = %q, want the echoed origin under a wildcard policy", got) + } + // With no Origin at all there is nothing to echo, so the literal is used. + plain := httptest.NewRequest(nethttp.MethodGet, "/x", nil) + plainRec := httptest.NewRecorder() + router.ServeHTTP(plainRec, plain) + if got := plainRec.Header().Get("Access-Control-Allow-Origin"); got != "*" { + t.Fatalf("Allow-Origin without an Origin = %q, want *", got) + } +} + +// TestCORSPreflight: an OPTIONS preflight for an allowed origin is answered +// 204 with the allow headers, which is what the browser checks before sending +// the real request. +func TestCORSPreflight(t *testing.T) { + policy := NewOriginPolicy([]string{"http://localhost:3000"}) + + req := httptest.NewRequest(nethttp.MethodOptions, "/api/v1/adapters", nil) + req.Header.Set("Origin", "http://localhost:3000") + req.Header.Set("Access-Control-Request-Method", "GET") + rec := httptest.NewRecorder() + + router := gin.New() + router.Use(CORS(policy)) + router.GET("/api/v1/adapters", func(c *gin.Context) { t.Fatal("preflight reached the handler") }) + router.ServeHTTP(rec, req) + + if rec.Code != nethttp.StatusNoContent { + t.Fatalf("preflight status = %d, want 204", rec.Code) + } + if got := rec.Header().Get("Access-Control-Allow-Origin"); got != "http://localhost:3000" { + t.Fatalf("preflight Allow-Origin = %q, want the echoed origin", got) + } + if got := rec.Header().Get("Access-Control-Allow-Methods"); !strings.Contains(got, "PUT") { + t.Fatalf("Allow-Methods = %q, want the method list", got) + } +} + +// TestOriginPolicyAllowsEmptyOrigin: same-origin navigation, curl and native +// clients send no Origin and must keep working without an allow-list entry. +func TestOriginPolicyAllowsEmptyOrigin(t *testing.T) { + policy := NewOriginPolicy([]string{"http://localhost:3000"}) + if !policy.Allows("") { + t.Fatal("empty origin rejected; same-origin clients would break") + } +} + +// TestOriginPolicyCheckOrigin drives the upgrader hook the WS handlers use. +func TestOriginPolicyCheckOrigin(t *testing.T) { + policy := NewOriginPolicy([]string{"http://localhost:3000"}) + + req := httptest.NewRequest(nethttp.MethodGet, "/ws/adapter", nil) + req.Header.Set("Origin", "https://evil.example") + if policy.CheckOrigin(req) { + t.Error("cross-origin handshake accepted") + } + + req.Header.Set("Origin", "http://localhost:3000") + if !policy.CheckOrigin(req) { + t.Error("allow-listed origin rejected") + } +} + +// TestOriginPolicyCheckOriginSameHost covers the case that made a bare +// allow-list unusable: a browser sends Origin on a *same-origin* handshake too, +// and a deployment serving the UI from the API's own port must not have to list +// its own URL. +func TestOriginPolicyCheckOriginSameHost(t *testing.T) { + policy := NewOriginPolicy(nil) // nothing configured at all + + req := httptest.NewRequest(nethttp.MethodGet, "http://example.test:8080/ws/adapter", nil) + req.Header.Set("Origin", "http://example.test:8080") + if !policy.CheckOrigin(req) { + t.Fatal("same-origin handshake rejected; browsers always send Origin") + } + + // Port still distinguishes, so a different port is cross-origin. + req.Header.Set("Origin", "http://example.test:9999") + if policy.CheckOrigin(req) { + t.Fatal("different port treated as same-origin") + } + + // A host that merely ends in the right name is not the same origin. + req.Header.Set("Origin", "http://example.test:8080.evil.example") + if policy.CheckOrigin(req) { + t.Fatal("suffix-extended host treated as same-origin") + } +} + +// mintToken produces a real signed token through the same code path the server +// uses, so the WS test is not passing on a string shape. +func mintToken(t *testing.T, userID string) string { + t.Helper() + SetJWTSecret("test-secret") + token, err := signForTest(userID) + if err != nil { + t.Fatalf("mint token: %v", err) + } + return token +} + +// TestWSTokenRequired is the M4 acceptance test: the handshake is refused +// without a valid token and accepted with one. +func TestWSTokenRequired(t *testing.T) { + valid := mintToken(t, "user-1") + + tests := []struct { + name string + query string + header string + wantCode int + wantPass bool + }{ + {"no token at all", "", "", nethttp.StatusUnauthorized, false}, + {"garbage token", "?token=not-a-jwt", "", nethttp.StatusUnauthorized, false}, + {"valid query token", "?token=" + valid, "", nethttp.StatusOK, true}, + {"valid bearer header", "", "Bearer " + valid, nethttp.StatusOK, true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + req := httptest.NewRequest(nethttp.MethodGet, "/ws/adapter"+tt.query, nil) + if tt.header != "" { + req.Header.Set("Authorization", tt.header) + } + rec := httptest.NewRecorder() + + router := gin.New() + var passed bool + var gotUser string + router.GET("/ws/adapter", WSTokenRequired(), func(c *gin.Context) { + passed = true + if value, ok := c.Get("userID"); ok { + gotUser, _ = value.(string) + } + c.Status(nethttp.StatusOK) + }) + router.ServeHTTP(rec, req) + + if rec.Code != tt.wantCode { + t.Fatalf("status = %d, want %d", rec.Code, tt.wantCode) + } + if passed != tt.wantPass { + t.Fatalf("handler reached = %v, want %v", passed, tt.wantPass) + } + if tt.wantPass && gotUser != "user-1" { + t.Fatalf("context userID = %q, want the claim value", gotUser) + } + }) + } +} + +// TestMaxBodyBytesRejectsOversizeBody is the M15 test for the body cap: a body +// past the limit must not be read whole. +func TestMaxBodyBytesRejectsOversizeBody(t *testing.T) { + router := gin.New() + router.Use(MaxBodyBytes(64)) + router.POST("/login", func(c *gin.Context) { + var body map[string]any + if err := c.ShouldBindJSON(&body); err != nil { + if !BodyTooLarge(c, err) { + c.JSON(nethttp.StatusBadRequest, gin.H{"error": err.Error()}) + } + return + } + c.Status(nethttp.StatusOK) + }) + + big := `{"pad":"` + strings.Repeat("x", 200) + `"}` + req := httptest.NewRequest(nethttp.MethodPost, "/login", strings.NewReader(big)) + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + router.ServeHTTP(rec, req) + + if rec.Code != nethttp.StatusRequestEntityTooLarge { + t.Fatalf("status = %d, want 413", rec.Code) + } + + var resp map[string]string + if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil { + t.Fatalf("decode error body: %v", err) + } + if resp["error"] == "" { + t.Fatal("413 carried no error message") + } +} + +// TestMaxBodyBytesAllowsSmallBody guards against the cap breaking normal input. +func TestMaxBodyBytesAllowsSmallBody(t *testing.T) { + router := gin.New() + router.Use(MaxBodyBytes(1 << 20)) + router.POST("/login", func(c *gin.Context) { c.Status(nethttp.StatusOK) }) + + req := httptest.NewRequest(nethttp.MethodPost, "/login", strings.NewReader(`{"a":1}`)) + rec := httptest.NewRecorder() + router.ServeHTTP(rec, req) + + if rec.Code != nethttp.StatusOK { + t.Fatalf("status = %d, want 200", rec.Code) + } +} + +// TestMaxBodyBytesSkipsGET keeps the read-only path untouched. +func TestMaxBodyBytesSkipsGET(t *testing.T) { + router := gin.New() + router.Use(MaxBodyBytes(1)) + router.GET("/health", func(c *gin.Context) { c.Status(nethttp.StatusOK) }) + + req := httptest.NewRequest(nethttp.MethodGet, "/health", strings.NewReader(strings.Repeat("x", 100))) + rec := httptest.NewRecorder() + router.ServeHTTP(rec, req) + + if rec.Code != nethttp.StatusOK { + t.Fatalf("status = %d, want 200 — GET body should not be capped", rec.Code) + } +} diff --git a/internal/middleware/origin.go b/internal/middleware/origin.go new file mode 100644 index 00000000..4ce2d0b3 --- /dev/null +++ b/internal/middleware/origin.go @@ -0,0 +1,94 @@ +package middleware + +import ( + "net/http" + "net/url" +) + +// OriginPolicy decides which browser origins an HTTP response and a WebSocket +// handshake accept. +// +// CORS and CheckOrigin answer the same question, so they read one policy built +// once at startup from the same config list. Two copies of that decision drift: +// a working REST call plus a rejected socket is the failure mode. +type OriginPolicy struct { + // allowAny is set by a "*" entry and means "do not check". + allowAny bool + // allowed is matched exactly: scheme + host + port, no trailing slash. A + // prefix match would let `http://localhost:3000.evil.example` pass for + // `http://localhost:3000`. + allowed map[string]struct{} +} + +// NewOriginPolicy builds a policy from the configured allow-list. +// +// An empty list allows no *cross* origin. Same-origin still works for the +// WebSocket handshake — see CheckOrigin — and for CORS it never needed an entry +// because the browser sends no Origin on a same-origin request. +func NewOriginPolicy(origins []string) *OriginPolicy { + p := &OriginPolicy{allowed: make(map[string]struct{}, len(origins))} + for _, origin := range origins { + if origin == "*" { + p.allowAny = true + continue + } + p.allowed[origin] = struct{}{} + } + return p +} + +// Allows reports whether the literal origin string may talk to us. An empty +// origin (curl, a native client, a same-origin GET) is always allowed: no page +// is in a position to forge it, which is why it needs no entry. +func (p *OriginPolicy) Allows(origin string) bool { + if origin == "" { + return true + } + if p == nil { + return false + } + if p.allowAny { + return true + } + _, ok := p.allowed[origin] + return ok +} + +// AllowsRequest is the single decision CORS and the upgrader both consume. +// +// Beyond the list it accepts a same-origin request. Browsers omit Origin on a +// same-origin GET (so CORS never needed the entry) but always send it on a +// handshake, so the two surfaces cannot share only Allows() without one of +// them needing a URL for the deployment's own host. +func (p *OriginPolicy) AllowsRequest(r *http.Request) bool { + origin := r.Header.Get("Origin") + if p.Allows(origin) { + return true + } + return sameOrigin(r, origin) +} + +// AllowsAny reports whether the policy was configured with "*". CORS uses it +// to decide between echoing the origin and sending a literal "*". +func (p *OriginPolicy) AllowsAny() bool { + return p != nil && p.allowAny +} + +// CheckOrigin is the websocket.Upgrader hook. +// +// A rejected origin fails the handshake with 403 before any app-level bytes are +// exchanged. The decision itself is AllowsRequest, shared with CORS. +func (p *OriginPolicy) CheckOrigin(r *http.Request) bool { + return p.AllowsRequest(r) +} + +// sameOrigin reports whether origin names the host the request was addressed +// to. Only the host:port is compared, because the scheme the browser reports +// and the scheme this process sees differ whenever TLS is terminated upstream. +func sameOrigin(r *http.Request, origin string) bool { + parsed, err := url.Parse(origin) + if err != nil || parsed.Host == "" { + return false + } + return parsed.Host == r.Host +} diff --git a/internal/server/main_test.go b/internal/server/main_test.go new file mode 100644 index 00000000..673c1ea3 --- /dev/null +++ b/internal/server/main_test.go @@ -0,0 +1,17 @@ +package server + +import ( + "os" + "testing" + + "github.com/e-spl/e-sp-line2/pkg/logger" +) + +// repository.NewRepositories logs during construction, so the logger has to be +// live before any test builds a server. Same reason internal/adapter has this. +func TestMain(m *testing.M) { + logger.Init("error") + code := m.Run() + _ = logger.Sync() + os.Exit(code) +} diff --git a/internal/server/security_test.go b/internal/server/security_test.go new file mode 100644 index 00000000..83711a1d --- /dev/null +++ b/internal/server/security_test.go @@ -0,0 +1,191 @@ +package server + +import ( + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/golang-jwt/jwt/v5" + "github.com/gorilla/websocket" +) + +// testServer builds a real *Server and returns its router behind an httptest +// server, so the WS tests exercise the registered routes and their middleware +// chain rather than the handler functions in isolation. +func testServer(t *testing.T) (*httptest.Server, string) { + t.Helper() + + cfg := testConfig(t) + cfg.JWT.Secret = "ws-test-secret" + cfg.Server.AllowedOrigins = []string{"http://trusted.example"} + + srv, err := New(cfg) + if err != nil { + t.Fatalf("New: %v", err) + } + t.Cleanup(func() { _ = srv.services.Close() }) + + ts := httptest.NewServer(srv.router) + t.Cleanup(ts.Close) + + // A token minted with the same secret the middleware was given. + claims := jwt.MapClaims{ + "user_id": "user-1", + "username": "tester", + "role": "user", + "exp": time.Now().Add(time.Hour).Unix(), + } + token, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString([]byte(cfg.JWT.Secret)) + if err != nil { + t.Fatalf("mint token: %v", err) + } + return ts, token +} + +// dialWS attempts a handshake and returns the error, if any. +func dialWS(t *testing.T, url, origin string) error { + t.Helper() + header := http.Header{} + if origin != "" { + header.Set("Origin", origin) + } + conn, resp, err := websocket.DefaultDialer.Dial(url, header) + if conn != nil { + _ = conn.Close() + } + if err != nil { + return err + } + _ = resp + return nil +} + +// TestWSHandshakeRejectsMissingToken is the M4 end-to-end regression test: the +// socket route is now behind WSTokenRequired, so a bare handshake gets 401. +func TestWSHandshakeRejectsMissingToken(t *testing.T) { + ts, _ := testServer(t) + url := "ws" + strings.TrimPrefix(ts.URL, "http") + "/ws/adapter?instance_id=x" + + _, resp, err := websocket.DefaultDialer.Dial(url, nil) + if err == nil { + t.Fatal("handshake without a token was accepted") + } + if resp == nil { + t.Fatalf("no HTTP response to inspect: %v", err) + } + if resp.StatusCode != http.StatusUnauthorized { + t.Fatalf("status = %d, want 401", resp.StatusCode) + } +} + +// TestWSHandshakeRejectsGarbageToken separates "some string was sent" from +// "a valid JWT was sent". +func TestWSHandshakeRejectsGarbageToken(t *testing.T) { + ts, _ := testServer(t) + url := "ws" + strings.TrimPrefix(ts.URL, "http") + "/ws/adapter?instance_id=x&token=nope" + + _, resp, err := websocket.DefaultDialer.Dial(url, nil) + if err == nil { + t.Fatal("handshake with a garbage token was accepted") + } + if resp == nil { + t.Fatalf("no HTTP response to inspect: %v", err) + } + if resp.StatusCode != http.StatusUnauthorized { + t.Fatalf("status = %d, want 401", resp.StatusCode) + } +} + +// TestWSHandshakeRejectsUntrustedOrigin is the other M4 half: even a correctly +// authenticated handshake is refused from an origin that is not allow-listed. +func TestWSHandshakeRejectsUntrustedOrigin(t *testing.T) { + ts, token := testServer(t) + url := "ws" + strings.TrimPrefix(ts.URL, "http") + "/ws/adapter?instance_id=x&token=" + token + + if err := dialWS(t, url, "https://evil.example"); err == nil { + t.Fatal("cross-origin handshake was accepted") + } +} + +// TestWSHandshakeAcceptsValidTokenAndOrigin is the positive control; without it +// the three rejection tests could pass on a route that never works. +func TestWSHandshakeAcceptsValidTokenAndOrigin(t *testing.T) { + ts, token := testServer(t) + url := "ws" + strings.TrimPrefix(ts.URL, "http") + "/ws/adapter?instance_id=x&token=" + token + + if err := dialWS(t, url, "http://trusted.example"); err != nil { + t.Fatalf("allow-listed handshake rejected: %v", err) + } +} + +// TestWSAppRouteAlsoAuthenticated guards the second WS route. It used to share +// the permissive upgrader and no middleware at all. +func TestWSAppRouteAlsoAuthenticated(t *testing.T) { + ts, token := testServer(t) + base := "ws" + strings.TrimPrefix(ts.URL, "http") + "/ws/app?app_id=app-1" + + if _, resp, err := websocket.DefaultDialer.Dial(base, nil); err == nil || resp == nil || + resp.StatusCode != http.StatusUnauthorized { + t.Fatalf("app route without a token: err=%v status=%v, want 401", err, resp) + } + if err := dialWS(t, base+"&token="+token, "http://trusted.example"); err != nil { + t.Fatalf("authenticated app handshake rejected: %v", err) + } +} + +// TestCORSPreflightOnRealRouter runs the M2 check through the assembled router, +// including its middleware order, not just the CORS function. +func TestCORSPreflightOnRealRouter(t *testing.T) { + ts, _ := testServer(t) + + req, err := http.NewRequest(http.MethodOptions, ts.URL+"/api/v1/adapters", nil) + if err != nil { + t.Fatalf("new request: %v", err) + } + req.Header.Set("Origin", "http://trusted.example") + req.Header.Set("Access-Control-Request-Method", "GET") + + resp, err := http.DefaultClient.Do(req) + if err != nil { + t.Fatalf("preflight: %v", err) + } + defer resp.Body.Close() + + if got := resp.Header.Get("Access-Control-Allow-Origin"); got != "http://trusted.example" { + t.Fatalf("Allow-Origin = %q, want the trusted origin", got) + } + + untrusted, err := http.NewRequest(http.MethodOptions, ts.URL+"/api/v1/adapters", nil) + if err != nil { + t.Fatalf("new untrusted request: %v", err) + } + untrusted.Header.Set("Origin", "https://evil.example") + resp2, err := http.DefaultClient.Do(untrusted) + if err != nil { + t.Fatalf("untrusted preflight: %v", err) + } + defer resp2.Body.Close() + + if got := resp2.Header.Get("Access-Control-Allow-Origin"); got != "" { + t.Fatalf("Allow-Origin = %q for an untrusted origin, want empty", got) + } +} + +// TestOversizeBodyGets413 runs the body cap through the real router so the +// middleware is proven to be registered, not merely to work standalone. +func TestOversizeBodyGets413(t *testing.T) { + ts, _ := testServer(t) + + big := `{"username":"u","password":"` + strings.Repeat("x", 3<<20) + `"}` + resp, err := http.Post(ts.URL+"/api/v1/auth/login", "application/json", strings.NewReader(big)) + if err != nil { + t.Fatalf("post: %v", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusRequestEntityTooLarge { + t.Fatalf("status = %d, want 413", resp.StatusCode) + } +} diff --git a/internal/server/server.go b/internal/server/server.go index fb4781b4..6d8f4774 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -23,11 +23,12 @@ import ( // Server represents the HTTP server type Server struct { - cfg *config.Config - httpServer *http.Server - router *gin.Engine - services *service.Services - gateway *adaptergateway.Gateway + cfg *config.Config + httpServer *http.Server + router *gin.Engine + services *service.Services + gateway *adaptergateway.Gateway + originPolicy *middleware.OriginPolicy } // New creates a new server instance @@ -49,19 +50,25 @@ func New(cfg *config.Config) (*Server, error) { router := gin.New() router.Use(gin.Recovery()) router.Use(middleware.Logger()) - router.Use(middleware.CORS()) + + // One policy object drives both the CORS response headers and every + // WebSocket CheckOrigin, so the two cannot disagree about which origins + // are trusted. + originPolicy := middleware.NewOriginPolicy(cfg.Server.AllowedOrigins) + router.Use(middleware.CORS(originPolicy)) + router.Use(middleware.MaxBodyBytes(cfg.Server.MaxBodyBytes)) s := &Server{ - cfg: cfg, - router: router, - services: services, + cfg: cfg, + router: router, + services: services, + originPolicy: originPolicy, } // Initialize the adapter gateway (接入器) WebSocket server. - s.gateway = adaptergateway.NewGateway( - adaptergateway.DefaultGatewayConfig(), - services.AdapterGateway, - ) + gwCfg := adaptergateway.DefaultGatewayConfig() + gwCfg.CheckOrigin = originPolicy.CheckOrigin + s.gateway = adaptergateway.NewGateway(gwCfg, services.AdapterGateway) // Register the change callback: whenever an adapter is created, updated or // deleted, the gateway reloads client-mode outbound connections live. @@ -96,14 +103,34 @@ func New(cfg *config.Config) (*Server, error) { logger.Info("Embedded WebUI enabled (desktop build)") } + // Hardening: without these a client can hold a connection open with a + // partial header block (Slowloris) or a trickled body indefinitely. + // + // WebSocket routes are unaffected: gorilla hijacks the net.Conn during the + // upgrade and clears the deadlines, so a long-lived socket is not cut at + // WriteTimeout. That is the reason these are non-zero rather than "the + // socket would break". s.httpServer = &http.Server{ - Addr: fmt.Sprintf(":%d", cfg.Port), - Handler: router, + Addr: fmt.Sprintf(":%d", cfg.Port), + Handler: router, + ReadHeaderTimeout: seconds(cfg.Server.ReadHeaderTimeout), + ReadTimeout: seconds(cfg.Server.ReadTimeout), + WriteTimeout: seconds(cfg.Server.WriteTimeout), + IdleTimeout: seconds(cfg.Server.IdleTimeout), } return s, nil } +// seconds converts a config duration in seconds to a time.Duration. A +// non-positive value becomes 0, which net/http reads as "no timeout". +func seconds(n int) time.Duration { + if n <= 0 { + return 0 + } + return time.Duration(n) * time.Second +} + // setupRoutes configures all API routes func (s *Server) setupRoutes() { // Health check @@ -273,21 +300,28 @@ func (s *Server) setupRoutes() { } // WebSocket routes + // + // /ws/adapter and /ws/app take a token at the handshake (?token=): a + // socket that does not authenticate is now rejected with 401 instead of + // being accepted and then trusted for whatever instance_id / app_id the + // caller typed. The adapter-gateway routes below keep their own ?key= + // credential — that is a per-adapter key, not a user session. ws := s.router.Group("/ws") { // Bridge (桥) reconnect endpoint. Inbound messages reported by bridges // are persisted AND fanned out to the adapter gateway (接入器). // The gateway is passed so outbound commands can be routed back to the // correct bridge instance. - ws.GET("/adapter", handler.AdapterWebSocket( + ws.GET("/adapter", middleware.WSTokenRequired(), handler.AdapterWebSocket( s.services.Adapter, s.services.Message, s.gateway, + s.originPolicy, func(payload map[string]interface{}) { s.broadcastToAdapterGateway(payload) }, )) - ws.GET("/app", handler.AppWebSocket(s.services.Message)) + ws.GET("/app", middleware.WSTokenRequired(), handler.AppWebSocket(s.services.Message, s.originPolicy)) // Adapter gateway (接入器) — external systems connect here with ?key=xxx // Default listen path is simply /ws (简洁). diff --git a/internal/server/server_test.go b/internal/server/server_test.go new file mode 100644 index 00000000..778ac43b --- /dev/null +++ b/internal/server/server_test.go @@ -0,0 +1,145 @@ +package server + +import ( + "os" + "path/filepath" + "testing" + "time" + + "github.com/e-spl/e-sp-line2/internal/config" +) + +// TestNewAppliesServerTimeouts is the M15 regression test: the http.Server must +// carry the hardening timeouts, not just an Addr and a Handler. +// +// It reads the built *http.Server off the constructor so a later refactor that +// drops a field fails here instead of only under load. +func TestNewAppliesServerTimeouts(t *testing.T) { + cfg := testConfig(t) + cfg.Server.ReadHeaderTimeout = 3 + cfg.Server.ReadTimeout = 11 + cfg.Server.WriteTimeout = 13 + cfg.Server.IdleTimeout = 17 + + srv, err := New(cfg) + if err != nil { + t.Fatalf("New: %v", err) + } + defer func() { _ = srv.services.Close() }() + + cases := []struct { + name string + got time.Duration + want time.Duration + }{ + {"ReadHeaderTimeout", srv.httpServer.ReadHeaderTimeout, 3 * time.Second}, + {"ReadTimeout", srv.httpServer.ReadTimeout, 11 * time.Second}, + {"WriteTimeout", srv.httpServer.WriteTimeout, 13 * time.Second}, + {"IdleTimeout", srv.httpServer.IdleTimeout, 17 * time.Second}, + } + for _, tc := range cases { + if tc.got != tc.want { + t.Errorf("%s = %v, want %v", tc.name, tc.got, tc.want) + } + } +} + +// TestNewTranslatesZeroTimeoutToDisabled keeps the "0 means off" contract: a +// zero in config must not become a zero-second deadline, which would expire +// every connection instantly if handed to net/http as-is. +func TestNewTranslatesZeroTimeoutToDisabled(t *testing.T) { + cfg := testConfig(t) + cfg.Server.ReadHeaderTimeout = 0 + cfg.Server.ReadTimeout = 0 + cfg.Server.WriteTimeout = 0 + cfg.Server.IdleTimeout = 0 + + srv, err := New(cfg) + if err != nil { + t.Fatalf("New: %v", err) + } + defer func() { _ = srv.services.Close() }() + + if srv.httpServer.ReadHeaderTimeout != 0 || srv.httpServer.ReadTimeout != 0 || + srv.httpServer.WriteTimeout != 0 || srv.httpServer.IdleTimeout != 0 { + t.Fatalf("zero config produced non-zero timeouts: %+v", srv.httpServer) + } +} + +// TestNewBuildsSharedOriginPolicy proves CORS and the WS upgrader were handed +// the same policy object. Two independently-built policies drift, and the drift +// shows up as "REST works, socket 403s". +func TestNewBuildsSharedOriginPolicy(t *testing.T) { + cfg := testConfig(t) + cfg.Server.AllowedOrigins = []string{"http://trusted.example"} + + srv, err := New(cfg) + if err != nil { + t.Fatalf("New: %v", err) + } + defer func() { _ = srv.services.Close() }() + + if srv.originPolicy == nil { + t.Fatal("no origin policy on the server") + } + if !srv.originPolicy.Allows("http://trusted.example") { + t.Error("configured origin not allowed by the server policy") + } + if srv.originPolicy.Allows("https://evil.example") { + t.Error("unlisted origin allowed by the server policy") + } +} + +// chdir moves into dir for the duration of the test. testing.Chdir needs +// go1.24 while go.mod still declares 1.22, so this does it by hand. +func chdir(t *testing.T, dir string) { + t.Helper() + old, err := os.Getwd() + if err != nil { + t.Fatalf("getwd: %v", err) + } + if err := os.Chdir(dir); err != nil { + t.Fatalf("chdir %s: %v", dir, err) + } + t.Cleanup(func() { + if err := os.Chdir(old); err != nil { + t.Fatalf("restore wd: %v", err) + } + }) +} + +// testConfig is a minimal config pointing at a throwaway SQLite file so New can +// build the services stack without touching the developer's data/ directory. +// +// The form-options registry is a hard startup dependency, so the temp dir gets +// a stub copy before anything runs. +func testConfig(t *testing.T) *config.Config { + t.Helper() + + dir := t.TempDir() + chdir(t, dir) + if err := os.MkdirAll(filepath.Join(dir, "config"), 0o755); err != nil { + t.Fatalf("mkdir config: %v", err) + } + if err := os.WriteFile(filepath.Join(dir, "config", "form-options.yaml"), []byte("platform_codes: []\n"), 0o600); err != nil { + t.Fatalf("write form options: %v", err) + } + + return &config.Config{ + Host: "127.0.0.1", + Port: 0, + Version: "test", + LogLevel: "error", + Database: config.DatabaseConfig{Driver: "sqlite", DSN: "test.db"}, + JWT: config.JWTConfig{Secret: "test-secret", Expiration: 1}, + Adapter: config.AdapterConfig{AdaptersDir: "adapters", PythonBin: "python3"}, + Server: config.ServerConfig{ + AllowedOrigins: []string{"http://localhost:3000"}, + ReadHeaderTimeout: 5, + ReadTimeout: 60, + WriteTimeout: 60, + IdleTimeout: 120, + MaxBodyBytes: 1 << 20, + }, + } +} From 00b2ddb5928552951222b4fb8afadf83bb3ef5cc Mon Sep 17 00:00:00 2001 From: TyperBody Date: Sat, 7 Nov 2026 20:10:02 +0800 Subject: [PATCH 3/3] docs, handlers, adapters: follow the new origin, token and body contract MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit API reference gains the origin allow-list, the server limits table, the 413 code, and the WebSocket handshake token — a changed endpoint updates the doc under the same commit, per pr-qa. SECURITY.md stops describing the `*` plus credentials pair and the any-origin upgrader as outstanding, and now states the residual gap: the handshake proves a valid token, not ownership of the `instance_id` behind it. The two bridge adapters append `?token=` to their WS URL, quoting it. Without that a bridge with a token configured still fails the handshake it used to pass. READMEs, bridge-protocol, inactive-fields and the troubleshooting snippet follow. Handlers route bind failures through middleware.BodyTooLarge so an over-size body is 413, not a parse error. The adapter gateway gets the same CheckOrigin hook for its custom listen paths. Co-authored-by: multica-agent --- .github/SECURITY.md | 11 ++- README.md | 8 +- README.zh-CN.md | 8 +- adapters/taobao/esp_bridge.py | 12 ++- adapters/xianyu/esp_bridge.py | 11 ++- config/config.yaml | 26 ++++++ docs/developer-guide/api-reference.md | 71 ++++++++++++++++- docs/developer-guide/bridge-protocol.md | 12 ++- docs/developer-guide/inactive-fields.md | 6 +- docs/user-guide/troubleshooting.md | 7 +- internal/handler/adapter.go | 9 ++- internal/handler/adapter_gateway.go | 9 ++- internal/handler/auth.go | 9 ++- internal/handler/command.go | 5 +- internal/handler/instance.go | 9 ++- internal/handler/platform.go | 9 ++- internal/handler/route.go | 9 ++- internal/handler/tool.go | 13 ++- internal/handler/user.go | 17 +++- main.go | 7 ++ main_test.go | 101 ++++++++++++++++++++++++ 21 files changed, 331 insertions(+), 38 deletions(-) create mode 100644 main_test.go diff --git a/.github/SECURITY.md b/.github/SECURITY.md index c8c0c64a..54e3af7f 100644 --- a/.github/SECURITY.md +++ b/.github/SECURITY.md @@ -38,7 +38,8 @@ In scope, for example: - a request sent anywhere other than the platform endpoint it targets; - the JWT secret accepted in its default `change-this-secret-in-production` value in a deployment that never changed it; - path traversal through upload or file handling; -- the WebSocket upgrade accepting a cross-origin connection that should be rejected. +- the WebSocket upgrade accepting a cross-origin connection that should be rejected, or an unauthenticated socket being accepted at all; +- a request body read without a size ceiling, or a connection held open without a header deadline. Out of scope: @@ -54,6 +55,9 @@ Out of scope: - 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. - The `/api/v1` surface except `/auth/register` and `/auth/login` sits behind `AuthRequired`. `AdminRequired` exists but is not wired into any route yet. +- Both WebSocket routes (`/ws/adapter`, `/ws/app`) require a valid JWT at the handshake (`?token=`) and reject an unlisted `Origin` before upgrading. The gateway routes (`/ws`, `/ws/adapter-gateway/...`) keep their own per-adapter `?key=`. +- CORS and the WebSocket `CheckOrigin` read one allow-list, `server.allowed_origins`. The accepted origin is echoed rather than sent as `*`, which is the only form where the accompanying `Access-Control-Allow-Credentials` has an effect. +- `http.Server` runs with header, read, write and idle timeouts, and every request body is capped at `server.max_body_bytes` (8 MiB default) — an unbounded body used to be read whole into memory. - An adapter's `security` policy declares `sensitive_fields` (for Taobao and Xianyu, `cookie`) so a manifest can mark what must not be echoed back. ### What is not there yet @@ -62,8 +66,9 @@ 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 WebSocket handshake check proves the caller holds a valid token, not that it owns the `instance_id` / `app_id` it asks to bind. One logged-in user can still attach to another user's instance. +- The default `jwt.secret` is a placeholder. Startup logs a WARN while it is unchanged; change it before you deploy. It is not fatal, so a forgotten deployment still comes up — quietly forgeable. + ### Front end diff --git a/README.md b/README.md index 6e4d2410..4fd56680 100644 --- a/README.md +++ b/README.md @@ -130,8 +130,12 @@ The dev server starts on `http://localhost:3000`. ### WebSocket -- `ws://localhost:8080/ws/adapter?instance_id=xxx` - Adapter WebSocket -- `ws://localhost:8080/ws/app?app_id=xxx` - Application WebSocket +- `ws://localhost:8080/ws/adapter?instance_id=xxx&token=` - Adapter WebSocket +- `ws://localhost:8080/ws/app?app_id=xxx&token=` - Application WebSocket + +Both sockets need a token at the handshake; the same JWT `/auth/login` returns, +passed as `?token=` (or an `Authorization: Bearer` header from a non-browser +client). An unlisted `Origin` is refused too — see `server.allowed_origins`. The full contract lives in the [API reference](docs/developer-guide/api-reference.md). diff --git a/README.zh-CN.md b/README.zh-CN.md index d9dda240..bdc51f2d 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -121,8 +121,12 @@ pnpm dev ### WebSocket 接口 -- `ws://localhost:8080/ws/adapter?instance_id=xxx` - 接入器 WebSocket -- `ws://localhost:8080/ws/app?app_id=xxx` - 应用 WebSocket +- `ws://localhost:8080/ws/adapter?instance_id=xxx&token=` - 接入器 WebSocket +- `ws://localhost:8080/ws/app?app_id=xxx&token=` - 应用 WebSocket + +两条 WS 都要在握手带令牌(与 `/auth/login` 返回的同一个 JWT,走 `?token=`; +非浏览器客户端可改发 `Authorization: Bearer`)。`Origin` 不在白名单同样会被拒, +白名单见 `server.allowed_origins`。 ## 架构设计 diff --git a/adapters/taobao/esp_bridge.py b/adapters/taobao/esp_bridge.py index ed00cb7a..4eb0a1d1 100644 --- a/adapters/taobao/esp_bridge.py +++ b/adapters/taobao/esp_bridge.py @@ -28,6 +28,7 @@ import uuid from dataclasses import dataclass, field from typing import Any, Callable, Dict, List, Optional +from urllib.parse import quote import requests import websockets @@ -188,9 +189,16 @@ def __init__( self.running = False def _ws_url(self) -> str: - """构造后端适配器 WebSocket 地址。""" + """构造后端适配器 WebSocket 地址。 + + 握手带 `?token=`:后端在升级前校验 JWT,缺失或过期会回 401 而 + 不是接受连接。浏览器 WebSocket API 设不了请求头,所以走 query。 + """ 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={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..b41ee492 100644 --- a/adapters/xianyu/esp_bridge.py +++ b/adapters/xianyu/esp_bridge.py @@ -24,6 +24,7 @@ import uuid from dataclasses import dataclass, field from typing import Any, Dict, Optional, Callable +from urllib.parse import quote import requests import websockets @@ -170,8 +171,16 @@ def __init__( self.running = False def _ws_url(self) -> str: + """构造后端适配器 WebSocket 地址。 + + 握手带 `?token=`:后端在升级前校验 JWT,缺失或过期会回 401 而 + 不是接受连接。浏览器 WebSocket API 设不了请求头,所以走 query。 + """ 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={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..31a34af7 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -5,6 +5,28 @@ host: "0.0.0.0" port: 8080 version: "1.0.0" +# HTTP hardening. +# +# allowed_origins is the browser-origin allow-list shared by CORS and the +# WebSocket handshake. It is NOT a same-origin allow-list: a deployment served +# behind one reverse proxy needs no entries at all, because the browser then +# sends no Origin. The defaults exist for the Vite dev server (make +# build-frontend-dev / run-desktop) only — replace them with your UI host when +# the UI lives somewhere else. An entry of "*" allows any origin. +# +# The timeouts are seconds; 0 disables the corresponding net/http timeout. +# Active WebSocket connections are unaffected (the upgrade takes the socket off +# these deadlines), so read_timeout/write_timeout do not cut long-lived sockets. +server: + allowed_origins: + - "http://localhost:3000" + - "http://127.0.0.1:3000" + read_header_timeout: 5 + read_timeout: 60 + write_timeout: 60 + idle_timeout: 120 + max_body_bytes: 8388608 # 8 MiB; adapter configs are JSON blobs, not files + # Database settings database: driver: "sqlite" # sqlite or postgres @@ -28,6 +50,10 @@ redis: log_level: "info" # debug, info, warn, error # JWT settings +# +# Change the secret before exposing the API. Startup logs a WARN while this is +# still the shipped value, because every copy of the repository has it and +# tokens are HS256-signed with it. jwt: secret: "change-this-secret-in-production" expiration: 24 # hours diff --git a/docs/developer-guide/api-reference.md b/docs/developer-guide/api-reference.md index a065a3d4..2ce40126 100644 --- a/docs/developer-guide/api-reference.md +++ b/docs/developer-guide/api-reference.md @@ -10,6 +10,54 @@ E-SP-Line2 provides a RESTful API and WebSocket interfaces for managing platform - **Authentication**: Bearer Token - **Response format**: JSON +## CORS and origins + +The server only names a browser origin it was configured to accept, so the +`Access-Control-Allow-Origin` value is the caller's own origin, never `*`, and +it is paired with `Access-Control-Allow-Credentials: true` (a pair that only +means something when the origin is echoed rather than starred). + +The allow-list is one setting, `server.allowed_origins`, shared by the REST +surface and the WebSocket handshake: + +```yaml +server: + allowed_origins: + - "http://localhost:3000" # the Vite dev server default +``` + +- A deployment served behind one reverse proxy is same-origin and needs no + entries. The REST surface sees no `Origin` at all, and the WebSocket + handshake compares the `Origin` host against the request `Host`, so the + embedded desktop build (UI and API on one port) works untouched. +- Add the UI's real scheme+host+port when the UI is hosted separately. +- An entry of `"*"` allows any origin. The origin is still echoed, so the + credentials flag stays meaningful. +- Matching is exact. `http://localhost:3000.evil.example` does not pass for + `http://localhost:3000`, and a different port is a different origin. +- A request with no `Origin` at all (curl, a native client) always passes. + +An origin that is not listed still gets a normal HTTP response; the browser +just refuses to hand it to the page. The WebSocket handshake is stricter — the +upgrade is refused with `403`. + +## Server limits + +`http.Server` runs with the timeouts below and every request body is capped. +Both are configured under `server` in `config/config.yaml` and are in seconds; +`0` disables the corresponding timeout. + +| Setting | Default | Effect | +| --- | --- | --- | +| `read_header_timeout` | 5 | A client must finish sending headers in this window (Slowloris) | +| `read_timeout` | 60 | Whole-request read budget, body included | +| `write_timeout` | 60 | Response write budget | +| `idle_timeout` | 120 | Keep-alive reuse budget | +| `max_body_bytes` | 8388608 (8 MiB) | Request body cap; over it the API answers `413` | + +Active WebSocket connections are not affected: the upgrade takes the socket off +the HTTP deadlines, so a long-lived socket is not cut at `write_timeout`. + ## Authentication endpoints ### User registration @@ -752,9 +800,27 @@ Public. Returns the message field contract that [message-fields.md](./message-fi ## WebSocket interface +Both socket routes authenticate **at the handshake**: the token goes in the +query string as `?token=` because the browser `WebSocket` constructor +cannot set request headers. A non-browser client may send +`Authorization: Bearer ` instead. Either way, the token is the same JWT +`/auth/login` returns and it is signature-checked before the upgrade. + +Failure is an ordinary HTTP response, so a client can tell "not +authenticated" from "network down": + +| Situation | Response | +| --- | --- | +| No token | `401` | +| Malformed, unsigned or expired token | `401` | +| `Origin` present and not allow-listed | `403` | + +The check proves the caller holds a valid token. It does not prove the caller +owns the `instance_id` / `app_id` in the query — those remain caller-supplied. + ### Adapter WebSocket -**URL**: `ws://localhost:8080/ws/adapter?instance_id=` +**URL**: `ws://localhost:8080/ws/adapter?instance_id=&token=` **Message format**: ```json @@ -776,7 +842,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 @@ -805,6 +871,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 over `server.max_body_bytes` | | 500 | Internal server error | ## Rate limiting diff --git a/docs/developer-guide/bridge-protocol.md b/docs/developer-guide/bridge-protocol.md index 2894db24..a76ab335 100644 --- a/docs/developer-guide/bridge-protocol.md +++ b/docs/developer-guide/bridge-protocol.md @@ -9,7 +9,7 @@ ## 1. 概述 - **传输**:WebSocket -- **连接端点**:`ws://:/ws/adapter?instance_id=` +- **连接端点**:`ws://:/ws/adapter?instance_id=&token=` - **方向**:桥进程主动连接后端核心(桥是 Client,核心是 Server) - **消息格式**:JSON 文本帧 @@ -41,9 +41,17 @@ Authorization: Bearer ### 2.2 建立 WebSocket 连接 ``` -ws://:/ws/adapter?instance_id= +ws://:/ws/adapter?instance_id=&token= ``` +`token` 与 2.1 的 `Authorization` 是同一个 JWT。浏览器 `WebSocket` API 设不了 +请求头,所以握手令牌走 query;非浏览器客户端也可以改发 +`Authorization: Bearer `。后端在升级前验签,失败回 `401`,`Origin` +不在白名单回 `403`,客户端据状态码就能区分"没登录"与"网络不通"。 + +> 令牌只证明调用方持有有效 JWT,不证明它拥有 query 里的 `instance_id`—— +> 归属校验仍由实例接口负责。 + ### 2.3 握手 连接建立后,后端发送 `connected` 消息: diff --git a/docs/developer-guide/inactive-fields.md b/docs/developer-guide/inactive-fields.md index 3877017c..78760fae 100644 --- a/docs/developer-guide/inactive-fields.md +++ b/docs/developer-guide/inactive-fields.md @@ -151,7 +151,7 @@ | 链路 | 入口 | 处理代码 | | --- | --- | --- | -| 桥(Bridge) | `/ws/adapter?instance_id=xxx` | [`handler/websocket.go`](../../internal/handler/websocket.go) `registerToolsFromFrame` | +| 桥(Bridge) | `/ws/adapter?instance_id=xxx&token=` | [`handler/websocket.go`](../../internal/handler/websocket.go) `registerToolsFromFrame` | | 接入框架(server 模式) | `/ws/adapter-gateway?key=xxx` | [`adaptergateway/client.go`](../../internal/adaptergateway/client.go) `handleMessage` | | 接入框架(client 模式) | E-SP-Line2 主动外拨到框架 | [`adaptergateway/client_connector.go`](../../internal/adaptergateway/client_connector.go) `handleInbound` | @@ -241,7 +241,7 @@ tool = adapter.get_gateway_tool('publish_item') # 单个定义 ``` 网关将其转换为**桥协议**帧(`type: tool_call`,`command_type: tool_call`)下发到 -`/ws/adapter?instance_id=xianyu-1` 的连接: +`/ws/adapter?instance_id=xianyu-1&token=` 的连接: ```json { @@ -443,7 +443,7 @@ curl -X POST -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/jso ## 5. 桥(Bridge)实现工具 -桥在 `/ws/adapter?instance_id=` 连接上接收 `tool_call` 帧,执行后回 `tool_result`。 +桥在 `/ws/adapter?instance_id=&token=` 连接上接收 `tool_call` 帧,执行后回 `tool_result`。 Python 伪代码: diff --git a/docs/user-guide/troubleshooting.md b/docs/user-guide/troubleshooting.md index cbe80b94..98a34f69 100644 --- a/docs/user-guide/troubleshooting.md +++ b/docs/user-guide/troubleshooting.md @@ -161,9 +161,12 @@ queue: **Implement reconnect logic** ```javascript -// Frontend reconnect example +// Frontend reconnect example. The handshake needs the JWT (the browser +// WebSocket API cannot set an Authorization header, so it goes in the query) +// and an allow-listed Origin. function connect() { - const ws = new WebSocket('ws://localhost:8080/ws/app'); + const token = localStorage.getItem('auth_token'); + const ws = new WebSocket(`ws://localhost:8080/ws/app?app_id=app-1&token=${token}`); ws.onclose = () => { setTimeout(connect, 5000); diff --git a/internal/handler/adapter.go b/internal/handler/adapter.go index ba922e59..ee85724c 100644 --- a/internal/handler/adapter.go +++ b/internal/handler/adapter.go @@ -4,6 +4,7 @@ import ( "net/http" "strconv" + "github.com/e-spl/e-sp-line2/internal/middleware" "github.com/e-spl/e-sp-line2/internal/service" "github.com/gin-gonic/gin" ) @@ -58,7 +59,9 @@ func CreateAdapter(adapterService *service.AdapterService) gin.HandlerFunc { return func(c *gin.Context) { var req service.CreateAdapterRequest if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + if !middleware.BodyTooLarge(c, err) { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + } return } @@ -78,7 +81,9 @@ func UpdateAdapter(adapterService *service.AdapterService) gin.HandlerFunc { id := c.Param("id") var req service.UpdateAdapterRequest if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + if !middleware.BodyTooLarge(c, err) { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + } return } diff --git a/internal/handler/adapter_gateway.go b/internal/handler/adapter_gateway.go index d2047a48..48716421 100644 --- a/internal/handler/adapter_gateway.go +++ b/internal/handler/adapter_gateway.go @@ -4,6 +4,7 @@ import ( "net/http" "strconv" + "github.com/e-spl/e-sp-line2/internal/middleware" "github.com/e-spl/e-sp-line2/internal/service" "github.com/gin-gonic/gin" ) @@ -32,7 +33,9 @@ func CreateGatewayAdapter(gateway *service.AdapterGatewayService) gin.HandlerFun return func(c *gin.Context) { var req service.CreateGatewayAdapterRequest if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + if !middleware.BodyTooLarge(c, err) { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + } return } @@ -68,7 +71,9 @@ func UpdateGatewayAdapter(gateway *service.AdapterGatewayService) gin.HandlerFun id := c.Param("id") var req service.UpdateGatewayAdapterRequest if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + if !middleware.BodyTooLarge(c, err) { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + } return } diff --git a/internal/handler/auth.go b/internal/handler/auth.go index 774533cb..d5189066 100644 --- a/internal/handler/auth.go +++ b/internal/handler/auth.go @@ -3,6 +3,7 @@ package handler import ( "net/http" + "github.com/e-spl/e-sp-line2/internal/middleware" "github.com/e-spl/e-sp-line2/internal/service" "github.com/gin-gonic/gin" ) @@ -12,7 +13,9 @@ func Register(authService *service.AuthService) gin.HandlerFunc { return func(c *gin.Context) { var req service.RegisterRequest if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + if !middleware.BodyTooLarge(c, err) { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + } return } @@ -31,7 +34,9 @@ func Login(authService *service.AuthService) gin.HandlerFunc { return func(c *gin.Context) { var req service.LoginRequest if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + if !middleware.BodyTooLarge(c, err) { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + } return } diff --git a/internal/handler/command.go b/internal/handler/command.go index 59eb06e2..072a132e 100644 --- a/internal/handler/command.go +++ b/internal/handler/command.go @@ -4,6 +4,7 @@ import ( "net/http" "strconv" + "github.com/e-spl/e-sp-line2/internal/middleware" "github.com/e-spl/e-sp-line2/internal/service" "github.com/gin-gonic/gin" ) @@ -46,7 +47,9 @@ func CreateCommand(commandService *service.CommandService) gin.HandlerFunc { return func(c *gin.Context) { var req service.CreateCommandRequest if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + if !middleware.BodyTooLarge(c, err) { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + } return } diff --git a/internal/handler/instance.go b/internal/handler/instance.go index 0153a962..9b4eb102 100644 --- a/internal/handler/instance.go +++ b/internal/handler/instance.go @@ -4,6 +4,7 @@ import ( "net/http" "strconv" + "github.com/e-spl/e-sp-line2/internal/middleware" "github.com/e-spl/e-sp-line2/internal/service" "github.com/gin-gonic/gin" ) @@ -46,7 +47,9 @@ func CreateInstance(instanceService *service.InstanceService) gin.HandlerFunc { return func(c *gin.Context) { var req service.CreateInstanceRequest if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + if !middleware.BodyTooLarge(c, err) { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + } return } @@ -70,7 +73,9 @@ func UpdateInstance(instanceService *service.InstanceService) gin.HandlerFunc { id := c.Param("id") var req service.UpdateInstanceRequest if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + if !middleware.BodyTooLarge(c, err) { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + } return } diff --git a/internal/handler/platform.go b/internal/handler/platform.go index f478a46e..ffd8bf95 100644 --- a/internal/handler/platform.go +++ b/internal/handler/platform.go @@ -4,6 +4,7 @@ import ( "net/http" "strconv" + "github.com/e-spl/e-sp-line2/internal/middleware" "github.com/e-spl/e-sp-line2/internal/service" "github.com/gin-gonic/gin" ) @@ -46,7 +47,9 @@ func CreatePlatform(platformService *service.PlatformService) gin.HandlerFunc { return func(c *gin.Context) { var req service.CreatePlatformRequest if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + if !middleware.BodyTooLarge(c, err) { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + } return } @@ -66,7 +69,9 @@ func UpdatePlatform(platformService *service.PlatformService) gin.HandlerFunc { id := c.Param("id") var req service.UpdatePlatformRequest if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + if !middleware.BodyTooLarge(c, err) { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + } return } diff --git a/internal/handler/route.go b/internal/handler/route.go index 72c38dd2..e00d55ff 100644 --- a/internal/handler/route.go +++ b/internal/handler/route.go @@ -4,6 +4,7 @@ import ( "net/http" "strconv" + "github.com/e-spl/e-sp-line2/internal/middleware" "github.com/e-spl/e-sp-line2/internal/service" "github.com/gin-gonic/gin" ) @@ -46,7 +47,9 @@ func CreateRoute(routeService *service.RouteService) gin.HandlerFunc { return func(c *gin.Context) { var req service.CreateRouteRequest if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + if !middleware.BodyTooLarge(c, err) { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + } return } @@ -66,7 +69,9 @@ func UpdateRoute(routeService *service.RouteService) gin.HandlerFunc { id := c.Param("id") var req service.UpdateRouteRequest if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + if !middleware.BodyTooLarge(c, err) { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + } return } diff --git a/internal/handler/tool.go b/internal/handler/tool.go index ae88f935..0f455f57 100644 --- a/internal/handler/tool.go +++ b/internal/handler/tool.go @@ -4,6 +4,7 @@ import ( "net/http" "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/gin-gonic/gin" ) @@ -44,7 +45,13 @@ func SetToolEnabled(gateway *adaptergateway.Gateway) gin.HandlerFunc { var body struct { Enabled *bool `json:"enabled"` } - if err := c.ShouldBindJSON(&body); err != nil || body.Enabled == nil { + if err := c.ShouldBindJSON(&body); err != nil { + if !middleware.BodyTooLarge(c, err) { + c.JSON(http.StatusBadRequest, gin.H{"error": "enabled is required"}) + } + return + } + if body.Enabled == nil { c.JSON(http.StatusBadRequest, gin.H{"error": "enabled is required"}) return } @@ -76,7 +83,9 @@ func CallTool(gateway *adaptergateway.Gateway) gin.HandlerFunc { var req v3.ToolCallRequest if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request body"}) + if !middleware.BodyTooLarge(c, err) { + c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request body"}) + } return } if req.Tool == "" && req.SemanticID == "" { diff --git a/internal/handler/user.go b/internal/handler/user.go index da21b2fb..3294df98 100644 --- a/internal/handler/user.go +++ b/internal/handler/user.go @@ -3,6 +3,7 @@ package handler import ( "net/http" + "github.com/e-spl/e-sp-line2/internal/middleware" "github.com/e-spl/e-sp-line2/internal/service" "github.com/gin-gonic/gin" ) @@ -28,7 +29,9 @@ func CreateUser(userService *service.UserService) gin.HandlerFunc { Role string `json:"role"` } if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + if !middleware.BodyTooLarge(c, err) { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + } return } if err := userService.Create(req.Username, req.Password, req.Role); err != nil { @@ -47,7 +50,9 @@ func UpdateUserRole(userService *service.UserService) gin.HandlerFunc { Role string `json:"role" binding:"required"` } if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + if !middleware.BodyTooLarge(c, err) { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + } return } if err := userService.UpdateRole(id, req.Role); err != nil { @@ -66,7 +71,9 @@ func UpdateUserStatus(userService *service.UserService) gin.HandlerFunc { Status string `json:"status" binding:"required"` } if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + if !middleware.BodyTooLarge(c, err) { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + } return } if err := userService.SetStatus(id, req.Status); err != nil { @@ -108,7 +115,9 @@ func SetRegistrationStatus(userService *service.UserService) gin.HandlerFunc { Enabled bool `json:"enabled"` } if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + if !middleware.BodyTooLarge(c, err) { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + } return } if err := userService.SetRegistrationEnabled(req.Enabled); err != nil { diff --git a/main.go b/main.go index 06a19cdf..b31a5df2 100644 --- a/main.go +++ b/main.go @@ -78,6 +78,13 @@ func main() { logger.Info("Starting E-SP-Line2 server...") logger.Infof("Server version: %s (build=%s, compiled=%s)", cfg.Version, version, buildTime) + // A deployment left on the shipped secret is signing tokens with a value + // that is in the repository. Shout about it, but still start: a WARN keeps + // `go run .` useful on a fresh clone, which is the common case. + if warning := config.JWTSecretWarning(cfg.JWT.Secret); warning != "" { + logger.Warn(warning) + } + // Create server srv, err := server.New(cfg) if err != nil { diff --git a/main_test.go b/main_test.go new file mode 100644 index 00000000..7b580521 --- /dev/null +++ b/main_test.go @@ -0,0 +1,101 @@ +package main + +import ( + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + "time" +) + +// TestStartupWarnsOnPlaceholderSecret is the M3 acceptance test in its +// strongest form: it starts the real binary with the shipped secret and asserts +// the WARN is actually printed. +// +// The unit tests in internal/config cover the predicate; this one covers the +// wiring — main has to call it after logger.Init, and a refactor that drops +// that call must not stay green. +func TestStartupWarnsOnPlaceholderSecret(t *testing.T) { + runStartup(t, "change-this-secret-in-production", "placeholder") +} + +// TestStartupIsSilentWithRealSecret is the control. Without it, a build that +// warns unconditionally would pass the test above. +func TestStartupIsSilentWithRealSecret(t *testing.T) { + runStartup(t, "a-real-deployment-secret", "") +} + +func runStartup(t *testing.T, secret, wantInLog string) { + t.Helper() + if testing.Short() { + t.Skip("builds and runs the binary; skipped under -short") + } + + bin := filepath.Join(t.TempDir(), "espl") + build := exec.Command("go", "build", "-o", bin, ".") + build.Env = os.Environ() + if out, err := build.CombinedOutput(); err != nil { + t.Fatalf("build: %v\n%s", err, out) + } + + dir := t.TempDir() + if err := os.MkdirAll(filepath.Join(dir, "config"), 0o755); err != nil { + t.Fatalf("mkdir config: %v", err) + } + cfg := "host: \"127.0.0.1\"\n" + + "port: 0\n" + + "database:\n driver: sqlite\n dsn: \"test.db\"\n" + + "log_level: info\n" + + "jwt:\n secret: \"" + secret + "\"\n expiration: 1\n" + + "adapter:\n adapters_dir: adapters\n python_bin: python3\n" + if err := os.WriteFile(filepath.Join(dir, "config", "config.yaml"), []byte(cfg), 0o600); err != nil { + t.Fatalf("write config: %v", err) + } + // Startup has a hard dependency on the form-options registry. + if err := os.WriteFile(filepath.Join(dir, "config", "form-options.yaml"), []byte("platform_codes: []\n"), 0o600); err != nil { + t.Fatalf("write form options: %v", err) + } + + cmd := exec.Command(bin) + cmd.Dir = dir + out := &strings.Builder{} + cmd.Stdout = out + cmd.Stderr = out + + if err := cmd.Start(); err != nil { + t.Fatalf("start: %v", err) + } + + // Give it long enough to get past logger.Init and either warn or bind. + deadline := time.Now().Add(20 * time.Second) + for time.Now().Before(deadline) { + if strings.Contains(out.String(), "Starting E-SP-Line2 server") { + break + } + time.Sleep(50 * time.Millisecond) + } + time.Sleep(500 * time.Millisecond) // let the warn line flush + + _ = cmd.Process.Kill() + _, _ = cmd.Process.Wait() + + log := out.String() + if !strings.Contains(log, "Starting E-SP-Line2 server") { + t.Fatalf("server did not reach startup; log:\n%s", log) + } + + if wantInLog == "placeholder" { + if !strings.Contains(log, "WARN") || !strings.Contains(log, "placeholder") { + t.Fatalf("no WARN about the placeholder secret; log:\n%s", log) + } + if !strings.Contains(log, "jwt.secret") { + t.Fatalf("warning does not name jwt.secret; log:\n%s", log) + } + return + } + + if strings.Contains(log, "placeholder") { + t.Fatalf("unexpected placeholder warning with a real secret; log:\n%s", log) + } +}