Skip to content
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
13 changes: 9 additions & 4 deletions accept.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ type AcceptOptions struct {
// In such a case, example.com is the origin and chat.example.com is the request host.
// One would set this field to []string{"example.com"} to authorize example.com to connect.
//
// Each pattern is matched case insensitively against the request origin host
// with path.Match.
// See https://golang.org/pkg/path/#Match
// Each pattern is matched case insensitively with path.Match (see
// https://golang.org/pkg/path/#Match). By default, it is matched
// against the request origin host. If the pattern contains a URI
// scheme ("://"), it will be matched against "scheme://host".
//
// Please ensure you understand the ramifications of enabling this.
// If used incorrectly your WebSocket server will be open to CSRF attacks.
Expand Down Expand Up @@ -240,7 +241,11 @@ func authenticateOrigin(r *http.Request, originHosts []string) error {
}

for _, hostPattern := range originHosts {
matched, err := match(hostPattern, u.Host)
target := u.Host
if strings.Contains(hostPattern, "://") {
target = u.Scheme + "://" + u.Host
}
matched, err := match(hostPattern, target)
if err != nil {
return fmt.Errorf("failed to parse path pattern %q: %w", hostPattern, err)
}
Expand Down
36 changes: 36 additions & 0 deletions accept_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,42 @@ func Test_authenticateOrigin(t *testing.T) {
},
success: false,
},
{
name: "originPatternsWithSchemeHttps",
origin: "https://two.example.com",
host: "example.com",
originPatterns: []string{
"https://*.example.com",
},
success: true,
},
{
name: "originPatternsWithSchemeMismatch",
origin: "https://two.example.com",
host: "example.com",
originPatterns: []string{
"http://*.example.com",
},
success: false,
},
{
name: "originPatternsWithSchemeAndPort",
origin: "https://example.com:8443",
host: "example.com",
originPatterns: []string{
"https://example.com:8443",
},
success: true,
},
{
name: "backwardsCompatHostOnlyPattern",
origin: "http://two.example.com",
host: "example.com",
originPatterns: []string{
"*.example.com",
},
success: true,
},
}

for _, tc := range testCases {
Expand Down