Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ public final class BankNotifications {
* read from public samples pins none, so any key DNSSEC proves its domain published is accepted.
*/
public static BankNotifications withDns() {
DkimVerifier dkim = DkimVerifier.withDns();
return pinning(DkimVerifier.withDns());
}

/** {@link #withDns()}'s pins over any verifier, so a test can sign with its own key and still meet them. */
static BankNotifications pinning(DkimVerifier dkim) {
for (Map.Entry<String, Set<String>> bank : pinnedKeys().entrySet()) {
dkim = dkim.pinnedKeys(bank.getKey(), bank.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cz.insuit.bank.notification;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -38,15 +37,26 @@ void readsASignedNotification() throws Exception {
}

@Test
void pinsOnlyTheBanksReadFromRealEmails() {
// An empty set would make the verifier refuse every key of that bank; leaving it out trusts DNSSEC instead.
assertTrue(BankNotifications.pinnedKeys().containsKey("airbank.cz"));
assertFalse(BankNotifications.pinnedKeys().containsKey("moneta.cz"));
void readsABankThatPinsNoKeyWithAnyKeyItsDomainPublishes() throws Exception {
// MONETA is read from public samples and pins nothing, so withDns() takes whatever key DNSSEC proves it
// published.
Transaction transaction =
pinned("moneta.cz").read(signed(fixture("/moneta/incoming-synthetic.eml"), "moneta.cz"));

assertEquals("moneta.cz", transaction.signedBy());
}

@Test
void refusesAKeyThatABankWhichPinsDoesNotName() throws Exception {
Unverified refused =
assertThrows(Unverified.class, () -> pinned("airbank.cz").read(signed("incoming.eml")));

assertTrue(String.valueOf(refused.getMessage()).contains("not one of the keys pinned"), refused.getMessage());
}

@Test
void doesNotTrustAnUnsignedOne() throws Exception {
assertThrows(Unverified.class, () -> notifications().read(fixture("incoming.eml")));
assertThrows(Unverified.class, () -> notifications().read(fixture("/airbank/incoming.eml")));
}

@Test
Expand Down Expand Up @@ -171,7 +181,7 @@ void neverLooksUpAKeyForADomainTheMessageChose() throws Exception {
throw new IOException("resolver timed out");
});

byte[] foreign = signed(fixture("incoming.eml"), "attacker.example");
byte[] foreign = signed(fixture("/airbank/incoming.eml"), "attacker.example");

assertThrows(Unverified.class, () -> lookingUp.read(foreign));
assertEquals(List.of(), looked);
Expand Down Expand Up @@ -204,12 +214,20 @@ void keepsTheMessageIdOnlyWhenTheSignatureCoversIt() throws Exception {
.messageId());
}

/** As {@link BankNotifications#withDns()}, pins included, but with the test's key published for {@code domain}. */
private static BankNotifications pinned(String domain) {
String record = "v=DKIM1; k=rsa; p="
+ Base64.getEncoder().encodeToString(key.getPublic().getEncoded());
return BankNotifications.pinning(DkimVerifier.with(
(selector, name) -> selector.equals("ab") && name.equals(domain) ? List.of(record) : List.of()));
}

private static byte[] signed(String name) throws Exception {
return signed(name, "Date:To:From:Subject");
}

private static byte[] signed(String name, String headers) throws Exception {
return signed(fixture(name), "airbank.cz", headers);
return signed(fixture("/airbank/" + name), "airbank.cz", headers);
}

private static byte[] signed(byte[] message, String domain) throws Exception {
Expand All @@ -235,7 +253,7 @@ private static byte[] signed(byte[] message, String domain, String headers) thro
}

private static byte[] fixture(String name) throws IOException {
try (InputStream in = BankNotificationsTest.class.getResourceAsStream("/airbank/" + name)) {
try (InputStream in = BankNotificationsTest.class.getResourceAsStream(name)) {
return Objects.requireNonNull(in, name).readAllBytes();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ void refusesABogusAnswer() {
assertThrows(PermFailException.class, () -> dns.getRecords("dns/txt", "x", "dnssec-failed.org"));
}

@Test
void stillRefusesCsobSlovakia() {
// ČSOB SK pins no key, so DNSSEC alone would decide; csob.sk is not signed, so its e-mails stay Unverified.
assertThrows(PermFailException.class, () -> dns.getRecords("dns/txt", "x", "csob.sk"));
}

@Test
void refusesAnUnsignedZone() {
// google.com is not DNSSEC-signed; its DKIM key would come back, unproven.
Expand Down