Skip to content

Commit b00bc40

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

File tree

2 files changed

+17
-32
lines changed

2 files changed

+17
-32
lines changed

http/config.go

+9-27
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
@@ -44,14 +31,6 @@ type ServerConfig struct {
4431
// websites to include resources from the API but not _read_ them.
4532
AllowGet bool
4633

47-
// DisallowUserAgents specifies a blacklist of user agents that are not
48-
// allowed to perform POST requests if they are not providing Origin
49-
// and/or Referer headers. As mitigation for things like
50-
// https://bugzilla.mozilla.org/show_bug.cgi?id=429594.
51-
// Defaults to ["Firefox"]. The matching against the user-agent
52-
// string is made with strings.Contains().
53-
DisallowUserAgents []string
54-
5534
// corsOpts is a set of options for CORS headers.
5635
corsOpts *cors.Options
5736

@@ -191,12 +170,15 @@ func allowUserAgent(r *http.Request, cfg *ServerConfig) bool {
191170
return true
192171
}
193172

194-
// If not, check that request is not from a blacklisted UA.
173+
// Allow if the user agent does not start with Mozilla... (i.e. curl)
195174
ua := r.Header.Get("User-agent")
196-
for _, forbiddenUA := range disallowedUserAgents {
197-
if strings.Contains(ua, forbiddenUA) {
198-
return false
199-
}
175+
if !strings.HasPrefix(ua, "Mozilla") {
176+
return true
200177
}
201-
return true
178+
179+
// Disallow otherwise.
180+
//
181+
// This means the request probably came from a browser and thus, it
182+
// should have included Origin or referer headers.
183+
return false
202184
}

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)