fix(ui): elastic config host validation

This commit is contained in:
Shekar Siri 2024-01-19 14:55:25 +01:00
parent 1fbff97660
commit 133cbc049d
3 changed files with 43 additions and 4 deletions

View file

@ -1,8 +1,8 @@
import Record from 'Types/Record';
import { validateURL } from 'App/validate'
export const API_KEY_ID_LENGTH = 20;
export const API_KEY_LENGTH = 22;
export const API_KEY_ID_LENGTH = 5;
export const API_KEY_LENGTH = 5;
export default Record({
projectId: undefined,
@ -20,7 +20,7 @@ export default Record({
}),
methods: {
validateKeys() {
return this.apiKeyId.length >= API_KEY_ID_LENGTH && this.apiKey.length >= API_KEY_LENGTH && validateURL(this.host);
return this.apiKeyId.length > API_KEY_ID_LENGTH && this.apiKey.length > API_KEY_LENGTH && validateURL(this.host);
},
validate() {
return this.host !== '' && this.apiKeyId !== '' && this.apiKey !== '' && this.indexes !== '' && !!this.port &&

View file

@ -5,7 +5,9 @@ export function validateIP(value) {
export function validateURL(value) {
if (typeof value !== 'string') return false;
return /^(http|https):\/\/(?:www\.)?[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,}(\/\S*)?$/i.test(value);
const urlRegex = /^(http|https):\/\/(?:www\.)?[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,}(\/\S*)?$/i;
const ipRegex = /^(http|https):\/\/(?:localhost|(\d{1,3}\.){3}\d{1,3})(:\d+)?(\/\S*)?$/i;
return urlRegex.test(value) || ipRegex.test(value);
}
function escapeRegexp(s) {

View file

@ -0,0 +1,37 @@
import { validateURL } from './validate';
describe('validateURL', () => {
test('validates standard URLs', () => {
expect(validateURL('http://www.example.com')).toBeTruthy();
expect(validateURL('https://example.com')).toBeTruthy();
expect(validateURL('https://sub.example.com/path')).toBeTruthy();
});
test('validates localhost URLs', () => {
expect(validateURL('http://localhost')).toBeTruthy();
expect(validateURL('https://localhost:8080')).toBeTruthy();
expect(validateURL('http://localhost/path')).toBeTruthy();
});
test('validates IP address URLs', () => {
expect(validateURL('http://127.0.0.1')).toBeTruthy();
expect(validateURL('https://192.168.1.1')).toBeTruthy();
expect(validateURL('http://192.168.1.1:3000/path')).toBeTruthy();
});
test('rejects invalid URLs', () => {
expect(validateURL('justastring')).toBeFalsy();
expect(validateURL('http://')).toBeFalsy();
expect(validateURL('https://.com')).toBeFalsy();
expect(validateURL('256.256.256.256')).toBeFalsy(); // Invalid IP
expect(validateURL('http://example')).toBeFalsy(); // Missing TLD
});
test('rejects non-string inputs', () => {
expect(validateURL(12345)).toBeFalsy();
expect(validateURL({ url: 'http://example.com' })).toBeFalsy();
expect(validateURL(['http://example.com'])).toBeFalsy();
expect(validateURL(null)).toBeFalsy();
expect(validateURL(undefined)).toBeFalsy();
});
});