[Bug Report] Uncaught KeyError and AttributeError (HTTP 500) in SaveHandler on invalid /save payloads
Bug Description
While testing the environment persistence endpoints, I noticed that SaveHandler (/save) crashes with an unhandled exception (HTTP 500) when receiving malformed JSON, non-object JSON bodies, bodies without the "data" key, or non-string entries in "data".
In py/visdom/server/handlers/web_handlers.py:731-747:
class SaveHandler(BaseHandler):
@staticmethod
async def wrap_func(handler, args):
envs = args["data"]
envs = [escape_eid(eid) for eid in envs]
ret = await save_envs_off_loop(handler, envs)
handler.write(json.dumps(ret))
@check_auth
@check_readonly
async def post(self):
args = tornado.escape.json_decode(
tornado.escape.to_basestring(self.request.body)
)
await self.wrap_func(self, args)- Non-JSON body:
tornado.escape.json_decoderaisesValueError-> uncaught HTTP 500. - Non-mapping body (e.g.
[1, 2]):args["data"]raisesTypeError-> uncaught HTTP 500. - Missing
"data"key (e.g.{}):args["data"]raisesKeyError: 'data'-> uncaught HTTP 500. - Non-list
"data"(e.g.{"data": 123}or{"data": None}): raisesTypeError-> uncaught HTTP 500. - Non-string elements in
"data"(e.g.{"data": [123]}):escape_eidraisesAttributeError: 'int' object has no attribute 'strip'-> uncaught HTTP 500.
This was explicitly pinned in py/tests/integration/env_transfer.py:222-229:
def test_a_body_without_data_is_an_unhandled_error(self):
"""Documents today's contract: the missing key surfaces as a 500.
Every sibling route validates its body and answers 400. ``/save`` reads
``args["data"]`` straight, so a body without it is a server error rather
than a client one. Pinned so that fixing it is a visible change.
"""
self.assertEqual(self.post_json("/save", {}).code, 500)Sibling routes like /fork_env, /close, /upload_env, and /data validate their inputs and return HTTP 400 with a descriptive error reason.
Reproduction Steps
- Start the visdom server:
python -m visdom.server - Send POST request to
/savewith missing"data":curl -X POST http://localhost:8097/save -H "Content-Type: application/json" -d '{}' - Server returns
500: Internal Server Errordue to uncaughtKeyError: 'data'.
Expected behavior
SaveHandler should validate that the request body is valid JSON, an object, contains the required "data" key, and that "data" is a list of non-empty strings, returning HTTP 400 Bad Request with a helpful error reason instead of crashing with HTTP 500.
Source: fossasia/visdom