Skip to content

Commit e2c1049

Browse files
committed
Fix bug in validation that allowed ports in TLS passthrough
1 parent d59b726 commit e2c1049

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/components/component-utils.tsx

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,32 @@ export const noPropagation = <E extends React.BaseSyntheticEvent>(
2323
callback(event);
2424
}
2525

26+
/**
27+
* An input validation function combines check logic with an explanatory error
28+
* message, and provides a convenient mechanism to check the validity of an
29+
* input itself (in which case it sets the input's validity state) or a raw
30+
* input value (in which case it just returns the result, with no side effects).
31+
*/
32+
export interface InputValidationFunction {
33+
(input: HTMLInputElement | string): boolean;
34+
}
35+
2636
export const inputValidation = (
2737
checkFn: (input: string) => boolean,
2838
errorMessage: string
29-
) => (input: HTMLInputElement) => {
30-
const inputValue = input.value;
31-
if (!inputValue || checkFn(inputValue)) {
32-
input.setCustomValidity('');
39+
): InputValidationFunction => (input) => {
40+
const isInput = typeof input !== 'string';
41+
const inputValue = isInput ? input.value : input;
42+
43+
if (isInput) {
44+
if (!inputValue || checkFn(inputValue)) {
45+
input.setCustomValidity('');
46+
} else {
47+
input.setCustomValidity(errorMessage);
48+
}
49+
input.reportValidity();
50+
return input.validity.valid;
3351
} else {
34-
input.setCustomValidity(errorMessage);
52+
return checkFn(inputValue);
3553
}
36-
input.reportValidity();
37-
return input.validity.valid;
3854
}

src/components/settings/string-settings-list.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import { observer } from 'mobx-react';
44

55
import { styled } from '../../styles';
66
import { Icon } from '../../icons';
7-
import { isValidHost } from '../../model/network';
87

9-
import { inputValidation } from '../component-utils';
8+
import { InputValidationFunction } from '../component-utils';
109
import { TextInput } from '../common/inputs';
1110
import { SettingsButton } from './settings-components';
1211

@@ -36,8 +35,7 @@ export class StringSettingsList extends React.Component<{
3635
onDelete: (value: string) => void,
3736
onAdd: (value: string) => void,
3837
placeholder: string,
39-
40-
validationFn: (input: HTMLInputElement) => void
38+
validationFn: InputValidationFunction
4139
}> {
4240

4341
@observable
@@ -70,7 +68,7 @@ export class StringSettingsList extends React.Component<{
7068
/>
7169
<SettingsButton
7270
disabled={
73-
!isValidHost(this.inputValue) ||
71+
!this.props.validationFn(this.inputValue) ||
7472
values.includes(this.inputValue)
7573
}
7674
onClick={this.addHost}

0 commit comments

Comments
 (0)