Skip to content

Commit

Permalink
http: replace host in chromium WSURL with own host
Browse files Browse the repository at this point in the history
We are making chromium listen in 0.0.0.0, it is reasonable to think that
however a client reached us, that's how it will reach chromium.
  • Loading branch information
nadiamoe committed Jun 20, 2024
1 parent b0d9bd2 commit 1a7721d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
34 changes: 34 additions & 0 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package http
import (
"encoding/json"
"log/slog"
"net"
"net/http"
"net/url"

"github.com/grafana/crocochrome"
)
Expand Down Expand Up @@ -49,6 +51,15 @@ func (s *Server) Create(rw http.ResponseWriter, r *http.Request) {
return
}

newURL, err := replaceHost(session.ChromiumVersion.WebSocketDebuggerURL, r.Host)
if err != nil {
s.logger.Error("replacing chromium url with host header", "err", err)
rw.WriteHeader(http.StatusInternalServerError)
return
}

session.ChromiumVersion.WebSocketDebuggerURL = newURL

rw.Header().Add("content-type", "application/json")
_ = json.NewEncoder(rw).Encode(session)
}
Expand All @@ -66,3 +77,26 @@ func (s *Server) Delete(rw http.ResponseWriter, r *http.Request) {
return
}
}

// replaceHost returns a new url with its hostname replaced with host. The port is kept as it is.
func replaceHost(urlStr, host string) (string, error) {
parsedURL, err := url.Parse(urlStr)
if err != nil {
return "", err
}

_, port, err := net.SplitHostPort(parsedURL.Host)
if err != nil {
return "", err
}

// Get rid of the port if a port is present in host.
host, _, err = net.SplitHostPort(host)
if err != nil {
return "", err
}

parsedURL.Host = net.JoinHostPort(host, port)

return parsedURL.String(), nil
}
48 changes: 48 additions & 0 deletions http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log/slog"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"

Expand Down Expand Up @@ -49,4 +50,51 @@ func TestHTTP(t *testing.T) {
t.Fatalf("webSocketDebuggerUrl is unexpectedly empty")
}
})

t.Run("replaces chromium url with host header", func(t *testing.T) {
hb := testutil.NewHeartbeat(t)
port := testutil.HTTPInfo(t, testutil.ChromiumVersion)
cc := crocochrome.New(logger, crocochrome.Options{ChromiumPath: hb.Path, ChromiumPort: port})
api := crocohttp.New(logger, cc)

server := httptest.NewServer(api)
t.Cleanup(server.Close)

req, err := http.NewRequest(http.MethodPost, server.URL+"/sessions", nil)
if err != nil {
t.Fatalf("building http request: %v", err)
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("making request: %v", err)
}

defer resp.Body.Close()

var response struct {
ID string `json:"ID"`
ChromiumVersion struct {
WebSocketDebuggerURL string `json:"webSocketDebuggerUrl"`
} `json:"chromiumVersion"`
}

err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
t.Fatalf("decoding response: %v", err)
}

parsedURL, err := url.Parse(response.ChromiumVersion.WebSocketDebuggerURL)
if err != nil {
t.Fatalf("parsing response url: %v", err)
}

if parsedURL.Hostname() != "127.0.0.1" {
t.Fatalf("expected returned url to have 127.0.0.1 as host, got %q", response.ChromiumVersion.WebSocketDebuggerURL)
}

if parsedURL.Port() != "9222" {
t.Fatalf("expected returned url to have 9222 as port, got %q", response.ChromiumVersion.WebSocketDebuggerURL)
}
})
}

0 comments on commit 1a7721d

Please sign in to comment.