Skip to content

Commit 0fb09c4

Browse files
committed
Bugfix MD5KnownUserHash when safetynet is activated
1 parent e3ef2ae commit 0fb09c4

File tree

6 files changed

+37
-11
lines changed

6 files changed

+37
-11
lines changed

QueueIT.Security/src/queueit/security/CookieValidateResultRepository.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public IValidateResult getValidationResult(IQueue queue) {
7474
return null;
7575
}
7676

77-
String expectedHash = generateHash(queueId, originalUrl, placeInQueue.toString(), redirectType, timeStamp);
77+
String expectedHash = generateHash(queueId, originalUrl, placeInQueue, redirectType, timeStamp);
7878

7979
if (!expectedHash.equals(actualHash))
8080
return null;
@@ -107,7 +107,7 @@ public void setValidationResult(IQueue queue, IValidateResult validationResult)
107107
String redirectType = confirmedResult.getKnownUser().getRedirectType().toString();
108108
Long timeStamp = confirmedResult.getKnownUser().getTimeStamp().getTime() / 1000;
109109

110-
String hash = generateHash(queueId, originalUrl, placeInQueue.toString(), redirectType, timeStamp.toString());
110+
String hash = generateHash(queueId, originalUrl, placeInQueue, redirectType, timeStamp.toString());
111111

112112
setCookie(queue, queueId, originalUrl, placeInQueue, redirectType, timeStamp.toString(), hash);
113113
}
@@ -136,10 +136,10 @@ private void addCookie(Cookie cookie, HttpServletResponse response)
136136
response.addCookie(cookie);
137137
}
138138

139-
private String generateHash(String queueId, String originalUrl, String placeInQueue, String redirectType, String timeStamp) {
139+
private String generateHash(String queueId, String originalUrl, Integer placeInQueue, String redirectType, String timeStamp) {
140140
try {
141141
StringBuilder sb = new StringBuilder();
142-
sb.append(queueId).append(originalUrl).append(placeInQueue).append(redirectType).append(timeStamp).append(KnownUserFactory.getSecretKey());
142+
sb.append(queueId).append(originalUrl).append(placeInQueue != null ? placeInQueue.toString() : "0").append(redirectType).append(timeStamp).append(KnownUserFactory.getSecretKey());
143143

144144
MessageDigest digest = MessageDigest.getInstance("SHA-256");
145145
byte[] hash = digest.digest(sb.toString().getBytes("UTF-8"));

QueueIT.Security/src/queueit/security/Hashing.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ public static Integer decryptPlaceInQueue(String encryptedPlaceInQueue) {
2121
}
2222

2323
public static String encryptPlaceInQueue(Integer placeInQueue) {
24-
25-
char[] placeInQueueChars = String.format("%07d", placeInQueue).toCharArray();
24+
char[] placeInQueueChars = String.format("%07d", placeInQueue != null ? placeInQueue : 0).toCharArray();
2625

2726
char[] encryptedPlaceInQueue = UUID.randomUUID().toString().toCharArray();
2827
encryptedPlaceInQueue[9] = placeInQueueChars[6];

QueueIT.Security/src/queueit/security/KnownUserBase.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ public RedirectType getRedirectType() {
5454
}
5555

5656
void setPlaceInQueue(Integer value) {
57-
if (value <= 0 || value >= 9999999) {
57+
if (value == null)
58+
this.placeInQueue = null;
59+
else if (value <= 0 || value >= 9999999) {
5860
this.placeInQueue = null;
5961
} else {
6062
this.placeInQueue = value;

QueueIT.Security/src/queueit/security/Md5KnownUser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* last update: 2012-10-25
1111
*/
1212
class Md5KnownUser extends KnownUserBase {
13-
Md5KnownUser(UUID queueId, int placeInQueue, Date timeStamp, String customerId, String eventId, RedirectType redirectType, URI originalUrl) {
13+
Md5KnownUser(UUID queueId, Integer placeInQueue, Date timeStamp, String customerId, String eventId, RedirectType redirectType, URI originalUrl) {
1414
this.queueId = queueId;
1515
this.setPlaceInQueue(placeInQueue);
1616
this.timeStamp = timeStamp;

QueueIT.Security/test/queueit/security/HashingTest.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public void tearDown() {
3939
*/
4040
@Test
4141
public void testDecryptPlaceInQueue() {
42-
System.out.println("DecryptPlaceInQueue");
4342
String encryptedPlaceInQueue = "21206da6-3f0a-468d-9325-471d070bbbfd";
4443
Integer expResult = 3613;
4544
Integer result = Hashing.decryptPlaceInQueue(encryptedPlaceInQueue);
@@ -48,7 +47,6 @@ public void testDecryptPlaceInQueue() {
4847

4948
@Test (expected=InvalidKnownUserUrlException.class)
5049
public void testDecryptPlaceInQueue_null_arg() {
51-
System.out.println("DecryptPlaceInQueue");
5250
String encryptedPlaceInQueue = null;
5351
Integer expResult = 7;
5452
Integer result = Hashing.decryptPlaceInQueue(encryptedPlaceInQueue);
@@ -57,12 +55,24 @@ public void testDecryptPlaceInQueue_null_arg() {
5755

5856
@Test (expected=InvalidKnownUserUrlException.class)
5957
public void testDecryptPlaceInQueue_empty_arg() {
60-
System.out.println("DecryptPlaceInQueue");
6158
String encryptedPlaceInQueue = "";
6259
Integer expResult = 7;
6360
Integer result = Hashing.decryptPlaceInQueue(encryptedPlaceInQueue);
6461
assertEquals(expResult, result);
6562
}
6663

64+
@Test
65+
public void encryptPlaceInQueue_null_arg() {
66+
Integer placeInQueue = null;
67+
String result = Hashing.encryptPlaceInQueue(placeInQueue);
68+
char[] resultChars = result.toCharArray();
69+
assertEquals('0', resultChars[9]);
70+
assertEquals('0', resultChars[26]);
71+
assertEquals('0', resultChars[7]);
72+
assertEquals('0', resultChars[20]);
73+
assertEquals('0', resultChars[11]);
74+
assertEquals('0', resultChars[3]);
75+
assertEquals('0', resultChars[30]);
76+
}
6777

6878
}

QueueIT.Security/test/queueit/security/Md5KnownUserTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,21 @@ public void Md5KnownUser_Constructor_PlaceInQueueNotKnown_Test() {
4949
//Act
5050
Md5KnownUser knownUser = new Md5KnownUser(expectedQueueId, 9999999, expectedTimeStamp, null, null, RedirectType.Unknown, null);
5151

52+
//Assert
53+
assertEquals(expectedQueueId, knownUser.getQueueId());
54+
assertEquals(null, knownUser.getPlaceInQueue());
55+
assertEquals(expectedTimeStamp, knownUser.getTimeStamp());
56+
}
57+
58+
@Test
59+
public void Md5KnownUser_Constructor_PlaceInQueueIsNull_Test() {
60+
//Arrange
61+
UUID expectedQueueId = UUID.randomUUID();
62+
Date expectedTimeStamp = Calendar.getInstance().getTime();
63+
64+
//Act
65+
Md5KnownUser knownUser = new Md5KnownUser(expectedQueueId, null, expectedTimeStamp, null, null, RedirectType.Unknown, null);
66+
5267
//Assert
5368
assertEquals(expectedQueueId, knownUser.getQueueId());
5469
assertEquals(null, knownUser.getPlaceInQueue());

0 commit comments

Comments
 (0)