nim-lang / nimforum

Lightweight alternative to Discourse written in Nim
https://forum.nim-lang.org/
MIT License
760 stars 70 forks source link

Forum returns 5xx when search term includes "(" or ")" #312

Open adokitkat opened 2 years ago

adokitkat commented 2 years ago

I just noticed last night, if your forum search term includes "(" or ")", the forum will crash, and you will get a Cloudflare bad gateway error. It quickly restarts but this is clearly not supposed to happen.

Source

I have tested it and it really crashes.

adokitkat commented 2 years ago

This answers the malformed MATCH expression:

https://stackoverflow.com/questions/11708736/sqlite-match-operator

dom96 commented 2 years ago

Wow, this is really interesting. I was wondering how the forum was crashing here, turns out it doesn't. I'm not quite sure why CloudFlare returns a 502 after the search with a "(" but it seems to be browser-local. You can test this yourself by doing the search with "(" in Firefox, waiting as long as you want, opening the forum in Chrome (it will load), then clicking the home button in Firefox (and you'll see the 502 from CloudFlare). I guess maybe CloudFlare is doing some stupid caching here based on the session.

But the forum certainly doesn't crash so the bug ain't that critical :)

cyraxjoe commented 2 years ago

Definitely there is something going on, testing from curl, I get the error with traceback (unless that is the actual expected behavior).

