fix: harden endpoints against malformed input (final-review findings)
- /api/repeaters: bad ?limit= falls back to 25 instead of crashing the handler
- repeaters.load(): tolerate a corrupted repeaters.json (JSONDecodeError -> [])
- tune_repeater/do_POST: catch TypeError so a null field yields {ok:false} not a reset
This commit is contained in:
@@ -196,7 +196,7 @@ class Radio:
|
|||||||
def attempt(name, fn):
|
def attempt(name, fn):
|
||||||
try:
|
try:
|
||||||
fn(); steps[name] = True
|
fn(); steps[name] = True
|
||||||
except (CIVError, ValueError, OSError) as e:
|
except (CIVError, ValueError, TypeError, OSError) as e:
|
||||||
steps[name] = False; errors[name] = str(e)
|
steps[name] = False; errors[name] = str(e)
|
||||||
|
|
||||||
attempt("freq", lambda: self.set_frequency(int(round(rec["output_mhz"] * 1e6))))
|
attempt("freq", lambda: self.set_frequency(int(round(rec["output_mhz"] * 1e6))))
|
||||||
|
|||||||
+1
-1
@@ -72,7 +72,7 @@ def load(path):
|
|||||||
try:
|
try:
|
||||||
with open(path) as f:
|
with open(path) as f:
|
||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
except FileNotFoundError:
|
except (FileNotFoundError, json.JSONDecodeError):
|
||||||
return []
|
return []
|
||||||
return data if isinstance(data, list) else data.get("repeaters", [])
|
return data if isinstance(data, list) else data.get("repeaters", [])
|
||||||
|
|
||||||
|
|||||||
@@ -90,7 +90,10 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
from urllib.parse import urlparse, parse_qs
|
from urllib.parse import urlparse, parse_qs
|
||||||
qs = parse_qs(urlparse(self.path).query)
|
qs = parse_qs(urlparse(self.path).query)
|
||||||
q = qs.get("q", [""])[0]
|
q = qs.get("q", [""])[0]
|
||||||
|
try:
|
||||||
limit = int(qs.get("limit", ["25"])[0])
|
limit = int(qs.get("limit", ["25"])[0])
|
||||||
|
except ValueError:
|
||||||
|
limit = 25
|
||||||
recs = repeaters.search(repeaters.load(REPEATERS_JSON), q, limit)
|
recs = repeaters.search(repeaters.load(REPEATERS_JSON), q, limit)
|
||||||
self._json({"ok": True, "repeaters": recs})
|
self._json({"ok": True, "repeaters": recs})
|
||||||
else:
|
else:
|
||||||
@@ -101,7 +104,7 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
try:
|
try:
|
||||||
body = json.loads(self.rfile.read(length) or b"{}")
|
body = json.loads(self.rfile.read(length) or b"{}")
|
||||||
self._json(apply_control(self.path, body))
|
self._json(apply_control(self.path, body))
|
||||||
except (CIVError, OSError, ValueError, KeyError) as e:
|
except (CIVError, OSError, ValueError, TypeError, KeyError) as e:
|
||||||
self._json({"ok": False, "error": str(e)}, 200)
|
self._json({"ok": False, "error": str(e)}, 200)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user