confserver: Send an error response if JSON request is malformatted
This commit is contained in:
parent
ec4c75b692
commit
2f2f0fbcbd
2 changed files with 25 additions and 1 deletions
|
@ -79,7 +79,13 @@ def run_server(kconfig, sdkconfig, default_version=MAX_PROTOCOL_VERSION):
|
||||||
line = sys.stdin.readline()
|
line = sys.stdin.readline()
|
||||||
if not line:
|
if not line:
|
||||||
break
|
break
|
||||||
|
try:
|
||||||
req = json.loads(line)
|
req = json.loads(line)
|
||||||
|
except ValueError as e: # json module throws JSONDecodeError (sublcass of ValueError) on Py3 but ValueError on Py2
|
||||||
|
response = {"version": default_version, "error": ["JSON formatting error: %s" % e]}
|
||||||
|
json.dump(response, sys.stdout)
|
||||||
|
print("\n")
|
||||||
|
continue
|
||||||
before = confgen.get_json_values(config)
|
before = confgen.get_json_values(config)
|
||||||
before_ranges = get_ranges(config)
|
before_ranges = get_ranges(config)
|
||||||
before_visible = get_visible(config)
|
before_visible = get_visible(config)
|
||||||
|
|
|
@ -64,6 +64,8 @@ def main():
|
||||||
|
|
||||||
test_load_save(p, temp_sdkconfig_path)
|
test_load_save(p, temp_sdkconfig_path)
|
||||||
|
|
||||||
|
test_invalid_json(p)
|
||||||
|
|
||||||
print("Done. All passed.")
|
print("Done. All passed.")
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
|
@ -143,5 +145,21 @@ def test_load_save(p, temp_sdkconfig_path):
|
||||||
assert len(load_result["ranges"]) > 0
|
assert len(load_result["ranges"]) > 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_invalid_json(p):
|
||||||
|
print("Testing invalid JSON formatting...")
|
||||||
|
|
||||||
|
bad_escaping = r'{ "version" : 2, "load" : "c:\some\path\not\escaped\as\json" }'
|
||||||
|
p.send("%s\n" % bad_escaping)
|
||||||
|
readback = expect_json(p)
|
||||||
|
print(readback)
|
||||||
|
assert "json" in readback["error"][0].lower()
|
||||||
|
|
||||||
|
not_really_json = 'Hello world!!'
|
||||||
|
p.send("%s\n" % not_really_json)
|
||||||
|
readback = expect_json(p)
|
||||||
|
print(readback)
|
||||||
|
assert "json" in readback["error"][0].lower()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in a new issue