From 2e51b5c70d19959feacaf864707b1b70aa227e4c Mon Sep 17 00:00:00 2001 From: Arciiix Date: Wed, 28 Aug 2024 11:11:22 +0200 Subject: [PATCH 1/4] Introduce static field with allowed HttpUrl schemas --- .../IntegrationProperty.cs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/backend/src/Notifo.Domain.Integrations.Abstractions/IntegrationProperty.cs b/backend/src/Notifo.Domain.Integrations.Abstractions/IntegrationProperty.cs index 80db4098..732262c4 100644 --- a/backend/src/Notifo.Domain.Integrations.Abstractions/IntegrationProperty.cs +++ b/backend/src/Notifo.Domain.Integrations.Abstractions/IntegrationProperty.cs @@ -5,11 +5,11 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using Notifo.Domain.Integrations.Resources; +using Notifo.Infrastructure.Validation; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Text.RegularExpressions; -using Notifo.Domain.Integrations.Resources; -using Notifo.Infrastructure.Validation; #pragma warning disable SA1313 // Parameter names should begin with lower-case letter @@ -17,6 +17,8 @@ namespace Notifo.Domain.Integrations; public sealed record IntegrationProperty(string Name, PropertyType Type) { + private static readonly string[] AllowedHttpUrlSchemes = { "http", "https" }; + public string? DefaultValue { get; init; } public string? EditorDescription { get; init; } @@ -185,10 +187,7 @@ private bool TryGetString(string? input, [MaybeNullWhen(true)] out string error, break; case PropertyFormat.HttpUrl: - // We only allow "http" and "https" schemas to enable the usage of URL field for HttpClient requests. - if (!Uri.TryCreate(input, UriKind.Absolute, out var uri) - || (!string.Equals(uri.Scheme, "http", StringComparison.OrdinalIgnoreCase) && !string.Equals(uri.Scheme, "https", StringComparison.OrdinalIgnoreCase)) - ) + if (!Uri.TryCreate(input, UriKind.Absolute, out var uri) || !AllowedHttpUrlSchemes.Contains(uri.Scheme, StringComparer.OrdinalIgnoreCase)) { error = Texts.IntegrationPropertyFormatHttpUrl; return false; From 9114dcb47b7310c6b47da7f267ea1bb6aed54f38 Mon Sep 17 00:00:00 2001 From: Arciiix Date: Wed, 28 Aug 2024 11:15:09 +0200 Subject: [PATCH 2/4] Rename HttpUrl PropertyFormat tests --- .../Integrations/IntegrationPropertyTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/tests/Notifo.Domain.Tests/Integrations/IntegrationPropertyTests.cs b/backend/tests/Notifo.Domain.Tests/Integrations/IntegrationPropertyTests.cs index 2e41cdc5..2ce322ab 100644 --- a/backend/tests/Notifo.Domain.Tests/Integrations/IntegrationPropertyTests.cs +++ b/backend/tests/Notifo.Domain.Tests/Integrations/IntegrationPropertyTests.cs @@ -162,7 +162,7 @@ public void Should_not_fail_if_undefined_value_is_not_an_allowed_value(string? i [InlineData("localhost.com/test")] [InlineData("192.168.0.101")] [InlineData("randomString")] - public void Should_fail_if_url_is_invalid(string? input) + public void Should_fail_if_http_url_is_invalid(string? input) { var source = new Dictionary { @@ -182,7 +182,7 @@ public void Should_fail_if_url_is_invalid(string? input) [InlineData("http://localhost/test")] [InlineData("https://example.com/test?query=example")] [InlineData("http://login:password@test.pl/random")] - public void Should_get_url_if_value_is_valid(string? input) + public void Should_get_http_url_if_value_is_valid(string? input) { var source = new Dictionary { From de3cd5521bf134c433d0d7d2ae8cf725eecc4a34 Mon Sep 17 00:00:00 2001 From: Arciiix Date: Wed, 28 Aug 2024 11:16:47 +0200 Subject: [PATCH 3/4] Move the using statements down to ensure consistency --- .../IntegrationProperty.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/src/Notifo.Domain.Integrations.Abstractions/IntegrationProperty.cs b/backend/src/Notifo.Domain.Integrations.Abstractions/IntegrationProperty.cs index 732262c4..7dd9f64e 100644 --- a/backend/src/Notifo.Domain.Integrations.Abstractions/IntegrationProperty.cs +++ b/backend/src/Notifo.Domain.Integrations.Abstractions/IntegrationProperty.cs @@ -5,11 +5,11 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== -using Notifo.Domain.Integrations.Resources; -using Notifo.Infrastructure.Validation; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Text.RegularExpressions; +using Notifo.Domain.Integrations.Resources; +using Notifo.Infrastructure.Validation; #pragma warning disable SA1313 // Parameter names should begin with lower-case letter From 2433234deaa73ecc43df3f61523ba5764b59f624 Mon Sep 17 00:00:00 2001 From: Arciiix Date: Wed, 28 Aug 2024 11:19:57 +0200 Subject: [PATCH 4/4] Add a non-HTTP URL test case for HttpUrl PropertyFormat --- .../Notifo.Domain.Tests/Integrations/IntegrationPropertyTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/tests/Notifo.Domain.Tests/Integrations/IntegrationPropertyTests.cs b/backend/tests/Notifo.Domain.Tests/Integrations/IntegrationPropertyTests.cs index 2ce322ab..9b647ea9 100644 --- a/backend/tests/Notifo.Domain.Tests/Integrations/IntegrationPropertyTests.cs +++ b/backend/tests/Notifo.Domain.Tests/Integrations/IntegrationPropertyTests.cs @@ -162,6 +162,7 @@ public void Should_not_fail_if_undefined_value_is_not_an_allowed_value(string? i [InlineData("localhost.com/test")] [InlineData("192.168.0.101")] [InlineData("randomString")] + [InlineData("mqtt://localhost:1883/")] public void Should_fail_if_http_url_is_invalid(string? input) { var source = new Dictionary