Skip to content

Commit a5f75b4

Browse files
authored
Merge pull request #184 from Shopify/temp-address-security-issue
Address an issue where a toxiproxy can be used to bypass the Same-Origin Policy in web browsers
2 parents a9ae929 + 7dae2ef commit a5f75b4

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

api.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import (
77
"net"
88
"net/http"
99
"os"
10+
"strings"
1011

1112
"github.com/Shopify/toxiproxy/toxics"
12-
"github.com/sirupsen/logrus"
1313
"github.com/gorilla/mux"
14+
"github.com/sirupsen/logrus"
1415
)
1516

1617
type ApiServer struct {
@@ -46,6 +47,16 @@ func (server *ApiServer) PopulateConfig(filename string) {
4647
}
4748
}
4849

50+
func StopBrowsersMiddleware(h http.Handler) http.Handler {
51+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
52+
if strings.HasPrefix(r.UserAgent(), "Mozilla/") {
53+
http.Error(w, "User agent not allowed", 403)
54+
} else {
55+
h.ServeHTTP(w, r)
56+
}
57+
})
58+
}
59+
4960
func (server *ApiServer) Listen(host string, port string) {
5061
r := mux.NewRouter()
5162
r.HandleFunc("/reset", server.ResetState).Methods("POST")
@@ -62,7 +73,8 @@ func (server *ApiServer) Listen(host string, port string) {
6273
r.HandleFunc("/proxies/{proxy}/toxics/{toxic}", server.ToxicDelete).Methods("DELETE")
6374

6475
r.HandleFunc("/version", server.Version).Methods("GET")
65-
http.Handle("/", r)
76+
77+
http.Handle("/", StopBrowsersMiddleware(r))
6678

6779
logrus.WithFields(logrus.Fields{
6880
"host": host,

api_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,36 @@ func WithServer(t *testing.T, f func(string)) {
3636
f("http://localhost:8475")
3737
}
3838

39+
func TestBrowserGets403(t *testing.T) {
40+
WithServer(t, func(addr string) {
41+
client := http.Client{}
42+
43+
req, _ := http.NewRequest("GET", "http://localhost:8475/proxies", nil)
44+
req.Header.Add("User-Agent", "Mozilla/5.0 (Linux; Android 4.4.2); Nexus 5 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Mobile Safari/537.36 OPR/20.0.1396.72047")
45+
46+
resp, _ := client.Do(req)
47+
48+
if resp.StatusCode != 403 {
49+
t.Fatal("Browser-like UserAgent was not denied access to Toxiproxy")
50+
}
51+
})
52+
}
53+
54+
func TestNonBrowserGets200(t *testing.T) {
55+
WithServer(t, func(addr string) {
56+
client := http.Client{}
57+
58+
req, _ := http.NewRequest("GET", "http://localhost:8475/proxies", nil)
59+
req.Header.Add("User-Agent", "Wget/2.1")
60+
61+
resp, _ := client.Do(req)
62+
63+
if resp.StatusCode == 403 {
64+
t.Fatal("Non-Browser-like UserAgent was denied access to Toxiproxy")
65+
}
66+
})
67+
}
68+
3969
func TestIndexWithNoProxies(t *testing.T) {
4070
WithServer(t, func(addr string) {
4171
client := tclient.NewClient(addr)

0 commit comments

Comments
 (0)