[
https://issues.apache.org/jira/browse/EMAIL-127?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Dominik Stadler updated EMAIL-127:
----------------------------------
Description:
I have a unit test which shows that dollar sign and backslash cause
ImageHtmlEmail to throw an Exception (unfortunately not easy to provide a
complete unit test, but it should be easy to enhance one of the existing tests
for ImageHtmlEmail):
{quote}
FileUtils.writeStringToFile(file, "<html>teststring
<img title=\"$\" src=\"file://" + file.getAbsolutePath() + "\"/></html>");
sender.sendHTMLEmail(files, mailConfig, emailConfig,
REPORT_DIR);
{quote}
It will throw the following Exception:
{quote}
java.lang.IllegalArgumentException: Illegal group reference
at java.util.regex.Matcher.appendReplacement(Matcher.java:808)
at
org.apache.commons.mail.ImageHtmlEmail.replacePattern(ImageHtmlEmail.java:180)
at
org.apache.commons.mail.ImageHtmlEmail.buildMimeMessage(ImageHtmlEmail.java:103)
at org.apache.commons.mail.Email.send(Email.java:1427)
at EmailSender.sendHTMLEmail(EmailSender.java:93)
at
EmailSenderTest.testImageHtmlEmailDollarBackslash(EmailSenderTest.java:801)
{quote}
the cause is that Matcher.appendReplacement() actually treats Dollar and
Backslash as special characters. From the javadoc of Matcher:
{quote}
* Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in
* the replacement string may cause the results to be different than if it
* were being treated as a literal replacement string. Dollar signs may be
* treated as references to captured subsequences as described above, and
* backslashes are used to escape literal characters in the replacement
* string.
{quote}
A possible fix is to replace line 180 of ImageHtmlEmail.java with the
following, i.e. replace "$" by "\$" and "\" by "\\" to have them properly
escaped.
{code}
matcher.appendReplacement(stringBuffer, (matcher.group(1) + "cid:" + cid +
matcher.group(3)).replace("\\", "\\\\").replace("$", "\\$"));
{code}
was:
I have a unit test which shows that dollar sign and backslash cause
ImageHtmlEmail to throw an Exception (unfortunately not easy to provide a
complete unit test, but it should be easy to enhance one of the existing tests
for ImageHtmlEmail):
{quote}
FileUtils.writeStringToFile(file, "<html>teststring
<img title=\"$\" src=\"file://" + file.getAbsolutePath() + "\"/></html>");
sender.sendHTMLEmail(files, mailConfig, emailConfig,
REPORT_DIR);
{quote}
It will throw the following Exception:
{quote}
java.lang.IllegalArgumentException: Illegal group reference
at java.util.regex.Matcher.appendReplacement(Matcher.java:808)
at
org.apache.commons.mail.ImageHtmlEmail.replacePattern(ImageHtmlEmail.java:180)
at
org.apache.commons.mail.ImageHtmlEmail.buildMimeMessage(ImageHtmlEmail.java:103)
at org.apache.commons.mail.Email.send(Email.java:1427)
at EmailSender.sendHTMLEmail(EmailSender.java:93)
at
EmailSenderTest.testImageHtmlEmailDollarBackslash(EmailSenderTest.java:801)
{quote}
the cause is that Matcher.appendReplacement() actually treats Dollar and
Backslash as special characters. From the javadoc of Matcher:
{quote}
* Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in
* the replacement string may cause the results to be different than if it
* were being treated as a literal replacement string. Dollar signs may be
* treated as references to captured subsequences as described above, and
* backslashes are used to escape literal characters in the replacement
* string.
{quote}
A possible fix is to replace line 180 of ImageHtmlEmail.java with the
following, i.e. replace "$" by "\$" and "\" by "\\" to have them properly
escaped.
{code}
matcher.appendReplacement(stringBuffer, (matcher.group(1) + "cid:" + cid +
matcher.group(3)).replace("$", "\\$").replace("\\", "\\\\"));
{code}
first replace backslash then dollar to not escape backslash from
dollar-escaping again...
> ImageHtmlEmail: Dollar or Backslash in certain places in <img ...
> src...>-tags causes exceptions "IllegalArgumentException: Illegal group
> reference"
> ----------------------------------------------------------------------------------------------------------------------------------------------------
>
> Key: EMAIL-127
> URL: https://issues.apache.org/jira/browse/EMAIL-127
> Project: Commons Email
> Issue Type: Bug
> Affects Versions: 1.3.1
> Environment: Linux
> Reporter: Dominik Stadler
> Priority: Blocker
>
> I have a unit test which shows that dollar sign and backslash cause
> ImageHtmlEmail to throw an Exception (unfortunately not easy to provide a
> complete unit test, but it should be easy to enhance one of the existing
> tests for ImageHtmlEmail):
> {quote}
> FileUtils.writeStringToFile(file, "<html>teststring
> <img title=\"$\" src=\"file://" + file.getAbsolutePath() + "\"/></html>");
> sender.sendHTMLEmail(files, mailConfig, emailConfig,
> REPORT_DIR);
> {quote}
> It will throw the following Exception:
> {quote}
> java.lang.IllegalArgumentException: Illegal group reference
> at java.util.regex.Matcher.appendReplacement(Matcher.java:808)
> at
> org.apache.commons.mail.ImageHtmlEmail.replacePattern(ImageHtmlEmail.java:180)
> at
> org.apache.commons.mail.ImageHtmlEmail.buildMimeMessage(ImageHtmlEmail.java:103)
> at org.apache.commons.mail.Email.send(Email.java:1427)
> at EmailSender.sendHTMLEmail(EmailSender.java:93)
> at
> EmailSenderTest.testImageHtmlEmailDollarBackslash(EmailSenderTest.java:801)
> {quote}
> the cause is that Matcher.appendReplacement() actually treats Dollar and
> Backslash as special characters. From the javadoc of Matcher:
> {quote}
> * Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in
> * the replacement string may cause the results to be different than if it
> * were being treated as a literal replacement string. Dollar signs may be
> * treated as references to captured subsequences as described above, and
> * backslashes are used to escape literal characters in the replacement
> * string.
> {quote}
> A possible fix is to replace line 180 of ImageHtmlEmail.java with the
> following, i.e. replace "$" by "\$" and "\" by "\\" to have them properly
> escaped.
> {code}
> matcher.appendReplacement(stringBuffer, (matcher.group(1) + "cid:" + cid +
> matcher.group(3)).replace("\\", "\\\\").replace("$", "\\$"));
> {code}
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira