secure-shop¶
A minimal FiveM example that demonstrates server-authoritative client-server patterns and input validation / security best practices for CFX resource development.
It pairs the repo's client-server-patterns and security-best-practices
skills with runnable code.
What it demonstrates¶
- The client sends only an item id and a quantity over the wire.
- The server owns the prices (in
config.lua), looks them up by id, validates every field, checks funds, deducts money, and grants the item. - Per-player, server-enforced anti-spam cooldown, cleaned up on
playerDropped. - A command +
RegisterKeyMappinginstead of a per-frame input poll, plus a low-frequency proximity thread (Wait 500/1000) so the resource idles cheaply. - An ESX / QBCore bridge with a standalone fallback for money, inventory, and notifications.
The trust model (read this)¶
The client is attacker-controlled. Never trust it with anything that matters.
A cheater can fire TriggerServerEvent('secure-shop:server:buy', ...) with any
values they like. So the client sends the minimum: an itemId and a qty.
The server then:
- Confirms the event came from a real player (
src > 0). - Confirms
itemIdis a string naming a real entry inConfig.Items. - Confirms
qtyis a finite, positive integer withinConfig.MaxQuantity - rejecting
nil, strings, tables,NaN,inf, floats, zero, and negatives. - Computes
total = item.price * qtyfrom its own config. - Checks the player can afford
total, deducts it, then grants the item (refunding if the grant fails).
If the client were trusted to send a price, an attacker would send price = 0
(or a negative price) and take items for free or even gain money. That is why
the price is never read from the wire - only the id and quantity are.
Files¶
| File | Role |
|---|---|
fxmanifest.lua |
Resource manifest (cerulean, gta5) |
config.lua |
Shared config - the catalogue and the authoritative prices |
server/main.lua |
NetEvent handler, validation, funds, cooldown |
client/main.lua |
Keybind, proximity gate, result notifications |
Install¶
- Copy the
secure-shopfolder into your serverresources/directory. - Add
ensure secure-shopto yourserver.cfg. - (Optional) Make sure
es_extendedorqb-corestarts before this resource, or setConfig.Frameworkexplicitly to'esx','qb', or'standalone'. - Make sure the inventory item names in
Config.Items(item = ...) exist in your framework's item database, or purchases will be charged but the grant step will fail (and refund).
Try it¶
Walk to Config.ShopCoords (the 24/7 store on Innocence Blvd by default) and
press E. The demo buys 1x the first configured item (Water). Watch the
server validate the request and the notification report the price.
Standalone mode has no money or inventory backend, so Config.StandaloneAlwaysAfford
lets the purchase "succeed" for testing only. Leave it false in production.
How to extend with NUI¶
This example deliberately has no UI so the security flow stays readable. To turn it into a real shop:
- Add an
html/folder and declareui_page+filesinfxmanifest.lua. - Open the NUI on the keybind (
SetNuiFocus(true, true)and aSendNUIMessage) instead of buying immediately. - From the UI, on "buy" call
fetch('https://secure-shop/buy', ...)and handle it withRegisterNUICallback('buy', ...). - In that callback, forward only
{ itemId, qty }tosecure-shop:server:buy. Never send the price from the UI - the server still owns it. The entire server side stays exactly as it is.