ux: warn people entering 127.0.0.1 without ::1 on netmasks

Massimo Melina committed Oct 20, 2024 at 00:39 UTC 56dae01c7f792ce64d6d8b5e542dc0498a80b8c1
2 files changed +15 -2
admin/src/mui.ts
+14 -1
@@ -19,6 +19,7 @@ import { SvgIconProps } from '@mui/material/SvgIcon/SvgIcon'
19 import _ from 'lodash'
20 import { ALL as COUNTRIES } from './countries'
21 import { apiCall } from '@hfs/shared/api'
22 +import { StringFieldProps } from '@hfs/mui-grid-form/StringField'
23
24 export function spinner() {
25 return h(CircularProgress)
@@ -257,7 +258,19 @@ export function useToggleButton(onTitle: string, offTitle: undefined | string, i
258 return [state, el] as const
259 }
260
260 -export const NetmaskField = StringField
261 +export function NetmaskField(props: StringFieldProps) {
262 + const warned = useRef(false)
263 + return h(StringField, {
264 + ...props,
265 + onTyping(v) {
266 + if (!warned.current && v?.includes('127.0.0.1') && !v.includes('::1')) {
267 + warned.current = true
268 + alertDialog(`Hostname "localhost" is normally translated as ::1 instead of 127.0.0.1`, 'warning')
269 + }
270 + return props.onTyping?.(v) ?? v
271 + },
272 + })
273 +}
274
275 export function Country({ code, ip, def, long, short }: { code: string, ip?: string, def?: ReactNode, long?: boolean, short?: boolean }) {
276 const good = ip && !isIpLocalHost(ip) && !isIpLan(ip)
mui-grid-form/StringField.ts
+1 -1
@@ -5,7 +5,7 @@ import { FieldProps } from '.'
5 import { Autocomplete, InputAdornment, TextField } from '@mui/material'
6 import { StandardTextFieldProps } from '@mui/material/TextField/TextField'
7
8 -interface StringFieldProps extends FieldProps<string>, Partial<Omit<StandardTextFieldProps, 'label' | 'onChange' | 'value'>> {
8 +export interface StringFieldProps extends FieldProps<string>, Partial<Omit<StandardTextFieldProps, 'label' | 'onChange' | 'value'>> {
9 typing?: boolean // change state as the user is typing
10 onTyping?: (v: string) => string | false
11 min?: number