diff --git a/core/src/main/java/hudson/util/FormFieldValidator.java b/core/src/main/java/hudson/util/FormFieldValidator.java index 186bcd3a10fa4a1258ecad40037b2569499cbf7a..7d6a37eb0620ac153e2bdc5ed1417f2ea0c439a4 100644 --- a/core/src/main/java/hudson/util/FormFieldValidator.java +++ b/core/src/main/java/hudson/util/FormFieldValidator.java @@ -582,6 +582,8 @@ public abstract class FormFieldValidator { * Verifies that the 'value' parameter is correct base64 encoded text. * * @since 1.257 + * @deprecated as of 1.305 + * Use {@link FormValidation#validateBase64(String, boolean, boolean, String)} instead. */ public static class Base64 extends FormFieldValidator { private final boolean allowWhitespace; diff --git a/core/src/main/java/hudson/util/FormValidation.java b/core/src/main/java/hudson/util/FormValidation.java index cb176973aefb70d32aca11931d11c110e1a97350..96f1f75d7e9f2c6966f9d4f5d414a309087f5785 100644 --- a/core/src/main/java/hudson/util/FormValidation.java +++ b/core/src/main/java/hudson/util/FormValidation.java @@ -306,6 +306,35 @@ public abstract class FormValidation extends IOException implements HttpResponse } } + /** + * Makes sure that the given string is a base64 encoded text. + * + * @param allowWhitespace + * if you allow whitespace (CR,LF,etc) in base64 encoding + * @param allowEmpty + * Is empty string allowed? + * @param errorMessage + * Error message. + * @since 1.305 + */ + public static FormValidation validateBase64(String value, boolean allowWhitespace, boolean allowEmpty, String errorMessage) { + try { + String v = value; + if(!allowWhitespace) { + if(v.indexOf(' ')>=0 || v.indexOf('\n')>=0) + return error(errorMessage); + } + v=v.trim(); + if(!allowEmpty && v.length()==0) + return error(errorMessage); + + com.trilead.ssh2.crypto.Base64.decode(v.toCharArray()); + return ok(); + } catch (IOException e) { + return error(errorMessage); + } + } + /** * Convenient base class for checking the validity of URLs. *