-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.go
More file actions
391 lines (345 loc) · 11 KB
/
Copy pathplugin.go
File metadata and controls
391 lines (345 loc) · 11 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package traefik_api_key_auth
import (
"context"
"crypto/subtle"
"encoding/json"
"fmt"
"net/http"
"os"
"path"
"strings"
)
const maxCredentialLength = 4096
type Config struct {
AuthenticationHeader bool `json:"authenticationHeader,omitempty"`
AuthenticationHeaderName string `json:"authenticationHeaderName,omitempty"`
BearerHeader bool `json:"bearerHeader,omitempty"`
BearerHeaderName string `json:"bearerHeaderName,omitempty"`
QueryParam bool `json:"queryParam,omitempty"`
QueryParamName string `json:"queryParamName,omitempty"`
PathSegment bool `json:"pathSegment,omitempty"`
PermissiveMode bool `json:"permissiveMode,omitempty"`
Keys []string `json:"keys,omitempty"`
RemoveHeadersOnSuccess bool `json:"removeHeadersOnSuccess,omitempty"`
InternalForwardHeaderName string `json:"internalForwardHeaderName,omitempty"`
InternalErrorRoute string `json:"internalErrorRoute,omitempty"`
ExemptPaths []string `json:"exemptPaths,omitempty"`
}
type Response struct {
Message string `json:"message"`
StatusCode int `json:"status_code"`
}
type keyMaterial struct {
raw string
bytes []byte
}
func CreateConfig() *Config {
return &Config{
AuthenticationHeader: true,
AuthenticationHeaderName: "X-API-KEY",
BearerHeader: true,
BearerHeaderName: "Authorization",
QueryParam: false,
QueryParamName: "token",
PathSegment: false,
PermissiveMode: false,
Keys: make([]string, 0),
RemoveHeadersOnSuccess: true,
InternalForwardHeaderName: "",
InternalErrorRoute: "",
ExemptPaths: nil,
}
}
type KeyAuth struct {
next http.Handler
authenticationHeader bool
authenticationHeaderName string
bearerHeader bool
bearerHeaderName string
queryParam bool
queryParamName string
pathSegment bool
permissiveMode bool
keysByLength map[int][]keyMaterial
removeHeadersOnSuccess bool
internalForwardHeaderName string
internalErrorRoute string
exemptPaths []string
}
// resolveKeys expands config keys: entries "env:VAR_NAME" are replaced by the value of the environment variable.
// Returns the final list of keys and an error if no keys remain or when an env key is missing.
func resolveKeys(rawKeys []string) ([]string, error) {
seen := make(map[string]struct{}, len(rawKeys))
keys := make([]string, 0, len(rawKeys))
for _, k := range rawKeys {
k = strings.TrimSpace(k)
if k == "" {
continue
}
if strings.HasPrefix(k, "env:") {
envVar := strings.TrimSpace(strings.TrimPrefix(k, "env:"))
if envVar == "" {
return nil, fmt.Errorf("invalid env key reference: %q", k)
}
v := strings.TrimSpace(os.Getenv(envVar))
if v == "" {
return nil, fmt.Errorf("environment variable %q is empty or missing", envVar)
}
k = v
}
if len(k) > maxCredentialLength {
return nil, fmt.Errorf("configured key exceeds maximum length (%d)", maxCredentialLength)
}
if _, exists := seen[k]; exists {
continue
}
seen[k] = struct{}{}
keys = append(keys, k)
}
if len(keys) == 0 {
return nil, fmt.Errorf("must specify at least one valid key (or use env:VAR_NAME)")
}
return keys, nil
}
func buildKeyIndex(keys []string) map[int][]keyMaterial {
index := make(map[int][]keyMaterial, len(keys))
for _, k := range keys {
m := keyMaterial{raw: k, bytes: []byte(k)}
index[len(k)] = append(index[len(k)], m)
}
return index
}
func normalizeRoutePrefix(value string) string {
v := strings.TrimSpace(value)
if v == "" {
return ""
}
if !strings.HasPrefix(v, "/") {
v = "/" + v
}
clean := path.Clean(v)
if clean == "." {
return "/"
}
return clean
}
func normalizeExemptPaths(raw []string) []string {
if len(raw) == 0 {
return nil
}
seen := make(map[string]struct{}, len(raw))
normalized := make([]string, 0, len(raw))
for _, p := range raw {
n := normalizeRoutePrefix(p)
if n == "" {
continue
}
if n != "/" {
n = strings.TrimSuffix(n, "/")
}
if _, exists := seen[n]; exists {
continue
}
seen[n] = struct{}{}
normalized = append(normalized, n)
}
return normalized
}
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
_ = ctx
if config == nil {
return nil, fmt.Errorf("config cannot be nil")
}
// Do not log config to avoid leaking keys
_, _ = os.Stdout.WriteString("traefik_api_key_auth: creating plugin " + name + "\n")
resolvedKeys, err := resolveKeys(config.Keys)
if err != nil {
return nil, err
}
if !config.AuthenticationHeader && !config.BearerHeader && !config.QueryParam && !config.PathSegment {
return nil, fmt.Errorf("at least one method must be true")
}
authHeaderName := strings.TrimSpace(config.AuthenticationHeaderName)
if config.AuthenticationHeader && authHeaderName == "" {
return nil, fmt.Errorf("authenticationHeaderName cannot be empty when authenticationHeader is enabled")
}
bearerHeaderName := strings.TrimSpace(config.BearerHeaderName)
if config.BearerHeader && bearerHeaderName == "" {
return nil, fmt.Errorf("bearerHeaderName cannot be empty when bearerHeader is enabled")
}
queryParamName := strings.TrimSpace(config.QueryParamName)
if config.QueryParam && queryParamName == "" {
return nil, fmt.Errorf("queryParamName cannot be empty when queryParam is enabled")
}
internalErrorRoute := normalizeRoutePrefix(config.InternalErrorRoute)
return &KeyAuth{
next: next,
authenticationHeader: config.AuthenticationHeader,
authenticationHeaderName: authHeaderName,
bearerHeader: config.BearerHeader,
bearerHeaderName: bearerHeaderName,
queryParam: config.QueryParam,
queryParamName: queryParamName,
pathSegment: config.PathSegment,
permissiveMode: config.PermissiveMode,
keysByLength: buildKeyIndex(resolvedKeys),
removeHeadersOnSuccess: config.RemoveHeadersOnSuccess,
internalForwardHeaderName: strings.TrimSpace(config.InternalForwardHeaderName),
internalErrorRoute: internalErrorRoute,
exemptPaths: normalizeExemptPaths(config.ExemptPaths),
}, nil
}
// findMatchingKey returns the matching valid key if the provided key matches any of them using constant-time comparison.
// Empty provided key never matches. Used to reduce timing attacks.
func findMatchingKey(provided string, keysByLength map[int][]keyMaterial) string {
provided = strings.TrimSpace(provided)
if provided == "" || len(provided) > maxCredentialLength {
return ""
}
candidates := keysByLength[len(provided)]
if len(candidates) == 0 {
return ""
}
providedB := []byte(provided)
for _, valid := range candidates {
if subtle.ConstantTimeCompare(providedB, valid.bytes) == 1 {
return valid.raw
}
}
return ""
}
// extractBearerToken returns the token from "Authorization: Bearer <token>" or empty string if not in that form.
func extractBearerToken(headerValue string) string {
headerValue = strings.TrimSpace(headerValue)
if headerValue == "" {
return ""
}
scheme, token, found := strings.Cut(headerValue, " ")
if !found || !strings.EqualFold(strings.TrimSpace(scheme), "Bearer") {
return ""
}
token = strings.TrimSpace(token)
if token == "" || strings.ContainsAny(token, " \t") {
return ""
}
if len(token) > maxCredentialLength {
return ""
}
return token
}
// pathSegmentMatchesKey returns the matching key if any path segment (between slashes) exactly matches a valid key.
// Uses constant-time comparison; avoids substring matching for security.
func pathSegmentMatchesKey(pathValue string, keysByLength map[int][]keyMaterial) string {
start := -1
for i := 0; i <= len(pathValue); i++ {
if i == len(pathValue) || pathValue[i] == '/' {
if start >= 0 && i > start {
if matched := findMatchingKey(pathValue[start:i], keysByLength); matched != "" {
return matched
}
}
start = -1
continue
}
if start == -1 {
start = i
}
}
return ""
}
func requestPathForLog(req *http.Request) string {
if req == nil || req.URL == nil {
return "/"
}
p := req.URL.EscapedPath()
if p == "" {
return "/"
}
return p
}
func (ka *KeyAuth) ok(rw http.ResponseWriter, req *http.Request, matchedKey string) {
_, _ = os.Stdout.WriteString("traefik_api_key_auth: valid credentials for path " + requestPathForLog(req) + "\n")
if ka.internalForwardHeaderName != "" {
req.Header.Del(ka.internalForwardHeaderName)
req.Header.Set(ka.internalForwardHeaderName, matchedKey)
}
req.RequestURI = req.URL.RequestURI()
ka.next.ServeHTTP(rw, req)
}
func (ka *KeyAuth) permissiveOk(rw http.ResponseWriter, req *http.Request) {
_, _ = os.Stderr.WriteString("traefik_api_key_auth: no valid credentials for path \"" + requestPathForLog(req) + "\"; allowing in permissive mode\n")
req.RequestURI = req.URL.RequestURI()
ka.next.ServeHTTP(rw, req)
}
func (ka *KeyAuth) isExempt(pathValue string) bool {
for _, prefix := range ka.exemptPaths {
if prefix == "/" {
return true
}
if pathValue == prefix || strings.HasPrefix(pathValue, prefix+"/") {
return true
}
}
return false
}
func (ka *KeyAuth) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
pathValue := req.URL.Path
if ka.isExempt(pathValue) {
req.RequestURI = req.URL.RequestURI()
ka.next.ServeHTTP(rw, req)
return
}
if ka.authenticationHeader {
provided := req.Header.Get(ka.authenticationHeaderName)
if matched := findMatchingKey(provided, ka.keysByLength); matched != "" {
if ka.removeHeadersOnSuccess {
req.Header.Del(ka.authenticationHeaderName)
}
ka.ok(rw, req, matched)
return
}
}
if ka.bearerHeader {
token := extractBearerToken(req.Header.Get(ka.bearerHeaderName))
if matched := findMatchingKey(token, ka.keysByLength); matched != "" {
if ka.removeHeadersOnSuccess {
req.Header.Del(ka.bearerHeaderName)
}
ka.ok(rw, req, matched)
return
}
}
if ka.queryParam {
qs := req.URL.Query()
provided := qs.Get(ka.queryParamName)
if matched := findMatchingKey(provided, ka.keysByLength); matched != "" {
qs.Del(ka.queryParamName)
req.URL.RawQuery = qs.Encode()
ka.ok(rw, req, matched)
return
}
}
if ka.pathSegment {
if matched := pathSegmentMatchesKey(pathValue, ka.keysByLength); matched != "" {
ka.ok(rw, req, matched)
return
}
}
if ka.permissiveMode {
ka.permissiveOk(rw, req)
return
}
if ka.internalErrorRoute != "" {
req.URL.Path = ka.internalErrorRoute
req.URL.RawQuery = ""
req.RequestURI = req.URL.RequestURI()
ka.next.ServeHTTP(rw, req)
return
}
rw.Header().Set("Content-Type", "application/json; charset=utf-8")
rw.WriteHeader(http.StatusForbidden)
response := Response{Message: "Invalid or missing API Key", StatusCode: http.StatusForbidden}
if err := json.NewEncoder(rw).Encode(response); err != nil {
_, _ = os.Stderr.WriteString("traefik_api_key_auth: failed to write response: " + err.Error() + "\n")
}
}