-
Notifications
You must be signed in to change notification settings - Fork 52
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
Invalid JSON body makes server crash #159
Comments
@Rybadour I'm afraid I can't reproduce this. Could you describe what you're doing (or better yet, provide a curl command that makes express-pouchdb crash?) The error you're getting doesn't seem json parsing related, it does need 'req.baseUrl' though which was introduced in express 4.3.0. Do you have that? (Upgrade to at least 4.8.0 if not, we use something out of it too). |
Marten I am currently running express v4.10 The reason I felt this was JSON parsing related was due to the fact that this error only arises when I send a request with "Content-Type: application/json" and a body that isn't perfect JSON. I'm using the chrome extension Postman to send artificial requests to the server running locally. |
I tried to create a route to handle the error instead. It seems simply calling next() at least once prior to express-pouchdb handling the request ensures that the server does not crash. |
Thanks! I managed to reproduce this. curl commands: marten@marten-laptop:~/git/pouchdb-server/bin$ curl -X PUT http://localhost:5985/db
{"ok":true}
marten@marten-laptop:~/git/pouchdb-server/bin$ curl -X POST http://localhost:5985/db -d '{"_id": "foo"}' -H 'Content-Type: application/json'
{"ok":true,"id":"232471af-5055-4d4f-ac9a-64904a263579","rev":"1-1b6b3445636d60aff83d9cd09c8eaf8a"}
marten@marten-laptop:~/git/pouchdb-server/bin$ curl -X PUT http://localhost:5985/db/foo -d '{{' -H 'Content-Type: application/json'
SyntaxError: Unexpected token {<br> at Object.parse (native)<br> at parse (/home/marten/git/express-pouchdb/node_modules/body-parser/lib/types/json.js:76:17)<br> at /home/marten/git/express-pouchdb/node_modules/body-parser/lib/read.js:98:18<br> at IncomingMessage.onEnd (/home/marten/git/express-pouchdb/node_modules/raw-body/index.js:136:7)<br> at IncomingMessage.g (events.js:180:16)<br> at IncomingMessage.EventEmitter.emit (events.js:92:17)<br> at _stream_readable.js:920:16<br> at process._tickCallback (node.js:415:13)
marten@marten-laptop:~/git/pouchdb-server/bin$ Stacktrace: marten@marten-laptop:~/git/pouchdb-server/bin$ ./pouchdb-server
[info] pouchdb-server has started on http://127.0.0.1:5985/
[info] navigate to http://127.0.0.1:5985/_utils for the Fauxton UI.
[info] PUT /db 201 - 127.0.0.1
[info] POST /db 201 - 127.0.0.1
/home/marten/git/express-pouchdb/lib/utils.js:53
var rawPath = req.originalUrl.slice(req.baseUrl.length);
^
TypeError: Cannot read property 'length' of undefined
at Object.exports.rawPath (/home/marten/git/express-pouchdb/lib/utils.js:53:50)
at Array.0 (/home/marten/git/express-pouchdb/lib/routes/http_log.js:21:31)
at listener (/home/marten/git/express-pouchdb/node_modules/on-finished/index.js:164:15)
at onFinish (/home/marten/git/express-pouchdb/node_modules/on-finished/index.js:95:5)
at callback (/home/marten/git/express-pouchdb/node_modules/on-finished/node_modules/ee-first/index.js:33:10)
at ServerResponse.onevent (/home/marten/git/express-pouchdb/node_modules/on-finished/node_modules/ee-first/index.js:66:5)
at ServerResponse.EventEmitter.emit (events.js:117:20)
at ServerResponse.OutgoingMessage._finish (http.js:1018:8)
at ServerResponse.OutgoingMessage.end (http.js:1001:10)
at ServerResponse.res.end (/home/marten/git/express-pouchdb/node_modules/compression/index.js:117:15)
marten@marten-laptop:~/git/pouchdb-server/bin$ |
Sending a request like:
Results in this response:
And this error which crashes the server (whole log of the process):
"PUT /db/foo/1 400 720 - 38.156 ms" Thanks for your help, let me know if there is anything else I can provide |
Reported by @Rybadour:
'Agreed, when the body is JSON and it's not perfect the whole server goes down with an error like this:
That's kinda unacceptable.'
The text was updated successfully, but these errors were encountered: