-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsources.go
More file actions
172 lines (148 loc) · 4.52 KB
/
sources.go
File metadata and controls
172 lines (148 loc) · 4.52 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
package clientip
import (
"context"
"errors"
"net/http"
"net/netip"
)
const (
// SourceForwarded resolves from the RFC7239 Forwarded header.
SourceForwarded = "forwarded"
// SourceXForwardedFor resolves from the X-Forwarded-For header.
SourceXForwardedFor = "x_forwarded_for"
// SourceXRealIP resolves from the X-Real-IP header.
SourceXRealIP = "x_real_ip"
// SourceRemoteAddr resolves from Request.RemoteAddr.
SourceRemoteAddr = "remote_addr"
)
type extractionResult struct {
IP netip.Addr
TrustedProxyCount int
DebugInfo *ChainDebugInfo
Source string
}
type sourceExtractor interface {
Extract(ctx context.Context, r *http.Request) (extractionResult, error)
Name() string
}
func requestPath(r *http.Request) string {
if r == nil || r.URL == nil {
return ""
}
return r.URL.Path
}
func (e *Extractor) logSecurityWarning(ctx context.Context, r *http.Request, sourceName, event, msg string, attrs ...any) {
remoteAddr := ""
if r != nil {
remoteAddr = r.RemoteAddr
}
baseAttrs := []any{
"event", event,
"source", sourceName,
"path", requestPath(r),
"remote_addr", remoteAddr,
}
baseAttrs = append(baseAttrs, attrs...)
e.config.logger.WarnContext(ctx, msg, baseAttrs...)
}
func proxyValidationWarningDetails(err error) (event, msg string, ok bool) {
switch {
case errors.Is(err, ErrNoTrustedProxies):
return securityEventNoTrustedProxies, "no trusted proxies found in request chain", true
case errors.Is(err, ErrTooFewTrustedProxies):
return securityEventTooFewTrustedProxies, "trusted proxy count below configured minimum", true
case errors.Is(err, ErrTooManyTrustedProxies):
return securityEventTooManyTrustedProxies, "trusted proxy count exceeds configured maximum", true
default:
return "", "", false
}
}
func (e *Extractor) logProxyValidationWarning(ctx context.Context, r *http.Request, sourceName string, err error) {
event, msg, ok := proxyValidationWarningDetails(err)
if !ok {
return
}
var proxyErr *ProxyValidationError
if errors.As(err, &proxyErr) {
e.logSecurityWarning(ctx, r, sourceName, event, msg,
"trusted_proxy_count", proxyErr.TrustedProxyCount,
"min_trusted_proxies", proxyErr.MinTrustedProxies,
"max_trusted_proxies", proxyErr.MaxTrustedProxies,
)
return
}
e.logSecurityWarning(ctx, r, sourceName, event, msg)
}
func (e *Extractor) extractChainSource(
ctx context.Context,
r *http.Request,
sourceName string,
headerValues []string,
chainForUntrusted func() string,
untrustedProxyMessage string,
chainTooLongMessage string,
parseValues func([]string) ([]string, error),
handleParseError func(error),
) (extractionResult, error) {
if len(e.config.trustedProxyCIDRs) > 0 {
remoteAddr := ""
if r != nil {
remoteAddr = r.RemoteAddr
}
remoteIP := parseRemoteAddr(remoteAddr)
if !e.isTrustedProxy(remoteIP) {
chain := ""
if chainForUntrusted != nil {
chain = chainForUntrusted()
}
e.config.metrics.RecordSecurityEvent(securityEventUntrustedProxy)
e.logSecurityWarning(ctx, r, sourceName, securityEventUntrustedProxy, untrustedProxyMessage)
e.config.metrics.RecordExtractionFailure(sourceName)
return extractionResult{}, &ProxyValidationError{
ExtractionError: ExtractionError{
Err: ErrUntrustedProxy,
Source: sourceName,
},
Chain: chain,
TrustedProxyCount: 0,
MinTrustedProxies: e.config.minTrustedProxies,
MaxTrustedProxies: e.config.maxTrustedProxies,
}
}
}
parts, err := parseValues(headerValues)
if err != nil {
if errors.Is(err, ErrChainTooLong) {
var chainErr *ChainTooLongError
if errors.As(err, &chainErr) {
e.logSecurityWarning(ctx, r, sourceName, securityEventChainTooLong, chainTooLongMessage,
"chain_length", chainErr.ChainLength,
"max_length", chainErr.MaxLength,
)
} else {
e.logSecurityWarning(ctx, r, sourceName, securityEventChainTooLong, chainTooLongMessage)
}
}
if handleParseError != nil {
handleParseError(err)
}
e.config.metrics.RecordExtractionFailure(sourceName)
return extractionResult{}, err
}
ip, trustedCount, debugInfo, err := e.clientIPFromChainWithDebug(sourceName, parts)
if err != nil {
e.logProxyValidationWarning(ctx, r, sourceName, err)
e.config.metrics.RecordExtractionFailure(sourceName)
return extractionResult{}, err
}
e.config.metrics.RecordExtractionSuccess(sourceName)
result := extractionResult{
IP: ip,
TrustedProxyCount: trustedCount,
Source: sourceName,
}
if e.config.debugMode {
result.DebugInfo = debugInfo
}
return result, nil
}