Better error validation for printer profiles
Data types loaded from disk were not properly ensured to match expected types and input validation also had deficits. Should fix #714
This commit is contained in:
1 parent
f5eef06e88
commit
f238ef40ec
5 files changed
+148
-89
No files matched your search
@@ -15,6 +15,7 @@ from octoprint.server.util.flask import restricted_access
|
||||
from octoprint.util import dict_merge
|
||||
|
||||
from octoprint.server import printerProfileManager
|
||||
from octoprint.printer.profile import InvalidProfileError, CouldNotOverwriteError, SaveError
|
||||
|
||||
|
||||
@api.route("/printerprofiles", methods=["GET"])
|
||||
@@ -42,18 +43,26 @@ def printerProfilesAdd():
|
||||
del base_profile["id"]
|
||||
if "name" in base_profile:
|
||||
del base_profile["name"]
|
||||
profile = dict_merge(base_profile, json_data["profile"])
|
||||
if not _validate_profile(profile):
|
||||
return None, None, make_response("Profile is invalid, missing obligatory values", 400)
|
||||
|
||||
return _overwrite_profile(profile)
|
||||
profile = dict_merge(base_profile, json_data["profile"])
|
||||
try:
|
||||
saved_profile = printerProfileManager.save(profile, allow_overwrite=False)
|
||||
except InvalidProfileError:
|
||||
return make_response("Profile is invalid", 400)
|
||||
except CouldNotOverwriteError:
|
||||
return make_response("Profile already exists and overwriting was not allowed", 400)
|
||||
except Exception as e:
|
||||
return make_response("Could not save profile: %s" % e.message, 500)
|
||||
else:
|
||||
return jsonify(dict(profile=_convert_profile(saved_profile)))
|
||||
|
||||
@api.route("/printerprofiles/<string:identifier>", methods=["GET"])
|
||||
def printerProfilesGet(identifier):
|
||||
profile = printerProfileManager.get(identifier)
|
||||
if profile is None:
|
||||
make_response("Unknown profile: %s" % identifier, 404)
|
||||
return jsonify(_convert_profile(profile))
|
||||
return make_response("Unknown profile: %s" % identifier, 404)
|
||||
else:
|
||||
return jsonify(_convert_profile(profile))
|
||||
|
||||
@api.route("/printerprofiles/<string:identifier>", methods=["DELETE"])
|
||||
@restricted_access
|
||||
@@ -84,15 +93,17 @@ def printerProfilesUpdate(identifier):
|
||||
del new_profile["default"]
|
||||
|
||||
new_profile["id"] = identifier
|
||||
if not _validate_profile(new_profile):
|
||||
make_response("Combined profile is invalid, missing obligatory values", 400)
|
||||
|
||||
try:
|
||||
saved_profile = printerProfileManager.save(new_profile, allow_overwrite=True, make_default=make_default)
|
||||
except InvalidProfileError:
|
||||
return make_response("Profile is invalid", 400)
|
||||
except CouldNotOverwriteError:
|
||||
return make_response("Profile already exists and overwriting was not allowed", 400)
|
||||
except Exception as e:
|
||||
make_response("Could not save profile: %s" % e.message)
|
||||
|
||||
return jsonify(dict(profile=_convert_profile(saved_profile)))
|
||||
return make_response("Could not save profile: %s" % e.message, 500)
|
||||
else:
|
||||
return jsonify(dict(profile=_convert_profile(saved_profile)))
|
||||
|
||||
def _convert_profiles(profiles):
|
||||
result = dict()
|
||||
@@ -109,59 +120,3 @@ def _convert_profile(profile):
|
||||
converted["default"] = (profile["id"] == default)
|
||||
converted["current"] = (profile["id"] == current)
|
||||
return converted
|
||||
|
||||
def _validate_profile(profile):
|
||||
if not "name" in profile \
|
||||
and "volume" in profile \
|
||||
and "width" in profile["volume"] \
|
||||
and "depth" in profile["volume"] \
|
||||
and "height" in profile["volume"] \
|
||||
and "formFactor" in profile["volume"] \
|
||||
and "heatedBed" in profile \
|
||||
and "extruder" in profile \
|
||||
and "count" in profile["extruder"] \
|
||||
and "offsets" in profile["extruder"] \
|
||||
and len(profile["extruder"]["offsets"]) == profile["extruder"]["count"]:
|
||||
return False
|
||||
|
||||
for dimension in ("width", "depth", "height"):
|
||||
try:
|
||||
profile["volume"][dimension] = float(profile["volume"][dimension])
|
||||
except:
|
||||
return False
|
||||
|
||||
if not profile["volume"]["formFactor"] in ("rectangular", "circular"):
|
||||
return False
|
||||
|
||||
try:
|
||||
profile["heatedBed"] = bool(profile["heatedBed"])
|
||||
except:
|
||||
return False
|
||||
|
||||
try:
|
||||
profile["extruder"]["count"] = int(profile["extruder"]["count"])
|
||||
except:
|
||||
return False
|
||||
|
||||
converted_offsets = []
|
||||
for offset in profile["extruder"]["offsets"]:
|
||||
try:
|
||||
converted_offsets.append((float(offset[0]), float(offset[1])))
|
||||
except:
|
||||
return False
|
||||
profile["extruder"]["offsets"] = converted_offsets
|
||||
|
||||
return True
|
||||
|
||||
def _overwrite_profile(profile):
|
||||
if not "id" in profile and not "name" in profile:
|
||||
return None, None, make_response("Profile must contain either id or name")
|
||||
elif not "name" in profile:
|
||||
return None, None, make_response("Profile must contain a name")
|
||||
|
||||
try:
|
||||
saved_profile = printerProfileManager.save(profile, allow_overwrite=False)
|
||||
except Exception as e:
|
||||
return None, None, make_response("Could not save profile: %s" % e.message)
|
||||
|
||||
return jsonify(dict(profile=_convert_profile(saved_profile)))
|
||||
Reference in new issue
Block a user