Skip to content

Commit fcb9850

Browse files
jub0bsseankhliao
authored andcommitted
net/http: reduce allocs in CrossOriginProtection.Check
Rather than repeatedly creating error values on CrossOriginProtection.Check's unhappy paths, return non-exported and effectively constant error variables. For #73626. Change-Id: Ibaa036c29417071b3601b8d200ab0902359d1bb9 GitHub-Last-Rev: e704d63 GitHub-Pull-Request: #74251 Reviewed-on: https://go-review.googlesource.com/c/go/+/681178 Reviewed-by: Sean Liao <[email protected]> Reviewed-by: qiu laidongfeng2 <[email protected]> Reviewed-by: Junyang Shao <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 11f11f2 commit fcb9850

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/net/http/csrf.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (c *CrossOriginProtection) Check(req *Request) error {
136136
if c.isRequestExempt(req) {
137137
return nil
138138
}
139-
return errors.New("cross-origin request detected from Sec-Fetch-Site header")
139+
return errCrossOriginRequest
140140
}
141141

142142
origin := req.Header.Get("Origin")
@@ -159,10 +159,15 @@ func (c *CrossOriginProtection) Check(req *Request) error {
159159
if c.isRequestExempt(req) {
160160
return nil
161161
}
162-
return errors.New("cross-origin request detected, and/or browser is out of date: " +
163-
"Sec-Fetch-Site is missing, and Origin does not match Host")
162+
return errCrossOriginRequestFromOldBrowser
164163
}
165164

165+
var (
166+
errCrossOriginRequest = errors.New("cross-origin request detected from Sec-Fetch-Site header")
167+
errCrossOriginRequestFromOldBrowser = errors.New("cross-origin request detected, and/or browser is out of date: " +
168+
"Sec-Fetch-Site is missing, and Origin does not match Host")
169+
)
170+
166171
// isRequestExempt checks the bypasses which require taking a lock, and should
167172
// be deferred until the last moment.
168173
func (c *CrossOriginProtection) isRequestExempt(req *Request) bool {

0 commit comments

Comments
 (0)