fix(platform): sanitize Spring mail test failures

This commit is contained in:
sechmachine
2026-08-15 03:24:59 +07:00
parent 62a21132e2
commit bf6f9af78b
3 changed files with 83 additions and 5 deletions
@@ -9,6 +9,7 @@ import com.lab.labtimesheet.feature.integration.model.dto.SmtpForm;
import com.lab.labtimesheet.feature.integration.service.SmtpConfigurationService;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import org.springframework.mail.MailException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
@@ -76,7 +77,7 @@ class SmtpController {
try {
smtp.testDraft(action.getDraftId(), adminId(principal), principal.getName());
return "redirect:/admin/smtp?tested";
} catch (IllegalArgumentException | IllegalStateException failure) {
} catch (IllegalArgumentException | IllegalStateException | MailException failure) {
bindingResult.reject("smtp.test.failed", TEST_FAILURE_MESSAGE);
return renderActionError(model, bindingResult);
}
@@ -26,6 +26,7 @@ import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.mail.MailSendException;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
@@ -158,7 +159,7 @@ class SmtpOnboardingWebIntegrationTest {
.andReturn().getResponse().getContentAsString();
String draftId = html.replaceAll("(?s).*name=\"draftId\" value=\"([0-9]+)\".*", "$1");
String rawDiagnostic = "AUTH rejected for smtp-secret-raw-diagnostic";
probe.failureMessage = rawDiagnostic;
probe.failure = new MailSendException(rawDiagnostic);
mockMvc.perform(post("/admin/smtp/test")
.with(user("admin@example.com").roles("ADMIN"))
@@ -185,12 +186,12 @@ class SmtpOnboardingWebIntegrationTest {
static final class RecordingProbe implements SmtpProbe {
private final List<String> recipients = new ArrayList<>();
private String failureMessage;
private MailSendException failure;
@Override
public void send(SmtpConnection connection, String recipient, String subject, String body) {
if (failureMessage != null) {
throw new IllegalStateException(failureMessage);
if (failure != null) {
throw failure;
}
recipients.add(recipient);
}