Skip to content

Commit 9c8f2dc

Browse files
committed
try "parse, don't validate" on ICMP type
1 parent 23addb9 commit 9c8f2dc

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

app/forms/firewall-rules-common.tsx

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -354,13 +354,17 @@ const isDuplicateProtocol = (
354354
return false
355355
}
356356

357-
const icmpTypeValidation = (value: string | number | undefined) => {
358-
if (value === undefined || value === '') return undefined // allow empty
357+
type ParseResult<T> = { success: true; data: T } | { success: false; message: string }
358+
359+
const parseIcmpType = (
360+
value: string | number | undefined
361+
): ParseResult<number | undefined> => {
362+
if (value === undefined || value === '') return { success: true, data: undefined }
359363
const parsed = typeof value === 'string' ? parseInt(value, 10) : value
360364
if (isNaN(parsed) || parsed < 0 || parsed > 255) {
361-
return `ICMP type must be a number between 0 and 255`
365+
return { success: false, message: `ICMP type must be a number between 0 and 255` }
362366
}
363-
return undefined
367+
return { success: true, data: parsed }
364368
}
365369

366370
const icmpCodeValidation = (value: string | undefined) => {
@@ -418,23 +422,18 @@ const ProtocolFilters = ({ control }: { control: Control<FirewallRuleValues> })
418422
if (values.protocolType === 'tcp' || values.protocolType === 'udp') {
419423
addProtocolIfUnique({ type: values.protocolType })
420424
} else if (values.protocolType === 'icmp') {
421-
if (values.icmpType === undefined || values.icmpType === '') {
425+
// this parse should never fail because we've already validated, but doing
426+
// it this way keeps the just-in-case early return logic consistent
427+
const parseResult = parseIcmpType(values.icmpType)
428+
if (!parseResult.success) return
429+
430+
const icmpType = parseResult.data
431+
if (icmpType === undefined) {
422432
// All ICMP types
423433
addProtocolIfUnique({ type: 'icmp', value: null })
424434
} else {
425435
// Specific ICMP type
426-
const parsedIcmpType =
427-
typeof values.icmpType === 'string'
428-
? parseInt(values.icmpType, 10)
429-
: values.icmpType
430-
431-
if (isNaN(parsedIcmpType) || parsedIcmpType < 0 || parsedIcmpType > 255) {
432-
return
433-
}
434-
435-
const icmpValue: VpcFirewallIcmpFilter = {
436-
icmpType: parsedIcmpType,
437-
}
436+
const icmpValue: VpcFirewallIcmpFilter = { icmpType }
438437
if (values.icmpCode) {
439438
icmpValue.code = values.icmpCode
440439
}
@@ -473,7 +472,10 @@ const ProtocolFilters = ({ control }: { control: Control<FirewallRuleValues> })
473472
placeholder=""
474473
allowArbitraryValues
475474
items={icmpTypeItems}
476-
validate={icmpTypeValidation}
475+
validate={(value) => {
476+
const result = parseIcmpType(value)
477+
if (!result.success) return result.message
478+
}}
477479
/>
478480

479481
{selectedIcmpType !== undefined && selectedIcmpType !== '' && (

0 commit comments

Comments
 (0)