diff --git a/app/common/src/test/java/stirling/software/common/util/CustomHtmlSanitizerTest.java b/app/common/src/test/java/stirling/software/common/util/CustomHtmlSanitizerTest.java index eaf992f71..9667348d6 100644 --- a/app/common/src/test/java/stirling/software/common/util/CustomHtmlSanitizerTest.java +++ b/app/common/src/test/java/stirling/software/common/util/CustomHtmlSanitizerTest.java @@ -1,41 +1,44 @@ package stirling.software.common.util; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.argThat; +import static org.mockito.Mockito.*; import java.util.stream.Stream; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import stirling.software.common.model.ApplicationProperties; import stirling.software.common.service.SsrfProtectionService; class CustomHtmlSanitizerTest { + // Changed: Promote mocks to fields so we can re-stub behavior per test where necessary. + private SsrfProtectionService ssrfProtectionService; + private ApplicationProperties applicationProperties; + private ApplicationProperties.System systemProperties; + private CustomHtmlSanitizer customHtmlSanitizer; @BeforeEach void setUp() { - SsrfProtectionService mockSsrfProtectionService = mock(SsrfProtectionService.class); - stirling.software.common.model.ApplicationProperties mockApplicationProperties = - mock(stirling.software.common.model.ApplicationProperties.class); - stirling.software.common.model.ApplicationProperties.System mockSystem = - mock(stirling.software.common.model.ApplicationProperties.System.class); + ssrfProtectionService = mock(SsrfProtectionService.class); + applicationProperties = mock(ApplicationProperties.class); + systemProperties = mock(ApplicationProperties.System.class); - // Allow all URLs by default for basic tests - when(mockSsrfProtectionService.isUrlAllowed(org.mockito.ArgumentMatchers.anyString())) - .thenReturn(true); - when(mockApplicationProperties.getSystem()).thenReturn(mockSystem); - when(mockSystem.getDisableSanitize()).thenReturn(false); // Enable sanitization for tests + // Default behavior: allow all URLs and enable sanitization + when(ssrfProtectionService.isUrlAllowed(anyString())).thenReturn(true); + when(applicationProperties.getSystem()).thenReturn(systemProperties); + when(systemProperties.getDisableSanitize()).thenReturn(false); - customHtmlSanitizer = - new CustomHtmlSanitizer(mockSsrfProtectionService, mockApplicationProperties); + customHtmlSanitizer = new CustomHtmlSanitizer(ssrfProtectionService, applicationProperties); } @ParameterizedTest @@ -56,10 +59,11 @@ class CustomHtmlSanitizerTest { "
This is valid HTML with formatting.
", new String[] {"", "", ""}),
Arguments.of(
- " Text with bold, italic, underline, "
- + "emphasis, strong, Text with bold, italic, underline,"
+ + " emphasis, strong,"
+ + " Safe contentstrikethrough, "
- + "strike, subscript, superscript, "
- + "teletype, code
, big, small.strikethrough, strike,"
+ + " subscript, superscript, teletype,"
+ + " code
, big, small.";
@@ -182,7 +186,13 @@ class CustomHtmlSanitizerTest {
void testSanitizeDisallowsDataUrlImages() {
// Arrange
String htmlWithDataUrlImage =
- "
";
+ "
";
+
+ // Changed: Explicitly tell SSRF service to reject data: URLs so the custom AttributePolicy
+ // drops the src attribute. Without this, a permissive SSRF mock might allow data: URLs.
+ when(ssrfProtectionService.isUrlAllowed(argThat(v -> v != null && v.startsWith("data:"))))
+ .thenReturn(false);
// Act
String sanitizedHtml = customHtmlSanitizer.sanitize(htmlWithDataUrlImage);
@@ -257,9 +267,9 @@ class CustomHtmlSanitizerTest {
void testSanitizeRemovesObjectAndEmbed() {
// Arrange
String htmlWithObjects =
- "