Skip to content

Commit 274b160

Browse files
committed
Ensure the hook secret can't get removed from memory.
If the shared dict ran out of memory, then the hook secret could end up getting removed, leading to errors like this: #48 (comment) By storing the hook secret as a global variable, it should not longer be subject to getting removed from the shared dict.
1 parent e6827dd commit 274b160

File tree

7 files changed

+15
-10
lines changed

7 files changed

+15
-10
lines changed

.luacheckrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
globals = {
2+
"AUTO_SSL_HOOK_SECRET",
23
"ngx",
34
}
45

lib/resty/auto-ssl/init_master.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ end
3030
local function generate_hook_sever_secret()
3131
-- Generate the secret token.
3232
local random = resty_random.bytes(32)
33-
ngx.shared.auto_ssl:set("hook_server:secret", str.to_hex(random))
33+
AUTO_SSL_HOOK_SECRET = str.to_hex(random)
3434
end
3535

3636
local function generate_config(auto_ssl_instance)

lib/resty/auto-ssl/servers/hook.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ return function(auto_ssl_instance)
88
local path = ngx.var.request_uri
99
local params = ngx.req.get_post_args()
1010

11-
if ngx.var.http_x_hook_secret ~= ngx.shared.auto_ssl:get("hook_server:secret") then
11+
if ngx.var.http_x_hook_secret ~= AUTO_SSL_HOOK_SECRET then
1212
return ngx.exit(ngx.HTTP_FORBIDDEN)
1313
end
1414

lib/resty/auto-ssl/ssl_certificate.lua

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ local function convert_to_der_and_cache(domain, fullchain_pem, privkey_pem, newl
2121
-- across multiple servers).
2222
local _, set_fullchain_err = ngx.shared.auto_ssl:set("domain:fullchain_der:" .. domain, fullchain_der, 3600)
2323
if set_fullchain_err then
24-
ngx.log(ngx.ERR, "auto-ssl: failed to set shdict cache of certificate chain for " .. domain, set_fullchain_err)
24+
ngx.log(ngx.ERR, "auto-ssl: failed to set shdict cache of certificate chain for " .. domain .. ": ", set_fullchain_err)
2525
end
2626

2727
local _, set_privkey_err = ngx.shared.auto_ssl:set("domain:privkey_der:" .. domain, privkey_der, 3600)
2828
if set_privkey_err then
29-
ngx.log(ngx.ERR, "auto-ssl: failed to set shdict cache of private key for " .. domain, set_privkey_err)
29+
ngx.log(ngx.ERR, "auto-ssl: failed to set shdict cache of private key for " .. domain .. ": ", set_privkey_err)
3030
end
3131

3232
return fullchain_der, privkey_der, newly_issued
@@ -184,7 +184,10 @@ local function set_ocsp_stapling(domain, fullchain_der, newly_issued)
184184

185185
-- Cache the OCSP stapling response for 1 hour (this is what nginx does by
186186
-- default).
187-
ngx.shared.auto_ssl:set("domain:ocsp:" .. domain, ocsp_resp, 3600)
187+
local _, set_ocsp_err = ngx.shared.auto_ssl:set("domain:ocsp:" .. domain, ocsp_resp, 3600)
188+
if set_ocsp_err then
189+
ngx.log(ngx.ERR, "auto-ssl: failed to set shdict cache of OCSP response for " .. domain .. ": ", set_ocsp_err)
190+
end
188191
end
189192

190193
-- Set the OCSP stapling response.

lib/resty/auto-ssl/ssl_providers/lets_encrypt.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function _M.issue_cert(auto_ssl_instance, domain)
1515
assert(type(hook_port) == "number", "hook_port must be a number")
1616
assert(hook_port <= 65535, "hook_port must be below 65536")
1717

18-
local hook_secret = ngx.shared.auto_ssl:get("hook_server:secret")
18+
local hook_secret = AUTO_SSL_HOOK_SECRET
1919
assert(type(hook_secret) == "string", "hook_server:secret must be a string")
2020

2121
local env_vars =

lib/resty/auto-ssl/utils/start_sockproc.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ local function start()
55
local exit_status = os.execute("umask 0022 && " .. auto_ssl.lua_root .. "/bin/resty-auto-ssl/start_sockproc")
66
-- Lua 5.2+ returns boolean. Prior versions return status code.
77
if exit_status == 0 or exit_status == true then
8-
ngx.shared.auto_ssl:set("sockproc_started", true)
8+
local _, set_err = ngx.shared.auto_ssl:set("sockproc_started", true)
9+
if set_err then
10+
ngx.log(ngx.ERR, "auto-ssl: failed to set shdict for sockproc_started: ", set_err)
11+
end
912
else
1013
ngx.log(ngx.ERR, "auto-ssl: failed to start sockproc")
1114
end

t/multiple_workers.t

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ $TEST_NGINX_USER
9595
-- Since repeat_each is being used, clear the cached information across
9696
-- test runs so we try to issue a new cert each time.
9797
ngx.log(ngx.DEBUG, "auto-ssl: delete: domain:fullchain_der:" .. host)
98-
ngx.shared.auto_ssl:delete("domain:fullchain_der:" .. host)
99-
ngx.shared.auto_ssl:delete("domain:privkey_der:" .. host)
100-
ngx.shared.auto_ssl:delete("domain:ocsp:" .. host)
98+
ngx.shared.auto_ssl:flush_all()
10199
ngx.shared.test_counts:flush_all()
102100
os.execute("rm -rf $TEST_NGINX_RESTY_AUTO_SSL_DIR/storage/file/*")
103101

0 commit comments

Comments
 (0)