Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address an issue where a toxiproxy can be used to bypass the Same-Origin Policy in web browsers #184

Merged
merged 2 commits into from
Jun 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"net"
"net/http"
"os"
"strings"

"github.com/Shopify/toxiproxy/toxics"
"github.com/sirupsen/logrus"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
)

type ApiServer struct {
Expand Down Expand Up @@ -46,6 +47,16 @@ func (server *ApiServer) PopulateConfig(filename string) {
}
}

func StopBrowsersMiddleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.UserAgent(), "Mozilla/") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is matching against browser user agents that simple?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to MDN, all the common browsers' user agents start with Mozilla/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http://webaim.org/blog/user-agent-string-history/ User agents have a sad history of pretending to be each other

http.Error(w, "User agent not allowed", 403)
} else {
h.ServeHTTP(w, r)
}
})
}

func (server *ApiServer) Listen(host string, port string) {
r := mux.NewRouter()
r.HandleFunc("/reset", server.ResetState).Methods("POST")
Expand All @@ -62,7 +73,8 @@ func (server *ApiServer) Listen(host string, port string) {
r.HandleFunc("/proxies/{proxy}/toxics/{toxic}", server.ToxicDelete).Methods("DELETE")

r.HandleFunc("/version", server.Version).Methods("GET")
http.Handle("/", r)

http.Handle("/", StopBrowsersMiddleware(r))

logrus.WithFields(logrus.Fields{
"host": host,
Expand Down
30 changes: 30 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,36 @@ func WithServer(t *testing.T, f func(string)) {
f("http://localhost:8475")
}

func TestBrowserGets403(t *testing.T) {
WithServer(t, func(addr string) {
client := http.Client{}

req, _ := http.NewRequest("GET", "http://localhost:8475/proxies", nil)
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")

resp, _ := client.Do(req)

if resp.StatusCode != 403 {
t.Fatal("Browser-like UserAgent was not denied access to Toxiproxy")
}
})
}

func TestNonBrowserGets200(t *testing.T) {
WithServer(t, func(addr string) {
client := http.Client{}

req, _ := http.NewRequest("GET", "http://localhost:8475/proxies", nil)
req.Header.Add("User-Agent", "Wget/2.1")

resp, _ := client.Do(req)

if resp.StatusCode == 403 {
t.Fatal("Non-Browser-like UserAgent was denied access to Toxiproxy")
}
})
}

func TestIndexWithNoProxies(t *testing.T) {
WithServer(t, func(addr string) {
client := tclient.NewClient(addr)
Expand Down