Debian Codegraph @

debian/patches/CVE-2025-24358.patch

raw
1 From: Patrick O'Doherty <[email protected]>
2 Date: Thu, 23 Jan 2025 12:14:50 -0800
3 Subject: Merge commit from fork
4
5 * csrf: use context to determine TLS state
6
7 r.URL.Scheme is never populated for "server" requests, and so the
8 referer check never runs.
9
10 Instead we now ask the caller application to signal this explicitly via
11 request conext, and then enforce the check accordingly.
12
13 Separately, browsers do not always send the full URL as a Referer,
14 especially in the same-origin context meaning we cannot compare its host
15 against our trusted origin list. If the referer does not contain a host
16 we populate r.URL.Host with r.Host which is expected to be sent by all
17 clients as the first header of their request.
18
19 Add tests against the Origin header before attempting to enforce
20 same-origin restrictions using the Referer header.
21
22 Matching the Django CSRF behavior: if the Origin is present in either
23 the cleartext or TLS case we will evaluate it.
24
25 IFF we are in TLS and we have no Origin we will evaluate the Referer
26 against the allowlist. In doing so we take care to permit "path only"
27 Referers that are sent in same-origin context.
28
29 * add csrf.TLSRequest helper API to set request TLS context
30
31 Add a csrf.TLSRequest public API method that sets the appropriate TLS
32 context key and signals to the midldeware the need to run the
33 additiontal Referer checks.
34
35 * Enable Referer-based origin checks by default
36
37 Reverse the default position and presume that that the server is using
38 TLS either directly or via an upstream proxy and require the user to
39 explicitly disable referer-based checks.
40
41 This safe default means that users that upgrade the library without
42 making any other code changes will benefit from the Referer checks that
43 they thought were active already. Without this change we risk that some
44 codebases will mistakenly remain vulnerable even while using a patched
45 version of the library.
46
47 Origin: upstream, https://github.com/gorilla/csrf/commit/9dd6af1f6d30fc79fb0d972394deebdabad6b5eb
48
49 diff --git a/csrf.go b/csrf.go
50 index 97a3925..5dda254 100644
51 --- a/csrf.go
52 +++ b/csrf.go
53 @@ -1,10 +1,12 @@
54 package csrf
55
56 import (
57 + "context"
58 "errors"
59 "fmt"
60 "net/http"
61 "net/url"
62 + "slices"
63
64 "github.com/gorilla/securecookie"
65 )
66 @@ -22,6 +24,14 @@ const (
67 errorPrefix string = "gorilla/csrf: "
68 )
69
70 +type contextKey string
71 +
72 +// PlaintextHTTPContextKey is the context key used to store whether the request
73 +// is being served via plaintext HTTP. This is used to signal to the middleware
74 +// that strict Referer checking should not be enforced as is done for HTTPS by
75 +// default.
76 +const PlaintextHTTPContextKey contextKey = "plaintext"
77 +
78 var (
79 // The name value used in form fields.
80 fieldName = tokenKey
81 @@ -41,6 +51,9 @@ var (
82 // ErrNoReferer is returned when a HTTPS request provides an empty Referer
83 // header.
84 ErrNoReferer = errors.New("referer not supplied")
85 + // ErrBadOrigin is returned when the Origin header is present and is not a
86 + // trusted origin.
87 + ErrBadOrigin = errors.New("origin invalid")
88 // ErrBadReferer is returned when the scheme & host in the URL do not match
89 // the supplied Referer header.
90 ErrBadReferer = errors.New("referer invalid")
91 @@ -242,10 +255,50 @@ func (cs *csrf) ServeHTTP(w http.ResponseWriter, r *http.Request) {
92 // HTTP methods not defined as idempotent ("safe") under RFC7231 require
93 // inspection.
94 if !contains(safeMethods, r.Method) {
95 - // Enforce an origin check for HTTPS connections. As per the Django CSRF
96 - // implementation (https://goo.gl/vKA7GE) the Referer header is almost
97 - // always present for same-domain HTTP requests.
98 - if r.URL.Scheme == "https" {
99 + var isPlaintext bool
100 + val := r.Context().Value(PlaintextHTTPContextKey)
101 + if val != nil {
102 + isPlaintext, _ = val.(bool)
103 + }
104 +
105 + // take a copy of the request URL to avoid mutating the original
106 + // attached to the request.
107 + // set the scheme & host based on the request context as these are not
108 + // populated by default for server requests
109 + // ref: https://pkg.go.dev/net/http#Request
110 + requestURL := *r.URL // shallow clone
111 +
112 + requestURL.Scheme = "https"
113 + if isPlaintext {
114 + requestURL.Scheme = "http"
115 + }
116 + if requestURL.Host == "" {
117 + requestURL.Host = r.Host
118 + }
119 +
120 + // if we have an Origin header, check it against our allowlist
121 + origin := r.Header.Get("Origin")
122 + if origin != "" {
123 + parsedOrigin, err := url.Parse(origin)
124 + if err != nil {
125 + r = envError(r, ErrBadOrigin)
126 + cs.opts.ErrorHandler.ServeHTTP(w, r)
127 + return
128 + }
129 + if !sameOrigin(&requestURL, parsedOrigin) && !slices.Contains(cs.opts.TrustedOrigins, parsedOrigin.Host) {
130 + r = envError(r, ErrBadOrigin)
131 + cs.opts.ErrorHandler.ServeHTTP(w, r)
132 + return
133 + }
134 + }
135 +
136 + // If we are serving via TLS and have no Origin header, prevent against
137 + // CSRF via HTTP machine in the middle attacks by enforcing strict
138 + // Referer origin checks. Consider an attacker who performs a
139 + // successful HTTP Machine-in-the-Middle attack and uses this to inject
140 + // a form and cause submission to our origin. We strictly disallow
141 + // cleartext HTTP origins and evaluate the domain against an allowlist.
142 + if origin == "" && !isPlaintext {
143 // Fetch the Referer value. Call the error handler if it's empty or
144 // otherwise fails to parse.
145 referer, err := url.Parse(r.Referer())
146 @@ -255,18 +308,17 @@ func (cs *csrf) ServeHTTP(w http.ResponseWriter, r *http.Request) {
147 return
148 }
149
150 - valid := sameOrigin(r.URL, referer)
151 -
152 - if !valid {
153 - for _, trustedOrigin := range cs.opts.TrustedOrigins {
154 - if referer.Host == trustedOrigin {
155 - valid = true
156 - break
157 - }
158 - }
159 + // disallow cleartext HTTP referers when serving via TLS
160 + if referer.Scheme == "http" {
161 + r = envError(r, ErrBadReferer)
162 + cs.opts.ErrorHandler.ServeHTTP(w, r)
163 + return
164 }
165
166 - if !valid {
167 + // If the request is being served via TLS and the Referer is not the
168 + // same origin, check the domain against our allowlist. We only
169 + // check when we have host information from the referer.
170 + if referer.Host != "" && referer.Host != r.Host && !slices.Contains(cs.opts.TrustedOrigins, referer.Host) {
171 r = envError(r, ErrBadReferer)
172 cs.opts.ErrorHandler.ServeHTTP(w, r)
173 return
174 @@ -308,6 +360,15 @@ func (cs *csrf) ServeHTTP(w http.ResponseWriter, r *http.Request) {
175 contextClear(r)
176 }
177
178 +// PlaintextHTTPRequest accepts as input a http.Request and returns a new
179 +// http.Request with the PlaintextHTTPContextKey set to true. This is used to
180 +// signal to the CSRF middleware that the request is being served over plaintext
181 +// HTTP and that Referer-based origin allow-listing checks should be skipped.
182 +func PlaintextHTTPRequest(r *http.Request) *http.Request {
183 + ctx := context.WithValue(r.Context(), PlaintextHTTPContextKey, true)
184 + return r.WithContext(ctx)
185 +}
186 +
187 // unauthorizedhandler sets a HTTP 403 Forbidden status and writes the
188 // CSRF failure reason to the response.
189 func unauthorizedHandler(w http.ResponseWriter, r *http.Request) {
190 diff --git a/csrf_test.go b/csrf_test.go
191 index 0559a59..0281680 100644
192 --- a/csrf_test.go
193 +++ b/csrf_test.go
194 @@ -1,6 +1,7 @@
195 package csrf
196
197 import (
198 + "fmt"
199 "net/http"
200 "net/http/httptest"
201 "strings"
202 @@ -16,10 +17,7 @@ func TestProtect(t *testing.T) {
203 s := http.NewServeMux()
204 s.HandleFunc("/", testHandler)
205
206 - r, err := http.NewRequest("GET", "/", nil)
207 - if err != nil {
208 - t.Fatal(err)
209 - }
210 + r := createRequest("GET", "/", false)
211
212 rr := httptest.NewRecorder()
213 p := Protect(testKey)(s)
214 @@ -46,10 +44,7 @@ func TestCookieOptions(t *testing.T) {
215 s := http.NewServeMux()
216 s.HandleFunc("/", testHandler)
217
218 - r, err := http.NewRequest("GET", "/", nil)
219 - if err != nil {
220 - t.Fatal(err)
221 - }
222 + r := createRequest("GET", "/", false)
223
224 rr := httptest.NewRecorder()
225 p := Protect(testKey, CookieName("nameoverride"), Secure(false), HttpOnly(false), Path("/pathoverride"), Domain("domainoverride"), MaxAge(173))(s)
226 @@ -86,10 +81,7 @@ func TestMethods(t *testing.T) {
227
228 // Test idempontent ("safe") methods
229 for _, method := range safeMethods {
230 - r, err := http.NewRequest(method, "/", nil)
231 - if err != nil {
232 - t.Fatal(err)
233 - }
234 + r := createRequest(method, "/", false)
235
236 rr := httptest.NewRecorder()
237 p.ServeHTTP(rr, r)
238 @@ -107,10 +99,7 @@ func TestMethods(t *testing.T) {
239 // Test non-idempotent methods (should return a 403 without a cookie set)
240 nonIdempotent := []string{"POST", "PUT", "DELETE", "PATCH"}
241 for _, method := range nonIdempotent {
242 - r, err := http.NewRequest(method, "/", nil)
243 - if err != nil {
244 - t.Fatal(err)
245 - }
246 + r := createRequest(method, "/", false)
247
248 rr := httptest.NewRecorder()
249 p.ServeHTTP(rr, r)
250 @@ -133,10 +122,7 @@ func TestNoCookie(t *testing.T) {
251 p := Protect(testKey)(s)
252
253 // POST the token back in the header.
254 - r, err := http.NewRequest("POST", "http://www.gorillatoolkit.org/", nil)
255 - if err != nil {
256 - t.Fatal(err)
257 - }
258 + r := createRequest("POST", "/", false)
259
260 rr := httptest.NewRecorder()
261 p.ServeHTTP(rr, r)
262 @@ -158,19 +144,13 @@ func TestBadCookie(t *testing.T) {
263 }))
264
265 // Obtain a CSRF cookie via a GET request.
266 - r, err := http.NewRequest("GET", "http://www.gorillatoolkit.org/", nil)
267 - if err != nil {
268 - t.Fatal(err)
269 - }
270 + r := createRequest("GET", "/", false)
271
272 rr := httptest.NewRecorder()
273 p.ServeHTTP(rr, r)
274
275 // POST the token back in the header.
276 - r, err = http.NewRequest("POST", "http://www.gorillatoolkit.org/", nil)
277 - if err != nil {
278 - t.Fatal(err)
279 - }
280 + r = createRequest("POST", "/", false)
281
282 // Replace the cookie prefix
283 badHeader := strings.Replace(cookieName+"=", rr.Header().Get("Set-Cookie"), "_badCookie", -1)
284 @@ -193,10 +173,7 @@ func TestVaryHeader(t *testing.T) {
285 s.HandleFunc("/", testHandler)
286 p := Protect(testKey)(s)
287
288 - r, err := http.NewRequest("HEAD", "https://www.golang.org/", nil)
289 - if err != nil {
290 - t.Fatal(err)
291 - }
292 + r := createRequest("GET", "/", true)
293
294 rr := httptest.NewRecorder()
295 p.ServeHTTP(rr, r)
296 @@ -211,16 +188,13 @@ func TestVaryHeader(t *testing.T) {
297 }
298 }
299
300 -// Requests with no Referer header should fail.
301 +// TestNoReferer checks that HTTPS requests with no Referer header fail.
302 func TestNoReferer(t *testing.T) {
303 s := http.NewServeMux()
304 s.HandleFunc("/", testHandler)
305 p := Protect(testKey)(s)
306
307 - r, err := http.NewRequest("POST", "https://golang.org/", nil)
308 - if err != nil {
309 - t.Fatal(err)
310 - }
311 + r := createRequest("POST", "https://golang.org/", true)
312
313 rr := httptest.NewRecorder()
314 p.ServeHTTP(rr, r)
315 @@ -243,20 +217,12 @@ func TestBadReferer(t *testing.T) {
316 }))
317
318 // Obtain a CSRF cookie via a GET request.
319 - r, err := http.NewRequest("GET", "https://www.gorillatoolkit.org/", nil)
320 - if err != nil {
321 - t.Fatal(err)
322 - }
323 -
324 + r := createRequest("GET", "/", true)
325 rr := httptest.NewRecorder()
326 p.ServeHTTP(rr, r)
327
328 // POST the token back in the header.
329 - r, err = http.NewRequest("POST", "https://www.gorillatoolkit.org/", nil)
330 - if err != nil {
331 - t.Fatal(err)
332 - }
333 -
334 + r = createRequest("POST", "/", true)
335 setCookie(rr, r)
336 r.Header.Set("X-CSRF-Token", token)
337
338 @@ -289,50 +255,47 @@ func TestTrustedReferer(t *testing.T) {
339 }
340
341 for _, item := range testTable {
342 - s := http.NewServeMux()
343 + t.Run(fmt.Sprintf("TrustedOrigin: %v", item.trustedOrigin), func(t *testing.T) {
344
345 - p := Protect(testKey, TrustedOrigins(item.trustedOrigin))(s)
346 + s := http.NewServeMux()
347
348 - var token string
349 - s.Handle("/", http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
350 - token = Token(r)
351 - }))
352 + p := Protect(testKey, TrustedOrigins(item.trustedOrigin))(s)
353
354 - // Obtain a CSRF cookie via a GET request.
355 - r, err := http.NewRequest("GET", "https://www.gorillatoolkit.org/", nil)
356 - if err != nil {
357 - t.Fatal(err)
358 - }
359 + var token string
360 + s.Handle("/", http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
361 + token = Token(r)
362 + }))
363
364 - rr := httptest.NewRecorder()
365 - p.ServeHTTP(rr, r)
366 + // Obtain a CSRF cookie via a GET request.
367 + r := createRequest("GET", "/", true)
368
369 - // POST the token back in the header.
370 - r, err = http.NewRequest("POST", "https://www.gorillatoolkit.org/", nil)
371 - if err != nil {
372 - t.Fatal(err)
373 - }
374 + rr := httptest.NewRecorder()
375 + p.ServeHTTP(rr, r)
376
377 - setCookie(rr, r)
378 - r.Header.Set("X-CSRF-Token", token)
379 + // POST the token back in the header.
380 + r = createRequest("POST", "/", true)
381
382 - // Set a non-matching Referer header.
383 - r.Header.Set("Referer", "http://golang.org/")
384 + setCookie(rr, r)
385 + r.Header.Set("X-CSRF-Token", token)
386
387 - rr = httptest.NewRecorder()
388 - p.ServeHTTP(rr, r)
389 + // Set a non-matching Referer header.
390 + r.Header.Set("Referer", "https://golang.org/")
391
392 - if item.shouldPass {
393 - if rr.Code != http.StatusOK {
394 - t.Fatalf("middleware failed to pass to the next handler: got %v want %v",
395 - rr.Code, http.StatusOK)
396 - }
397 - } else {
398 - if rr.Code != http.StatusForbidden {
399 - t.Fatalf("middleware failed reject a non-matching Referer header: got %v want %v",
400 - rr.Code, http.StatusForbidden)
401 + rr = httptest.NewRecorder()
402 + p.ServeHTTP(rr, r)
403 +
404 + if item.shouldPass {
405 + if rr.Code != http.StatusOK {
406 + t.Fatalf("middleware failed to pass to the next handler: got %v want %v",
407 + rr.Code, http.StatusOK)
408 + }
409 + } else {
410 + if rr.Code != http.StatusForbidden {
411 + t.Fatalf("middleware failed reject a non-matching Referer header: got %v want %v",
412 + rr.Code, http.StatusForbidden)
413 + }
414 }
415 - }
416 + })
417 }
418 }
419
420 @@ -347,23 +310,16 @@ func TestWithReferer(t *testing.T) {
421 }))
422
423 // Obtain a CSRF cookie via a GET request.
424 - r, err := http.NewRequest("GET", "http://www.gorillatoolkit.org/", nil)
425 - if err != nil {
426 - t.Fatal(err)
427 - }
428 -
429 + r := createRequest("GET", "/", true)
430 rr := httptest.NewRecorder()
431 p.ServeHTTP(rr, r)
432
433 // POST the token back in the header.
434 - r, err = http.NewRequest("POST", "http://www.gorillatoolkit.org/", nil)
435 - if err != nil {
436 - t.Fatal(err)
437 - }
438 + r = createRequest("POST", "/", true)
439
440 setCookie(rr, r)
441 r.Header.Set("X-CSRF-Token", token)
442 - r.Header.Set("Referer", "http://www.gorillatoolkit.org/")
443 + r.Header.Set("Referer", "https://www.gorillatoolkit.org/")
444
445 rr = httptest.NewRecorder()
446 p.ServeHTTP(rr, r)
447 @@ -387,26 +343,19 @@ func TestNoTokenProvided(t *testing.T) {
448 s.Handle("/", http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
449 token = Token(r)
450 }))
451 -
452 // Obtain a CSRF cookie via a GET request.
453 - r, err := http.NewRequest("GET", "http://www.gorillatoolkit.org/", nil)
454 - if err != nil {
455 - t.Fatal(err)
456 - }
457 + r := createRequest("GET", "/", true)
458
459 rr := httptest.NewRecorder()
460 p.ServeHTTP(rr, r)
461
462 // POST the token back in the header.
463 - r, err = http.NewRequest("POST", "http://www.gorillatoolkit.org/", nil)
464 - if err != nil {
465 - t.Fatal(err)
466 - }
467 + r = createRequest("POST", "/", true)
468
469 setCookie(rr, r)
470 // By accident we use the wrong header name for the token...
471 r.Header.Set("X-CSRF-nekot", token)
472 - r.Header.Set("Referer", "http://www.gorillatoolkit.org/")
473 + r.Header.Set("Referer", "https://www.gorillatoolkit.org/")
474
475 rr = httptest.NewRecorder()
476 p.ServeHTTP(rr, r)
477 @@ -419,3 +368,177 @@ func TestNoTokenProvided(t *testing.T) {
478 func setCookie(rr *httptest.ResponseRecorder, r *http.Request) {
479 r.Header.Set("Cookie", rr.Header().Get("Set-Cookie"))
480 }
481 +
482 +func TestProtectScenarios(t *testing.T) {
483 + tests := []struct {
484 + name string
485 + safeMethod bool
486 + originUntrusted bool
487 + originHTTP bool
488 + originTrusted bool
489 + secureRequest bool
490 + refererTrusted bool
491 + refererUntrusted bool
492 + refererHTTPDowngrade bool
493 + refererRelative bool
494 + tokenValid bool
495 + tokenInvalid bool
496 + want bool
497 + }{
498 + {
499 + name: "safe method pass",
500 + safeMethod: true,
501 + want: true,
502 + },
503 + {
504 + name: "cleartext POST with trusted origin & valid token pass",
505 + originHTTP: true,
506 + tokenValid: true,
507 + want: true,
508 + },
509 + {
510 + name: "cleartext POST with untrusted origin reject",
511 + originUntrusted: true,
512 + tokenValid: true,
513 + },
514 + {
515 + name: "cleartext POST with HTTP origin & invalid token reject",
516 + originHTTP: true,
517 + },
518 + {
519 + name: "cleartext POST without origin with valid token pass",
520 + tokenValid: true,
521 + want: true,
522 + },
523 + {
524 + name: "cleartext POST without origin with invalid token reject",
525 + },
526 + {
527 + name: "TLS POST with HTTP origin & no referer & valid token reject",
528 + tokenValid: true,
529 + secureRequest: true,
530 + originHTTP: true,
531 + },
532 + {
533 + name: "TLS POST without origin and without referer reject",
534 + secureRequest: true,
535 + tokenValid: true,
536 + },
537 + {
538 + name: "TLS POST without origin with untrusted referer reject",
539 + secureRequest: true,
540 + refererUntrusted: true,
541 + tokenValid: true,
542 + },
543 + {
544 + name: "TLS POST without origin with trusted referer & valid token pass",
545 + secureRequest: true,
546 + refererTrusted: true,
547 + tokenValid: true,
548 + want: true,
549 + },
550 + {
551 + name: "TLS POST without origin from _cleartext_ same domain referer with valid token reject",
552 + secureRequest: true,
553 + refererHTTPDowngrade: true,
554 + tokenValid: true,
555 + },
556 + {
557 + name: "TLS POST without origin from relative referer with valid token pass",
558 + secureRequest: true,
559 + refererRelative: true,
560 + tokenValid: true,
561 + want: true,
562 + },
563 + {
564 + name: "TLS POST without origin from relative referer with invalid token reject",
565 + secureRequest: true,
566 + refererRelative: true,
567 + tokenInvalid: true,
568 + },
569 + }
570 +
571 + for _, tt := range tests {
572 + t.Run(tt.name, func(t *testing.T) {
573 + var token string
574 + var flag bool
575 + mux := http.NewServeMux()
576 + mux.Handle("/", http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
577 + token = Token(r)
578 + }))
579 + mux.Handle("/submit", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
580 + flag = true
581 + }))
582 + p := Protect(testKey)(mux)
583 +
584 + // Obtain a CSRF cookie via a GET request.
585 + r := createRequest("GET", "/", tt.secureRequest)
586 + rr := httptest.NewRecorder()
587 + p.ServeHTTP(rr, r)
588 +
589 + r = createRequest("POST", "/submit", tt.secureRequest)
590 + if tt.safeMethod {
591 + r = createRequest("GET", "/submit", tt.secureRequest)
592 + }
593 +
594 + // Set the Origin header
595 + switch {
596 + case tt.originUntrusted:
597 + r.Header.Set("Origin", "http://www.untrusted-origin.org")
598 + case tt.originTrusted:
599 + r.Header.Set("Origin", "https://www.gorillatoolkit.org")
600 + case tt.originHTTP:
601 + r.Header.Set("Origin", "http://www.gorillatoolkit.org")
602 + }
603 +
604 + // Set the Referer header
605 + switch {
606 + case tt.refererTrusted:
607 + p = Protect(testKey, TrustedOrigins([]string{"external-trusted-origin.test"}))(mux)
608 + r.Header.Set("Referer", "https://external-trusted-origin.test/foobar")
609 + case tt.refererUntrusted:
610 + r.Header.Set("Referer", "http://www.invalid-referer.org")
611 + case tt.refererHTTPDowngrade:
612 + r.Header.Set("Referer", "http://www.gorillatoolkit.org/foobar")
613 + case tt.refererRelative:
614 + r.Header.Set("Referer", "/foobar")
615 + }
616 +
617 + // Set the CSRF token & associated cookie
618 + switch {
619 + case tt.tokenInvalid:
620 + setCookie(rr, r)
621 + r.Header.Set("X-CSRF-Token", "this-is-an-invalid-token")
622 + case tt.tokenValid:
623 + setCookie(rr, r)
624 + r.Header.Set("X-CSRF-Token", token)
625 + }
626 +
627 + rr = httptest.NewRecorder()
628 + p.ServeHTTP(rr, r)
629 +
630 + if tt.want && rr.Code != http.StatusOK {
631 + t.Fatalf("middleware failed to pass to the next handler: got %v want %v",
632 + rr.Code, http.StatusOK)
633 + }
634 +
635 + if tt.want && !flag {
636 + t.Fatalf("middleware failed to pass to the next handler: got %v want %v",
637 + flag, true)
638 +
639 + }
640 + if !tt.want && flag {
641 + t.Fatalf("middleware failed to reject the request: got %v want %v", flag, false)
642 + }
643 + })
644 + }
645 +}
646 +
647 +func createRequest(method, path string, useTLS bool) *http.Request {
648 + r := httptest.NewRequest(method, path, nil)
649 + r.Host = "www.gorillatoolkit.org"
650 + if !useTLS {
651 + return PlaintextHTTPRequest(r)
652 + }
653 + return r
654 +}
655 diff --git a/helpers_test.go b/helpers_test.go
656 index 399db8d..f40c996 100644
657 --- a/helpers_test.go
658 +++ b/helpers_test.go
659 @@ -83,10 +83,7 @@ func TestMultipartFormToken(t *testing.T) {
660 }
661 }))
662
663 - r, err := http.NewRequest("GET", "/", nil)
664 - if err != nil {
665 - t.Fatal(err)
666 - }
667 + r := createRequest("GET", "/", true)
668
669 rr := httptest.NewRecorder()
670 p := Protect(testKey)(s)
671 @@ -107,13 +104,13 @@ func TestMultipartFormToken(t *testing.T) {
672
673 mp.Close()
674
675 - r, err = http.NewRequest("POST", "http://www.gorillatoolkit.org/", &b)
676 - if err != nil {
677 - t.Fatal(err)
678 - }
679 + r = httptest.NewRequest("POST", "/", &b)
680 + r.Host = "www.gorillatoolkit.org"
681
682 // Add the multipart header.
683 r.Header.Set("Content-Type", mp.FormDataContentType())
684 + // Add Origin to pass the same-origin check.
685 + r.Header.Set("Origin", "https://www.gorillatoolkit.org")
686
687 // Send back the issued cookie.
688 setCookie(rr, r)
689 @@ -246,10 +243,8 @@ func TestTemplateField(t *testing.T) {
690 }))
691
692 testFieldName := "custom_field_name"
693 - r, err := http.NewRequest("GET", "/", nil)
694 - if err != nil {
695 - t.Fatal(err)
696 - }
697 + r := createRequest("GET", "/", false)
698 + // r, err := http.NewRequest("GET", "/", nil)
699
700 rr := httptest.NewRecorder()
701 p := Protect(testKey, FieldName(testFieldName))(s)
702 @@ -299,10 +294,7 @@ func TestUnsafeSkipCSRFCheck(t *testing.T) {
703 w.WriteHeader(teapot)
704 }))
705
706 - r, err := http.NewRequest("POST", "/", nil)
707 - if err != nil {
708 - t.Fatal(err)
709 - }
710 + r := createRequest("POST", "/", false)
711
712 // Must be used prior to the CSRF handler being invoked.
713 p := skipCheck(Protect(testKey)(s))