Skip to content

Commit c7f6fce

Browse files
committed
http: block all ^Mozilla user agents without Origin nor Referer
1 parent cd51b41 commit c7f6fce

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

http/config.go

+9-19
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,6 @@ const (
1515
ACACredentials = "Access-Control-Allow-Credentials"
1616
)
1717

18-
// disallowedUserAgents specifies a denylist of user agents that are not
19-
// allowed to perform POST requests if they are not providing Origin
20-
// and/or Referer headers. As mitigation for things like
21-
// https://bugzilla.mozilla.org/show_bug.cgi?id=429594. Defaults to
22-
// Firefox-related things. The matching against the user-agent string
23-
// is made with strings.Contains().
24-
var disallowedUserAgents = []string{
25-
"Firefox",
26-
"Focus",
27-
"Klar",
28-
"FxiOS",
29-
}
30-
3118
type ServerConfig struct {
3219
// APIPath is the prefix of all request paths.
3320
// Example: host:port/api/v0/add. Here the APIPath is /api/v0
@@ -191,12 +178,15 @@ func allowUserAgent(r *http.Request, cfg *ServerConfig) bool {
191178
return true
192179
}
193180

194-
// If not, check that request is not from a blacklisted UA.
181+
// Allow if the user agent does not start with Mozilla... (i.e. curl)
195182
ua := r.Header.Get("User-agent")
196-
for _, forbiddenUA := range disallowedUserAgents {
197-
if strings.Contains(ua, forbiddenUA) {
198-
return false
199-
}
183+
if !strings.HasPrefix(ua, "Mozilla") {
184+
return true
200185
}
201-
return true
186+
187+
// Disallow otherwise.
188+
//
189+
// This means the request probably came from a browser and thus, it
190+
// should have included Origin or referer headers.
191+
return false
202192
}

http/errors_test.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func TestUnhandledMethod(t *testing.T) {
174174
func TestDisallowedUserAgents(t *testing.T) {
175175
tcs := []httpTestCase{
176176
{
177-
// Block Firefox
177+
// Block Mozilla* browsers that do not provide origins.
178178
Method: "POST",
179179
AllowGet: false,
180180
Code: http.StatusForbidden,
@@ -192,10 +192,13 @@ func TestDisallowedUserAgents(t *testing.T) {
192192
},
193193
},
194194
{
195-
// Do not block Chrome
196-
Method: "POST",
197-
AllowGet: false,
198-
Code: http.StatusOK,
195+
// Do not block a Mozilla* browser that provides an
196+
// allowed Origin
197+
Method: "POST",
198+
AllowGet: false,
199+
AllowOrigins: []string{"*"},
200+
Origin: "null",
201+
Code: http.StatusOK,
199202
ReqHeaders: map[string]string{
200203
"User-Agent": "Mozilla/5.0 (Linux; U; Android 4.1.1; en-gb; Build/KLP) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30",
201204
},

0 commit comments

Comments
 (0)