snoopdave commented on code in PR #317:
URL: https://github.com/apache/roller/pull/317#discussion_r4636769053


##########
app/src/main/java/org/apache/roller/weblogger/ui/rendering/velocity/StringGrowth.java:
##########
@@ -0,0 +1,207 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.roller.weblogger.ui.rendering.velocity;
+
+import java.util.Set;
+
+import org.apache.roller.weblogger.ui.rendering.model.UtilitiesModel;
+
+/**
+ * Some string methods build a result much longer than their receiver and
+ * arguments, so checking the result's length after the call is too late. This
+ * works out, before a call from a template runs, the longest string it can
+ * build, and names the methods whose result can't be bounded that way.
+ *
+ * <p>Methods are matched by name so that methods added in later Java versions
+ * (String.indent, String.formatted, StringBuilder.repeat) are covered when
+ * Roller runs on them.
+ */
+final class StringGrowth {
+
+    /** String methods that are not offered to templates. */
+    private static final Set<String> REFUSED_STRING_METHODS = Set.of(
+            "repeat", "format", "formatted", "join", "indent");
+
+    /** String methods whose result is never longer than the receiver or an 
argument. */
+    private static final Set<String> NON_GROWING_STRING_METHODS = Set.of(
+            "substring", "subSequence", "trim", "strip", "stripLeading",
+            "stripTrailing", "stripIndent", "translateEscapes", "intern",
+            "toString", "valueOf", "copyValueOf");
+
+    /** StringBuilder and StringBuffer methods that are not offered to 
templates. */
+    private static final Set<String> REFUSED_BUILDER_METHODS = 
Set.of("repeat");
+
+    /** Characters a number, boolean or other value appended to a builder may 
take. */
+    private static final long SCALAR_LENGTH = 32;
+
+    /** Most characters toUpperCase or toLowerCase makes of one character. */
+    private static final long CASE_GROWTH = 3;
+
+    private StringGrowth() {
+    }
+
+    /**
+     * @return true if the method must not be offered on this receiver: it can
+     *         build a string whose length can't be worked out from the call
+     */
+    static boolean isRefused(Object receiver, String methodName, Class<?> 
returnType) {
+        if (receiver instanceof String) {
+            if (REFUSED_STRING_METHODS.contains(methodName)) {
+                return true;
+            }
+            // a method this class doesn't know about that returns a string
+            return returnType != null && 
CharSequence.class.isAssignableFrom(returnType)
+                    && !NON_GROWING_STRING_METHODS.contains(methodName)
+                    && !isBoundedStringMethod(methodName);
+        }
+        if (receiver instanceof StringBuilder || receiver instanceof 
StringBuffer) {
+            return REFUSED_BUILDER_METHODS.contains(methodName);
+        }
+        return false;
+    }
+
+    private static boolean isBoundedStringMethod(String methodName) {
+        switch (methodName) {
+            case "replace":
+            case "replaceAll":
+            case "replaceFirst":
+            case "concat":
+            case "toUpperCase":
+            case "toLowerCase":
+                return true;
+            default:
+                return false;
+        }
+    }
+
+    /**
+     * @return the most characters the call can put in one string, or 0 when
+     *         it doesn't build a string longer than its receiver or arguments
+     */
+    static long maxResultLength(Object receiver, String methodName, Object[] 
args) {
+        Object[] a = args == null ? new Object[0] : args;
+        if (receiver instanceof String) {
+            return forString((String) receiver, methodName, a);
+        }
+        if (receiver instanceof StringBuilder || receiver instanceof 
StringBuffer) {
+            return forBuilder((CharSequence) receiver, methodName, a);
+        }
+        if (receiver instanceof UtilitiesModel && "replace".equals(methodName)

Review Comment:
   **Important:** The pre-call bound covers `$utils.replace`, but 
`$utils.escapeHTML`, `$utils.escapeXML`, and `$utils.encode` can also expand a 
near-limit string before the post-call length check runs. Bound these methods 
before invocation or build their results through a counted writer, and add an 
allocation-ceiling regression test.



##########
app/src/main/java/org/apache/roller/weblogger/ui/rendering/velocity/StringGrowth.java:
##########
@@ -0,0 +1,207 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.roller.weblogger.ui.rendering.velocity;
+
+import java.util.Set;
+
+import org.apache.roller.weblogger.ui.rendering.model.UtilitiesModel;
+
+/**
+ * Some string methods build a result much longer than their receiver and
+ * arguments, so checking the result's length after the call is too late. This
+ * works out, before a call from a template runs, the longest string it can
+ * build, and names the methods whose result can't be bounded that way.
+ *
+ * <p>Methods are matched by name so that methods added in later Java versions
+ * (String.indent, String.formatted, StringBuilder.repeat) are covered when
+ * Roller runs on them.
+ */
+final class StringGrowth {
+
+    /** String methods that are not offered to templates. */
+    private static final Set<String> REFUSED_STRING_METHODS = Set.of(
+            "repeat", "format", "formatted", "join", "indent");
+
+    /** String methods whose result is never longer than the receiver or an 
argument. */
+    private static final Set<String> NON_GROWING_STRING_METHODS = Set.of(
+            "substring", "subSequence", "trim", "strip", "stripLeading",
+            "stripTrailing", "stripIndent", "translateEscapes", "intern",
+            "toString", "valueOf", "copyValueOf");
+
+    /** StringBuilder and StringBuffer methods that are not offered to 
templates. */
+    private static final Set<String> REFUSED_BUILDER_METHODS = 
Set.of("repeat");
+
+    /** Characters a number, boolean or other value appended to a builder may 
take. */
+    private static final long SCALAR_LENGTH = 32;
+
+    /** Most characters toUpperCase or toLowerCase makes of one character. */
+    private static final long CASE_GROWTH = 3;
+
+    private StringGrowth() {
+    }
+
+    /**
+     * @return true if the method must not be offered on this receiver: it can
+     *         build a string whose length can't be worked out from the call
+     */
+    static boolean isRefused(Object receiver, String methodName, Class<?> 
returnType) {
+        if (receiver instanceof String) {
+            if (REFUSED_STRING_METHODS.contains(methodName)) {
+                return true;
+            }
+            // a method this class doesn't know about that returns a string
+            return returnType != null && 
CharSequence.class.isAssignableFrom(returnType)
+                    && !NON_GROWING_STRING_METHODS.contains(methodName)
+                    && !isBoundedStringMethod(methodName);
+        }
+        if (receiver instanceof StringBuilder || receiver instanceof 
StringBuffer) {
+            return REFUSED_BUILDER_METHODS.contains(methodName);
+        }
+        return false;
+    }
+
+    private static boolean isBoundedStringMethod(String methodName) {
+        switch (methodName) {
+            case "replace":
+            case "replaceAll":
+            case "replaceFirst":
+            case "concat":
+            case "toUpperCase":
+            case "toLowerCase":
+                return true;
+            default:
+                return false;
+        }
+    }
+
+    /**
+     * @return the most characters the call can put in one string, or 0 when
+     *         it doesn't build a string longer than its receiver or arguments
+     */
+    static long maxResultLength(Object receiver, String methodName, Object[] 
args) {
+        Object[] a = args == null ? new Object[0] : args;
+        if (receiver instanceof String) {
+            return forString((String) receiver, methodName, a);
+        }
+        if (receiver instanceof StringBuilder || receiver instanceof 
StringBuffer) {
+            return forBuilder((CharSequence) receiver, methodName, a);
+        }
+        if (receiver instanceof UtilitiesModel && "replace".equals(methodName)
+                && a.length >= 3 && a[0] instanceof String) {
+            long maxCount = a.length > 3 && a[3] instanceof Number
+                    ? ((Number) a[3]).longValue() : -1;
+            if (!(a[1] instanceof String) || ((String) a[1]).isEmpty()) {
+                // StringUtils.replace leaves the text alone for an empty 
target
+                return 0;
+            }
+            return literalReplace((String) a[0], (String) a[1], length(a[2]), 
maxCount);
+        }
+        return 0;
+    }
+
+    private static long forString(String s, String methodName, Object[] a) {
+        long len = s.length();
+        switch (methodName) {
+            case "replace":
+                if (a.length == 2 && a[0] instanceof CharSequence) {
+                    return literalReplace(s, a[0].toString(), length(a[1]), 
-1);
+                }
+                return len;
+            case "replaceAll":
+            case "replaceFirst":
+                // DeadlineRegex runs these and checks the limit as it goes
+                return 0;
+            case "concat":
+                return add(len, length(a.length > 0 ? a[0] : null));
+            case "toUpperCase":
+            case "toLowerCase":
+                return multiply(CASE_GROWTH, len);

Review Comment:
   **Important:** This three-times-length bound rejects ordinary case 
conversion whose result fits the limit: calling `toUpperCase()` on four million 
ASCII characters fails under the 10 MB default even though the result is still 
four million characters. Use a tighter bound for non-expanding input, and test 
a large ASCII string below the configured limit.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to