How to validate buffers sent through RemoteEvents

A size read from a client's buffer can make the server allocate up to 1 GiB. How to cap buffer.len, size from server limits and check every decoded value.

warningLuaudit check: unchecked-client-buffers

What it looks like

A remote takes a buffer from the client, usually to send compact binary data such as a drawing, a replay or a build layout. The server checks it's a buffer, then trusts what's inside: a size or count read from the first few bytes decides how much memory it allocates, how many records it reads or how far it copies.

Luau
local size = buffer.readu32(data, 0)
local canvas = buffer.create(size)

What an exploiter could do

They can send a few bytes that claim a huge size. buffer.create accepts up to 1 GiB, so one call can make the server set aside that much memory, which can slow or crash the server for everyone. A count that doesn't match the real length makes the handler error halfway through, which can leave state half-written, such as a cleared list or a busy flag that's never reset. Decoded floats and ids that aren't checked reach the game as NaN, infinity or ids that don't exist.

How to fix it

  • Cap buffer.len before reading anything, using a limit the server sets from the largest legitimate message.
  • Let the server decide sizes. Allocate from the server's own limits, not from a number in the buffer, and check that the buffer is exactly as long as its own count says.
  • Check every decoded value as you would a plain argument: ids against an allowlist, floats inside a range written so NaN fails, and counts against an upper bound.
  • Decode and check the whole buffer before changing anything, so a bad message changes nothing.

Vulnerable example

Vulnerable
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local canvases: { [Player]: buffer } = {}

ReplicatedStorage.Remotes.UploadCanvas.OnServerEvent:Connect(function(player: Player, data: unknown)
	if typeof(data) ~= "buffer" then
		return
	end
	local size = buffer.readu32(data, 0)
	local canvas = buffer.create(size)
	local written = 0
	for read = 4, buffer.len(data) - 2, 2 do
		local run = buffer.readu8(data, read)
		local colour = buffer.readu8(data, read + 1)
		buffer.fill(canvas, written, colour, run)
		written += run
	end
	canvases[player] = canvas
end)

Players.PlayerRemoving:Connect(function(player)
	canvases[player] = nil
end)

Fixed example

Fixed
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local pixels = 64 * 64
local colours = 16
local canvases: { [Player]: buffer } = {}

ReplicatedStorage.Remotes.UploadCanvas.OnServerEvent:Connect(function(player: Player, data: unknown)
	if typeof(data) ~= "buffer" then
		return
	end
	local length = buffer.len(data)
	if length < 2 or length > pixels * 2 or length % 2 ~= 0 then
		return
	end
	local canvas = buffer.create(pixels)
	local written = 0
	for read = 0, length - 2, 2 do
		local run = buffer.readu8(data, read)
		local colour = buffer.readu8(data, read + 1)
		if run == 0 or colour >= colours or written + run > pixels then
			return
		end
		buffer.fill(canvas, written, colour, run)
		written += run
	end
	if written ~= pixels then
		return
	end
	canvases[player] = canvas
end)

Players.PlayerRemoving:Connect(function(player)
	canvases[player] = nil
end)

How to spot this in your own scripts, and the cases that only look like it, are part of the Luaudit check your AI runs.