Skip to content

Commit

Permalink
accept.go: Improve unauthorized origin error message
Browse files Browse the repository at this point in the history
Closes #247
  • Loading branch information
nhooyr committed Jan 9, 2021
1 parent 482f584 commit 29251d0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 4 additions & 1 deletion accept.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,10 @@ func authenticateOrigin(r *http.Request, originHosts []string) error {
return nil
}
}
return fmt.Errorf("request Origin %q is not authorized for Host %q", origin, r.Host)
if u.Host == "" {
return fmt.Errorf("request Origin %q is not a valid URL with a host", origin)
}
return fmt.Errorf("request Origin %q is not authorized for Host %q", u.Host, r.Host)
}

func match(pattern, s string) (bool, error) {
Expand Down
18 changes: 17 additions & 1 deletion accept_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,23 @@ func TestAccept(t *testing.T) {
r.Header.Set("Origin", "harhar.com")

_, err := Accept(w, r, nil)
assert.Contains(t, err, `request Origin "harhar.com" is not authorized for Host`)
assert.Contains(t, err, `request Origin "harhar.com" is not a valid URL with a host`)
})

// #247
t.Run("unauthorizedOriginErrorMessage", func(t *testing.T) {
t.Parallel()

w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
r.Header.Set("Connection", "Upgrade")
r.Header.Set("Upgrade", "websocket")
r.Header.Set("Sec-WebSocket-Version", "13")
r.Header.Set("Sec-WebSocket-Key", "meow123")
r.Header.Set("Origin", "https://harhar.com")

_, err := Accept(w, r, nil)
assert.Contains(t, err, `request Origin "harhar.com" is not authorized for Host "example.com"`)
})

t.Run("badCompression", func(t *testing.T) {
Expand Down

0 comments on commit 29251d0

Please sign in to comment.