-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathmiddleware.go
More file actions
196 lines (171 loc) · 6.5 KB
/
middleware.go
File metadata and controls
196 lines (171 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc.
// SPDX-License-Identifier: Apache-2.0
package ratelimit
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"math"
"net/http"
"os"
"time"
"github.com/redis/go-redis/v9"
v1beta1 "github.com/stacklok/toolhive/cmd/thv-operator/api/v1beta1"
"github.com/stacklok/toolhive/pkg/auth"
"github.com/stacklok/toolhive/pkg/mcp"
"github.com/stacklok/toolhive/pkg/transport/types"
)
const (
// MiddlewareType is the type constant for the rate limit middleware.
MiddlewareType = "ratelimit"
// CodeRateLimited is the JSON-RPC error code for rate-limited requests.
// Per RFC THV-0057: implementation-defined code in the -32000 to -32099 range.
CodeRateLimited int64 = -32029
// MessageRateLimited is the JSON-RPC error message for rate-limited requests.
MessageRateLimited = "Rate limit exceeded"
// redisPasswordEnvVar is the environment variable containing the Redis password.
// Shared with session storage — the operator injects it from the same Secret.
redisPasswordEnvVar = "THV_SESSION_REDIS_PASSWORD" //nolint:gosec // G101: env var name, not a credential
)
// MiddlewareParams holds the parameters for the rate limit middleware factory.
type MiddlewareParams struct {
Namespace string `json:"namespace"`
ServerName string `json:"server_name"`
Config *v1beta1.RateLimitConfig `json:"config"`
RedisAddr string `json:"redis_addr,omitempty"`
RedisDB int32 `json:"redis_db,omitempty"`
}
// rateLimitMiddleware wraps rate limiting functionality for the factory pattern.
type rateLimitMiddleware struct {
handler types.MiddlewareFunction
client redis.UniversalClient
}
// ToolNameResolver resolves the rate-limit tool name from a parsed MCP request.
type ToolNameResolver func(*mcp.ParsedMCPRequest) string
// DefaultToolNameResolver uses the parsed MCP resource ID as the rate-limit tool name.
func DefaultToolNameResolver(parsed *mcp.ParsedMCPRequest) string {
if parsed == nil {
return ""
}
return parsed.ResourceID
}
// Handler returns the middleware function used by the proxy.
func (m *rateLimitMiddleware) Handler() types.MiddlewareFunction {
return m.handler
}
// Close cleans up the Redis client.
func (m *rateLimitMiddleware) Close() error {
if m.client != nil {
return m.client.Close()
}
return nil
}
// CreateMiddleware is the factory function for rate limit middleware.
func CreateMiddleware(config *types.MiddlewareConfig, runner types.MiddlewareRunner) error {
var params MiddlewareParams
if err := json.Unmarshal(config.Parameters, ¶ms); err != nil {
return fmt.Errorf("failed to unmarshal rate limit middleware parameters: %w", err)
}
if params.RedisAddr == "" {
return fmt.Errorf("rate limit middleware requires a Redis address")
}
// TODO: share a Redis client builder with session storage to get TLS,
// dial/read/write timeouts, and username support. For now, a basic client
// suffices since rate limiting and session storage target the same Redis.
client := redis.NewClient(&redis.Options{
Addr: params.RedisAddr,
DB: int(params.RedisDB),
Password: os.Getenv(redisPasswordEnvVar),
})
pingCtx, pingCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer pingCancel()
if err := client.Ping(pingCtx).Err(); err != nil {
_ = client.Close()
return fmt.Errorf("rate limit middleware: failed to connect to Redis at %s: %w", params.RedisAddr, err)
}
limiter, err := NewLimiter(client, params.Namespace, params.ServerName, params.Config)
if err != nil {
_ = client.Close()
return fmt.Errorf("failed to create rate limiter: %w", err)
}
mw := &rateLimitMiddleware{
handler: NewMiddleware(limiter, nil),
client: client,
}
runner.AddMiddleware(MiddlewareType, mw)
return nil
}
// NewMiddleware returns a middleware function that enforces rate limits
// on tools/call requests.
func NewMiddleware(limiter Limiter, resolveToolName ToolNameResolver) types.MiddlewareFunction {
if resolveToolName == nil {
resolveToolName = DefaultToolNameResolver
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Rate limits only apply to parsed tools/call requests.
// Non-JSON-RPC requests (health checks, SSE streams) have no
// parsed context and pass through unconditionally.
parsed := mcp.GetParsedMCPRequest(r.Context())
if parsed == nil || parsed.Method != "tools/call" {
next.ServeHTTP(w, r)
return
}
// When no identity is present (unauthenticated), userID stays empty
// and per-user buckets are skipped — only shared limits apply. CEL
// validation ensures perUser rate limits require auth to be enabled.
var userID string
if identity, ok := auth.IdentityFromContext(r.Context()); ok {
userID = identity.Subject
}
decision, err := limiter.Allow(r.Context(), resolveToolName(parsed), userID)
if err != nil {
slog.Warn("rate limit check failed, allowing request", "error", err)
next.ServeHTTP(w, r)
return
}
if !decision.Allowed {
writeRateLimited(w, parsed.ID, decision.RetryAfter)
return
}
next.ServeHTTP(w, r)
})
}
}
// rateLimitHandler returns the default rate-limit middleware used by tests and legacy callers.
func rateLimitHandler(limiter Limiter) types.MiddlewareFunction {
return NewMiddleware(limiter, nil)
}
// writeRateLimited writes an HTTP 429 response with a JSON-RPC error body.
func writeRateLimited(w http.ResponseWriter, requestID any, retryAfter time.Duration) {
retrySeconds := int(math.Ceil(retryAfter.Seconds()))
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Retry-After", fmt.Sprintf("%d", retrySeconds))
w.WriteHeader(http.StatusTooManyRequests)
//nolint:gosec // G104: writing a static JSON error response to an HTTP client
_, _ = w.Write(rateLimitedBody(requestID, retryAfter))
}
// rateLimitedBody returns the JSON-encoded body for a rate-limited JSON-RPC error.
func rateLimitedBody(requestID any, retryAfter time.Duration) []byte {
retrySeconds := math.Ceil(retryAfter.Seconds())
resp := map[string]any{
"jsonrpc": "2.0",
"error": map[string]any{
"code": CodeRateLimited,
"message": MessageRateLimited,
"data": map[string]any{
"retryAfterSeconds": retrySeconds,
},
},
"id": requestID,
}
data, err := json.Marshal(resp)
if err != nil {
return []byte(fmt.Sprintf(
`{"jsonrpc":"2.0","error":{"code":-32029,"message":"Rate limit exceeded","data":{"retryAfterSeconds":%.0f}},"id":null}`,
retrySeconds,
))
}
return data
}