fix: preserve zero values in autoSavePayload

TASK-BUG-04: The autoSavePayload was using the || operator to fall back
to server values when ban_time, find_time, or max_retry were empty or zero.
This silently dropped user intent to set these fields to 0, which is a
valid and meaningful value in fail2ban (e.g., ban_time=0 means permanent ban).

Replace the || fallback with explicit NaN and empty-string guards that
only fall back when:
1. The trimmed input is empty (user cleared the field)
2. The input is non-numeric (NaN)

This preserves valid zero values while still falling back appropriately
for invalid input.

- ban_time: 0 now correctly sends permanent ban instead of falling back
- find_time: 0 now sends the intended value instead of falling back
- max_retry: 0 now sends the intended value instead of falling back

Added comprehensive tests for:
- Preserving zero values in the payload
- Falling back for empty input
- Falling back for non-numeric input

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-04-22 21:30:31 +02:00
parent dfd1b9006b
commit 649ebf2dc7
2 changed files with 147 additions and 3 deletions

View File

@@ -210,9 +210,9 @@ function JailConfigDetail({
const autoSavePayload = useMemo<JailConfigUpdate>(
() => ({
ban_time: Number(banTime) || jail.ban_time,
find_time: Number(findTime) || jail.find_time,
max_retry: Number(maxRetry) || jail.max_retry,
ban_time: banTime.trim() === "" || Number.isNaN(Number(banTime)) ? jail.ban_time : Number(banTime),
find_time: findTime.trim() === "" || Number.isNaN(Number(findTime)) ? jail.find_time : Number(findTime),
max_retry: maxRetry.trim() === "" || Number.isNaN(Number(maxRetry)) ? jail.max_retry : Number(maxRetry),
fail_regex: failRegex,
ignore_regex: ignoreRegex,
date_pattern: datePattern !== "" ? datePattern : null,