import httpx import pytest from httpx import ASGITransport, AsyncClient from router.router import app from services.external import image_proxy from services.external.image_proxy import ImageFetchFailed, allowed, fetch_image OK_URL = "https://tong.visitkorea.or.kr/cms/resource/01/test_image.jpg" @pytest.mark.parametrize("url,ok", [ (OK_URL, True), ("https://www.culture.go.kr/upload/test.jpg", True), ("http://tong.visitkorea.or.kr/cms/a.jpg", False), ("https://evil.example.com/a.jpg", False), ("https://tong.visitkorea.or.kr.evil.example.com/a.jpg", False), ("https://tong.visitkorea.or.kr:8443/a.jpg", False), ("https://user:pw@tong.visitkorea.or.kr/a.jpg", False), ("https://169.254.169.254/latest/meta-data", False), ("not a url", False), ]) def test_allowed(url, ok): assert allowed(url) is ok def _client(handler) -> httpx.AsyncClient: return httpx.AsyncClient(transport=httpx.MockTransport(handler)) @pytest.mark.anyio async def test_fetch_image_returns_bytes_and_type(): async with _client(lambda r: httpx.Response(200, content=b"\xff\xd8jpeg", headers={"content-type": "image/jpeg; charset=binary"})) as c: assert await fetch_image(c, OK_URL) == (b"\xff\xd8jpeg", "image/jpeg") @pytest.mark.anyio @pytest.mark.parametrize("res", [ httpx.Response(302, headers={"location": "https://evil.example.com/a.jpg"}), httpx.Response(404), httpx.Response(200, content=b"", headers={"content-type": "text/html"}), httpx.Response(200, content=b"", headers={"content-type": "image/svg+xml"}), ]) async def test_fetch_image_rejects_redirect_error_and_non_image(res): async with _client(lambda r: res) as c: with pytest.raises(ImageFetchFailed): await fetch_image(c, OK_URL) @pytest.mark.anyio async def test_fetch_image_rejects_too_large(monkeypatch): monkeypatch.setattr(image_proxy, "MAX_BYTES", 10) async with _client(lambda r: httpx.Response(200, content=b"x" * 11, headers={"content-type": "image/png"})) as c: with pytest.raises(ImageFetchFailed): await fetch_image(c, OK_URL) @pytest.mark.anyio async def test_image_api_blocks_other_hosts(): async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c: res = await c.get("/v1/image", params={"url": "https://evil.example.com/a.jpg"}) assert res.status_code == 400 @pytest.mark.anyio async def test_image_api_serves_image_with_cache(monkeypatch): async def fake(_client, url): assert url == OK_URL return b"png-bytes", "image/png" monkeypatch.setattr("router.v1.image.image.fetch_image", fake) async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c: res = await c.get("/v1/image", params={"url": OK_URL}) assert res.status_code == 200 assert res.content == b"png-bytes" assert res.headers["content-type"] == "image/png" assert "max-age=86400" in res.headers["cache-control"] @pytest.mark.anyio async def test_image_api_reports_upstream_failure(monkeypatch): async def fail(_client, _url): raise ImageFetchFailed("status 404") monkeypatch.setattr("router.v1.image.image.fetch_image", fail) async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c: res = await c.get("/v1/image", params={"url": OK_URL}) assert res.status_code == 502 def test_allowed_follows_settings(monkeypatch): from config.settings import settings monkeypatch.setattr(settings, "image_proxy_hosts", " cdn.example.org , ") assert allowed("https://cdn.example.org/a.jpg") is True assert allowed(OK_URL) is False