Skip to content

Commit

Permalink
uri2fsn: improve error handling on unescaped URIs
Browse files Browse the repository at this point in the history
error out instead of returning an invalid path
  • Loading branch information
lazka committed Dec 11, 2016
1 parent d02a6c2 commit 8308600
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
8 changes: 4 additions & 4 deletions senf/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@


if PY2:
from urlparse import urlparse
urlparse
from urlparse import urlparse, urlunparse
urlparse, urlunparse
from urllib import pathname2url, url2pathname, quote, unquote
pathname2url, url2pathname, quote, unquote

Expand All @@ -35,8 +35,8 @@

iteritems = lambda d: d.iteritems()
elif PY3:
from urllib.parse import urlparse, quote, unquote
urlparse, quote, unquote
from urllib.parse import urlparse, quote, unquote, urlunparse
urlparse, quote, unquote, urlunparse
from urllib.request import pathname2url, url2pathname
pathname2url, url2pathname

Expand Down
13 changes: 9 additions & 4 deletions senf/_fsnative.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from . import _winapi as winapi
from ._compat import text_type, PY3, PY2, url2pathname, urlparse, quote, \
unquote
unquote, urlunparse


is_win = os.name == "nt"
Expand Down Expand Up @@ -535,10 +535,15 @@ def uri2fsn(uri):
path = parsed.path

if scheme != "file":
raise ValueError("Not a file URI")
raise ValueError("Not a file URI: %r" % uri)

if not path:
raise ValueError("Invalid file URI: %r" % uri)

uri = urlunparse(parsed)[7:]

if is_win:
path = url2pathname(netloc + path)
path = url2pathname(uri)
if netloc:
path = "\\\\" + path
if PY2:
Expand All @@ -547,7 +552,7 @@ def uri2fsn(uri):
raise ValueError("embedded null")
return path
else:
path = url2pathname(path)
path = url2pathname(uri)
if "\x00" in path:
raise ValueError("embedded null")
if PY3:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,16 @@ def test_uri2fsn():
assert isinstance(uri2fsn(u"file:///foo"), fsnative)
assert \
uri2fsn("file:///foo-%E1%88%B4") == path2fsn(b"/foo-\xe1\x88\xb4")
assert uri2fsn("file:NOPE") == fsnative(u"/NOPE")
assert uri2fsn("file:/NOPE") == fsnative(u"/NOPE")
with pytest.raises(ValueError):
assert uri2fsn("file://NOPE")
assert uri2fsn("file:///bla:foo@NOPE.com") == \
fsnative(u"/bla:foo@NOPE.com")
assert uri2fsn("file:///bla?x#b") == fsnative(u"/bla?x#b")
else:
assert uri2fsn("file:NOPE") == "\\NOPE"
assert uri2fsn("file:/NOPE") == "\\NOPE"
with pytest.raises(ValueError):
assert uri2fsn(u"file:///C:/%00")
with pytest.raises(ValueError):
Expand Down

0 comments on commit 8308600

Please sign in to comment.