WebUI: use secure random number generator for generating random port

Cryptographically strong random number generators are generally preferred over others.
This commit is contained in:
Chocobo1 2025-07-29 17:07:02 +08:00
commit 85c3f6227b
No known key found for this signature in database
GPG key ID: 210D9C873253A68C

View file

@ -2127,9 +2127,11 @@ Use ';' to split multiple entries. Can use wildcard '*'.)QBT_TR[CONTEXT=OptionsD
}; };
const generateRandomPort = () => { const generateRandomPort = () => {
const min = 1024; // don't use modulo operations to avoid 'modulo bias'
const max = 65535; const buffer = new Uint16Array(1);
const port = Math.floor(Math.random() * (max - min + 1) + min); let port = crypto.getRandomValues(buffer)[0];
while (port < 1024)
port = crypto.getRandomValues(buffer)[0];
document.getElementById("portValue").value = port; document.getElementById("portValue").value = port;
}; };