@@ -27,8 +27,8 @@ const (
27
27
featurePolicyHeader = "Feature-Policy"
28
28
expectCTHeader = "Expect-CT"
29
29
30
- ctxSecureHeaderKey = secureCtxKey ("SecureResponseHeader" )
31
- cspNonceSize = 16
30
+ ctxDefaultSecureHeaderKey = secureCtxKey ("SecureResponseHeader" )
31
+ cspNonceSize = 16
32
32
)
33
33
34
34
// SSLHostFunc a type whose pointer is the type of field `SSLHostFunc` of `Options` struct
@@ -97,6 +97,8 @@ type Options struct {
97
97
STSSeconds int64
98
98
// ExpectCTHeader allows the Expect-CT header value to be set with a custom value. Default is "".
99
99
ExpectCTHeader string
100
+ // SecureContextKey allows a custom key to be specified for context storage.
101
+ SecureContextKey string
100
102
}
101
103
102
104
// Secure is a middleware that helps setup a few basic security features. A single secure.Options struct can be
@@ -111,6 +113,9 @@ type Secure struct {
111
113
// cRegexAllowedHosts saves the compiled regular expressions of the AllowedHosts
112
114
// option for subsequent use in processRequest
113
115
cRegexAllowedHosts []* regexp.Regexp
116
+
117
+ // ctxSecureHeaderKey is the key used for context storage for request modification.
118
+ ctxSecureHeaderKey secureCtxKey
114
119
}
115
120
116
121
// New constructs a new Secure instance with the supplied options.
@@ -143,6 +148,11 @@ func New(options ...Options) *Secure {
143
148
}
144
149
}
145
150
151
+ s .ctxSecureHeaderKey = ctxDefaultSecureHeaderKey
152
+ if len (s .opt .SecureContextKey ) > 0 {
153
+ s .ctxSecureHeaderKey = secureCtxKey (s .opt .SecureContextKey )
154
+ }
155
+
146
156
return s
147
157
}
148
158
@@ -182,7 +192,7 @@ func (s *Secure) HandlerForRequestOnly(h http.Handler) http.Handler {
182
192
}
183
193
184
194
// Save response headers in the request context.
185
- ctx := context .WithValue (r .Context (), ctxSecureHeaderKey , responseHeader )
195
+ ctx := context .WithValue (r .Context (), s . ctxSecureHeaderKey , responseHeader )
186
196
187
197
// No headers will be written to the ResponseWriter.
188
198
h .ServeHTTP (w , r .WithContext (ctx ))
@@ -212,7 +222,7 @@ func (s *Secure) HandlerFuncWithNextForRequestOnly(w http.ResponseWriter, r *htt
212
222
// If there was an error, do not call next.
213
223
if err == nil && next != nil {
214
224
// Save response headers in the request context
215
- ctx := context .WithValue (r .Context (), ctxSecureHeaderKey , responseHeader )
225
+ ctx := context .WithValue (r .Context (), s . ctxSecureHeaderKey , responseHeader )
216
226
217
227
// No headers will be written to the ResponseWriter.
218
228
next (w , r .WithContext (ctx ))
@@ -450,7 +460,7 @@ func (s *Secure) ModifyResponseHeaders(res *http.Response) error {
450
460
res .Header .Set ("Location" , location )
451
461
}
452
462
453
- responseHeader := res .Request .Context ().Value (ctxSecureHeaderKey )
463
+ responseHeader := res .Request .Context ().Value (s . ctxSecureHeaderKey )
454
464
if responseHeader != nil {
455
465
for header , values := range responseHeader .(http.Header ) {
456
466
if len (values ) > 0 {
0 commit comments