-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add default headers to webserver responses (#97784)
* Add default headers to webserver responses * Set default server header * Fix other tests
- Loading branch information
Showing
4 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""Middleware that helps with the control of headers in our responses.""" | ||
from __future__ import annotations | ||
|
||
from collections.abc import Awaitable, Callable | ||
|
||
from aiohttp.web import Application, Request, StreamResponse, middleware | ||
|
||
from homeassistant.core import callback | ||
|
||
|
||
@callback | ||
def setup_headers(app: Application, use_x_frame_options: bool) -> None: | ||
"""Create headers middleware for the app.""" | ||
|
||
@middleware | ||
async def headers_middleware( | ||
request: Request, handler: Callable[[Request], Awaitable[StreamResponse]] | ||
) -> StreamResponse: | ||
"""Process request and add headers to the responses.""" | ||
response = await handler(request) | ||
response.headers["Referrer-Policy"] = "no-referrer" | ||
response.headers["X-Content-Type-Options"] = "nosniff" | ||
|
||
# Set an empty server header, to prevent aiohttp of setting one. | ||
response.headers["Server"] = "" | ||
|
||
if use_x_frame_options: | ||
response.headers["X-Frame-Options"] = "SAMEORIGIN" | ||
|
||
return response | ||
|
||
app.middlewares.append(headers_middleware) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""Test headers middleware.""" | ||
from http import HTTPStatus | ||
|
||
from aiohttp import web | ||
|
||
from homeassistant.components.http.headers import setup_headers | ||
|
||
from tests.typing import ClientSessionGenerator | ||
|
||
|
||
async def mock_handler(request): | ||
"""Return OK.""" | ||
return web.Response(text="OK") | ||
|
||
|
||
async def test_headers_added(aiohttp_client: ClientSessionGenerator) -> None: | ||
"""Test that headers are being added on each request.""" | ||
app = web.Application() | ||
app.router.add_get("/", mock_handler) | ||
|
||
setup_headers(app, use_x_frame_options=True) | ||
|
||
mock_api_client = await aiohttp_client(app) | ||
resp = await mock_api_client.get("/") | ||
|
||
assert resp.status == HTTPStatus.OK | ||
assert resp.headers["Referrer-Policy"] == "no-referrer" | ||
assert resp.headers["Server"] == "" | ||
assert resp.headers["X-Content-Type-Options"] == "nosniff" | ||
assert resp.headers["X-Frame-Options"] == "SAMEORIGIN" | ||
|
||
|
||
async def test_allow_framing(aiohttp_client: ClientSessionGenerator) -> None: | ||
"""Test that we allow framing when disabled.""" | ||
app = web.Application() | ||
app.router.add_get("/", mock_handler) | ||
|
||
setup_headers(app, use_x_frame_options=False) | ||
|
||
mock_api_client = await aiohttp_client(app) | ||
resp = await mock_api_client.get("/") | ||
|
||
assert resp.status == HTTPStatus.OK | ||
assert "X-Frame-Options" not in resp.headers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters