/upload_env persists an unvalidated environment, which then 500s on every open and compare
Bug Description
Environment files are only checked for the presence of jsons and reload. Their actual types are not validated.
The server expects an environment to have this structure:
{
"jsons": {
"win_id": {
...
}
},
"reload": {
...
}
}However, the current validation also accepts malformed values such as:
{"jsons": [], "reload": {}}or:
{"jsons": null, "reload": {}}or:
{"jsons": {"w1": "not a pane"}, "reload": {}}These environments are accepted by /upload_env, stored in state, written to disk, and broadcast to connected clients. Later, when the environment is opened or compared, the server assumes jsons is a mapping and crashes with an uncaught AttributeError.
For example:
POST /env/<eid>fails in load_env() because it calls .values() on jsons:
AttributeError: 'list' object has no attribute 'values'and:
POST /compare/main+<eid>fails in compare_envs() because it calls .keys():
AttributeError: 'list' object has no attribute 'keys'If the values inside jsons are also malformed, the failure happens when the pane is accessed:
AttributeError: 'str' object has no attribute 'get'Reproduction
- Start Visdom with an empty environment directory:
python -m visdom.server -port 8097 -env_path /tmp/visdom-envs- Create an invalid environment:
echo '{"jsons": [], "reload": {}}' > /tmp/broken.json- Upload it:
curl -s -F "file=@/tmp/broken.json" http://localhost:8097/upload_envThe upload succeeds:
{
"success": true,
"eid": "uploaded_broken_3ea1ac89",
"message": "Dashboard loaded successfully as 'uploaded_broken_3ea1ac89'"
}- Open the environment from the UI.
The environment is listed, but opening it results in HTTP 500.
- Select the broken environment together with another environment and run compare.
This also results in HTTP 500.
- Restart the server.
The environment is loaded again from disk, and the same errors occur.
The same problem can also be reproduced by placing any of the following directly in <env_path>:
{"jsons": [], "reload": {}}{"jsons": null, "reload": {}}{"jsons": {"w1": "not a pane"}, "reload": {}}Current validation
There are three places where the environment structure is checked, but all of them only check whether the keys exist.
In UploadEnvHandler.post():
if not (isinstance(data, dict) and "jsons" in data and "reload" in data):
self.set_status(400)
self.write({"success": False, "error": "This is not a valid Visdom JSON"})
returnThe data is then stored without checking the values:
self.state[new_eid] = {
"jsons": data["jsons"],
"reload": data["reload"]
}JSONStore.load_env() performs the same two-key check when loading an environment from disk.
LazyEnvData.prime() also only rejects an environment when jsons or reload is missing. An environment containing both keys but with the wrong types passes this check and fails later when the data is used.
This means:
- missing
jsons→ handled as invalid - missing
reload→ handled as invalid jsons=[]→ acceptedjsons=None→ acceptedjsons={"w1": "not a pane"}→ acceptedreload=[]→ accepted
Expected behavior
Malformed environments should be rejected before they are added to server state.
For uploads, /upload_env should return the existing:
400 This is not a valid Visdom JSONand should not write the invalid environment to disk or state.
For environment files already on disk, JSONStore.load_env() should skip invalid files in the same way it skips files that cannot be parsed.
If an invalid environment somehow reaches load_env() or compare_envs(), it should raise ValueError so the existing handlers can return the normal "invalid environment JSON format" notification instead of an uncaught 500.
Proposed fix
The environment shape can be checked in one place:
def env_is_well_formed(env):
"""Whether ``env`` has the shape expected by environment readers."""
if not isinstance(env, Mapping):
return False
jsons = env.get("jsons")
reload_data = env.get("reload")
if not isinstance(jsons, Mapping):
return False
if not isinstance(reload_data, Mapping):
return False
return all(isinstance(pane, Mapping) for pane in jsons.values())The same validation can then be used by:
UploadEnvHandler.post()JSONStore.load_env()load_env()compare_envs()
Using Mapping instead of only dict also allows the check to work with mapping-like objects such as LazyEnvData.
An empty/missing environment is still represented by {} and should not be treated as a malformed environment.
Impact
The default Visdom server does not require authentication, so a client that can access the server can upload a malformed environment that is then visible to other connected users.
The environment can be removed manually, but the UI does not indicate that the environment is invalid. The failure is only visible in the server logs as a 500 traceback.
Testing
I have implemented the validation with unit tests covering:
- malformed
jsonsvalues - malformed
reloadvalues - malformed pane values
- invalid environments loaded from disk
- invalid environments passed to the readers
- valid environments that should continue to work
The tests cover nine malformed cases along with valid environment cases.
The change is implemented locally and is ready for a PR if this approach looks reasonable.
Source: fossasia/visdom