toverainc / willow-application-server

Willow Application Server
Apache License 2.0
19 stars 9 forks source link

treewide: fix double JSON serialization #25

Closed stintel closed 1 year ago

stintel commented 1 year ago

When calling the /api/nvs/save API via curl, we were seeing the following stack trace:

willow-application-server-was-1 | File "/app/api.py", line 345, in save_nvs willow-application-server-was-1 | await post_nvs(request, False) willow-application-server-was-1 | File "/app/api.py", line 202, in post_nvs willow-application-server-was-1 | save_json_to_file(STORAGE_USER_NVS, data) willow-application-server-was-1 | File "/app/api.py", line 212, in save_json_to_file willow-application-server-was-1 | config_file.write(content) willow-application-server-was-1 | TypeError: write() argument must be str, not dict

This is because we're JSON serializing the json argument to shared.post_nvs(). In shared.post_nvs(), we're passing the json argument to reequests.post() as a json named argument. This will serialize it again. It is then deserialized by request.json() in api.post_nvs(). We then try to write it to a file, which works fine when calling the /api/nvs/save via the UI, as deserializing once will result in a string.

However, when we call the /api/nvs/save via curl, deserializing results in a python dict, which cannot be written to a file.

Do not serialize the data before passing it to shared.post_nvs() to fix this. We will then always end up with data being a dict in api.post_nvs(), so we need to serialize it before writing it to a file.

The same problem exists in post_config(), so fix it there also.