-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_security.go
More file actions
584 lines (518 loc) · 16.9 KB
/
api_security.go
File metadata and controls
584 lines (518 loc) · 16.9 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
package inspect
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
)
// APISecurityChecker performs security audits against REST API endpoints.
type APISecurityChecker struct {
BaseURL string
Headers map[string]string
Timeout time.Duration
}
// NewAPISecurityChecker creates an APISecurityChecker with sensible defaults.
func NewAPISecurityChecker(baseURL string) *APISecurityChecker {
return &APISecurityChecker{
BaseURL: strings.TrimRight(baseURL, "/"),
Headers: make(map[string]string),
Timeout: 10 * time.Second,
}
}
// JWTClaims represents decoded JWT claims for analysis.
type JWTClaims struct {
Header map[string]interface{}
Payload map[string]interface{}
}
// httpClient returns a configured HTTP client.
func (a *APISecurityChecker) httpClient() *http.Client {
return &http.Client{
Timeout: a.Timeout,
}
}
// newRequest creates a request with the configured headers.
func (a *APISecurityChecker) newRequest(method, url string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
for k, v := range a.Headers {
req.Header.Set(k, v)
}
return req, nil
}
// CheckCORS tests for CORS misconfiguration by sending requests with various
// Origin headers and checking if the server reflects arbitrary origins.
func (a *APISecurityChecker) CheckCORS(url string) []Finding {
var findings []Finding
client := a.httpClient()
testOrigins := []string{
"https://evil.com",
"https://attacker.example.org",
"null",
}
for _, origin := range testOrigins {
req, err := a.newRequest("GET", url, nil)
if err != nil {
continue
}
req.Header.Set("Origin", origin)
resp, err := client.Do(req)
if err != nil {
continue
}
resp.Body.Close()
acao := resp.Header.Get("Access-Control-Allow-Origin")
acac := resp.Header.Get("Access-Control-Allow-Credentials")
if acao == "*" && acac == "true" {
findings = append(findings, Finding{
Check: "api-cors",
Severity: SeverityCritical,
URL: url,
Message: "CORS allows all origins with credentials",
Evidence: fmt.Sprintf("Access-Control-Allow-Origin: %s, Access-Control-Allow-Credentials: %s", acao, acac),
Fix: "Restrict Access-Control-Allow-Origin to specific trusted domains and never combine wildcard with credentials",
})
break
}
if acao == origin && origin != "null" {
sev := SeverityMedium
if acac == "true" {
sev = SeverityHigh
}
findings = append(findings, Finding{
Check: "api-cors",
Severity: sev,
URL: url,
Message: fmt.Sprintf("CORS reflects arbitrary origin: %s", origin),
Evidence: fmt.Sprintf("Access-Control-Allow-Origin: %s", acao),
Fix: "Validate Origin against a whitelist of trusted domains",
})
}
if acao == "null" {
findings = append(findings, Finding{
Check: "api-cors",
Severity: SeverityMedium,
URL: url,
Message: "CORS allows null origin",
Evidence: "Access-Control-Allow-Origin: null",
Fix: "Do not allow null origin; use specific domain whitelist",
})
}
}
return findings
}
// CheckRateLimiting sends rapid requests to detect missing rate limiting.
func (a *APISecurityChecker) CheckRateLimiting(url string) []Finding {
var findings []Finding
client := a.httpClient()
const requestCount = 20
successCount := 0
var rateLimitHeaderFound bool
for i := 0; i < requestCount; i++ {
req, err := a.newRequest("GET", url, nil)
if err != nil {
continue
}
resp, err := client.Do(req)
if err != nil {
continue
}
resp.Body.Close()
if resp.StatusCode == http.StatusTooManyRequests {
rateLimitHeaderFound = true
break
}
// Check for rate limit headers
if resp.Header.Get("X-RateLimit-Limit") != "" ||
resp.Header.Get("X-Rate-Limit-Limit") != "" ||
resp.Header.Get("RateLimit-Limit") != "" ||
resp.Header.Get("Retry-After") != "" {
rateLimitHeaderFound = true
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
successCount++
}
}
if !rateLimitHeaderFound && successCount == requestCount {
findings = append(findings, Finding{
Check: "api-rate-limit",
Severity: SeverityMedium,
URL: url,
Message: fmt.Sprintf("No rate limiting detected after %d rapid requests", requestCount),
Evidence: fmt.Sprintf("All %d requests returned success with no rate limit headers", successCount),
Fix: "Implement rate limiting (e.g., X-RateLimit-Limit header and 429 responses)",
})
}
return findings
}
// CheckAuthHeaders checks for missing security headers on API responses.
func (a *APISecurityChecker) CheckAuthHeaders(url string) []Finding {
var findings []Finding
client := a.httpClient()
req, err := a.newRequest("GET", url, nil)
if err != nil {
return findings
}
resp, err := client.Do(req)
if err != nil {
return findings
}
resp.Body.Close()
requiredHeaders := []struct {
Name string
Severity Severity
Fix string
}{
{"X-Content-Type-Options", SeverityMedium, "Add header: X-Content-Type-Options: nosniff"},
{"Strict-Transport-Security", SeverityHigh, "Add header: Strict-Transport-Security: max-age=31536000; includeSubDomains"},
{"X-Frame-Options", SeverityMedium, "Add header: X-Frame-Options: DENY or SAMEORIGIN"},
{"Cache-Control", SeverityLow, "Add header: Cache-Control: no-store for sensitive API responses"},
{"X-Request-Id", SeverityInfo, "Add X-Request-Id header for request tracing and debugging"},
}
for _, h := range requiredHeaders {
if resp.Header.Get(h.Name) == "" {
findings = append(findings, Finding{
Check: "api-security-headers",
Severity: h.Severity,
URL: url,
Message: fmt.Sprintf("Missing security header: %s", h.Name),
Fix: h.Fix,
})
}
}
// Check for overly permissive cache headers on API responses
cacheControl := resp.Header.Get("Cache-Control")
if cacheControl != "" && !strings.Contains(cacheControl, "no-store") && !strings.Contains(cacheControl, "private") {
findings = append(findings, Finding{
Check: "api-security-headers",
Severity: SeverityLow,
URL: url,
Message: "API response may be cached publicly",
Evidence: fmt.Sprintf("Cache-Control: %s", cacheControl),
Fix: "Use Cache-Control: no-store or private for API responses with sensitive data",
})
}
return findings
}
// CheckVerbTampering tests unusual HTTP methods for unexpected access.
func (a *APISecurityChecker) CheckVerbTampering(url string) []Finding {
var findings []Finding
client := a.httpClient()
dangerousMethods := []struct {
Method string
Message string
}{
{"TRACE", "TRACE method enabled — may allow cross-site tracing (XST)"},
{"PUT", "PUT method accepted — may allow unauthorized resource modification"},
{"DELETE", "DELETE method accepted — may allow unauthorized resource deletion"},
{"PATCH", "PATCH method accepted — may allow unauthorized resource modification"},
}
for _, m := range dangerousMethods {
req, err := a.newRequest(m.Method, url, nil)
if err != nil {
continue
}
resp, err := client.Do(req)
if err != nil {
continue
}
body, _ := io.ReadAll(io.LimitReader(resp.Body, 1024))
resp.Body.Close()
// TRACE is especially dangerous if it echoes back the request
if m.Method == "TRACE" && resp.StatusCode == http.StatusOK {
findings = append(findings, Finding{
Check: "api-verb-tampering",
Severity: SeverityHigh,
URL: url,
Message: m.Message,
Evidence: fmt.Sprintf("Status: %d", resp.StatusCode),
Fix: "Disable TRACE method on the server",
})
continue
}
// For other methods, flag if they return success without auth
if m.Method != "TRACE" && resp.StatusCode >= 200 && resp.StatusCode < 300 {
findings = append(findings, Finding{
Check: "api-verb-tampering",
Severity: SeverityMedium,
URL: url,
Message: m.Message,
Evidence: fmt.Sprintf("Status: %d, Body: %s", resp.StatusCode, truncateEvidence(string(body), 80)),
Fix: fmt.Sprintf("Restrict %s method to authorized users or return 405 Method Not Allowed", m.Method),
})
}
}
// Check OPTIONS to see what methods are advertised
req, err := a.newRequest("OPTIONS", url, nil)
if err == nil {
resp, err := client.Do(req)
if err == nil {
resp.Body.Close()
allow := resp.Header.Get("Allow")
if allow == "" {
allow = resp.Header.Get("Access-Control-Allow-Methods")
}
if allow != "" {
upper := strings.ToUpper(allow)
if strings.Contains(upper, "TRACE") {
findings = append(findings, Finding{
Check: "api-verb-tampering",
Severity: SeverityMedium,
URL: url,
Message: "OPTIONS response advertises TRACE method",
Evidence: fmt.Sprintf("Allow: %s", allow),
Fix: "Remove TRACE from allowed methods",
})
}
}
}
}
return findings
}
// CheckErrorLeakage sends malformed requests and checks if error responses
// leak stack traces, internal paths, or version information.
func (a *APISecurityChecker) CheckErrorLeakage(url string) []Finding {
var findings []Finding
client := a.httpClient()
// Patterns that indicate information leakage
leakPatterns := []struct {
Pattern string
Message string
}{
{"at ", "Stack trace leaked in error response"},
{"goroutine ", "Go stack trace leaked in error response"},
{"Traceback", "Python traceback leaked in error response"},
{"Exception in", "Exception details leaked in error response"},
{"/usr/", "Internal file path leaked in error response"},
{"/home/", "Internal file path leaked in error response"},
{"/var/", "Internal file path leaked in error response"},
{"C:\\", "Windows file path leaked in error response"},
{"mysql", "Database information leaked in error response"},
{"postgres", "Database information leaked in error response"},
{"SQLSTATE", "SQL error leaked in error response"},
{"X-Powered-By", "Server technology leaked via header"},
}
// Send malformed requests to trigger errors
malformedRequests := []struct {
Method string
Path string
ContentType string
Body string
}{
{"GET", url + "/%00", "", ""},
{"POST", url, "application/json", "{invalid json"},
{"GET", url + "/../../../etc/passwd", "", ""},
{"GET", url + "?id=1'OR'1'='1", "", ""},
}
for _, mr := range malformedRequests {
var bodyReader io.Reader
if mr.Body != "" {
bodyReader = strings.NewReader(mr.Body)
}
req, err := a.newRequest(mr.Method, mr.Path, bodyReader)
if err != nil {
continue
}
if mr.ContentType != "" {
req.Header.Set("Content-Type", mr.ContentType)
}
resp, err := client.Do(req)
if err != nil {
continue
}
body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
resp.Body.Close()
bodyStr := string(body)
// Check response headers for leakage
if server := resp.Header.Get("Server"); server != "" {
// Check if it reveals detailed version info
if strings.ContainsAny(server, "0123456789.") && len(server) > 5 {
findings = append(findings, Finding{
Check: "api-error-leakage",
Severity: SeverityLow,
URL: mr.Path,
Message: "Server version information disclosed",
Evidence: fmt.Sprintf("Server: %s", server),
Fix: "Remove or genericize the Server header",
})
}
}
if xpb := resp.Header.Get("X-Powered-By"); xpb != "" {
findings = append(findings, Finding{
Check: "api-error-leakage",
Severity: SeverityLow,
URL: mr.Path,
Message: "X-Powered-By header discloses technology stack",
Evidence: fmt.Sprintf("X-Powered-By: %s", xpb),
Fix: "Remove the X-Powered-By header",
})
}
// Check body for leakage patterns
for _, lp := range leakPatterns {
if strings.Contains(bodyStr, lp.Pattern) {
findings = append(findings, Finding{
Check: "api-error-leakage",
Severity: SeverityMedium,
URL: mr.Path,
Message: lp.Message,
Evidence: truncateEvidence(bodyStr, 120),
Fix: "Return generic error messages in production; log details server-side only",
})
break // One finding per request is enough
}
}
}
return findings
}
// CheckJWTWeakness analyzes a JWT token structure for common weaknesses:
// none algorithm, weak signing, missing expiry, excessive claims.
func (a *APISecurityChecker) CheckJWTWeakness(token string) []Finding {
var findings []Finding
claims, err := ParseJWT(token)
if err != nil {
findings = append(findings, Finding{
Check: "api-jwt",
Severity: SeverityInfo,
URL: a.BaseURL,
Message: fmt.Sprintf("Could not parse JWT: %s", err.Error()),
})
return findings
}
// Check for "none" algorithm
if alg, ok := claims.Header["alg"]; ok {
algStr, _ := alg.(string)
algLower := strings.ToLower(algStr)
if algLower == "none" || algLower == "nonce" {
findings = append(findings, Finding{
Check: "api-jwt",
Severity: SeverityCritical,
URL: a.BaseURL,
Message: "JWT uses 'none' algorithm — signature verification bypassed",
Evidence: fmt.Sprintf("alg: %s", algStr),
Fix: "Never accept tokens with alg=none; enforce RS256 or ES256",
})
}
// Check for weak algorithms
if algLower == "hs256" {
findings = append(findings, Finding{
Check: "api-jwt",
Severity: SeverityLow,
URL: a.BaseURL,
Message: "JWT uses HS256 (symmetric) algorithm — consider asymmetric signing",
Evidence: fmt.Sprintf("alg: %s", algStr),
Fix: "Use RS256 or ES256 for better key management and rotation",
})
}
} else {
findings = append(findings, Finding{
Check: "api-jwt",
Severity: SeverityHigh,
URL: a.BaseURL,
Message: "JWT header missing 'alg' field",
Fix: "Include algorithm specification in JWT header",
})
}
// Check for missing expiry
if _, ok := claims.Payload["exp"]; !ok {
findings = append(findings, Finding{
Check: "api-jwt",
Severity: SeverityHigh,
URL: a.BaseURL,
Message: "JWT missing expiration claim (exp)",
Fix: "Always include 'exp' claim with a reasonable TTL",
})
} else {
// Check if expiry is excessively long (> 24 hours from token perspective)
if exp, ok := claims.Payload["exp"].(float64); ok {
if iat, ok := claims.Payload["iat"].(float64); ok {
duration := time.Duration(exp-iat) * time.Second
if duration > 24*time.Hour {
findings = append(findings, Finding{
Check: "api-jwt",
Severity: SeverityLow,
URL: a.BaseURL,
Message: fmt.Sprintf("JWT has long expiry: %s", duration.String()),
Fix: "Use short-lived tokens (15min-1hr) with refresh tokens for better security",
})
}
}
}
}
// Check for missing issuer
if _, ok := claims.Payload["iss"]; !ok {
findings = append(findings, Finding{
Check: "api-jwt",
Severity: SeverityLow,
URL: a.BaseURL,
Message: "JWT missing issuer claim (iss)",
Fix: "Include 'iss' claim and validate it on the server",
})
}
// Check for excessive claims (potential data exposure)
if len(claims.Payload) > 10 {
findings = append(findings, Finding{
Check: "api-jwt",
Severity: SeverityLow,
URL: a.BaseURL,
Message: fmt.Sprintf("JWT contains %d claims — possible excessive data exposure", len(claims.Payload)),
Fix: "Minimize JWT payload; store detailed data server-side and reference by ID",
})
}
// Check for sensitive data in claims
sensitiveKeys := []string{"password", "secret", "ssn", "credit_card", "cc_number"}
for _, key := range sensitiveKeys {
if _, ok := claims.Payload[key]; ok {
findings = append(findings, Finding{
Check: "api-jwt",
Severity: SeverityCritical,
URL: a.BaseURL,
Message: fmt.Sprintf("JWT contains sensitive data in claim: %s", key),
Fix: "Never store secrets or sensitive PII in JWT claims — they are base64 encoded, not encrypted",
})
}
}
return findings
}
// ParseJWT decodes a JWT token without signature validation (for analysis purposes).
func ParseJWT(token string) (*JWTClaims, error) {
parts := strings.Split(token, ".")
if len(parts) < 2 || len(parts) > 3 {
return nil, fmt.Errorf("invalid JWT format: expected 2-3 parts, got %d", len(parts))
}
header, err := decodeJWTSegment(parts[0])
if err != nil {
return nil, fmt.Errorf("invalid JWT header: %w", err)
}
payload, err := decodeJWTSegment(parts[1])
if err != nil {
return nil, fmt.Errorf("invalid JWT payload: %w", err)
}
return &JWTClaims{Header: header, Payload: payload}, nil
}
// decodeJWTSegment decodes a base64url-encoded JWT segment into a map.
func decodeJWTSegment(segment string) (map[string]interface{}, error) {
// Add padding if needed
switch len(segment) % 4 {
case 2:
segment += "=="
case 3:
segment += "="
}
data, err := base64.URLEncoding.DecodeString(segment)
if err != nil {
// Try standard encoding as fallback
data, err = base64.StdEncoding.DecodeString(segment)
if err != nil {
return nil, err
}
}
var result map[string]interface{}
if err := json.Unmarshal(data, &result); err != nil {
return nil, err
}
return result, nil
}