// =============================================================================
// SecurityAssertions.cs
// Security-specific assertion helpers for testing
// =============================================================================
using FluentAssertions;
using System.Net;
using System.Text.RegularExpressions;
namespace StellaOps.Security.Tests.Infrastructure;
///
/// Security-specific assertion methods for common security test patterns.
///
public static partial class SecurityAssertions
{
///
/// Assert that a URL is safe (not an internal/metadata endpoint).
///
public static void AssertUrlIsSafe(string url)
{
var uri = new Uri(url, UriKind.RelativeOrAbsolute);
if (!uri.IsAbsoluteUri) return;
// Check for localhost/loopback
uri.Host.Should().NotBe("localhost", "URL should not point to localhost");
uri.Host.Should().NotBe("127.0.0.1", "URL should not point to loopback");
uri.Host.Should().NotBe("::1", "URL should not point to IPv6 loopback");
uri.Host.Should().NotBe("0.0.0.0", "URL should not point to all interfaces");
// Check for metadata endpoints
uri.Host.Should().NotBe("169.254.169.254", "URL should not point to cloud metadata");
uri.Host.Should().NotContain("metadata.google.internal", "URL should not point to GCP metadata");
// Check for private IP ranges
if (IPAddress.TryParse(uri.Host, out var ip))
{
IsPrivateIp(ip).Should().BeFalse("URL should not point to private IP addresses");
}
// Check for file:// scheme
uri.Scheme.Should().NotBe("file", "URL should not use file:// scheme");
}
///
/// Assert that a path does not contain traversal sequences.
///
public static void AssertNoPathTraversal(string path)
{
path.Should().NotContain("..", "Path should not contain traversal sequences");
path.Should().NotContain("%2e%2e", "Path should not contain encoded traversal");
path.Should().NotContain("%252e", "Path should not contain double-encoded traversal");
path.Should().NotContain("\0", "Path should not contain null bytes");
}
///
/// Assert that content is properly escaped for HTML context.
///
public static void AssertHtmlEscaped(string content, string originalInput)
{
if (originalInput.Contains('<'))
{
content.Should().NotContain("