如何在Java中使用org.owasp.encoder.Encode?

问题描述 投票:0回答:1
public static void sendEmail(String to, String cc, String subject, String content) throws IOException {
    LOG.info(ESAPIValidation.sanitizeParam((String.format("EmailUtil.sendEMail() \n To: %s\n Subjet: %s \n  Content: %s", to, subject, content))));
    String host = ApplicationUtil.getProperty(ApplicationConstants.AL_WS_MAIL_HOST);
    String port = ApplicationUtil.getProperty(ApplicationConstants.AL_WS_MAIL_PORT);
    String from = ApplicationUtil.getProperty(ApplicationConstants.AL_WS_MAIL_FROM);
    String defaultToAdd = ApplicationUtil.getProperty(ApplicationConstants.AL_WS_DEFAULT_MAIL);
    String emailSystemPrefix = ApplicationUtil.getProperty(ApplicationConstants.EMAIL_PREFIX);

    if (StringUtil.isNullOrEmpty(emailSystemPrefix)) {
        emailSystemPrefix = "";
    } else {
        emailSystemPrefix =  emailSystemPrefix  + " - "; 
    }

    Properties properties = System.getProperties();
    properties.setProperty("mail.smtp.host", host);
    properties.setProperty("mail.smtp.port", port);
    Session session = Session.getDefaultInstance(properties);

    try {
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));

        addToList(message, to);
        
        addCCList(message, cc);
        if(cc == null || (cc != null && !cc.equals(defaultToAdd)))
        addCCList(message, defaultToAdd);

        content = addFooter(content);
        message.setSubject(emailSystemPrefix + subject);
        message.setContent(content.replace("textarea", "div"), "text/html; charset=utf-8");
        Transport.send(message);

    } catch (AddressException addressException) {
        LOG.warn("EmailUtil.triggerDBFailureMail() AddressException ", addressException);
    } catch (MessagingException messageException) {
        LOG.warn("EmailUtil.triggerDBFailureMail() MessagingException ", messageException);
    }
}

在此代码中,我遇到了 Veracode 问题:

CRLF 序列的不正确中和(“CRLF 注入”)(CWE ID 93)

message.setSubject(emailSystemPrefix + subject);

我正在尝试使用

org.owasp.encoder.Encode
来解决相同问题,但我不知道
Encode
类的用法。谁能简要介绍一下
Encode
类的实现,它在哪里使用,它解决了什么问题?

java owasp veracode
1个回答
0
投票

您可以在

docs
中阅读有关 Encode

课程的内容

Encode——上下文编码的流畅接口。

每种上下文编码方法都有两个版本。第一个接受一个字符串参数并以字符串形式返回编码版本。第二个版本将编码版本直接写入写入器。

请确保阅读并理解该方法编码的上下文。对不正确的上下文进行编码可能会导致暴露跨站点脚本漏洞。

CRLF注入

术语 CRLF 指回车符(ASCII 13, ) 换行(ASCII 10, )。它们用于记录线路的终止,但是在当今流行的操作系统中处理方式有所不同。例如:在 Windows 中,需要使用 CR 和 LF 来表示行尾,而在 Linux/UNIX 中只需要 LF。在 HTTP 协议中,总是使用 CR-LF 序列来终止一行。

当用户设法将 CRLF 提交到应用程序时,就会发生 CRLF 注入攻击。这通常是通过修改 HTTP 参数或 URL 来完成的。

© www.soinside.com 2019 - 2024. All rights reserved.