|
| 1 | +import pytest |
| 2 | +from aiohttp import web |
| 3 | +from aiohttp.test_utils import make_mocked_request |
| 4 | +from aiohttp.typedefs import Handler |
| 5 | + |
| 6 | +from ai.backend.web.security import ( |
| 7 | + SecurityPolicy, |
| 8 | + add_self_content_security_policy, |
| 9 | + reject_access_for_unsafe_file_policy, |
| 10 | + reject_metadata_local_link_policy, |
| 11 | + security_policy_middleware, |
| 12 | + set_content_type_nosniff_policy, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def default_app(): |
| 18 | + app = web.Application(middlewares=[security_policy_middleware]) |
| 19 | + app["security_policy"] = SecurityPolicy.default_policy() |
| 20 | + return app |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +async def async_handler() -> Handler: |
| 25 | + async def handler(request): |
| 26 | + return web.Response() |
| 27 | + |
| 28 | + return handler |
| 29 | + |
| 30 | + |
| 31 | +async def test_default_security_policy_reject_metadata_local_link( |
| 32 | + default_app, async_handler |
| 33 | +) -> None: |
| 34 | + request = make_mocked_request("GET", "/", headers={"Host": "169.254.169.254"}, app=default_app) |
| 35 | + with pytest.raises(web.HTTPForbidden): |
| 36 | + await security_policy_middleware(request, async_handler) |
| 37 | + |
| 38 | + |
| 39 | +async def test_default_security_policy_response(default_app, async_handler) -> None: |
| 40 | + request = make_mocked_request("GET", "/", headers={"Host": "localhost"}, app=default_app) |
| 41 | + response = await security_policy_middleware(request, async_handler) |
| 42 | + assert ( |
| 43 | + response.headers["Content-Security-Policy"] |
| 44 | + == "default-src 'self'; frame-ancestors 'none'; form-action 'self';" |
| 45 | + ) |
| 46 | + assert response.headers["X-Content-Type-Options"] == "nosniff" |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.parametrize( |
| 50 | + "meta_local_link", |
| 51 | + [ |
| 52 | + "metadata.google.internal", |
| 53 | + "169.254.169.254", |
| 54 | + "100.100.100.200", |
| 55 | + "alibaba.zaproxy.org", |
| 56 | + "metadata.oraclecloud.com", |
| 57 | + ], |
| 58 | +) |
| 59 | +async def test_reject_metadata_local_link_policy(async_handler, meta_local_link) -> None: |
| 60 | + test_app = web.Application() |
| 61 | + test_app["security_policy"] = SecurityPolicy( |
| 62 | + request_policies=[reject_metadata_local_link_policy], response_policies=[] |
| 63 | + ) |
| 64 | + request = make_mocked_request("GET", "/", headers={"Host": meta_local_link}, app=test_app) |
| 65 | + with pytest.raises(web.HTTPForbidden): |
| 66 | + await security_policy_middleware(request, async_handler) |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.parametrize( |
| 70 | + "url_suffix", |
| 71 | + [ |
| 72 | + "._darcs", |
| 73 | + ".bzr", |
| 74 | + ".hg", |
| 75 | + "BitKeeper", |
| 76 | + ".bak", |
| 77 | + ".log", |
| 78 | + ".git", |
| 79 | + ".svn", |
| 80 | + ], |
| 81 | +) |
| 82 | +async def test_reject_access_for_unsafe_file_policy(async_handler, url_suffix) -> None: |
| 83 | + test_app = web.Application() |
| 84 | + test_app["security_policy"] = SecurityPolicy( |
| 85 | + request_policies=[reject_access_for_unsafe_file_policy], response_policies=[] |
| 86 | + ) |
| 87 | + request = make_mocked_request( |
| 88 | + "GET", f"/{url_suffix}", headers={"Host": "localhost"}, app=test_app |
| 89 | + ) |
| 90 | + with pytest.raises(web.HTTPForbidden): |
| 91 | + await security_policy_middleware(request, async_handler) |
| 92 | + |
| 93 | + |
| 94 | +async def test_add_self_content_security_policy(async_handler) -> None: |
| 95 | + test_app = web.Application() |
| 96 | + test_app["security_policy"] = SecurityPolicy( |
| 97 | + request_policies=[], response_policies=[add_self_content_security_policy] |
| 98 | + ) |
| 99 | + request = make_mocked_request("GET", "/", headers={"Host": "localhost"}, app=test_app) |
| 100 | + response = await security_policy_middleware(request, async_handler) |
| 101 | + assert ( |
| 102 | + response.headers["Content-Security-Policy"] |
| 103 | + == "default-src 'self'; frame-ancestors 'none'; form-action 'self';" |
| 104 | + ) |
| 105 | + |
| 106 | + |
| 107 | +async def test_set_content_type_nosniff_policy(async_handler) -> None: |
| 108 | + test_app = web.Application() |
| 109 | + test_app["security_policy"] = SecurityPolicy( |
| 110 | + request_policies=[], response_policies=[set_content_type_nosniff_policy] |
| 111 | + ) |
| 112 | + request = make_mocked_request("GET", "/", headers={"Host": "localhost"}, app=test_app) |
| 113 | + response = await security_policy_middleware(request, async_handler) |
| 114 | + assert response.headers["X-Content-Type-Options"] == "nosniff" |
0 commit comments