Selaa lähdekoodia

fix issue with token keys too long for database key column

Jason Rivard 7 vuotta sitten
vanhempi
commit
12e9691cf3

+ 1 - 0
server/src/main/java/password/pwm/AppProperty.java

@@ -292,6 +292,7 @@ public enum AppProperty {
     TOKEN_RESEND_DELAY_MS                           ("token.resend.delayMS"),
     TOKEN_REMOVE_ON_CLAIM                           ("token.removeOnClaim"),
     TOKEN_VERIFY_PW_MODIFY_TIME                     ("token.verifyPwModifyTime"),
+    TOKEN_STORAGE_MAX_KEY_LENGTH                    ("token.storage.maxKeyLength"),
     TELEMETRY_SENDER_IMPLEMENTATION                 ("telemetry.senderImplementation"),
     TELEMETRY_SENDER_SETTINGS                       ("telemetry.senderSettings"),
     TELEMETRY_SEND_FREQUENCY_SECONDS                ("telemetry.sendFrequencySeconds"),

+ 5 - 1
server/src/main/java/password/pwm/svc/token/StoredTokenKey.java

@@ -22,8 +22,10 @@
 
 package password.pwm.svc.token;
 
+import password.pwm.AppProperty;
 import password.pwm.PwmApplication;
 import password.pwm.error.PwmUnrecoverableException;
+import password.pwm.util.java.StringUtil;
 import password.pwm.util.secure.SecureService;
 
 class StoredTokenKey implements TokenKey {
@@ -62,8 +64,10 @@ class StoredTokenKey implements TokenKey {
             throw new IllegalArgumentException("new key value has stored suffix");
         }
 
+        final int maxHashLength = Integer.parseInt(pwmApplication.getConfig().readAppProperty(AppProperty.TOKEN_STORAGE_MAX_KEY_LENGTH));
         final SecureService secureService = pwmApplication.getSecureService();
-        final String storedHash = secureService.hash(input) + SUFFIX;
+        final String generatedHash = secureService.hash(input);
+        final String storedHash = StringUtil.truncate(generatedHash, maxHashLength) + SUFFIX;
 
         return new StoredTokenKey(storedHash);
     }

+ 10 - 0
server/src/main/java/password/pwm/util/java/StringUtil.java

@@ -390,4 +390,14 @@ public abstract class StringUtil {
     public static boolean equals(final String input1, final String input2) {
         return StringUtils.equals(input1, input2);
     }
+
+    public static String truncate(final String input, final int length) {
+        if (input == null) {
+            return "";
+        }
+
+        return input.length() > length
+                ? input.substring(0, length)
+                : input;
+    }
 }

+ 1 - 0
server/src/main/resources/password/pwm/AppProperty.properties

@@ -278,6 +278,7 @@ token.maxUniqueCreateAttempts=100
 token.resend.delayMS=3000
 token.verifyPwModifyTime=true
 token.removeOnClaim=true
+token.storage.maxKeyLength=100
 urlshortener.url.regex=(https?://([^:@]+(:[^@]+)?@)?([a-zA-Z0-9.]+|d{1,3}.d{1,3}.d{1,3}.d{1,3}|[[0-9a-fA-F:]+])(:d{1,5})?/*[a-zA-Z0-9/\%_.]*?*[a-zA-Z0-9/\%_.=&#]*)
 wordlist.builtin.path=/WEB-INF/wordlist.zip
 wordlist.maxCharLength=64