Repository navigation
security: verify the bank's DKIM signature before parsing - #58
Conversation
…as the bank writing
landsman
left a comment
There was a problem hiding this comment.
The order is right: the sender picks the bank, the signature is checked against its domain and pins, then the body is parsed. Refusing a foreign d= before the lookup changes no verdict, because verify already dropped those results on signer.equals(expected); it only removes the DNS query, and TEMPERROR still comes only from the bank's own key.
Outside the diff: the class javadoc of DkimVerifier (lines 35 to 41) still shows the order this PR removes, parse first and then dkim.verify(raw, t.signedBy()), and it calls BankNotifications.parse, which does not exist. Anyone using DkimVerifier on its own copies that order. The example should verify against the domain the caller expects from its own config, then parse. Same for @param domain on line 111 ("e.g. Transaction.signedBy()").
…the bank writing A From Jakarta Mail refuses (an unclosed quote) ended as Unparsed(NOT_READ) before any signature check. It names no bank, so it is NOT_A_BANK_EMAIL now, as are bytes that are no e-mail: NOT_READ and UNSUPPORTED_FORMAT only ever follow a signature that holds.
…a template to read again Over 64 KiB a message was Unparsed(NOT_READ) before its sender was looked at, so a forged one padded past the limit read as the bank still writing. Its signature is never checked, so it is Unverified, as DkimVerifier already says; a notification is about 5 KiB.
… parsing, not the parsed one
|
The class javadoc of |
landsman
left a comment
There was a problem hiding this comment.
Round 2. The three fixes hold: an unreadable From and bytes that are no e-mail end as NOT_A_BANK_EMAIL without a key lookup, and I found no path that gives NOT_READ or UNSUPPORTED_FORMAT before the signature, so the new promise in the Reason javadoc and in usage.md is true. Two things below: one older than this PR, one that came from my own suggestion last round.
…no bank is not called a bank's The check in readAll made every message over 64 KiB Unverified, a newsletter from any sender included, while Unverified says it looks like a bank's. DkimVerifier already fails it before jDKIM sees the bytes, and the sender is now looked at first: Unverified from a bank's sender, NOT_A_BANK_EMAIL from anyone else.
…hich DKIM never covered DKIM signs the last copy of a header and Jakarta Mail reads the first, so anyone holding one real notification could replay it under a new Message-ID, past deduplication, or retitle it to pick another template. A bank sends one of each header reading depends on, so a repeat is Unverified.
landsman
left a comment
There was a problem hiding this comment.
Round 3: both fixes hold, and the headers checked are all that reading depends on (MailBody reads only Content-Type and Content-Transfer-Encoding, nothing reads Date). Good to merge from my side; one cosmetic line below.
readAll parsed the e-mail first and verified DKIM only with the domain the parser returned. An unsigned message from a supported bank's sender domain with a body no parser reads ended as
Unparsed(NOT_READ), never reaching verification. An app keepsUnparsedmail to read again after an upgrade and takes it as a sign the bank still writes, so anyone who knows the receiving address could produce that signal with a forged mail. The new test fails on the old code with exactly that.Now the sender domain picks the bank, the signature is verified against that bank's domain and pinned key, and only then is the body parsed. Unsigned or badly signed is
Unverifiedwhatever the body says; signed and unreadable isUnparsedas before. A From the mail library cannot read, or bytes that are no e-mail, name no bank and areNOT_A_BANK_EMAIL; over 64 KiB the verifier fails a message before jDKIM sees it, so a bank's sender getsUnverifiedand anyone elseNOT_A_BANK_EMAIL. SoNOT_READandUNSUPPORTED_FORMATonly ever follow a signature that holds. Public API is unchanged.Two older gaps, closed too. jDKIM looked up the key of every signature in the message, so the sender chose which names got a DNS query; only the bank's own domain is looked up now (
TemporaryFailurecould already come only from its key). And DKIM signs the last copy of a header while Jakarta Mail reads the first, so aMessage-IDadded above a signed one let a real notification be replayed past deduplication; a secondMessage-ID,Subject,Content-TypeorContent-Transfer-EncodingisUnverified.DkimVerifier's javadoc and the diagram in verification.md show the new order.Tests: unsigned bank sender with an unknown body, a repeated
Message-IDorSubjectand a bank's message over the limit areUnverified; an unreadable From and a large message from anyone else areNOT_A_BANK_EMAILwith no key lookup; a signature of a foreign domain causes no lookup. Each fails on the code before it../gradlew spotlessCheck :modules:bank-notifications:testpasses (14 inBankNotificationsTest).