Skip to content

Commit 8fd99e3

Browse files
committed
Simplify random_token implementation to use set_secure_random_alphanum.
Use nginx NDK to use set_secure_random_alphanum for our random_token implementation. This is both faster and simpler than our previous implementation. It's limited to 64 characters in length, but that should be fine for our purposes.
1 parent 1e17f46 commit 8fd99e3

File tree

3 files changed

+4
-42
lines changed

3 files changed

+4
-42
lines changed

src/api-umbrella/cli/read_config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ local function set_cached_random_tokens()
385385
if not config["web"]["rails_secret_token"] then
386386
deep_defaults(cached, {
387387
web = {
388-
rails_secret_token = random_token(128),
388+
rails_secret_token = random_token(64),
389389
},
390390
})
391391
end

src/api-umbrella/proxy/models/active_config.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local host_normalize = require "api-umbrella.utils.host_normalize"
55
local load_backends = require "api-umbrella.proxy.load_backends"
66
local mustache_unescape = require "api-umbrella.utils.mustache_unescape"
77
local plutils = require "pl.utils"
8+
local random_token = require "api-umbrella.utils.random_token"
89
local resolve_backend_dns = require "api-umbrella.proxy.jobs.resolve_backend_dns"
910
local tablex = require "pl.tablex"
1011
local utils = require "api-umbrella.proxy.utils"
@@ -183,7 +184,7 @@ end
183184

184185
local function parse_website_backend(website_backend)
185186
if not website_backend["_id"] then
186-
website_backend["_id"] = ndk.set_var.set_secure_random_alphanum(32)
187+
website_backend["_id"] = random_token(32)
187188
end
188189

189190
if website_backend["frontend_host"] then
Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,3 @@
1-
local resty_random = require "resty.random"
2-
3-
local encode_base64 = ngx.encode_base64
4-
local gsub = ngx.re.gsub
5-
local random_bytes = resty_random.bytes
6-
71
return function(length)
8-
local token = ""
9-
-- Loop until we've generated a valid token. The basic process:
10-
--
11-
-- 1. Generate secure random bytes.
12-
-- 2. Convert random bytes to base64.
13-
-- 3. Strip out special characters from base64 result, so we're left with
14-
-- just alphanumerics.
15-
--
16-
-- It should be extraordinarily rare that this needs to loop, but since we
17-
-- strip out some of the special characters from the resulting base64 string,
18-
-- this loops in case we strip more than expected.
19-
while string.len(token) < length do
20-
-- Attempt to generate cryptographically secure random bytes. We
21-
-- purposefully generate more bytes than we need, since we'll be stripping
22-
-- some of the base64 characters out.
23-
local num_bytes = length + 10
24-
local strong_random = random_bytes(num_bytes, true)
25-
if not strong_random then
26-
ngx.log(ngx.WARN, "Could not generate cryptographically secure random data. Falling back to non-secure random data.")
27-
strong_random = random_bytes(num_bytes, false)
28-
end
29-
30-
-- Encode with base64.
31-
token = token .. encode_base64(strong_random)
32-
33-
-- Strip +, /, and = out of the base64 result, since we just want a-z, A-Z,
34-
-- and 0-9 in our tokens.
35-
token = gsub(token, "[+/=]", "", "jo")
36-
37-
-- Take just the number of characters requested.
38-
token = string.sub(token, 1, length)
39-
end
40-
41-
return token
2+
return ndk.set_var.set_secure_random_alphanum(length)
423
end

0 commit comments

Comments
 (0)