Validate port numbers better
This commit is contained in:
parent
9806d6c12f
commit
2ada552220
File diff suppressed because one or more lines are too long
@ -4,7 +4,7 @@ import { Form, withFormik } from 'formik';
|
||||
import Navicon from 'containers/Navicon';
|
||||
import Checkbox from 'components/ui/Checkbox';
|
||||
import TextInput from 'components/ui/TextInput';
|
||||
import { isValidNick, isValidChannel, isValidUsername } from 'utils';
|
||||
import { isValidNick, isValidChannel, isValidUsername, isInt } from 'utils';
|
||||
|
||||
const getSortedDefaultChannels = createSelector(
|
||||
defaults => defaults.channels,
|
||||
@ -130,7 +130,7 @@ export default withFormik({
|
||||
|
||||
if (!values.port) {
|
||||
values.port = values.ssl ? '6697' : '6667';
|
||||
} else if (values.port < 1 || values.port > 65535) {
|
||||
} else if (!isInt(values.port, 1, 65535)) {
|
||||
errors.port = 'Invalid port';
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
import React from 'react';
|
||||
import { isChannel, isValidNick, isValidChannel, isValidUsername } from '..';
|
||||
import { isChannel, isValidNick, isValidChannel, isValidUsername, isInt } from '..';
|
||||
import linkify from '../linkify';
|
||||
|
||||
describe('isChannel()', () => {
|
||||
@ -76,6 +76,22 @@ describe('isValidUsername()', () => {
|
||||
));
|
||||
});
|
||||
|
||||
describe('isInt()', () => {
|
||||
it('validates integers', () => {
|
||||
expect(isInt('0')).toBe(true);
|
||||
expect(isInt('1337')).toBe(true);
|
||||
expect(isInt('0', 0, 65535)).toBe(true);
|
||||
expect(isInt('0', 1, 65535)).toBe(false);
|
||||
expect(isInt('1', 1, 65535)).toBe(true);
|
||||
expect(isInt('65535', 1, 65535)).toBe(true);
|
||||
expect(isInt('00065535', 1, 65535)).toBe(true);
|
||||
expect(isInt('65536', 1, 65535)).toBe(false);
|
||||
expect(isInt('1cake', 1, 65535)).toBe(false);
|
||||
expect(isInt('cake1', 1, 65535)).toBe(false);
|
||||
expect(isInt('', 1, 65535)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('linkify()', () => {
|
||||
const proto = href => (href.indexOf('http') !== 0 ? `http://${href}` : href);
|
||||
const linkTo = href => (
|
||||
|
@ -122,6 +122,21 @@ export function isValidUsername(username) {
|
||||
return true;
|
||||
}
|
||||
|
||||
export function isInt(str, min, max) {
|
||||
if (!str || str < min || str > max) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
const char = str.charCodeAt(i);
|
||||
if (char < 48 || char > 57) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export function timestamp(date = new Date()) {
|
||||
const h = padStart(date.getHours(), 2, '0');
|
||||
const m = padStart(date.getMinutes(), 2, '0');
|
||||
|
Loading…
Reference in New Issue
Block a user