Skip to content

Commit 01de39e

Browse files
committed
Prevent hash collisions by using length-prefixed encoding
1 parent 4df5c46 commit 01de39e

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

server/src/main/java/org/cloudfoundry/identity/uaa/login/LoginConsentHashUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public static String calculateConsentHash(LoginConsent consent) {
3737
String title = StringUtils.hasText(consent.getTitle()) ? consent.getTitle() : "";
3838
String text = StringUtils.hasText(consent.getText()) ? consent.getText() : "";
3939

40-
String content = title + "|" + text;
40+
// Format: <title_length>:<title>|<text_length>:<text>
41+
String content = title.length() + ":" + title + "|" + text.length() + ":" + text;
4142

4243
return consentHashCache.computeIfAbsent(content, c -> {
4344
try {

server/src/test/java/org/cloudfoundry/identity/uaa/login/LoginConsentHashUtilTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,19 @@ void testParseDurationToSeconds_CaseInsensitive() {
140140
assertThat(LoginConsentHashUtil.parseDurationToSeconds("1W")).isEqualTo(7 * 24 * 60 * 60);
141141
assertThat(LoginConsentHashUtil.parseDurationToSeconds("1Y")).isEqualTo(365 * 24 * 60 * 60);
142142
}
143+
144+
@Test
145+
void testHashCollision() {
146+
// Case 1: title contains pipe, short text
147+
LoginConsent consent1 = new LoginConsent(true, "abc|def", "ghi", "Accept", "Decline", null, "12h");
148+
149+
// Case 2: short title, text contains pipe
150+
LoginConsent consent2 = new LoginConsent(true, "abc", "def|ghi", "Accept", "Decline", null, "12h");
151+
152+
String hash1 = LoginConsentHashUtil.calculateConsentHash(consent1);
153+
String hash2 = LoginConsentHashUtil.calculateConsentHash(consent2);
154+
155+
assertThat(hash1).as("Different title/text combinations should produce different hashes")
156+
.isNotEqualTo(hash2);
157+
}
143158
}

0 commit comments

Comments
 (0)