Fix duplicate Ban file entries (#103953)
This commit is contained in:
parent
7f08f139d6
commit
aca48b5e45
2 changed files with 29 additions and 2 deletions
|
@ -243,5 +243,6 @@ class IpBanManager:
|
||||||
|
|
||||||
async def async_add_ban(self, remote_addr: IPv4Address | IPv6Address) -> None:
|
async def async_add_ban(self, remote_addr: IPv4Address | IPv6Address) -> None:
|
||||||
"""Add a new IP address to the banned list."""
|
"""Add a new IP address to the banned list."""
|
||||||
new_ban = self.ip_bans_lookup[remote_addr] = IpBan(remote_addr)
|
if remote_addr not in self.ip_bans_lookup:
|
||||||
await self.hass.async_add_executor_job(self._add_ban, new_ban)
|
new_ban = self.ip_bans_lookup[remote_addr] = IpBan(remote_addr)
|
||||||
|
await self.hass.async_add_executor_job(self._add_ban, new_ban)
|
||||||
|
|
|
@ -392,3 +392,29 @@ async def test_failed_login_attempts_counter(
|
||||||
resp = await client.get("/auth_false")
|
resp = await client.get("/auth_false")
|
||||||
assert resp.status == HTTPStatus.UNAUTHORIZED
|
assert resp.status == HTTPStatus.UNAUTHORIZED
|
||||||
assert app[KEY_FAILED_LOGIN_ATTEMPTS][remote_ip] == 2
|
assert app[KEY_FAILED_LOGIN_ATTEMPTS][remote_ip] == 2
|
||||||
|
|
||||||
|
|
||||||
|
async def test_single_ban_file_entry(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
) -> None:
|
||||||
|
"""Test that only one item is added to ban file."""
|
||||||
|
app = web.Application()
|
||||||
|
app["hass"] = hass
|
||||||
|
|
||||||
|
async def unauth_handler(request):
|
||||||
|
"""Return a mock web response."""
|
||||||
|
raise HTTPUnauthorized
|
||||||
|
|
||||||
|
app.router.add_get("/example", unauth_handler)
|
||||||
|
setup_bans(hass, app, 2)
|
||||||
|
mock_real_ip(app)("200.201.202.204")
|
||||||
|
|
||||||
|
manager: IpBanManager = app[KEY_BAN_MANAGER]
|
||||||
|
m_open = mock_open()
|
||||||
|
|
||||||
|
with patch("homeassistant.components.http.ban.open", m_open, create=True):
|
||||||
|
remote_ip = ip_address("200.201.202.204")
|
||||||
|
await manager.async_add_ban(remote_ip)
|
||||||
|
await manager.async_add_ban(remote_ip)
|
||||||
|
|
||||||
|
assert m_open.call_count == 1
|
||||||
|
|
Loading…
Add table
Reference in a new issue