Skip to content

Commit cb0a9f9

Browse files
committed
Fix: Unkwnown hashes raised exception
Problem: Many crawlers called URLs that do not exist on CRNs. The current implementation raises an error when the hash of the VM cannot be found, which fills the logs on Sentry. Solution: Return an HTTP Not Found status instead.
1 parent f311408 commit cb0a9f9

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

src/aleph/vm/orchestrator/views/__init__.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import aiodns
1212
import aiohttp
1313
from aiohttp import web
14-
from aiohttp.web_exceptions import HTTPNotFound
14+
from aiohttp.web_exceptions import HTTPBadRequest, HTTPNotFound
1515
from aleph_message.exceptions import UnknownHashError
1616
from aleph_message.models import ItemHash, MessageType
1717
from pydantic import ValidationError
@@ -65,7 +65,13 @@ async def run_code_from_path(request: web.Request) -> web.Response:
6565
path = request.match_info["suffix"]
6666
path = path if path.startswith("/") else f"/{path}"
6767

68-
message_ref = ItemHash(request.match_info["ref"])
68+
try:
69+
message_ref = ItemHash(request.match_info["ref"])
70+
except UnknownHashError as e:
71+
raise HTTPBadRequest(
72+
reason="Invalid message reference", text=f"Invalid message reference: {request.match_info['ref']}"
73+
) from e
74+
6975
pool: VmPool = request.app["vm_pool"]
7076
return await run_code_on_request(message_ref, path, pool, request)
7177

@@ -98,8 +104,10 @@ async def run_code_from_hostname(request: web.Request) -> web.Response:
98104
try:
99105
message_ref = ItemHash(await get_ref_from_dns(domain=f"_aleph-id.{request.host}"))
100106
logger.debug(f"Using DNS TXT record to obtain '{message_ref}'")
101-
except aiodns.error.DNSError as error:
102-
raise HTTPNotFound(reason="Invalid message reference") from error
107+
except aiodns.error.DNSError:
108+
return HTTPNotFound(reason="Invalid message reference")
109+
except UnknownHashError:
110+
return HTTPNotFound(reason="Invalid message reference")
103111

104112
pool = request.app["vm_pool"]
105113
return await run_code_on_request(message_ref, path, pool, request)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import pytest
2+
from aiohttp import ClientResponseError, web
3+
from aiohttp.test_utils import make_mocked_request
4+
from aiohttp.web_exceptions import HTTPBadRequest
5+
from aleph_message.exceptions import UnknownHashError
6+
from aleph_message.models import ItemHash
7+
8+
from aleph.vm.conf import settings
9+
from aleph.vm.orchestrator.views import run_code_from_path
10+
11+
12+
@pytest.mark.asyncio
13+
async def test_run_code_from_invalid_path(aiohttp_client):
14+
"""
15+
Test that the run_code_from_path endpoint raises the right
16+
error on invalid paths.
17+
"""
18+
item_hash = "invalid-item-hash"
19+
with pytest.raises(UnknownHashError):
20+
assert ItemHash(item_hash).is_storage(item_hash)
21+
22+
app = web.Application()
23+
24+
app.router.add_route("*", "/vm/{ref}{suffix:.*}", run_code_from_path),
25+
client = await aiohttp_client(app)
26+
27+
invalid_hash_request: web.Request = make_mocked_request(
28+
"GET",
29+
"/vm/" + item_hash,
30+
match_info={
31+
"ref": item_hash,
32+
"suffix": "/some/suffix",
33+
},
34+
headers={"Host": settings.DOMAIN_NAME},
35+
app=app,
36+
)
37+
with pytest.raises(HTTPBadRequest):
38+
await run_code_from_path(invalid_hash_request)
39+
40+
# Calling the view from an HTTP client should result in a Bad Request error.
41+
resp = await client.get("/vm/" + item_hash + "/some/suffix")
42+
assert resp.status == HTTPBadRequest.status_code
43+
text = await resp.text()
44+
assert text == f"Invalid message reference: {item_hash}"
45+
with pytest.raises(ClientResponseError):
46+
resp.raise_for_status()

0 commit comments

Comments
 (0)