Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIGSEGV with getFormParamsOption when body isn't form-urlencoded #243

Open
bobbbob98 opened this issue Jul 8, 2024 · 1 comment
Open

Comments

@bobbbob98
Copy link

As title says. getFormParams crashes in the same manner. Is this a bug or is it supposed to happen? If it's supposed to happen how would I check that the post body is actually a form? Because I feel like getFormParamsOption should still return none(string) even if the body isn't a form.

Example code:

import prologue

proc testGet*(ctx: Context) {.async.} =
  resp "<h1>Test</h1>"
  
proc testPost(ctx: Context) {.async.} =
    let reqbody = ctx.getFormParamsOption("text")
    
    if reqbody.isSome:
        resp "<h1>" & reqbody.get & "</h1>\n"
    else:
        resp "<h1>Nothing</h1>\n"

var app = newApp()
app.addRoute("/", testGet)
app.addRoute("/testpost", testPost, HttpPost)
app.run()

running
curl --form text=hello http://localhost:8080/testpost
works just fine but running
curl -X POST -H "Content-Type: application/json" -d '{"text":"example1"}' http://localhost:8080/testpost
crashes the server. Error log:

$ nim r protest.nim 
Hint: used config file '/home/bobbbob/.choosenim/toolchains/nim-2.0.8/config/nim.cfg' [Conf]
Hint: used config file '/home/bobbbob/.choosenim/toolchains/nim-2.0.8/config/config.nims' [Conf]
Hint: mm: orc; threads: on; opt: none (DEBUG BUILD, `-d:release` generates faster code)
10123 lines; 0.023s; 10.477MiB peakmem; proj: /home/bobbbob/protest.nim; out: /home/bobbbob/.cache/nim/protest_d/protest_EB947FA496BB993984B6850599C2BDE955F9E454 [SuccessX]
Hint: /home/bobbbob/.cache/nim/protest_d/protest_EB947FA496BB993984B6850599C2BDE955F9E454 [Exec]
DEBUG Prologue is serving at http://0.0.0.0:8080 
DEBUG Starting 16 threads
DEBUG Listening on port 8080
Traceback (most recent call last)
/home/bobbbob/.choosenim/toolchains/nim-2.0.8/lib/system.nim(568) eventLoop
/home/bobbbob/.nimble/pkgs2/httpx-0.3.7-6e0fc3133fbd20530d53ab2792e93d3151387b10/httpx.nim(478) processEvents
/home/bobbbob/.nimble/pkgs2/prologue-0.6.6-433777a19c0bf630753006f0646c5a6dbbfd4b74/prologue/core/application.nim(536) handler
/home/bobbbob/.nimble/pkgs2/prologue-0.6.6-433777a19c0bf630753006f0646c5a6dbbfd4b74/prologue/core/application.nim(501) handleRequest
/home/bobbbob/.nimble/pkgs2/prologue-0.6.6-433777a19c0bf630753006f0646c5a6dbbfd4b74/prologue/core/application.nim(460) handleContext (Async)
/home/bobbbob/.nimble/pkgs2/prologue-0.6.6-433777a19c0bf630753006f0646c5a6dbbfd4b74/prologue/core/middlewaresbase.nim(48) switch (Async)
/home/bobbbob/protest.nim(7) testPost (Async)
/home/bobbbob/.nimble/pkgs2/prologue-0.6.6-433777a19c0bf630753006f0646c5a6dbbfd4b74/prologue/core/context.nim(392) getFormParamsOption
/home/bobbbob/.choosenim/toolchains/nim-2.0.8/lib/pure/collections/tables.nim(1916) contains
/home/bobbbob/.choosenim/toolchains/nim-2.0.8/lib/pure/collections/tables.nim(1906) hasKey
/home/bobbbob/.choosenim/toolchains/nim-2.0.8/lib/pure/collections/tables.nim(1418) hasKey
/home/bobbbob/.choosenim/toolchains/nim-2.0.8/lib/pure/collections/hashcommon.nim(42) rawGet
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Segmentation fault (core dumped)
Error: execution of an external program failed: '/home/bobbbob/.cache/nim/protest_d/protest_EB947FA496BB993984B6850599C2BDE955F9E454'
@TelegramXPlus
Copy link

On the docs it says

Note that: getFormParams handles both form-urlencoded and multipart/form-data

So I don't think there a way to get the json param easily with a function. But you can see that inside ctx.request.body you actually have your data (although it is of type string). In order not to crash, you should check whether the Content-Type matches, for instance

if ctx.request.headers["Content-Type"] notin @["application/x-www-form-urlencoded", "multipart/form-data"]:
  resp "<h1>Not found</h1>"
  return

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants