|
| 1 | +package datadog.trace.instrumentation.jakarta.mail; |
| 2 | + |
| 3 | +import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; |
| 4 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 5 | + |
| 6 | +import com.google.auto.service.AutoService; |
| 7 | +import datadog.trace.agent.tooling.Instrumenter; |
| 8 | +import datadog.trace.agent.tooling.InstrumenterModule; |
| 9 | +import datadog.trace.api.iast.InstrumentationBridge; |
| 10 | +import datadog.trace.api.iast.Sink; |
| 11 | +import datadog.trace.api.iast.VulnerabilityTypes; |
| 12 | +import datadog.trace.api.iast.sink.EmailInjectionModule; |
| 13 | +import jakarta.mail.Message; |
| 14 | +import jakarta.mail.MessagingException; |
| 15 | +import jakarta.mail.Multipart; |
| 16 | +import jakarta.mail.Part; |
| 17 | +import java.io.IOException; |
| 18 | +import net.bytebuddy.asm.Advice; |
| 19 | +import org.slf4j.Logger; |
| 20 | +import org.slf4j.LoggerFactory; |
| 21 | + |
| 22 | +@AutoService(InstrumenterModule.class) |
| 23 | +public class JakartaMailInstrumentation extends InstrumenterModule.Iast |
| 24 | + implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice { |
| 25 | + |
| 26 | + private static Logger LOGGER = LoggerFactory.getLogger(JakartaMailInstrumentation.class); |
| 27 | + |
| 28 | + public JakartaMailInstrumentation() { |
| 29 | + super("jakarta-mail", "jakarta-mail-transport"); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public void methodAdvice(MethodTransformer transformer) { |
| 34 | + transformer.applyAdvice( |
| 35 | + named("send0").and(takesArgument(0, named("jakarta.mail.Message"))), |
| 36 | + JakartaMailInstrumentation.class.getName() + "$MailInjectionAdvice"); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public String instrumentedType() { |
| 41 | + return "jakarta.mail.Transport"; |
| 42 | + } |
| 43 | + |
| 44 | + public static class MailInjectionAdvice { |
| 45 | + @Sink(VulnerabilityTypes.EMAIL_HTML_INJECTION) |
| 46 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 47 | + public static void onSend(@Advice.Argument(0) final Message message) |
| 48 | + throws MessagingException, IOException { |
| 49 | + EmailInjectionModule emailInjectionModule = InstrumentationBridge.EMAIL_INJECTION; |
| 50 | + if (emailInjectionModule == null) { |
| 51 | + return; |
| 52 | + } |
| 53 | + if (message == null || message.getContent() == null) { |
| 54 | + return; |
| 55 | + } |
| 56 | + if (message.isMimeType("text/html")) { |
| 57 | + emailInjectionModule.onSendEmail(message.getContent()); |
| 58 | + } else if (message.isMimeType("multipart/*")) { |
| 59 | + Multipart parts = (Multipart) message.getContent(); |
| 60 | + for (int i = 0; i < parts.getCount(); i++) { |
| 61 | + final Part part = parts.getBodyPart(i); |
| 62 | + if (part != null && part.isMimeType("text/html")) { |
| 63 | + emailInjectionModule.onSendEmail(part.getContent()); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments