-
Notifications
You must be signed in to change notification settings - Fork 221
Produce a better error in case of malformed JSON #319
Conversation
$ curl localhost:8777 -X POST -i -d '{"action": "version", "version": 6},' && echo ␄ HTTP/1.1 200 OK Content-Type: text/json Access-Control-Allow-Origin: http://localhost Access-Control-Allow-Headers: * Content-Length: 67 {"result": null, "error": "Extra data: line 1 column 36 (char 35)"}␄ $ curl localhost:8777 -X POST -i -d '{"action": "version", "version": 6},' -H "Origin: foo" && echo ␄ HTTP/1.1 403 Forbidden Access-Control-Allow-Origin: http://localhost Access-Control-Allow-Headers: * ␄
Looks great to me :) |
I really wish @nvlled said something, it now feels like I stole their PR :< |
Sorry, I couldn't find the time to give a thorough review. But I do one nit: explicity initialize params outside of the try block. Something like: params = {}
try:
params = json.loads(req.body.decode('utf-8')) The bug I encountered was specifically caused by an uninitialized params variable. It would be nice to prevent this from happening when changes are made in the future. |
Ah, that should be solved in this PR! In the Lines 179 to 192 in 9c3310a
I'd still appreciate the review! Anyway, I guess I better make this not a draft since, well, the button says “Ready for review” :p |
Looks good to me! |
P.S. the tests are failing now due to protobuf update (packages anki/aqt don't pin dependencies), I'll have a fix shortly |
Thanks! |
This solves the same issue that #317 solves, albeit slightly differently. I was hoping that the author would reopen the issue... There's a few differences;
I also noticed this bit:
params['params'] = params.get('params', {})
. It's a tad dangerous since this part of code can be run when not allowed, andparams
can be present but not be a dictionary. In this case the plugin would “crash”, and while this only amounts to showing a window with an error, it can be theoretically triggered by a malicious website, so I opted for verifying request schema. (Anki comes withjsonschema
which is used to verify addon configurations.) This is not ideal, but I wanted to touch as little code as possible.I added a few tests that test behavior around the change.
@nvlled Could you please look at this and tell if I did something stupid? 😬