}. + * + * Accordingly, we've built a policy file that allows fairly similar + * functionality. All text-formatting tags that operate directly on the font, + * color or emphasis have been allowed. + */ +public class CustomSafePolicy implements SanitizerCustomPolicy { + + /** + * A policy that can be used to produce policies that sanitize to HTML sinks via + * {@link PolicyFactory#apply}. + */ + public static final PolicyFactory POLICY_DEFINITION = new HtmlPolicyBuilder() + .allowStandardUrlProtocols() + // Allow title="..." on any element. + .allowAttributes("title").globally() + // Allow href="..." on elements. + .allowAttributes("href").onElements("a") + // Defeat link spammers. + .requireRelNofollowOnLinks() + // Allow lang= with an alphabetic value on any element. + .allowAttributes("lang").matching(Pattern.compile("[a-zA-Z]{2,20}")) + .globally() + // The align attribute onelements can have any value below. + .allowAttributes("align") + .matching(true, "center", "left", "right", "justify", "char") + .onElements("p") + // These elements are allowed. + .allowElements("a", "p", "div", "i", "b", "em", "blockquote", "tt", "strong","br", "ul", "ol", "li") + .toFactory(); + + @Override + public PolicyFactory getSanitizerPolicy() { + return POLICY_DEFINITION; + } +} diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilCodec.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilCodec.java index a2272e059d..8ad74ba1ae 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilCodec.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilCodec.java @@ -29,6 +29,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Set; @@ -116,8 +117,8 @@ public class UtilCodec { * This method will start a configurable sanitizing process. The sanitizer can * be turns off through "sanitizer.enable=false", the default value is true. It * is possible to configure a custom policy using the properties - * "sanitizer.permissive.policy" and "sanitizer.custom.policy.class". The custom - * policy has to implement + * "sanitizer.permissive.policy" and "sanitizer.custom.permissive.policy.class". + * The custom policy has to implement * {@link org.apache.ofbiz.base.html.SanitizerCustomPolicy}. * * @param original @@ -143,7 +144,7 @@ public class UtilCodec { PolicyFactory policy = null; try { Class> customPolicyClass = Class.forName(UtilProperties.getPropertyValue("owasp", - "sanitizer.custom.policy.class")); + "sanitizer.custom.permissive.policy.class")); Object obj = customPolicyClass.newInstance(); if (SanitizerCustomPolicy.class.isAssignableFrom(customPolicyClass)) { Method meth = customPolicyClass.getMethod("getSanitizerPolicy"); @@ -153,7 +154,7 @@ public class UtilCodec { | InvocationTargetException | NoSuchMethodException | SecurityException | InstantiationException e) { // Just logging the error and falling back to default policy - Debug.logError(e, "Could not find custom sanitizer policy. Using default instead", module); + Debug.logError(e, "Could not find custom permissive sanitizer policy. Using default instead", module); } if (policy != null) { @@ -355,43 +356,67 @@ public class UtilCodec { /** * Uses a black-list approach for necessary characters for HTML. - *
* Does not allow various characters (after canonicalization), including - * "<", ">", "&" (if not followed by a space), and "%" (if not - * followed by a space). + * "<", ">", "&" and "%" (if not followed by a space). * - * @param value - * @param errorMessageList + * @param valueName field name checked + * @param value value checked + * @param errorMessageList an empty list passed by and modified in case of issues + * @param locale */ - public static String checkStringForHtmlStrictNone(String valueName, String value, List
errorMessageList) { + public static String checkStringForHtmlStrictNone(String valueName, String value, List errorMessageList, + Locale locale) { if (UtilValidate.isEmpty(value)) { return value; } + // canonicalize, strict (error on double-encoding) try { value = canonicalize(value, true); } catch (IntrusionException e) { // NOTE: using different log and user targeted error messages to allow the end-user message to be less technical - Debug.logError("Canonicalization (format consistency, character escaping that is mixed or double, etc) error for attribute named [" + valueName + "], String [" + value + "]: " + e.toString(), module); - errorMessageList.add("In field [" + valueName + "] found character escaping (mixed or double) that is not allowed or other format consistency error: " + e.toString()); + Debug.logError("Canonicalization (format consistency, character escaping that is mixed or double, etc) " + + "error for attribute named [" + valueName + "], String [" + value + "]: " + e.toString(), module); + String issueMsg = null; + if (locale.equals(new Locale("test"))) { // labels are not available in testClasses Gradle task + issueMsg = "In field [" + valueName + "] found character escaping (mixed or double) " + + "that is not allowed or other format consistency error: "; + } else { + issueMsg = UtilProperties.getMessage("SecurityUiLabels","PolicyNoneMixedOrDouble", + UtilMisc.toMap("valueName", valueName), locale); + } + errorMessageList.add(issueMsg + e.toString()); } // check for "<", ">" if (value.indexOf("<") >= 0 || value.indexOf(">") >= 0) { - errorMessageList.add("In field [" + valueName + "] less-than (<) and greater-than (>) symbols are not allowed."); + String issueMsg = null; + if (locale.equals(new Locale("test"))) { + issueMsg = "In field [" + valueName + "] less-than (<) and greater-than (>) symbols are not allowed."; + } else { + issueMsg = UtilProperties.getMessage("SecurityUiLabels","PolicyNoneLess-thanGreater-than", + UtilMisc.toMap("valueName", valueName), locale); + } + errorMessageList.add(issueMsg); } // check for js events - final String onEvent = "on" + StringUtils.substringBetween(value, " on", "="); - final boolean seekSegmentTime = value.contains("seekSegmentTime"); - if (null != onEvent || seekSegmentTime) { - if (jsEventList.stream().anyMatch(str -> StringUtils.containsIgnoreCase(str, onEvent)) || seekSegmentTime) { - errorMessageList.add("In field [" + valueName + "] js events are not allowed."); + String onEvent = "on" + StringUtils.substringBetween(value, " on", "="); + if (jsEventList.stream().anyMatch(str -> StringUtils.containsIgnoreCase(str, onEvent)) + || value.contains("seekSegmentTime")) { + String issueMsg = null; + if (locale.equals(new Locale("test"))) { + issueMsg = "In field [" + valueName + "] Javascript events are not allowed."; + } else { + issueMsg = UtilProperties.getMessage("SecurityUiLabels","PolicyNoneJsEvents", + UtilMisc.toMap("valueName", valueName), locale); } + errorMessageList.add(issueMsg); } // TODO: anything else to check for that can be used to get HTML or JavaScript going without these characters? + // // Another would be https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet#US-ASCII_encoding // But all our Tomcat connectors use UTF-8 // We don't care about Flash now rather deprecated @@ -401,7 +426,59 @@ public class UtilCodec { } /** - * A simple Map wrapper class that will do HTML encoding. To be used for passing a Map to something that will expand Strings with it as a context, etc. + * This method check if the input is safe HTML. + * It is possible to configure a safe policy using the properties + * "sanitizer.safe.policy" and "sanitizer.custom.safe.policy.class". + * The safe policy has to implement + * {@link org.apache.ofbiz.base.html.SanitizerCustomPolicy}. + * + * @param valueName field name checked + * @param value value checked + * @param errorMessageList an empty list passed by and modified in case of issues + * @param locale + */ + public static String checkStringForHtmlSafe(String valueName, String value, List errorMessageList, + Locale locale) { + PolicyFactory policy = null; + try { + Class> customPolicyClass = null; + if (locale.equals(new Locale("test"))) { + customPolicyClass = Class.forName("org.apache.ofbiz.base.html.CustomSafePolicy"); + } else { + customPolicyClass = Class.forName(UtilProperties.getPropertyValue("owasp", + "sanitizer.custom.safe.policy.class")); + } + Object obj = customPolicyClass.newInstance(); + if (SanitizerCustomPolicy.class.isAssignableFrom(customPolicyClass)) { + Method meth = customPolicyClass.getMethod("getSanitizerPolicy"); + policy = (PolicyFactory) meth.invoke(obj); + } + } catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException + | InvocationTargetException | NoSuchMethodException | SecurityException + | InstantiationException e) { + Debug.logError(e, "Could not find custom safe sanitizer policy. Using default instead." + + "Beware: the result is not rightly checked!", module); + } + + String filtered = policy.sanitize(value); + if (!value.equals(filtered)) { + String issueMsg = null; + if (locale.equals(new Locale("test"))) { + issueMsg = "In field [" + valueName + "] by our input policy, your input has not been accepted " + + "for security reason. Please check and modify accordingly, thanks."; + } else { + issueMsg = UtilProperties.getMessage("SecurityUiLabels","PolicySafe", + UtilMisc.toMap("valueName", valueName), locale); + } + errorMessageList.add(issueMsg); + } + + return value; + } + + /** + * A simple Map wrapper class that will do HTML encoding. + * To be used for passing a Map to something that will expand Strings with it as a context, etc. */ public static class HtmlEncodingMapWrapper implements Map { public static HtmlEncodingMapWrapper getHtmlEncodingMapWrapper(Map mapToWrap, SimpleEncoder encoder) { diff --git a/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilCodecTests.java b/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilCodecTests.java index d59a0fa0bd..4eea54f2b6 100644 --- a/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilCodecTests.java +++ b/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilCodecTests.java @@ -24,6 +24,7 @@ import static org.junit.Assert.assertNull; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Locale; import org.junit.Test; @@ -40,7 +41,7 @@ public class UtilCodecTests { public void checkStringForHtmlStrictNoneDetectsXSS() { String xssVector = "<script>alert(\"XSS vector\");</script>"; List errorList = new ArrayList<>(); - String canonicalizedXssVector = UtilCodec.checkStringForHtmlStrictNone("fieldName", xssVector, errorList); + String canonicalizedXssVector = UtilCodec.checkStringForHtmlStrictNone("fieldName", xssVector, errorList, new Locale("test")); assertEquals("", canonicalizedXssVector); assertEquals(1, errorList.size()); assertEquals("In field [fieldName] less-than (<) and greater-than (>) symbols are not allowed.", errorList.get(0)); @@ -70,6 +71,7 @@ public class UtilCodecTests { // jacopoc: temporarily commented because this test is failing after the upgrade of owasp-esapi (still investigating) //checkStringForHtmlStrictNone_test("double-ampersand", "f\";oo", "f%26quot%3boo"); checkStringForHtmlStrictNone_test("double-encoding", "%2%353Cscript", "%2%353Cscript", "In field [double-encoding] found character escaping (mixed or double) that is not allowed or other format consistency error: org.apache.ofbiz.base.util.UtilCodec$IntrusionException: Input validation failure"); + checkStringForHtmlStrictNone_test("js_event", "non_existent.foo\" onerror=\"alert('Hi!');", "non_existent.foo\" onerror=\"alert('Hi!');", "In field [js_event] Javascript events are not allowed."); } private static void encoderTest(String label, UtilCodec.SimpleEncoder encoder, String wanted, String toEncode) { @@ -78,8 +80,21 @@ public class UtilCodecTests { } private static void checkStringForHtmlStrictNone_test(String label, String fixed, String input, String... wantedMessages) { List gottenMessages = new ArrayList (); - assertEquals(label, fixed, UtilCodec.checkStringForHtmlStrictNone(label, input, gottenMessages)); + assertEquals(label, fixed, UtilCodec.checkStringForHtmlStrictNone(label, input, gottenMessages, new Locale("test"))); assertEquals(label, Arrays.asList(wantedMessages), gottenMessages); } + + @Test + public void testCheckStringForHtmlSafe() { + String xssVector = ""; + List errorList = new ArrayList<>(); + String canonicalizedXssVector = UtilCodec.checkStringForHtmlSafe("fieldName", xssVector, errorList, new Locale("test")); + assertEquals("", canonicalizedXssVector); + assertEquals(1, errorList.size()); + assertEquals("In field [fieldName] by our input policy, your input has not been accepted for security reason. " + + "Please check and modify accordingly, thanks.", errorList.get(0)); + } + + } diff --git a/framework/common/config/SecurityUiLabels.xml b/framework/common/config/SecurityUiLabels.xml index 1b3999a9f7..3a7a6d8da5 100644 --- a/framework/common/config/SecurityUiLabels.xml +++ b/framework/common/config/SecurityUiLabels.xml @@ -695,6 +695,22 @@ 受保护的视图 受保護的檢視 ++ +In field [${valueName}] found character escaping (mixed or double) that is not allowed or other format consistency error: +Le champ "${valueName}" contient un caractère d'échappement (mixte ou double) qui n'est pas autorisé ou une autre erreur de cohérence de format : ++ +In field [${valueName}] less-than (<) and greater-than (>) symbols are not allowed. +Dans le champ "${valueName}" les symboles inférieurs (<) et supérieurs (>) ne sont pas autorisés. ++ +In field [${valueName}] Javascript events are not allowed. +Dans le champ "${valueName}" les événements Javascript ne sont pas autorisés. ++ In field [${valueName}] by our input policy, your input has not been accepted for security reason. Please check and modify accordingly, thanks. +Dans le champ "${valueName}", conformément à notre politique de saisie, votre saisie n'a pas été acceptée pour des raisons de sécurité. Veuillez vérifier et modifier en conséquence, merci. +Click Here To Reset Password Cliquez ici pour créer un nouveau mot de passe diff --git a/framework/common/servicedef/services.xml b/framework/common/servicedef/services.xml index 083f81d5d5..22efa9ea55 100644 --- a/framework/common/servicedef/services.xml +++ b/framework/common/servicedef/services.xml @@ -63,7 +63,7 @@ under the License.Create a new note record - +