Skip to content
This repository was archived by the owner on Mar 16, 2026. It is now read-only.

Commit 1a68565

Browse files
jainejaine
authored andcommitted
auth: make signature keys sodium-safe
1 parent 718a348 commit 1a68565

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

ao/shared/auth.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,12 @@ function Auth.require_signature(msg)
197197
if not SIG_SECRET then
198198
return not REQUIRE_SIGNATURE, REQUIRE_SIGNATURE and "missing_signature_secret" or nil
199199
end
200+
local function canonical_key(secret)
201+
if not secret then return nil end
202+
if #secret == 32 then return secret end
203+
if #secret > 32 then return secret:sub(1, 32) end
204+
return secret .. string.rep("\0", 32 - #secret)
205+
end
200206
if openssl_ok and openssl.hmac then
201207
local raw = openssl.hmac.digest("sha256", target, SIG_SECRET, true)
202208
if not raw then return false, "sig_verify_failed" end
@@ -206,7 +212,8 @@ function Auth.require_signature(msg)
206212
end
207213
return true
208214
elseif sodium_ok and sodium.crypto_auth then
209-
local tag = sodium.crypto_auth(target, SIG_SECRET)
215+
local key = canonical_key(SIG_SECRET)
216+
local tag = sodium.crypto_auth(target, key)
210217
local hex = sodium.to_hex(tag)
211218
if hex:lower() ~= tostring(sig):lower() then
212219
return false, "bad_signature"

scripts/verify/contracts.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ if not sodium_ok then
3333
sodium_ok, sodium = pcall(require, "luasodium")
3434
end
3535

36+
local function canonical_key(secret)
37+
if not secret then return nil end
38+
if #secret == 32 then return secret end
39+
if #secret > 32 then return secret:sub(1, 32) end
40+
return secret .. string.rep("\0", 32 - #secret)
41+
end
42+
3643
local function hmac_sign(action, site_id, request_id)
3744
if not (SIG_SECRET and SIG_SECRET ~= "") then return nil end
3845
local target = string.format("%s|%s|%s", action or "", site_id or "", request_id or "")
@@ -43,7 +50,8 @@ local function hmac_sign(action, site_id, request_id)
4350
end
4451

4552
if sodium_ok and sodium.crypto_auth then
46-
local tag = sodium.crypto_auth(target, SIG_SECRET)
53+
local key = canonical_key(SIG_SECRET)
54+
local tag = sodium.crypto_auth(target, key)
4755
if tag then
4856
if sodium.to_hex then return sodium.to_hex(tag) end
4957
return (tag:gsub(".", function(c) return string.format("%02x", string.byte(c)) end))

0 commit comments

Comments
 (0)