fivem icon indicating copy to clipboard operation
fivem copied to clipboard

RegisterRawNuiCallback Crash

Open someone005 opened this issue 1 year ago • 4 comments

What happened?

I discovered that if you pass empty string to callback function, it will cause game crash.

Video

LUA Code:

RegisterRawNuiCallback('exampleCallback', function(rawData, cb)
    cb('')
end)

NUI JS Code

fetch(`https://${GetParentResourceName()}/exampleCallback`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json; charset=UTF-8',
    },
    body: ""
}).catch(err => {})

Expected result

Callback data should be properly packed

Reproduction steps

  1. Create resource with client lua script (description code) and JS Code implemented in script's NUI
  2. Start the resource

Importancy

Crash

Area(s)

Natives

Specific version(s)

FiveM: 9837 Server: 9481 Windows

Additional information

No response

someone005 avatar Sep 10 '24 22:09 someone005

It should be noted that this will happen if you return any invalid data during any of this, this entire section expects the end user to never send any invalid data via the callback.

AvarianKnight avatar Sep 11 '24 00:09 AvarianKnight

Yeah, it should noted, but there's one thing. Why the same data passed to RegisterNUICallback cb function doesn't crash the game?

someone005 avatar Sep 11 '24 06:09 someone005

This is a "raw callback" you're expected to return an empty table if you don't want to return anything, or a table with this kind of structure

struct RequestCallback {
	// optional, doesn't have to be set
	// headers to set for the call back
	Array<(string, string)> headers;

	// alternate to returning an array
	IDictionary<string, string> headers;

	// optional, doesn't have to be set
	// the status code 
	int status;

	// optional, doesn't have to be set
	// the data to return
	string body;
}

AvarianKnight avatar Sep 11 '24 13:09 AvarianKnight

Here's a patch patch I wrote up yesterday, though its not the cleanest, nor is it tested.

AvarianKnight avatar Sep 11 '24 13:09 AvarianKnight