Skip to content

Commit e3a825e

Browse files
authored
Add URN validation (#248)
This adds some additional validation for URNs and query strings.
1 parent 67017bd commit e3a825e

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

protovalidate/internal/extra_func.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,19 @@ def is_email(string: celtypes.Value) -> celpy.Result:
165165

166166
def is_uri(string: celtypes.Value) -> celpy.Result:
167167
url = urlparse.urlparse(string)
168-
if not all([url.scheme, url.netloc, url.path]):
168+
# urlparse correctly reads the scheme from URNs but parses everything
169+
# after (except the query string) as the path.
170+
if url.scheme == "urn":
171+
if not (url.path):
172+
return celtypes.BoolType(False)
173+
elif not all([url.scheme, url.netloc, url.path]):
169174
return celtypes.BoolType(False)
175+
176+
# If the query string contains percent-encoding, then try to decode it.
177+
# unquote will return the same string if it is improperly encoded.
178+
if "%" in url.query:
179+
return celtypes.BoolType(urlparse.unquote(url.query) != url.query)
180+
170181
return celtypes.BoolType(True)
171182

172183

0 commit comments

Comments
 (0)