From b920c4aaf9e5424f252821c17ef20f3169d21d36 Mon Sep 17 00:00:00 2001 From: Alex Karpov Date: Thu, 13 Feb 2025 16:54:46 +0200 Subject: [PATCH] NAS-134061 / 25.10 / Apps - UI validates `uri` type more strictly than middleware (#11530) * NAS-134061: Apps - UI validates `uri` type more strictly than middleware * NAS-134061: PR update --- .../validators/url-validation.service.spec.ts | 59 +++++++++++++++++++ .../validators/url-validation.service.ts | 2 +- 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/app/modules/forms/ix-forms/validators/url-validation.service.spec.ts diff --git a/src/app/modules/forms/ix-forms/validators/url-validation.service.spec.ts b/src/app/modules/forms/ix-forms/validators/url-validation.service.spec.ts new file mode 100644 index 00000000000..4dc81607665 --- /dev/null +++ b/src/app/modules/forms/ix-forms/validators/url-validation.service.spec.ts @@ -0,0 +1,59 @@ +import { TestBed } from '@angular/core/testing'; +import { UrlValidationService } from './url-validation.service'; + +describe('UrlValidationService', () => { + let service: UrlValidationService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(UrlValidationService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); + + it('should validate correct URLs', () => { + const validUrls = [ + 'http://example.com', + 'https://example.com', + 'https://example.com:8080/path', + 'ftp://ftp.example.com', + 'mqtt://server:1234', + 'whatever://anything', + 'ws://websocket.server', + 'wss://secure.websocket.server', + ]; + + validUrls.forEach((url) => { + expect(service.urlRegex.test(url)).toBe(true); + }); + }); + + it('should invalidate incorrect URLs', () => { + const invalidUrls = [ + 'example.com', + 'http//invalid.com', + '://invalid.com', + 'http://inva lid.com', + 'mqtt://server:', + 'wss://', + 'ftp://example.com:abc', + 'http:/example.com', + ]; + + invalidUrls.forEach((url) => { + expect(service.urlRegex.test(url)).toBe(false); + }); + }); + + it('should allow URLs with ports', () => { + expect(service.urlRegex.test('http://example.com:8080')).toBe(true); + expect(service.urlRegex.test('mqtt://server:1883')).toBe(true); + }); + + it('should allow URLs with paths and query params', () => { + expect(service.urlRegex.test('https://example.com/path/to/resource')).toBe(true); + expect(service.urlRegex.test('ftp://example.com/files?name=test')).toBe(true); + }); +}); diff --git a/src/app/modules/forms/ix-forms/validators/url-validation.service.ts b/src/app/modules/forms/ix-forms/validators/url-validation.service.ts index 73a24b9a143..7786483064d 100644 --- a/src/app/modules/forms/ix-forms/validators/url-validation.service.ts +++ b/src/app/modules/forms/ix-forms/validators/url-validation.service.ts @@ -2,5 +2,5 @@ import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class UrlValidationService { - urlRegex = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=.]+$/; + urlRegex = /^([a-zA-Z][a-zA-Z\d+\-.]*):\/\/([^\s:/?#]+)(:\d{1,5})?(\/[^\s]*)?$/; }