Skip to content

Commit c8ad43f

Browse files
Stops ParseResult.from_string from accepting ports not preceded by a ':' character
1 parent dda8bea commit c8ad43f

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/rfc3986/misc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
(
5252
"^(?:(?P<userinfo>{})@)?" # userinfo
5353
"(?P<host>{})" # host
54-
":?(?P<port>{})?$" # port
54+
"(?::(?P<port>{}))?$" # port
5555
).format(
5656
abnf_regexp.USERINFO_RE, abnf_regexp.HOST_PATTERN, abnf_regexp.PORT_RE
5757
)

tests/base.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from rfc3986 import exceptions as exc
1516

1617
class BaseTestParsesURIs:
1718
test_class = None
@@ -134,6 +135,24 @@ def test_handles_percent_in_fragment(self, uri_fragment_with_percent):
134135
uri = self.test_class.from_string(uri_fragment_with_percent)
135136
assert uri.fragment == "perc%25ent"
136137

138+
def test_handles_colon_but_no_port(self, uri_with_colon_but_no_port):
139+
try:
140+
uri = self.test_class.from_string(uri_with_colon_but_no_port)
141+
except Exception as e:
142+
assert isinstance(e, exc.InvalidAuthority)
143+
else:
144+
if uri.port is not None:
145+
raise AssertionError("No error thrown from URI with colon but no port")
146+
147+
def test_handles_port_but_no_colon(self, uri_with_port_but_no_colon):
148+
try:
149+
uri = self.test_class.from_string(uri_with_port_but_no_colon)
150+
except Exception as e:
151+
assert isinstance(e, exc.InvalidAuthority)
152+
else:
153+
if uri.port is not None:
154+
raise AssertionError("No error thrown from URI with port but no colon")
155+
137156

138157
class BaseTestUnsplits:
139158
test_class = None

tests/conftest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ def uri_without_authority_with_query_only():
116116
return "about:?foo=bar"
117117

118118

119+
@pytest.fixture
120+
def uri_with_colon_but_no_port():
121+
return "scheme://user@[v12.ip]:/path"
122+
123+
124+
@pytest.fixture
125+
def uri_with_port_but_no_colon():
126+
return "scheme://user@[v12.ip]8000/path"
127+
128+
119129
@pytest.fixture(params=valid_hosts)
120130
def relative_uri(request):
121131
return "//%s" % request.param

0 commit comments

Comments
 (0)