citizenfx / fivem

The source code for the Cfx.re modification frameworks, such as FiveM, RedM and LibertyM, as well as FXServer.
https://cfx.re/
3.57k stars 2.12k forks source link

RegisterRawNuiCallback Crash #2783

Open someone005 opened 2 months ago

someone005 commented 2 months ago

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

AvarianKnight commented 2 months ago

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.

someone005 commented 2 months ago

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

AvarianKnight commented 2 months ago

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 commented 2 months ago

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