Strengthen toggleItem security

- locked down `addItem` to be a lot more strict to reduce exploits
- fixed issue for if no supported inventory was found it now uses the correct functions
This commit is contained in:
Jim Shield committed 2026-11-05 18:03:24 +00:00
1 parent 8c947aae73
commit 40eed38dc2
4 files changed
+573 -55

No files matched your search

+105
View File
@@ -0,0 +1,105 @@
-- Client Item Registry
-- ---------------------------------------------------------------------------
-- Dynamically derived list of items a client is allowed to request through the
-- ":server:toggleItem" net event.
--
-- There is no hardcoded item list. The registry is built once, on first use,
-- from the host resource's own tables:
--
-- * Crafting - recipe results and ingredients
-- * Stores - shop item definitions ({ name = "item" })
-- * MaterialRecieve - material returns
-- * createUseableItem() registrations
-- * allowClientItem() calls
--
-- Because it reads the script's existing definitions, adding a new recipe or
-- shop item automatically makes it grantable. Servers do not need to maintain
-- a second list.
--
-- Items that are not found in any of the above are refused.
-- ---------------------------------------------------------------------------
ClientItemAllowlist = ClientItemAllowlist or {}
ClientItemUseable = ClientItemUseable or {}
local built = false
--- Default maximum a client may request of a dynamically discovered item.
ClientItemDefaultMax = ClientItemDefaultMax or 10
local function add(item, max)
if type(item) ~= "string" or item == "" then return end
if ClientItemAllowlist[item] == nil then
ClientItemAllowlist[item] = tonumber(max) or ClientItemDefaultMax
end
end
-- Walks a table and treats every string key and string value as a candidate
-- item name. Non-item strings are harmless because the caller still runs
-- doesItemExist() before granting anything.
local function scan(tbl, seen, depth)
if type(tbl) ~= "table" or seen[tbl] then return end
if depth and depth > 12 then return end
seen[tbl] = true
for k, v in pairs(tbl) do
if type(k) == "string" then add(k) end
if type(v) == "table" then
scan(v, seen, (depth or 0) + 1)
elseif type(v) == "string" then
add(v)
end
end
end
--- Builds the registry from whatever item tables the host resource exposes.
local function build()
if built then return end
built = true
local seen = {}
if type(Crafting) == "table" then scan(Crafting, seen, 0) end
if type(Stores) == "table" then scan(Stores, seen, 0) end
if type(MaterialRecieve) == "table" then scan(MaterialRecieve, seen, 0) end
-- Scripts keep item names in Config too (shops, sell tables, processing).
-- This is what makes the registry follow each server's own configuration.
if type(Config) == "table" then scan(Config, seen, 0) end
for item in pairs(ClientItemUseable) do add(item) end
local count = 0
for _ in pairs(ClientItemAllowlist) do count = count + 1 end
debugPrint("^6Bridge^7: ^2Client item registry built for ^3"..tostring(getScript()).."^7 - ^6"..count.."^7 entries")
if count == 0 then
print("^6Bridge^7: ^3"..tostring(getScript()).." ^1has no item definitions to build a client registry from^7.")
print("^6Bridge^7: ^1Client-side addItem/removeItem grants are disabled for this resource^7.")
print("^6Bridge^7: ^2Call ^3allowClientItem(\"item_name\", maxAmount)^2 if this resource needs them^7.")
end
end
--- Registers an item as client-grantable. Use this for items that are not
--- covered by the host resource's Crafting/Stores/MaterialRecieve tables.
--- @param item string
--- @param maxAmount number|nil
function allowClientItem(item, maxAmount)
if type(item) ~= "string" or item == "" then return end
ClientItemAllowlist[item] = tonumber(maxAmount) or ClientItemDefaultMax
end
--- Records an item registered through createUseableItem().
--- @param item string
function registerClientUseableItem(item)
if type(item) == "string" and item ~= "" then
ClientItemUseable[item] = true
end
end
--- Returns the maximum amount a client may request, or nil if not allowed.
--- Triggers the one-time build on first call.
--- @param item string
--- @return number|nil
function isClientItemAllowed(item)
if not built then build() end
return ClientItemAllowlist[item]
end
+93 -54
View File
@@ -1489,6 +1489,10 @@ end
--- ```
function createUseableItem(item, funct)
if doesItemExist(item) then
-- Record it in the client item registry. Items a script makes usable are
-- part of its own item definitions, so this keeps the registry dynamic.
if registerClientUseableItem then registerClientUseableItem(item) end
local useableFunc = {
{ framework = ESXExport,
func = function(item, funct)
@@ -1621,78 +1625,78 @@ end
--- ```
local MAX_ITEM_REQUEST = 1000
RegisterNetEvent(getScript()..":server:toggleItem", function(give, item, amount, newsrc, info, slot, token)
local excludeRes = {
[QBExport] = true,
[ESXExport] = true,
[VorpExport] = true,
}
local invokingRes = GetInvokingResource()
--debugPrint(GetInvokingResource())
if invokingRes and invokingRes ~= getScript() and not excludeRes[invokingRes] then
-- A client-triggered net event has no invoking resource.
local invokingRes = GetInvokingResource()
local isClient = (invokingRes == nil or invokingRes == "")
-- Server-side callers must be this resource or an allowed core.
if not isClient and invokingRes ~= getScript() and not excludeRes[invokingRes] then
debugPrint("^1Error^7: ^1Possible exploit^7, ^1vital function was called from an external resource^7")
return
end
if not doesItemExist(item) then
print("^1Error^7 - ^2Tried to "..(tostring(give) == "true" and "add" or "remove").." '^3"..item.."^7' but it doesn't exist")
if type(item) ~= "string" or not doesItemExist(item) then
print("^1Error^7 - ^2Tried to "..(give and "add" or "remove").." '^3"..tostring(item).."^7' but it doesn't exist")
return
end
local src = (newsrc and tonumber(newsrc)) or source
local giveItem = (give == true or give == 1)
if (give == true or give == 1) then
if newsrc == nil then -- this must be coming from client this would be blank
-- Resolve the target. Clients may only ever affect themselves.
local src
if isClient then
src = source
if not src or src <= 0 then return end
if newsrc ~= nil then
print("^1Exploit^7: ^3"..tostring(src).." ^1tried to target another player through toggleItem^7")
return
end
else
src = (newsrc and tonumber(newsrc)) or source
end
if not src or src <= 0 then return end
-- Bound and validate the amount.
local reqAmount = tonumber(amount) or 1
if reqAmount ~= math.floor(reqAmount) or reqAmount <= 0 or reqAmount > MAX_ITEM_REQUEST then
print("^1Exploit^7: ^3"..tostring(src).." ^1sent an invalid amount to toggleItem^7: "..tostring(amount).." ("..(giveItem and "add" or "remove").." "..item..")")
return
end
if type(info) ~= "table" then info = nil end
if type(slot) ~= "number" then slot = nil end
local action = giveItem and "addItem" or "removeItem"
local invName = ""
if giveItem then
if isClient then
-- Single-use token (10 second expiry).
if not checkToken(src, token, "item", item) then
return
end
end
end
local action = (tostring(give) == "true" and "addItem" or "removeItem")
local remamount = amount or 1
if item == nil then return end
local invName = ""
if give == 0 or give == false then
if remamount < 0 then
print("^1Error^7: ^7src - ^1Client tried to remove a minus number of item ^7- "..item)
return
end
if not hasItem(item, amount or 1, src) then
dupeWarn(src, item, amount)
else
for i = 1, #InvFunc do
local inv = InvFunc[i]
if isStarted(inv.invName) then
invName = inv.invName
inv.removeItem(src, item, remamount)
break
end
-- Only items this resource actually defines may be granted.
local allowedMax = isClientItemAllowed(item)
if not allowedMax then
print("^1Exploit^7: ^3"..tostring(src).." ^1requested ^3"..item.." ^1via toggleItem but it is not in this resource's item registry^7")
return
end
-----
-- Fallback for if no inventory found:
-----
if invName == "" then
print("^4ERROR^7: No Supported Inventory detected - ^2Falling back to core functions")
if isStarted(QBExport) or isStarted(QBXExport) then -- if qbcore or qbxcore, just use core functions
invName = isStarted(QBXExport) and QBXExport or isStarted(QBExport) and QBExport
Core.Functions.GetPlayer(src).Functions.RemoveItem(item, remamount, slot)
elseif ESX and isStarted(ESXExport) then -- if esx then use core functions
invName = ESX
ESX.GetPlayerFromId(src).removeInventoryItem(item, remamount)
end
else
debugPrint("^6Bridge^7: ^3"..action.."^7[^6"..invName.."^7] Player(^3"..src.."^7) "..getItemLabel(item).."("..item..") x"..(amount or 1))
if reqAmount > allowedMax then
print("^1Exploit^7: ^3"..tostring(src).." ^1requested x"..reqAmount.." of ^3"..item.." ^1(max ^3"..allowedMax.."^7)")
return
end
end
else
local amountToAdd = amount or 1
local amountToAdd = reqAmount
for i = 1, #InvFunc do
local inv = InvFunc[i]
if isStarted(inv.invName) then
@@ -1713,7 +1717,39 @@ RegisterNetEvent(getScript()..":server:toggleItem", function(give, item, amount,
ESX.GetPlayerFromId(src).addInventoryItem(item, amountToAdd)
end
else
debugPrint("^6Bridge^7: ^3"..action.."^7[^6"..invName.."^7] Player(^3"..src.."^7) "..getItemLabel(item).."("..item..") x"..(amount or 1))
debugPrint("^6Bridge^7: ^3"..action.."^7[^6"..invName.."^7] Player(^3"..src.."^7) "..getItemLabel(item).."("..item..") x"..amountToAdd)
end
else
if not hasItem(item, reqAmount, src) then
dupeWarn(src, item, amount)
else
for i = 1, #InvFunc do
local inv = InvFunc[i]
if isStarted(inv.invName) then
invName = inv.invName
inv.removeItem(src, item, reqAmount)
break
end
end
-----
-- Fallback for if no inventory found:
-----
if invName == "" then
print("^4ERROR^7: No Supported Inventory detected - ^2Falling back to core functions")
if isStarted(QBExport) or isStarted(QBXExport) then -- if qbcore or qbxcore, just use core functions
invName = isStarted(QBXExport) and QBXExport or isStarted(QBExport) and QBExport
Core.Functions.GetPlayer(src).Functions.RemoveItem(item, reqAmount, slot)
elseif ESX and isStarted(ESXExport) then -- if esx then use core functions
invName = ESX
ESX.GetPlayerFromId(src).removeInventoryItem(item, reqAmount)
end
else
debugPrint("^6Bridge^7: ^3"..action.."^7[^6"..invName.."^7] Player(^3"..src.."^7) "..getItemLabel(item).."("..item..") x"..reqAmount)
end
end
end
end)
@@ -1732,8 +1768,11 @@ end)
--- dupeWarn(playerId, "health_potion")
--- ```
function dupeWarn(src, item, message)
local name = getPlayer(src).name
print(message or "^5DupeWarn^7: "..name.." (^1"..tostring(src).."^7) ^2Tried to remove item '^3"..item.."^7'^2 but it wasn't there^7")
src = tonumber(src)
if not src or src <= 0 then return end
local player = getPlayer(src)
local name = (player and player.name) or ("Unknown("..tostring(src)..")")
print(message or "^5DupeWarn^7: "..name.." (^1"..tostring(src).."^7) ^2Tried to remove item '^3"..tostring(item).."^7'^2 but it wasn't there^7")
if not debugMode then
DropPlayer(src, name.."("..tostring(src)..") kicked by exploit protection")
end