Skip to content

Commit

Permalink
Fix http typing (home-assistant#66506)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p authored Feb 14, 2022
1 parent 5a02bae commit 370832f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions .core_files.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ components: &components
- homeassistant/components/group/*
- homeassistant/components/hassio/*
- homeassistant/components/homeassistant/**
- homeassistant/components/http/**
- homeassistant/components/image/*
- homeassistant/components/input_boolean/*
- homeassistant/components/input_button/*
Expand Down
11 changes: 6 additions & 5 deletions homeassistant/components/http/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Support to serve the Home Assistant API as WSGI application."""
from __future__ import annotations

from ipaddress import ip_network
from ipaddress import IPv4Network, IPv6Network, ip_network
import logging
import os
import ssl
from typing import Any, Final, Optional, TypedDict, cast
from typing import Any, Final, Optional, TypedDict, Union, cast

from aiohttp import web
from aiohttp.typedefs import StrOrURL
Expand Down Expand Up @@ -109,7 +109,7 @@ class ConfData(TypedDict, total=False):
ssl_key: str
cors_allowed_origins: list[str]
use_x_forwarded_for: bool
trusted_proxies: list[str]
trusted_proxies: list[IPv4Network | IPv6Network]
login_attempts_threshold: int
ip_ban_enabled: bool
ssl_profile: str
Expand Down Expand Up @@ -216,7 +216,7 @@ def __init__(
ssl_key: str | None,
server_host: list[str] | None,
server_port: int,
trusted_proxies: list[str],
trusted_proxies: list[IPv4Network | IPv6Network],
ssl_profile: str,
) -> None:
"""Initialize the HTTP Home Assistant server."""
Expand Down Expand Up @@ -399,7 +399,8 @@ async def start_http_server_and_save_config(

if CONF_TRUSTED_PROXIES in conf:
conf[CONF_TRUSTED_PROXIES] = [
str(ip.network_address) for ip in conf[CONF_TRUSTED_PROXIES]
str(cast(Union[IPv4Network, IPv6Network], ip).network_address)
for ip in conf[CONF_TRUSTED_PROXIES]
]

store.async_delay_save(lambda: conf, SAVE_DELAY)
8 changes: 6 additions & 2 deletions homeassistant/components/http/ban.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from contextlib import suppress
from datetime import datetime
from http import HTTPStatus
from ipaddress import ip_address
from ipaddress import IPv4Address, IPv6Address, ip_address
import logging
from socket import gethostbyaddr, herror
from typing import Any, Final
Expand Down Expand Up @@ -189,7 +189,11 @@ async def process_success_login(request: Request) -> None:
class IpBan:
"""Represents banned IP address."""

def __init__(self, ip_ban: str, banned_at: datetime | None = None) -> None:
def __init__(
self,
ip_ban: str | IPv4Address | IPv6Address,
banned_at: datetime | None = None,
) -> None:
"""Initialize IP Ban object."""
self.ip_address = ip_address(ip_ban)
self.banned_at = banned_at or dt_util.utcnow()
Expand Down
6 changes: 4 additions & 2 deletions homeassistant/components/http/forwarded.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from __future__ import annotations

from collections.abc import Awaitable, Callable
from ipaddress import ip_address
from ipaddress import IPv4Network, IPv6Network, ip_address
import logging
from types import ModuleType
from typing import Literal
Expand All @@ -17,7 +17,9 @@

@callback
def async_setup_forwarded(
app: Application, use_x_forwarded_for: bool | None, trusted_proxies: list[str]
app: Application,
use_x_forwarded_for: bool | None,
trusted_proxies: list[IPv4Network | IPv6Network],
) -> None:
"""Create forwarded middleware for the app.
Expand Down

0 comments on commit 370832f

Please sign in to comment.