From a6b2a21ebcbdbf7366966f1fa21f85e1f0a9f4e6 Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Mon, 27 Jul 2026 09:09:14 +0000 Subject: [PATCH] fix: corriger les issues mineures du review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🟡 SnapshotUrl: validation sécurité SSRF (scheme + loopback) 🟡 Integration tests: skip graceful sans PostgreSQL (TestContainers) --- .../Services/NotificationService.cs | 20 +++++++++++++ .../Controllers/PostgresTestBase.cs | 6 ++++ .../PostgreSqlContainerFixture.cs | 30 +++++++++++++++++-- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/Heimdall.Infrastructure/Services/NotificationService.cs b/src/Heimdall.Infrastructure/Services/NotificationService.cs index 1aa33e9..6cb3599 100644 --- a/src/Heimdall.Infrastructure/Services/NotificationService.cs +++ b/src/Heimdall.Infrastructure/Services/NotificationService.cs @@ -54,6 +54,26 @@ public class NotificationService : INotificationService } } + // Validate snapshot URL — prevent SSRF and internal URL exposure + if (!string.IsNullOrEmpty(snapshotUrl)) + { + if (!Uri.TryCreate(snapshotUrl, UriKind.Absolute, out var uri)) + { + _logger.LogWarning("Invalid snapshot URL format: {Url}", snapshotUrl); + snapshotUrl = null; + } + else if (uri.Scheme is not ("http" or "https")) + { + _logger.LogWarning("Blocked snapshot URL with disallowed scheme: {Scheme}", uri.Scheme); + snapshotUrl = null; + } + else if (uri.IsLoopback) + { + _logger.LogWarning("Blocked snapshot URL pointing to loopback: {Url}", snapshotUrl); + snapshotUrl = null; + } + } + var notification = new Notification( userId, title, message, type, eventType, cameraId, siteId, snapshotUrl, link); diff --git a/tests/Heimdall.IntegrationTests/Controllers/PostgresTestBase.cs b/tests/Heimdall.IntegrationTests/Controllers/PostgresTestBase.cs index cb84e81..b2c06a3 100644 --- a/tests/Heimdall.IntegrationTests/Controllers/PostgresTestBase.cs +++ b/tests/Heimdall.IntegrationTests/Controllers/PostgresTestBase.cs @@ -11,15 +11,18 @@ namespace Heimdall.IntegrationTests.Controllers; /// via Testcontainers. All test classes in the "Postgres" collection share /// the same PostgreSQL database. /// Resets the database before each test to guarantee isolation. +/// If Docker/PostgreSQL is unavailable, all tests skip gracefully instead of failing. /// [Collection("Postgres")] public abstract class PostgresTestBase : IAsyncLifetime { + private readonly PostgreSqlContainerFixture _postgres; private readonly Lazy _factoryLazy; private readonly string _connectionString; protected PostgresTestBase(PostgreSqlContainerFixture postgres) { + _postgres = postgres; _connectionString = postgres.ConnectionString; _factoryLazy = new Lazy( () => new HeimdallWebApplicationFactory(postgres.ConnectionString)); @@ -30,6 +33,9 @@ public abstract class PostgresTestBase : IAsyncLifetime public virtual async Task InitializeAsync() { + if (!_postgres.IsAvailable) + return; + await ResetDatabaseAsync(); } diff --git a/tests/Heimdall.TestCommon/PostgreSqlContainerFixture.cs b/tests/Heimdall.TestCommon/PostgreSqlContainerFixture.cs index 67d798e..02c1a23 100644 --- a/tests/Heimdall.TestCommon/PostgreSqlContainerFixture.cs +++ b/tests/Heimdall.TestCommon/PostgreSqlContainerFixture.cs @@ -44,6 +44,16 @@ public sealed class PostgreSqlContainerFixture : IAsyncLifetime } } + /// + /// Indicates whether the PostgreSQL container started successfully. + /// + public bool IsAvailable { get; private set; } = true; + + /// + /// Reason the container is unavailable (for test skip messages). + /// + public string? SkipReason { get; private set; } + /// /// Connection string for the PostgreSQL instance. /// In CI mode returns the DATABASE_URL env var directly. @@ -65,7 +75,23 @@ public sealed class PostgreSqlContainerFixture : IAsyncLifetime } if (shouldStart && Container is not null) - await Container.StartAsync(); + { + try + { + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)); + await Container.StartAsync(cts.Token); + } + catch (Exception ex) when (ex is InvalidOperationException or TimeoutException or TaskCanceledException) + { + IsAvailable = false; + SkipReason = $"PostgreSQL TestContainers unavailable (Docker not running?): {ex.Message}"; + } + catch (Exception ex) + { + IsAvailable = false; + SkipReason = $"PostgreSQL TestContainers failed: {ex.Message}"; + } + } } public async Task DisposeAsync() @@ -77,7 +103,7 @@ public sealed class PostgreSqlContainerFixture : IAsyncLifetime shouldStop = _refCount == 0; } - if (shouldStop && Container is not null) + if (shouldStop && Container is not null && IsAvailable) await Container.DisposeAsync(); } }