Skip to content

Commit 71e79dd

Browse files
fippojlaine
authored andcommitted
reject stun urls with transport set
which has been rejected in Chrome since early 2023: https://chromiumdash.appspot.com/commits?commit=f4d463ca875f51c26a7de0e594eb1516f3e60111
1 parent aae7d7b commit 71e79dd

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/aiortc/rtcicetransport.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
from .exceptions import InvalidStateError
1111
from .rtcconfiguration import RTCIceServer
1212

13+
# See https://datatracker.ietf.org/doc/html/rfc7064
14+
# transport is not defined by RFC 7064 and rejected by browsers.
1315
STUN_REGEX = re.compile(
1416
r"(?P<scheme>stun|stuns)\:(?P<host>[^?:]+)(\:(?P<port>[0-9]+?))?"
15-
# RFC 7064 does not define a "transport" option but some providers
16-
# include it, so just ignore it
17-
r"(\?transport=.*)?"
17+
r"(\?transport=(?P<transport>.*))?"
1818
)
19+
# See https://datatracker.ietf.org/doc/html/rfc7065
1920
TURN_REGEX = re.compile(
2021
r"(?P<scheme>turn|turns)\:(?P<host>[^?:]+)(\:(?P<port>[0-9]+?))?"
2122
r"(\?transport=(?P<transport>.*))?"
@@ -162,6 +163,12 @@ def parse_stun_turn_uri(uri: str) -> dict[str, Any]:
162163
parsed["transport"] = "udp"
163164
elif parsed["scheme"] == "turns" and not parsed["transport"]:
164165
parsed["transport"] = "tcp"
166+
elif parsed["scheme"] in ["stun", "stuns"]:
167+
if parsed["transport"] is not None:
168+
raise ValueError(
169+
"malformed uri: " + parsed["scheme"] + " must not contain transport"
170+
)
171+
del parsed["transport"]
165172

166173
return parsed
167174

tests/test_rtcicetransport.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,11 @@ def test_stun(self) -> None:
3737
{"stun_server": ("stun.l.google.com", 19302)},
3838
)
3939

40-
def test_stun_with_suffix(self) -> None:
40+
def test_stun_with_transport(self) -> None:
41+
with self.assertRaises(ValueError) as cm:
42+
parse_stun_turn_uri("stun:global.stun.twilio.com:3478?transport=udp")
4143
self.assertEqual(
42-
connection_kwargs(
43-
[RTCIceServer("stun:global.stun.twilio.com:3478?transport=udp")]
44-
),
45-
{"stun_server": ("global.stun.twilio.com", 3478)},
44+
str(cm.exception), "malformed uri: stun must not contain transport"
4645
)
4746

4847
def test_stun_multiple_servers(self) -> None:

0 commit comments

Comments
 (0)