Skip to content

fix(core): use CSPRNG for admin key auto-generation#13099

Open
sihyeonn wants to merge 1 commit intoapache:masterfrom
sihyeonn:fix/admin-key-csprng
Open

fix(core): use CSPRNG for admin key auto-generation#13099
sihyeonn wants to merge 1 commit intoapache:masterfrom
sihyeonn:fix/admin-key-csprng

Conversation

@sihyeonn
Copy link
Contributor

Description

Closes #13092

The autogenerate_admin_key() function in apisix/core/id.lua used math.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

  • Replace math.random() with resty.random.bytes(32, true) which calls OpenSSL RAND_bytes for cryptographically secure randomness
  • Falls back to resty.random.bytes(32) (non-strong) if the entropy pool is insufficient
  • Hex-encode the output via resty.string.to_hex(), producing a 64-character key

Both resty.random and resty.string are already available in the OpenResty distribution and are used elsewhere in APISIX (e.g., apisix/patch.lua, apisix/plugins/csrf.lua).

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>
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. bug Something isn't working labels Mar 18, 2026
Copy link
Contributor

@Baoyuantop Baoyuantop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test case for this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

2 participants