Skip to content

Commit

Permalink
Validate url passed to URLFile conforms to HTTP protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
aron committed Oct 16, 2024
1 parent 3edefe4 commit 05a13bf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
7 changes: 7 additions & 0 deletions python/cog/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@ class URLFile(io.IOBase):

def __init__(self, url: str) -> None:
parsed = urllib.parse.urlparse(url)
if parsed.scheme not in {
"http",
"https",
}:
raise ValueError(
"URLFile requires URL to conform to HTTP or HTTPS protocol"
)
object.__setattr__(self, "name", os.path.basename(parsed.path))
object.__setattr__(self, "__url__", url)

Expand Down
2 changes: 0 additions & 2 deletions python/tests/server/test_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import pytest
from cog.server.clients import ClientManager

pytest.mark.asyncio


@pytest.mark.asyncio
async def test_upload_files_without_url():
Expand Down
8 changes: 8 additions & 0 deletions python/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ def file_fixture(body: str):
)


def test_urlfile_protocol_validation():
with pytest.raises(ValueError):
URLFile("file:///etc/shadow")

with pytest.raises(ValueError):
URLFile("data:text/plain,hello")


@mock.patch("urllib.request.urlopen", return_value=file_fixture("hello world"))
def test_urlfile_acts_like_response(mock_urlopen):
u = URLFile("https://example.com/some/url")
Expand Down

0 comments on commit 05a13bf

Please sign in to comment.