fix: invalid cron will crash the homepage, close #975

This commit is contained in:
Maxi Quoß
2025-02-07 01:32:17 +01:00
parent 9107e4f252
commit e72dbb7621
3 changed files with 20 additions and 4 deletions

View File

@@ -3,6 +3,7 @@
import LL, { locale } from '$lib/i18n/i18n-svelte';
import { backendUrl, permission, pocketbase } from '$lib/stores/pocketbase';
import type { Device } from '$lib/types/device';
import { cronRegex } from '$lib/types/device';
import {
faBed,
faCircleArrowDown,
@@ -141,10 +142,18 @@
}
function getNextCronRelativeTime(expression: string, now: number) {
const cron = cronParser.parseExpression(expression);
return formatRelative(cron.next().toISOString(), now, {
locale: dateFnsLocale
});
if (!cronRegex.test(expression)) {
return 'Invalid cron';
}
try {
const cron = cronParser.parseExpression(expression);
return formatRelative(cron.next().toISOString(), now, {
locale: dateFnsLocale
});
} catch {
return 'Invalid cron';
}
}
</script>

View File

@@ -4,6 +4,7 @@
import LL from '$lib/i18n/i18n-svelte';
import { permission, pocketbase } from '$lib/stores/pocketbase';
import type { Device, Group, Port } from '$lib/types/device';
import { cronRegex } from '$lib/types/device';
import { faSave, faTrash, faX } from '@fortawesome/free-solid-svg-icons';
import { onMount } from 'svelte';
import Fa from 'svelte-fa';
@@ -392,6 +393,7 @@
bind:value={device.wake_cron}
disabled={!device.wake_cron_enabled}
required={device.wake_cron_enabled}
pattern={cronRegex.source}
/>
</div>
<div class="flex flex-col">
@@ -584,6 +586,7 @@
bind:value={device.shutdown_cron}
disabled={!device.shutdown_cron_enabled}
required={device.shutdown_cron_enabled}
pattern={cronRegex.source}
/>
</div>
<div class="flex flex-col">

View File

@@ -42,3 +42,7 @@ export type Port = RecordModel & {
export type Group = RecordModel & {
name: string;
};
export const cronRegex = new RegExp(
/(@(annually|yearly|monthly|weekly|daily|hourly))|((((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*) ?){5,7})/
);