Skip to content

Commit 790d74c

Browse files
committed
Add pcall guards to cjson.decode in commit, release, and expire Lua scripts
Malformed scope JSON stored in a reservation hash would crash the Lua script with an unhandled cjson error. Now all cjson.decode calls across all Lua scripts are pcall-guarded: - commit.lua: returns INTERNAL_ERROR on malformed JSON - release.lua: returns INTERNAL_ERROR on malformed JSON - expire.lua: silently skips budget adjustment (background sweep must not get stuck on corrupted data; reservation still expires) - extend.lua: already guarded in prior commit https://claude.ai/code/session_01RyMeX221wTfN1atWi98YK2
1 parent 8e77227 commit 790d74c

3 files changed

Lines changed: 10 additions & 3 deletions

File tree

cycles-protocol-service/cycles-protocol-service-data/src/main/resources/lua/commit.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ end
8787
if not (budgeted_scopes_json or affected_scopes_json) then
8888
return cjson.encode({error = "INTERNAL_ERROR", message = "Reservation missing scope data"})
8989
end
90-
local affected_scopes = cjson.decode(budgeted_scopes_json or affected_scopes_json)
90+
local ok, affected_scopes = pcall(cjson.decode, budgeted_scopes_json or affected_scopes_json)
91+
if not ok then
92+
return cjson.encode({error = "INTERNAL_ERROR", message = "Malformed scope JSON in reservation"})
93+
end
9194

9295
-- Calculate delta (actual - estimate)
9396
local delta = actual_amount - estimate_amount

cycles-protocol-service/cycles-protocol-service-data/src/main/resources/lua/expire.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ local affected_scopes_json = redis.call('HGET', key, 'affected_scopes')
4040
local budgeted_scopes_json = redis.call('HGET', key, 'budgeted_scopes')
4141

4242
if estimate_amount and estimate_unit and affected_scopes_json then
43-
local affected_scopes = cjson.decode(budgeted_scopes_json or affected_scopes_json)
43+
local ok, affected_scopes = pcall(cjson.decode, budgeted_scopes_json or affected_scopes_json)
44+
if not ok then affected_scopes = {} end
4445
for _, scope in ipairs(affected_scopes) do
4546
local budget_key = "budget:" .. scope .. ":" .. estimate_unit
4647
redis.call('HINCRBY', budget_key, 'reserved', -estimate_amount)

cycles-protocol-service/cycles-protocol-service-data/src/main/resources/lua/release.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ end
7070
if not (budgeted_scopes_json or affected_scopes_json) then
7171
return cjson.encode({error = "INTERNAL_ERROR", message = "Reservation missing scope data"})
7272
end
73-
local affected_scopes = cjson.decode(budgeted_scopes_json or affected_scopes_json)
73+
local ok, affected_scopes = pcall(cjson.decode, budgeted_scopes_json or affected_scopes_json)
74+
if not ok then
75+
return cjson.encode({error = "INTERNAL_ERROR", message = "Malformed scope JSON in reservation"})
76+
end
7477

7578
-- Release from all scopes
7679
for _, scope in ipairs(affected_scopes) do

0 commit comments

Comments
 (0)