Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug on Email.signWithDomainKey(File, String, String) #52

Closed
WinstonHKLee opened this issue Aug 24, 2016 · 2 comments
Closed

bug on Email.signWithDomainKey(File, String, String) #52

WinstonHKLee opened this issue Aug 24, 2016 · 2 comments

Comments

@WinstonHKLee
Copy link

WinstonHKLee commented Aug 24, 2016

Email.signWithDomainKey(File, String, String) new FileInputStream(File) and finally close(). ( too good coding habbit! )
This make the class member Email.dkimPrivateKeyInputStream unusable.
Later MimeMessageHelper.signMessageWithDKIM(MimeMessage, Email) new DkimSigner, the closed FileInputStream throws DataFetchException:

org.simplejavamail.mailer.internal.mailsender.MailSenderException: Error signing MimeMessage with DKIM
        at org.simplejavamail.mailer.internal.mailsender.MimeMessageHelper.signMessageWithDKIM(MimeMessageHelper.java:269) ~[simple-java-mail-4.1.1.jar:na]
        at org.simplejavamail.mailer.internal.mailsender.MimeMessageHelper.produceMimeMessage(MimeMessageHelper.java:82) ~[simple-java-mail-4.1.1.jar:na]
        at org.simplejavamail.mailer.internal.mailsender.MailSender.sendMailClosure(MailSender.java:219) ~[bin/:na]
        at org.simplejavamail.mailer.internal.mailsender.MailSender.send(MailSender.java:200) ~[bin/:na]
        at org.simplejavamail.mailer.Mailer.sendMail(Mailer.java:400) ~[bin/:na]
        at org.simplejavamail.mailer.Mailer.sendMail(Mailer.java:391) ~[bin/:na]
        at [...]
Caused by: net.markenwerk.utils.data.fetcher.DataFetchException: Fetch failed after 0 bytes have been copied successully.
        at net.markenwerk.utils.data.fetcher.AbstractBufferedDataFetcher.createException(AbstractBufferedDataFetcher.java:98) ~[bin/:na]
        at net.markenwerk.utils.data.fetcher.AbstractBufferedDataFetcher.doCopy(AbstractBufferedDataFetcher.java:90) ~[bin/:na]
        at net.markenwerk.utils.data.fetcher.AbstractDataFetcher.doCopy(AbstractDataFetcher.java:112) ~[bin/:na]
        at net.markenwerk.utils.data.fetcher.AbstractDataFetcher.copy(AbstractDataFetcher.java:106) ~[bin/:na]
        at net.markenwerk.utils.data.fetcher.AbstractDataFetcher.fetch(AbstractDataFetcher.java:73) ~[bin/:na]
        at net.markenwerk.utils.data.fetcher.AbstractDataFetcher.fetch(AbstractDataFetcher.java:61) ~[bin/:na]
        at net.markenwerk.utils.mail.dkim.DkimSigner.<init>(DkimSigner.java:220) ~[bin/:na]
        at org.simplejavamail.mailer.internal.mailsender.MimeMessageHelper.signMessageWithDKIM(MimeMessageHelper.java:260) ~[simple-java-mail-4.1.1.jar:na]
        ... 8 common frames omitted
Caused by: java.io.IOException: Stream Closed
        at java.io.FileInputStream.readBytes(Native Method) ~[na:1.8.0_91]
        at java.io.FileInputStream.read(FileInputStream.java:233) ~[na:1.8.0_91]
        at net.markenwerk.utils.data.fetcher.AbstractBufferedDataFetcher.doCopy(AbstractBufferedDataFetcher.java:79) ~[bin/:na]
        ... 14 common frames omitted

To fix, and taken I/O expense into account, I suggest to cache the byte[] into static Map<File,byte[]> inside Email.signWithDomainKey(File, String, String): (any security consideration on storing private key?)

class Email {
   static Map<File,byte[]> cachedDomainKeys = new HashMap<>();

public void signWithDomainKey(final File dkimPrivateKeyFile, final String signingDomain, final String selector) {
    [...]
    byte[] bytes = org.apache.commons.io.FileUtils.readFileToByteArray(dkimPrivateKeyFile);
    cachedDomainKeys.put(dkimPrivateKeyFile, bytes);
    dkimPrivateKeyInputStream = new ByteArrayInputStream(bytes);
    [...]

Thanks.

@bbottema
Copy link
Owner

bbottema commented Aug 24, 2016

Haha, yeah that's not going to work, thanks for reporting. I'll think of something this weekend probably. Meanwhile you can use the overloaded version with FileInputStream instead.

@bbottema bbottema added the bug label Aug 24, 2016
@bbottema bbottema added this to the 4.1.2 milestone Oct 12, 2016
@bbottema
Copy link
Owner

bbottema commented Nov 7, 2016

Released in v4.1.2

@bbottema bbottema closed this as completed Nov 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants