@@ -608,7 +608,7 @@ def test_supported_origin(self):
608
608
self .assertEqual (server .origin , "https://other.example.com" )
609
609
610
610
def test_unsupported_origin (self ):
611
- """Handshake succeeds when checking origins and the origin is unsupported."""
611
+ """Handshake fails when checking origins and the origin is unsupported."""
612
612
server = ServerProtocol (
613
613
origins = ["https://example.com" , "https://other.example.com" ]
614
614
)
@@ -624,13 +624,10 @@ def test_unsupported_origin(self):
624
624
"invalid Origin header: https://original.example.com" ,
625
625
)
626
626
627
- def test_supported_origin_by_regex (self ):
628
- """
629
- Handshake succeeds when checking origins and the origin is supported
630
- by a regular expression.
631
- """
627
+ def test_supported_origin_regex (self ):
628
+ """Handshake succeeds when checking origins and the origin is supported."""
632
629
server = ServerProtocol (
633
- origins = ["https://example.com" , re .compile (r"https://other.* " )]
630
+ origins = [re .compile (r"https://(?!original)[a-z]+\.example\.com " )]
634
631
)
635
632
request = make_request ()
636
633
request .headers ["Origin" ] = "https://other.example.com"
@@ -640,13 +637,10 @@ def test_supported_origin_by_regex(self):
640
637
self .assertHandshakeSuccess (server )
641
638
self .assertEqual (server .origin , "https://other.example.com" )
642
639
643
- def test_unsupported_origin_by_regex (self ):
644
- """
645
- Handshake succeeds when checking origins and the origin is unsupported
646
- by a regular expression.
647
- """
640
+ def test_unsupported_origin_regex (self ):
641
+ """Handshake fails when checking origins and the origin is unsupported."""
648
642
server = ServerProtocol (
649
- origins = ["https://example.com" , re .compile (r"https://other.* " )]
643
+ origins = [re .compile (r"https://(?!original)[a-z]+\.example\.com " )]
650
644
)
651
645
request = make_request ()
652
646
request .headers ["Origin" ] = "https://original.example.com"
@@ -660,6 +654,23 @@ def test_unsupported_origin_by_regex(self):
660
654
"invalid Origin header: https://original.example.com" ,
661
655
)
662
656
657
+ def test_partial_match_origin_regex (self ):
658
+ """Handshake fails when checking origins and the origin a partial match."""
659
+ server = ServerProtocol (
660
+ origins = [re .compile (r"https://(?!original)[a-z]+\.example\.com" )]
661
+ )
662
+ request = make_request ()
663
+ request .headers ["Origin" ] = "https://other.example.com.hacked"
664
+ response = server .accept (request )
665
+ server .send_response (response )
666
+
667
+ self .assertEqual (response .status_code , 403 )
668
+ self .assertHandshakeError (
669
+ server ,
670
+ InvalidOrigin ,
671
+ "invalid Origin header: https://other.example.com.hacked" ,
672
+ )
673
+
663
674
def test_no_origin_accepted (self ):
664
675
"""Handshake succeeds when the lack of an origin is accepted."""
665
676
server = ServerProtocol (origins = [None ])
0 commit comments