Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e11dd4e

Browse files
authored
fix: match Origin scheme if defined in OriginPatterns (#536)
This change aligns origin checks with RFC 6454 by adding scheme-aware matching to OriginPatterns. Fixes #529
1 parent 91013c1 commit e11dd4e

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

accept.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ type AcceptOptions struct {
4040
// In such a case, example.com is the origin and chat.example.com is the request host.
4141
// One would set this field to []string{"example.com"} to authorize example.com to connect.
4242
//
43-
// Each pattern is matched case insensitively against the request origin host
44-
// with path.Match.
45-
// See https://golang.org/pkg/path/#Match
43+
// Each pattern is matched case insensitively with path.Match (see
44+
// https://golang.org/pkg/path/#Match). By default, it is matched
45+
// against the request origin host. If the pattern contains a URI
46+
// scheme ("://"), it will be matched against "scheme://host".
4647
//
4748
// Please ensure you understand the ramifications of enabling this.
4849
// If used incorrectly your WebSocket server will be open to CSRF attacks.
@@ -240,7 +241,11 @@ func authenticateOrigin(r *http.Request, originHosts []string) error {
240241
}
241242

242243
for _, hostPattern := range originHosts {
243-
matched, err := match(hostPattern, u.Host)
244+
target := u.Host
245+
if strings.Contains(hostPattern, "://") {
246+
target = u.Scheme + "://" + u.Host
247+
}
248+
matched, err := match(hostPattern, target)
244249
if err != nil {
245250
return fmt.Errorf("failed to parse path pattern %q: %w", hostPattern, err)
246251
}

accept_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,42 @@ func Test_authenticateOrigin(t *testing.T) {
466466
},
467467
success: false,
468468
},
469+
{
470+
name: "originPatternsWithSchemeHttps",
471+
origin: "https://two.example.com",
472+
host: "example.com",
473+
originPatterns: []string{
474+
"https://*.example.com",
475+
},
476+
success: true,
477+
},
478+
{
479+
name: "originPatternsWithSchemeMismatch",
480+
origin: "https://two.example.com",
481+
host: "example.com",
482+
originPatterns: []string{
483+
"http://*.example.com",
484+
},
485+
success: false,
486+
},
487+
{
488+
name: "originPatternsWithSchemeAndPort",
489+
origin: "https://example.com:8443",
490+
host: "example.com",
491+
originPatterns: []string{
492+
"https://example.com:8443",
493+
},
494+
success: true,
495+
},
496+
{
497+
name: "backwardsCompatHostOnlyPattern",
498+
origin: "http://two.example.com",
499+
host: "example.com",
500+
originPatterns: []string{
501+
"*.example.com",
502+
},
503+
success: true,
504+
},
469505
}
470506

471507
for _, tc := range testCases {

0 commit comments

Comments
 (0)