fix(core): use CSPRNG for admin key auto-generation#13099
Open
sihyeonn wants to merge 1 commit intoapache:masterfrom
Open
fix(core): use CSPRNG for admin key auto-generation#13099sihyeonn wants to merge 1 commit intoapache:masterfrom
sihyeonn wants to merge 1 commit intoapache:masterfrom
Conversation
The autogenerate_admin_key() function used math.random() to generate admin API keys, which relies on a predictable PRNG seeded by math.randomseed(). This makes the generated keys guessable if the seed value can be inferred. Replace with resty.random.bytes() backed by OpenSSL RAND_bytes, which provides cryptographically secure random output. The key is hex-encoded, producing a 64-character string. Signed-off-by: Sihyeon Jang <sihyeon.jang@navercorp.com>
Baoyuantop
reviewed
Mar 18, 2026
Contributor
Baoyuantop
left a comment
There was a problem hiding this comment.
Please add a test case for this PR.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Closes #13092
The
autogenerate_admin_key()function inapisix/core/id.luausedmath.random()to generate admin API keys character by character.math.random()relies on a Lua PRNG that is predictable if the seed can be inferred, which is a security concern for authentication keys.Changes
math.random()withresty.random.bytes(32, true)which calls OpenSSLRAND_bytesfor cryptographically secure randomnessresty.random.bytes(32)(non-strong) if the entropy pool is insufficientresty.string.to_hex(), producing a 64-character keyBoth
resty.randomandresty.stringare already available in the OpenResty distribution and are used elsewhere in APISIX (e.g.,apisix/patch.lua,apisix/plugins/csrf.lua).