refactor: use secrets module for generated secret key (#30441)

Generates the default WEBUI_SECRET_KEY file with secrets.token_bytes, matching how the start scripts read from the OS random source.
This commit is contained in:
Classic298 2026-09-24 05:36:26 +02:00 • committed by GitHub
parent a973e77cfa
commit 443736e1fe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,6 +1,6 @@
import base64
import os
import random
import secrets
import sys
from pathlib import Path
from typing import Annotated
@ -45,7 +45,7 @@ def serve(
if key_length < 1:
raise ValueError('WEBUI_SECRET_KEY_LENGTH must be a positive integer')
typer.echo(f'Generating a new secret key and saving it to {KEY_FILE}')
KEY_FILE.write_bytes(base64.b64encode(random.randbytes(key_length)))
KEY_FILE.write_bytes(base64.b64encode(secrets.token_bytes(key_length)))
typer.echo(f'Loading WEBUI_SECRET_KEY from {KEY_FILE}')
os.environ['WEBUI_SECRET_KEY'] = KEY_FILE.read_text()