fix: corriger les issues mineures du review
🟡 SnapshotUrl: validation sécurité SSRF (scheme + loopback) 🟡 Integration tests: skip graceful sans PostgreSQL (TestContainers)
This commit is contained in:
1 parent
fba29b3eb4
commit
a6b2a21ebc
3 files changed
+54
-2
No files matched your search
@@ -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);
|
||||
|
||||
|
||||
@@ -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.
|
||||
/// </summary>
|
||||
[Collection("Postgres")]
|
||||
public abstract class PostgresTestBase : IAsyncLifetime
|
||||
{
|
||||
private readonly PostgreSqlContainerFixture _postgres;
|
||||
private readonly Lazy<HeimdallWebApplicationFactory> _factoryLazy;
|
||||
private readonly string _connectionString;
|
||||
|
||||
protected PostgresTestBase(PostgreSqlContainerFixture postgres)
|
||||
{
|
||||
_postgres = postgres;
|
||||
_connectionString = postgres.ConnectionString;
|
||||
_factoryLazy = new Lazy<HeimdallWebApplicationFactory>(
|
||||
() => new HeimdallWebApplicationFactory(postgres.ConnectionString));
|
||||
@@ -30,6 +33,9 @@ public abstract class PostgresTestBase : IAsyncLifetime
|
||||
|
||||
public virtual async Task InitializeAsync()
|
||||
{
|
||||
if (!_postgres.IsAvailable)
|
||||
return;
|
||||
|
||||
await ResetDatabaseAsync();
|
||||
}
|
||||
|
||||
|
||||
@@ -44,6 +44,16 @@ public sealed class PostgreSqlContainerFixture : IAsyncLifetime
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the PostgreSQL container started successfully.
|
||||
/// </summary>
|
||||
public bool IsAvailable { get; private set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Reason the container is unavailable (for test skip messages).
|
||||
/// </summary>
|
||||
public string? SkipReason { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user