From bc60cace70078944ea64699aa99e974a55ee50b6 Mon Sep 17 00:00:00 2001 From: Ivan Krutov Date: Thu, 15 Nov 2018 19:34:17 +0300 Subject: [PATCH 1/2] Removed deprecated CloseNotifier --- proxy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proxy.go b/proxy.go index 0058333..7221334 100644 --- a/proxy.go +++ b/proxy.go @@ -512,7 +512,7 @@ func withCloseNotifier(handler http.HandlerFunc) http.HandlerFunc { cancel() }() select { - case <-w.(http.CloseNotifier).CloseNotify(): + case <-r.Context().Done(): cancel() case <-ctx.Done(): } From c4198e151915e39a499fa01dfbe2954ddb7bbc95 Mon Sep 17 00:00:00 2001 From: Ivan Krutov Date: Thu, 15 Nov 2018 20:18:22 +0300 Subject: [PATCH 2/2] Fixed panic in route logic (fixes #251) --- proxy.go | 7 ++++++- proxy_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/proxy.go b/proxy.go index 7221334..10d5872 100644 --- a/proxy.go +++ b/proxy.go @@ -326,7 +326,12 @@ loop: protocolError() return } - sess, ok = value.(map[string]interface{})["sessionId"].(string) + valueMap, ok := value.(map[string]interface{}) + if !ok { + protocolError() + return + } + sess, ok = valueMap["sessionId"].(string) if !ok { protocolError() return diff --git a/proxy_test.go b/proxy_test.go index 4266136..5f7ebe8 100644 --- a/proxy_test.go +++ b/proxy_test.go @@ -1207,6 +1207,36 @@ func TestStartSessionJSONWireProtocol(t *testing.T) { AssertThat(t, value["value"].(map[string]interface{})["sessionId"], EqualTo{fmt.Sprintf("%s123", node.Sum())}) } +func TestPanicRouteProtocolError(t *testing.T) { + mux := http.NewServeMux() + mux.HandleFunc("/wd/hub/session", postOnly(func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(`{"value":[]}`)) + })) + selenium := httptest.NewServer(mux) + defer selenium.Close() + + host, port := hostportnum(selenium.URL) + node := Host{Name: host, Port: port, Count: 1} + + test.Lock() + defer test.Unlock() + + browsers := Browsers{Browsers: []Browser{ + {Name: "browser", DefaultVersion: "1.0", Versions: []Version{ + {Number: "1.0", Regions: []Region{ + {Hosts: Hosts{ + node, + }}, + }}, + }}}} + updateQuota(user, browsers) + + rsp, err := createSession(`{"desiredCapabilities":{"browserName":"browser", "version":"1.0"}}`) + + AssertThat(t, err, Is{nil}) + AssertThat(t, rsp.StatusCode, Is{http.StatusBadGateway}) +} + func TestDeleteSession(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc("/wd/hub/session/", func(w http.ResponseWriter, r *http.Request) {