Fix bugs, add log rotation, and optimize hot paths

- Fix AttributeError crash on empty request body in md5, hasher, textdiff,
  jwtdecoder, timestamp, passwordgen (get_json without silent=True / or {})
- Fix memory exhaustion in ipcalc: replace list(network.hosts()) with direct
  arithmetic — safe for /8 and larger networks
- Fix O(1M) loop in cronexplainer.get_next_runs: rewrite to skip by
  month/day/hour instead of iterating every minute
- Fix connection leak in notes.ensure_table: add try/finally around conn.close
- Fix admin._ensure_tables / notes._ensure_table running DDL on every request:
  guard with module-level flags (_tables_initialized, _table_ready)
- Fix update_website returning 200 when no row matched; delete_website returning
  success when nothing was deleted; add rowcount checks for both
- Add role validation in admin create_user / update_user (_VALID_ROLES guard)
- Add delimiter length guard in csvviewer (csv.reader requires single char)
- Fix loremipsum: wrap int(count) in try/except ValueError → 400 response
- Fix auth/token: use auth_header[7:] instead of fragile .replace()
- Fix app.py: remove duplicate import sys; cache DB liveness check with 30s TTL
  to avoid a new TCP connection on every frontend page load; move api/setup
  path guard before DB check
- Replace FileHandler with RotatingFileHandler (5 MB / 3 backups) in logger;
  fix relative log paths to absolute paths anchored to __file__
- Wrap all DB connections in try/finally conn.close() throughout admin and notes

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Nirodan
2026-05-06 10:06:29 +02:00
parent 31494c9dab
commit 98bb34f094
15 changed files with 363 additions and 196 deletions
+82 -12
View File
@@ -96,27 +96,97 @@ def build_summary(minute, hour, day, month, weekday):
def get_next_runs(minute_vals, hour_vals, day_vals, month_vals, weekday_vals):
# cron weekday 0=Sun..6=Sat,7=Sun -> python weekday 0=Mon..6=Sun
"""Return up to 5 upcoming run times.
Uses targeted jumps (skip month, day, hour) instead of iterating every
minute, so performance is acceptable even for rare expressions like
'0 0 29 2 *' (Feb 29 at midnight).
"""
# cron weekday 0=Sun..6=Sat,7=Sun → Python weekday 0=Mon..6=Sun
py_weekdays = set((wd + 6) % 7 for wd in weekday_vals)
minute_set = set(minute_vals)
hour_set = set(hour_vals)
day_set = set(day_vals)
month_set = set(month_vals)
minute_list = sorted(minute_vals)
hour_list = sorted(hour_vals)
month_list = sorted(month_vals)
if not minute_list or not hour_list or not month_list:
return []
now = datetime.now().replace(second=0, microsecond=0) + timedelta(minutes=1)
max_date = now + timedelta(days=4 * 366)
results = []
current = now
for _ in range(2 * 366 * 24 * 60):
if len(results) >= 5:
break
if (current.month in month_set and
current.day in day_set and
current.weekday() in py_weekdays and
current.hour in hour_set and
current.minute in minute_set):
results.append(current.isoformat())
current += timedelta(minutes=1)
def _advance_from_results():
"""Move current to the next candidate after a match."""
nonlocal current
next_mins = [m for m in minute_list if m > current.minute]
if next_mins:
current = current.replace(minute=next_mins[0])
return
next_hours = [h for h in hour_list if h > current.hour]
if next_hours:
current = current.replace(hour=next_hours[0], minute=minute_list[0])
return
current = (current + timedelta(days=1)).replace(hour=hour_list[0], minute=minute_list[0])
while current <= max_date and len(results) < 5:
# ── month ──────────────────────────────────────────────────────────
if current.month not in month_set:
next_months = [m for m in month_list if m > current.month]
if next_months:
current = current.replace(
month=next_months[0], day=1,
hour=hour_list[0], minute=minute_list[0]
)
else:
current = current.replace(
year=current.year + 1, month=month_list[0], day=1,
hour=hour_list[0], minute=minute_list[0]
)
continue
# ── day + weekday ──────────────────────────────────────────────────
if current.day not in day_set or current.weekday() not in py_weekdays:
current = (current + timedelta(days=1)).replace(
hour=hour_list[0], minute=minute_list[0]
)
continue
# ── hour ───────────────────────────────────────────────────────────
if current.hour not in hour_set:
next_hours = [h for h in hour_list if h > current.hour]
if next_hours:
current = current.replace(hour=next_hours[0], minute=minute_list[0])
else:
current = (current + timedelta(days=1)).replace(
hour=hour_list[0], minute=minute_list[0]
)
continue
# ── minute ─────────────────────────────────────────────────────────
if current.minute not in minute_set:
next_mins = [m for m in minute_list if m > current.minute]
if next_mins:
current = current.replace(minute=next_mins[0])
else:
next_hours = [h for h in hour_list if h > current.hour]
if next_hours:
current = current.replace(hour=next_hours[0], minute=minute_list[0])
else:
current = (current + timedelta(days=1)).replace(
hour=hour_list[0], minute=minute_list[0]
)
continue
# ── match ──────────────────────────────────────────────────────────
results.append(current.isoformat())
_advance_from_results()
return results
@@ -127,7 +197,7 @@ def explain_cron():
if not user:
return jsonify({"message": "Nicht autorisiert"}), 401
try:
data = request.get_json() or {}
data = request.get_json(silent=True) or {}
expression = data.get("expression", "").strip()
fields = expression.split()