diff --git a/frontend/app/types/integrations/elasticsearchConfig.js b/frontend/app/types/integrations/elasticsearchConfig.js index 2b3edbcd4..07791553c 100644 --- a/frontend/app/types/integrations/elasticsearchConfig.js +++ b/frontend/app/types/integrations/elasticsearchConfig.js @@ -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 && diff --git a/frontend/app/validate.js b/frontend/app/validate.js index d36958195..de9b4bdc2 100644 --- a/frontend/app/validate.js +++ b/frontend/app/validate.js @@ -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) { diff --git a/frontend/app/validate.test.js b/frontend/app/validate.test.js new file mode 100644 index 000000000..437bf5fd0 --- /dev/null +++ b/frontend/app/validate.test.js @@ -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(); + }); +});