Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/commoncode/urn.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ def decode(urn):
"""
segments = [unquote_plus(p) for p in urn.split(":")]

if len(segments) < 3:
raise URNValidationError("Invalid URN: missing namespace or object type.")

Comment thread
codewithfourtix marked this conversation as resolved.
Outdated
if not segments[0] == ("urn"):
raise URNValidationError("Invalid URN prefix. Expected 'urn'.")

Expand Down
8 changes: 8 additions & 0 deletions tests/test_urn.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,11 @@ def test_encode_decode_is_idempotent(self):
encoded = "urn:dje:component:SIP+Servlets+%28MSS%29:v+1.4.0.FINAL"
assert urn.encode(object_type, **fields) == encoded
assert urn.decode(encoded) == (object_type, fields)


class TruncatedURNTestCase(unittest.TestCase):
def test_truncated_urns_raise_validation_error(self):
for value in ("", "urn", "urn:", "urn:dje"):
with self.subTest(value=value):
with self.assertRaises(urn.URNValidationError):
urn.decode(value)