curl -G  -d q='example (' https://forum.nim-lang.org/search.json
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Jester route exception</title></head><body><h1>An error has occured in one of your routes.</h1><p><b>Detail: </b>/home/dom/nimforum2/src/forum.nim(817) forum<br/>
/home/dom/.nimble/pkgs/jester-#405be2e/jester.nim(493) serve<br/>
/home/dom/.nimble/pkgs/httpbeast-0.4.0/httpbeast.nim(540) run<br/>
/home/dom/.nimble/pkgs/httpbeast-0.4.0/httpbeast.nim(371) eventLoop<br/>
/home/dom/.nimble/pkgs/httpbeast-0.4.0/httpbeast.nim(283) processEvents<br/>
/home/dom/.nimble/pkgs/jester-#405be2e/jester.nim(496) :anonymous<br/>
/home/dom/.nimble/pkgs/jester-#405be2e/jester.nim(398) handleRequest<br/>
/home/dom/Nim/lib/pure/asyncmacro.nim(232) dispatch<br/>
/home/dom/Nim/lib/pure/asyncmacro.nim(28) dispatchNimAsyncContinue<br/>
/home/dom/.nimble/pkgs/jester-#405be2e/jester.nim(297) dispatchIter<br/>
/home/dom/Nim/lib/pure/asyncmacro.nim(232) match<br/>
/home/dom/Nim/lib/pure/asyncmacro.nim(28) matchNimAsyncContinue<br/>
/home/dom/Nim/lib/impure/db_sqlite.nim(337) matchIter<br/>
/home/dom/Nim/lib/impure/db_sqlite.nim(198) dbError<br/>
[[reraised from:<br/>
/home/dom/nimforum2/src/forum.nim(817) forum<br/>
/home/dom/.nimble/pkgs/jester-#405be2e/jester.nim(493) serve<br/>
/home/dom/.nimble/pkgs/httpbeast-0.4.0/httpbeast.nim(540) run<br/>
/home/dom/.nimble/pkgs/httpbeast-0.4.0/httpbeast.nim(371) eventLoop<br/>
/home/dom/.nimble/pkgs/httpbeast-0.4.0/httpbeast.nim(283) processEvents<br/>
/home/dom/.nimble/pkgs/jester-#405be2e/jester.nim(496) :anonymous<br/>
/home/dom/.nimble/pkgs/jester-#405be2e/jester.nim(398) handleRequest<br/>
/home/dom/Nim/lib/pure/asyncmacro.nim(232) dispatch<br/>
/home/dom/Nim/lib/pure/asyncmacro.nim(31) dispatchNimAsyncContinue<br/>
/home/dom/Nim/lib/pure/asyncmacro.nim(131) dispatchIter<br/>
/home/dom/Nim/lib/pure/asyncfutures.nim(389) read<br/>
]]<br/>
[[reraised from:<br/>
/home/dom/nimforum2/src/forum.nim(817) forum<br/>
/home/dom/.nimble/pkgs/jester-#405be2e/jester.nim(493) serve<br/>
/home/dom/.nimble/pkgs/httpbeast-0.4.0/httpbeast.nim(540) run<br/>
/home/dom/.nimble/pkgs/httpbeast-0.4.0/httpbeast.nim(371) eventLoop<br/>
/home/dom/.nimble/pkgs/httpbeast-0.4.0/httpbeast.nim(283) processEvents<br/>
/home/dom/.nimble/pkgs/jester-#405be2e/jester.nim(496) :anonymous<br/>
/home/dom/.nimble/pkgs/jester-#405be2e/jester.nim(398) handleRequest<br/>
/home/dom/Nim/lib/pure/asyncmacro.nim(232) handleRequestSlow<br/>
/home/dom/Nim/lib/pure/asyncmacro.nim(31) handleRequestSlowNimAsyncContinue<br/>
/home/dom/Nim/lib/pure/asyncmacro.nim(131) handleRequestSlowIter<br/>
/home/dom/Nim/lib/pure/asyncfutures.nim(389) read<br/>
]]<br/>
malformed MATCH expression: [example (]<br/>
Async traceback:<br/>
  /home/dom/nimforum2/src/forum.nim(817)                    forum<br/>
  /home/dom/.nimble/pkgs/jester-#405be2e/jester.nim(493)    serve<br/>
  /home/dom/.nimble/pkgs/httpbeast-0.4.0/httpbeast.nim(540) run<br/>
  /home/dom/.nimble/pkgs/httpbeast-0.4.0/httpbeast.nim(371) eventLoop<br/>
  /home/dom/.nimble/pkgs/httpbeast-0.4.0/httpbeast.nim(283) processEvents<br/>
  /home/dom/.nimble/pkgs/jester-#405be2e/jester.nim(496)    :anonymous<br/>
  /home/dom/.nimble/pkgs/jester-#405be2e/jester.nim(398)    handleRequest<br/>
  /home/dom/Nim/lib/pure/asyncmacro.nim(232)                dispatch<br/>
  /home/dom/Nim/lib/pure/asyncmacro.nim(28)                 dispatchNimAsyncContinue<br/>
  /home/dom/.nimble/pkgs/jester-#405be2e/jester.nim(297)    dispatchIter<br/>
  /home/dom/Nim/lib/pure/asyncmacro.nim(232)                match<br/>
  /home/dom/Nim/lib/pure/asyncmacro.nim(28)                 matchNimAsyncContinue<br/>
  /home/dom/Nim/lib/impure/db_sqlite.nim(337)               matchIter<br/>
  /home/dom/Nim/lib/impure/db_sqlite.nim(198)               dbError<br/>
  #[<br/>
    /home/dom/nimforum2/src/forum.nim(817)                    forum<br/>
    /home/dom/.nimble/pkgs/jester-#405be2e/jester.nim(493)    serve<br/>
    /home/dom/.nimble/pkgs/httpbeast-0.4.0/httpbeast.nim(540) run<br/>
    /home/dom/.nimble/pkgs/httpbeast-0.4.0/httpbeast.nim(371) eventLoop<br/>
    /home/dom/.nimble/pkgs/httpbeast-0.4.0/httpbeast.nim(283) processEvents<br/>
    /home/dom/.nimble/pkgs/jester-#405be2e/jester.nim(496)    :anonymous<br/>
    /home/dom/.nimble/pkgs/jester-#405be2e/jester.nim(398)    handleRequest<br/>
    /home/dom/Nim/lib/pure/asyncmacro.nim(232)                dispatch<br/>
    /home/dom/Nim/lib/pure/asyncmacro.nim(31)                 dispatchNimAsyncContinue<br/>
    /home/dom/Nim/lib/pure/asyncmacro.nim(131)                dispatchIter<br/>
    /home/dom/Nim/lib/pure/asyncfutures.nim(389)              read<br/>
  ]#<br/>
Exception message: malformed MATCH expression: [example (]<br/>
</p></body></html>
cyraxjoe commented 2 years ago

Maybe something like https://github.com/groue/GRDB.swift/issues/129#issuecomment-249520891would be good enough?

adokitkat commented 2 years ago

So what should we do? I've already proposed to replace parentheses and also an automatic fix for missing parentheses... We can also just catch a DbError and give no results but it's not up to me which version is chosen I guess.