Fix hostname/IP validation to prevent whitespace input in the SSL and Create New Service form (closes #114)

This commit is contained in:
Tola Leng
2026-01-24 20:58:33 +07:00
parent 4f367b92da
commit 370f613080
6 changed files with 48 additions and 37 deletions
@@ -25,6 +25,10 @@ export const useServiceSchema = () => {
type: z.enum(["http", "ping", "tcp", "dns"]), type: z.enum(["http", "ping", "tcp", "dns"]),
url: z.string() url: z.string()
.min(1, t("urlDomainHostRequired")) .min(1, t("urlDomainHostRequired"))
.refine(
(value) => !/\s/.test(value),
t("spacesNotAllowed")
)
.refine( .refine(
(value) => value.trim().length > 0, (value) => value.trim().length > 0,
t("enterValidUrlHostnameDomain") t("enterValidUrlHostnameDomain")
@@ -17,27 +17,33 @@ import { alertConfigService, AlertConfiguration } from "@/services/alertConfigSe
import { sslNotificationTemplateService, SslNotificationTemplate } from "@/services/sslNotificationTemplateService"; import { sslNotificationTemplateService, SslNotificationTemplate } from "@/services/sslNotificationTemplateService";
import { useLanguage } from "@/contexts/LanguageContext"; import { useLanguage } from "@/contexts/LanguageContext";
const formSchema = z.object({
domain: z.string().min(1, "Domain is required"),
warning_threshold: z.coerce.number().int().min(1).max(365),
expiry_threshold: z.coerce.number().int().min(1).max(30),
notification_channels: z.array(z.string()).optional(),
alert_template: z.string().optional(),
check_interval: z.coerce.number().int().min(1).max(30).optional()
});
interface AddSSLCertificateFormProps { interface AddSSLCertificateFormProps {
onSubmit: (data: AddSSLCertificateDto) => Promise<void>; onSubmit: (data: AddSSLCertificateDto) => Promise<void>;
onCancel: () => void; onCancel: () => void;
isPending?: boolean; isPending?: boolean;
} }
export const AddSSLCertificateForm = ({ export const AddSSLCertificateForm = ({
onSubmit, onSubmit,
onCancel, onCancel,
isPending = false isPending = false
}: AddSSLCertificateFormProps) => { }: AddSSLCertificateFormProps) => {
const { t } = useLanguage(); const { t } = useLanguage();
const formSchema = z.object({
domain: z.string()
.min(1, t("domain") + " is required")
.refine(
(value) => !/\s/.test(value),
t("spacesNotAllowed")
),
warning_threshold: z.coerce.number().int().min(1).max(365),
expiry_threshold: z.coerce.number().int().min(1).max(30),
notification_channels: z.array(z.string()).optional(),
alert_template: z.string().optional(),
check_interval: z.coerce.number().int().min(1).max(30).optional()
});
const [alertConfigs, setAlertConfigs] = useState<AlertConfiguration[]>([]); const [alertConfigs, setAlertConfigs] = useState<AlertConfiguration[]>([]);
const [sslTemplates, setSslTemplates] = useState<SslNotificationTemplate[]>([]); const [sslTemplates, setSslTemplates] = useState<SslNotificationTemplate[]>([]);
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
@@ -78,7 +84,7 @@ export const AddSSLCertificateForm = ({
setIsLoading(false); setIsLoading(false);
} }
}; };
fetchData(); fetchData();
}, [t]); }, [t]);
@@ -89,16 +95,16 @@ export const AddSSLCertificateForm = ({
domain: values.domain, domain: values.domain,
warning_threshold: values.warning_threshold, warning_threshold: values.warning_threshold,
expiry_threshold: values.expiry_threshold, expiry_threshold: values.expiry_threshold,
notification_channel: values.notification_channels && values.notification_channels.length > 0 notification_channel: values.notification_channels && values.notification_channels.length > 0
? values.notification_channels.join(',') ? values.notification_channels.join(',')
: '', : '',
notification_id: values.notification_channels && values.notification_channels.length > 0 notification_id: values.notification_channels && values.notification_channels.length > 0
? values.notification_channels.join(',') ? values.notification_channels.join(',')
: '', : '',
template_id: values.alert_template && values.alert_template !== 'none' ? values.alert_template : '', template_id: values.alert_template && values.alert_template !== 'none' ? values.alert_template : '',
check_interval: values.check_interval check_interval: values.check_interval
}; };
await onSubmit(certData); await onSubmit(certData);
form.reset(); form.reset();
} catch (error) { } catch (error) {
@@ -136,7 +142,7 @@ export const AddSSLCertificateForm = ({
</FormItem> </FormItem>
)} )}
/> />
<div className="grid grid-cols-1 md:grid-cols-2 gap-4"> <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<FormField <FormField
control={form.control} control={form.control}
@@ -154,7 +160,6 @@ export const AddSSLCertificateForm = ({
</FormItem> </FormItem>
)} )}
/> />
<FormField <FormField
control={form.control} control={form.control}
name="expiry_threshold" name="expiry_threshold"
@@ -189,7 +194,6 @@ export const AddSSLCertificateForm = ({
</FormItem> </FormItem>
)} )}
/> />
<FormField <FormField
control={form.control} control={form.control}
name="notification_channels" name="notification_channels"
@@ -197,7 +201,7 @@ export const AddSSLCertificateForm = ({
<FormItem> <FormItem>
<FormLabel>{t('notificationChannel')} (Multi-select)</FormLabel> <FormLabel>{t('notificationChannel')} (Multi-select)</FormLabel>
<div className="space-y-3"> <div className="space-y-3">
<Select <Select
onValueChange={handleAddNotificationChannel} onValueChange={handleAddNotificationChannel}
value="" value=""
> >
@@ -247,7 +251,7 @@ export const AddSSLCertificateForm = ({
)} )}
</div> </div>
<FormDescription className="flex items-center gap-1"> <FormDescription className="flex items-center gap-1">
<Bell className="h-4 w-4" /> <Bell className="h-4 w-4" />
{t('whereToSend')} {t('whereToSend')}
</FormDescription> </FormDescription>
<FormMessage /> <FormMessage />
@@ -289,7 +293,6 @@ export const AddSSLCertificateForm = ({
</FormItem> </FormItem>
)} )}
/> />
<DialogFooter> <DialogFooter>
<Button type="button" variant="outline" onClick={onCancel}>{t('cancel')}</Button> <Button type="button" variant="outline" onClick={onCancel}>{t('cancel')}</Button>
<Button type="submit" disabled={isPending || isLoading}> <Button type="submit" disabled={isPending || isLoading}>
@@ -88,6 +88,7 @@ export const servicesTranslations: ServicesTranslations = {
serviceNameRequired: "Service name is required", serviceNameRequired: "Service name is required",
urlDomainHostRequired: "URL/Domain/Host is required", urlDomainHostRequired: "URL/Domain/Host is required",
enterValidUrlHostnameDomain: "Please enter a valid URL, hostname, or domain", enterValidUrlHostnameDomain: "Please enter a valid URL, hostname, or domain",
spacesNotAllowed: "Spaces are not allowed",
// Dashboard // Dashboard
upServices: "UP SERVICES", upServices: "UP SERVICES",
@@ -88,6 +88,7 @@ export const servicesTranslations: ServicesTranslations = {
serviceNameRequired: "តម្រូវឱ្យមានឈ្មោះសេវាកម្ម", serviceNameRequired: "តម្រូវឱ្យមានឈ្មោះសេវាកម្ម",
urlDomainHostRequired: "តម្រូវឱ្យមាន URL/ដែន/ម៉ាស៊ីន", urlDomainHostRequired: "តម្រូវឱ្យមាន URL/ដែន/ម៉ាស៊ីន",
enterValidUrlHostnameDomain: "សូមបញ្ចូល URL, ឈ្មោះម៉ាស៊ីន ឬដែនដែលត្រឹមត្រូវ", enterValidUrlHostnameDomain: "សូមបញ្ចូល URL, ឈ្មោះម៉ាស៊ីន ឬដែនដែលត្រឹមត្រូវ",
spacesNotAllowed: "មិនអនុញ្ញាតឱ្យមានចន្លោះទំនេរទេ",
// Dashboard // Dashboard
upServices: "សេវាកម្មដំណើរការ", upServices: "សេវាកម្មដំណើរការ",
+14 -13
View File
@@ -1,19 +1,19 @@
export interface ServicesTranslations { export interface ServicesTranslations {
serviceStatus: string; serviceStatus: string;
uptime: string; uptime: string;
lastChecked: string; lastChecked: string;
noServices: string; noServices: string;
currentlyMonitoring: string; currentlyMonitoring: string;
retry: string; retry: string;
overview: string; overview: string;
newService: string; newService: string;
rowsPerPage: string; rowsPerPage: string;
search: string; search: string;
allTypes: string; allTypes: string;
createNewService: string; createNewService: string;
createNewServiceDesc: string; createNewServiceDesc: string;
// ServiceBasicFields.tsx // ServiceBasicFields.tsx
serviceName: string; serviceName: string;
@@ -87,6 +87,7 @@ export interface ServicesTranslations {
serviceNameRequired: string; serviceNameRequired: string;
urlDomainHostRequired: string; urlDomainHostRequired: string;
enterValidUrlHostnameDomain: string; enterValidUrlHostnameDomain: string;
spacesNotAllowed: string;
// Dashboard // Dashboard
upServices: string; upServices: string;
@@ -112,4 +112,5 @@ export interface SSLTranslations {
lastNotification: string; lastNotification: string;
collectionId: string; collectionId: string;
noCertificatesFound: string; noCertificatesFound: string;
spacesNotAllowed: string;
} }