Skip to content

Commit 3a222a6

Browse files
zubriclaude
andcommitted
fix(PW-3126): ignore ou components with invalid length when parsing branch from DN
parseBranch now skips ou values that are not exactly 3 characters, gracefully handling malformed DNs without throwing or returning incorrect branch codes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 98b7e6c commit 3a222a6

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/main/java/com/prowidesoftware/swift/model/DistinguishedName.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,14 @@ public static String parseBIC(final String dn) {
9797
/**
9898
* Parses the branch code from a SWIFT Distinguished Name string.
9999
*
100-
* <p>The branch is represented by the {@code ou} component. When multiple {@code ou} components
101-
* are present, the one closest to {@code o=&lt;bic8&gt;} (i.e. the rightmost) is the branch code,
100+
* <p>The branch is represented by the {@code ou} component. Only {@code ou} values with exactly
101+
* 3 characters are considered valid; others are silently ignored. When multiple valid {@code ou}
102+
* components are present, the one closest to {@code o=&lt;bic8&gt;} (i.e. the rightmost) is used,
102103
* as DNs are read right-to-left from least to most specific:
103104
* <pre>cn=a,ou=dept,ou=bbb,o=biccode,o=swift → branch is "BBB"</pre>
104105
*
105106
* @param dn the Distinguished Name string, may be null or blank
106-
* @return the branch code in uppercase, or null if not present or if the input is blank
107+
* @return the branch code in uppercase, or null if not present, invalid, or if the input is blank
107108
*/
108109
protected static String parseBranch(final String dn) {
109110
if (StringUtils.isBlank(dn)) {
@@ -112,11 +113,14 @@ protected static String parseBranch(final String dn) {
112113
String branch = null;
113114
for (String s : StringUtils.split(dn, ",")) {
114115
if (Strings.CS.startsWith(s, "ou=")) {
115-
// keep iterating — last match is the rightmost (closest to o=<bic8>)
116-
branch = StringUtils.substringAfter(s, "ou=");
116+
String value = StringUtils.substringAfter(s, "ou=");
117+
if (value.length() == 3) {
118+
// keep iterating — last valid match is the rightmost (closest to o=<bic8>)
119+
branch = value;
120+
}
117121
}
118122
}
119-
return StringUtils.isBlank(branch) ? null : StringUtils.upperCase(branch);
123+
return branch != null ? StringUtils.upperCase(branch) : null;
120124
}
121125

122126
/**

src/test/java/com/prowidesoftware/swift/model/DistinguishedNameTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,16 @@ void testParseBranchBlank() {
9999
void testParseBranchNoMatch() {
100100
assertNull(DistinguishedName.parseBranch("o=biccode,o=swift"));
101101
}
102+
103+
@Test
104+
void testParseBranchIgnoresInvalidOULength() {
105+
// ou values with length != 3 must be ignored
106+
assertNull(DistinguishedName.parseBranch("ou=toolong,o=biccode,o=swift"));
107+
assertNull(DistinguishedName.parseBranch("ou=ab,o=biccode,o=swift"));
108+
assertNull(DistinguishedName.parseBranch("ou=x,o=biccode,o=swift"));
109+
// valid ou present alongside invalid ones — valid one wins
110+
assertEquals("XXX", DistinguishedName.parseBranch("ou=toolong,ou=xxx,o=biccode,o=swift"));
111+
// multiple invalid ou, no valid one
112+
assertNull(DistinguishedName.parseBranch("ou=ab,ou=toolong,o=biccode,o=swift"));
113+
}
102114
}

0 commit comments

Comments
 (0)