Skip to content

(CU-86b56qtwv) Fix NPE when create invalid BIC #245

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

Merged
merged 8 commits into from
Jun 6, 2025
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Prowide Core - CHANGELOG

#### 10.2.8 - SNAPSHOT
* Fix: Enhanced the `DefaultMtMetadataStrategy` to prevent NPE when the message headers are malformed

#### 10.2.7 - May 2025
* (PW-2055) Fixed the default message metadata extraction for ACK/NAK to set the service message block 1 BIC as receiver, not as sender
* (PW-2055) Enhanced the `SwiftMessageUtils` extractors to support the service 21 message type (ACK/NAK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
*/
public class DefaultMtMetadataStrategy implements MessageMetadataStrategy {

private static final java.util.logging.Logger log =
java.util.logging.Logger.getLogger(DefaultMtMetadataStrategy.class.getName());

/**
* Extracts the MT main reference using {@link SwiftMessageUtils#reference(SwiftMessage)}
*/
Expand Down Expand Up @@ -70,10 +73,7 @@ public Optional<Calendar> tradeDate(AbstractMessage message) {
@Override
public Optional<String> sender(AbstractMessage message) {
final String sender = SwiftMessageUtils.sender(asSwiftMessage(message));
if (sender != null) {
return Optional.of(new BIC(sender).getBic11());
}
return Optional.empty();
return getBIC(sender);
}

/**
Expand All @@ -83,8 +83,22 @@ public Optional<String> sender(AbstractMessage message) {
@Override
public Optional<String> receiver(AbstractMessage message) {
final String receiver = SwiftMessageUtils.receiver(asSwiftMessage(message));
if (receiver != null) {
return Optional.of(new BIC(receiver).getBic11());
return getBIC(receiver);
}

/**
*
* @param inputBIC the BIC to validate and convert to BIC11 format
* @return the BIC11 format of the input BIC, if valid, or an empty Optional if the input is null or invalid.
*/
private static Optional<String> getBIC(String inputBIC) {
if (inputBIC != null) {
BIC bic = new BIC(inputBIC);
if (bic.getBic11() != null) {
return Optional.of(bic.getBic11());
} else {
log.fine("Invalid BIC: " + inputBIC);
}
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.jupiter.api.Assertions.*;

import com.prowidesoftware.swift.model.Money;
import com.prowidesoftware.swift.model.MtSwiftMessage;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Calendar;
Expand Down Expand Up @@ -119,4 +120,67 @@ public void testParseAckWithOriginalMessageCopy() throws IOException {

assertEquals("ACK", strategy.identifier(mt).orElse(null));
}

@Test
public void testInvalidSenderInputMessage() {
MtSwiftMessage mt = MtSwiftMessage.parse(
"{1:F01AAAAAAAAAAAA1234567890}{2:I199CHASUS33XXXXN}{4:\n" + ":20:REF987654FFF\n" + "-}");

assertNotNull(mt.getReceiver());
assertEquals("CHASUS33XXX", mt.getReceiver());

assertNotNull(mt.getSender());
assertEquals("AAAAAAAAAAA", mt.getSender());
}

@Test
public void testInvalidReceiverInputMessage() {
MtSwiftMessage mt = MtSwiftMessage.parse(
"{1:F01CHASUS33AXXX1234567890}{2:I199AAAAAAAAAAAAN}{4:\n" + ":20:REF987654FFF\n" + "-}");

assertNotNull(mt.getSender());
assertEquals("CHASUS33XXX", mt.getSender());

assertNotNull(mt.getReceiver());
assertEquals("AAAAAAAAAAA", mt.getReceiver());
}

@Test
public void testInvalidSenderOutputMessage() {
MtSwiftMessage mt = MtSwiftMessage.parse(
"{1:F01CHASUS33XXXX1234567890}{2:O1990811060227AAAAAAAAAAAA55529746000602270811N}{4:\n"
+ ":20:REF987654FFF\n" + "-}");

assertNotNull(mt.getSender());
assertEquals("AAAAAAAAAAA", mt.getSender());

assertNotNull(mt.getReceiver());
assertEquals("CHASUS33XXX", mt.getReceiver());
}

@Test
public void testInvalidReceiverOutputMessage() {
MtSwiftMessage mt = MtSwiftMessage.parse(
"{1:F01CHASUS33AXXX1234567890}{2:O1990811060227AAAAAAAAAAAA55529746000602270811N}{4:\n"
+ ":20:REF987654FFF\n" + "-}");

assertNotNull(mt.getReceiver());
assertEquals("CHASUS33XXX", mt.getReceiver());

assertNotNull(mt.getSender());
assertEquals("AAAAAAAAAAA", mt.getSender());
}

@Test
public void testMalformedBlock2() {
// block 2 has the structure of an Input block, but the block direction is set to "O" for Output
MtSwiftMessage mt = MtSwiftMessage.parse(
"{1:F01CHASUS33AXXX1234567890}{2:O199AAAAAAAAAAAAN}{4:\n" + ":20:REF987654FFF\n" + "-}");

assertNotNull(mt.getReceiver());
assertEquals("CHASUS33XXX", mt.getReceiver());

// sender BIC cannot be parsed from the malformed block 2
assertNull(mt.getSender());
}
}
Loading