[wizard] Use secrets module for fallback AP password generation (#13864)

This commit is contained in:
J. Nick Koston
2026-02-09 03:25:41 -06:00
committed by GitHub
parent 6ee185c58a
commit 5370687001
2 changed files with 13 additions and 3 deletions
+1 -2
View File
@@ -1,6 +1,5 @@
import base64 import base64
from pathlib import Path from pathlib import Path
import random
import secrets import secrets
import string import string
from typing import Literal, NotRequired, TypedDict, Unpack from typing import Literal, NotRequired, TypedDict, Unpack
@@ -130,7 +129,7 @@ def wizard_file(**kwargs: Unpack[WizardFileKwargs]) -> str:
if len(ap_name) > 32: if len(ap_name) > 32:
ap_name = ap_name_base ap_name = ap_name_base
kwargs["fallback_name"] = ap_name kwargs["fallback_name"] = ap_name
kwargs["fallback_psk"] = "".join(random.choice(letters) for _ in range(12)) kwargs["fallback_psk"] = "".join(secrets.choice(letters) for _ in range(12))
base = BASE_CONFIG_FRIENDLY if kwargs.get("friendly_name") else BASE_CONFIG base = BASE_CONFIG_FRIENDLY if kwargs.get("friendly_name") else BASE_CONFIG
+12 -1
View File
@@ -2,7 +2,7 @@
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
from unittest.mock import MagicMock from unittest.mock import MagicMock, patch
import pytest import pytest
from pytest import MonkeyPatch from pytest import MonkeyPatch
@@ -632,3 +632,14 @@ def test_wizard_accepts_rpipico_board(tmp_path: Path, monkeypatch: MonkeyPatch):
# rpipico doesn't support WiFi, so no api_encryption_key or ota_password # rpipico doesn't support WiFi, so no api_encryption_key or ota_password
assert "api_encryption_key" not in call_kwargs assert "api_encryption_key" not in call_kwargs
assert "ota_password" not in call_kwargs assert "ota_password" not in call_kwargs
def test_fallback_psk_uses_secrets_choice(
default_config: dict[str, Any],
) -> None:
"""Test that fallback PSK is generated using secrets.choice."""
with patch("esphome.wizard.secrets.choice", return_value="X") as mock_choice:
config = wz.wizard_file(**default_config)
assert 'password: "XXXXXXXXXXXX"' in config
assert mock_choice.call_count == 12