fix(security): harden gRPC stream WAF, MCP fs arg validation, and PAT server binding

- ServeRPC now applies real-IP + WAF interceptors to streaming RPCs
  (RequestTask, IOStream), not just unary calls; without them
  authHandler.check saw an empty real IP so brute-force BlockIP counters
  never keyed on a source and the WAF block table was bypassed at the
  stream entrypoint. Factored real-IP resolution into ctxWithRealIP shared
  by unary and stream paths.
- handleFsRead rejects negative offset/length; handleFsWrite validates
  if_match_sha256 is 64 hex chars before dispatch.
- createAPIToken always verifies each server_id exists (even for admins),
  so an admin cannot bind a PAT to a not-yet-created server id that a
  future server would auto-inherit.
- Add relay zero-length-chunk rejection regression test.

Co-authored-by: cloudcode <cloudcode@users.noreply.github.com>
This commit is contained in:
naiba
2026-05-31 05:51:32 +00:00
co-authored by cloudcode
parent 834ae25024
commit 4f79fd6c2b
5 changed files with 165 additions and 23 deletions
+12
View File
@@ -1,6 +1,7 @@
package controller
import (
"encoding/hex"
"encoding/json"
"errors"
"time"
@@ -144,6 +145,12 @@ func handleFsRead(c *gin.Context, raw json.RawMessage) (any, error) {
if args.Path == "" {
return nil, errMCPInvalidArgs("path required")
}
if args.Offset < 0 {
return nil, errMCPInvalidArgs("offset must be >= 0")
}
if args.Length < 0 {
return nil, errMCPInvalidArgs("length must be >= 0")
}
out, err := rpc.CallAgent(c.Request.Context(), args.ServerID, model.TaskTypeFsRead,
model.FsReadRequest{
Path: args.Path,
@@ -189,6 +196,11 @@ func handleFsWrite(c *gin.Context, raw json.RawMessage) (any, error) {
if args.Path == "" {
return nil, errMCPInvalidArgs("path required")
}
if args.IfMatchSHA256 != "" {
if _, decErr := hex.DecodeString(args.IfMatchSHA256); decErr != nil || len(args.IfMatchSHA256) != 64 {
return nil, errMCPInvalidArgs("if_match_sha256 must be 64 hex chars")
}
}
out, err := rpc.CallAgent(c.Request.Context(), args.ServerID, model.TaskTypeFsWrite,
model.FsWriteRequest{
Path: args.Path,