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


##########
app/src/main/java/org/apache/roller/weblogger/ui/rendering/velocity/RenderLimitUberspector.java:
##########
@@ -0,0 +1,149 @@
+/*
+ * 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.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Iterator;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.velocity.util.introspection.AbstractChainableUberspector;
+import org.apache.velocity.util.introspection.Info;
+import org.apache.velocity.util.introspection.VelMethod;
+import org.apache.velocity.util.introspection.VelPropertyGet;
+
+/**
+ * Chained after SecureUberspector, this keeps method calls from templates
+ * inside the render limits: each call and property read checks the deadline,
+ * a string a call returns may be no longer than the output limit, a call that
+ * could build a longer string is stopped before it runs, and string methods
+ * whose result can't be bounded from the call (String.repeat, String.format
+ * and others; see StringGrowth) are not offered. Regular expressions given to
+ * String methods check the deadline as they run; see DeadlineRegex.
+ */
+public class RenderLimitUberspector extends AbstractChainableUberspector {
+
+    private static final Log LOG = 
LogFactory.getLog(RenderLimitUberspector.class);
+
+    @Override
+    public Iterator getIterator(Object obj, Info i) {
+        RenderSession.checkDeadline();
+        return super.getIterator(obj, i);
+    }
+
+    @Override
+    public VelMethod getMethod(Object obj, String methodName, Object[] args, 
Info i) {
+        VelMethod method = super.getMethod(obj, methodName, args, i);
+        if (method == null) {
+            return null;
+        }
+        if (StringGrowth.isRefused(obj, methodName, method.getReturnType())) {
+            LOG.debug("Refusing " + obj.getClass().getSimpleName() + "." + 
methodName
+                    + " at " + i);
+            return null;
+        }
+        return new LimitedMethod(method);
+    }
+
+    @Override
+    public VelPropertyGet getPropertyGet(Object obj, String identifier, Info 
i) {
+        VelPropertyGet getter = super.getPropertyGet(obj, identifier, i);
+        return getter == null ? null : new LimitedPropertyGet(getter);
+    }
+
+    private static Object checked(Object result) {
+        if (result instanceof CharSequence) {
+            RenderSession.checkLength(((CharSequence) result).length());
+        }
+        return result;
+    }
+
+    private static final class LimitedMethod implements VelMethod {
+
+        private final VelMethod method;
+
+        LimitedMethod(VelMethod method) {
+            this.method = method;
+        }
+
+        @Override
+        public Object invoke(Object o, Object[] params)
+                throws IllegalAccessException, InvocationTargetException {
+            RenderSession.checkDeadline();
+            if (RenderSession.current() != null) {
+                RenderSession.checkLength(StringGrowth.maxResultLength(
+                        o, method.getMethodName(), params));
+                if (o instanceof String) {
+                    Object result = DeadlineRegex.invoke((String) o, 
method.getMethodName(), params);
+                    if (result != DeadlineRegex.NOT_HANDLED) {
+                        return checked(result);
+                    }
+                }
+            }
+            return checked(method.invoke(o, params));

Review Comment:
   **Important:** `StringGrowth.maxResultLength()` bounds 
`UtilitiesModel.replace`, but other expanding utility methods such as 
`escapeHTML()` and `autoformat()` reach `method.invoke()` without a pre-call 
bound. `checked()` sees their results only after allocation. Bound these 
methods before invoking them, and add an allocation regression test.



##########
app/src/main/java/org/apache/roller/weblogger/ui/rendering/velocity/DeadlineRegex.java:
##########
@@ -0,0 +1,195 @@
+/*
+ * 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.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Runs the String methods that take a regular expression (matches,
+ * replaceAll, replaceFirst and split) over text that checks the render
+ * deadline as the expression reads it, so a slow expression stops with the
+ * render instead of holding the thread, and replaceAll and replaceFirst stop
+ * at the output limit as they build the result. Each gives the same result
+ * as the String method; see String's documentation for the Pattern call each
+ * one is equivalent to.
+ */
+final class DeadlineRegex {
+
+    /** Returned when the call isn't one of these methods. */
+    static final Object NOT_HANDLED = new Object();
+
+    private DeadlineRegex() {
+    }
+
+    /**
+     * @return the result of the call, or NOT_HANDLED when it isn't one of
+     *         these methods or its arguments don't fit them
+     */
+    static Object invoke(String s, String methodName, Object[] args) {
+        if (args == null || args.length == 0 || args.length > 2 || args[0] == 
null) {
+            return NOT_HANDLED;
+        }
+        // Velocity converts arguments to the method's parameter types after
+        // this sees them, so convert them the same way here
+        String regex = args[0].toString();
+        Object second = args.length > 1 ? args[1] : null;
+        CharSequence text = new DeadlineText(s);
+        switch (methodName) {
+            case "matches":
+                return args.length == 1 ? 
Pattern.compile(regex).matcher(text).matches()
+                        : NOT_HANDLED;
+            case "replaceAll":
+            case "replaceFirst":
+                return second == null ? NOT_HANDLED : 
replace(Pattern.compile(regex).matcher(text),
+                        second.toString(), "replaceAll".equals(methodName));
+            case "split":
+                Integer limit = args.length == 1 ? Integer.valueOf(0) : 
toInt(second);
+                if (limit == null) {
+                    return NOT_HANDLED;
+                }
+                return split(s, Pattern.compile(regex).matcher(text), limit);
+            default:
+                return NOT_HANDLED;
+        }
+    }
+
+    /** No more pieces than a #foreach can visit 
(directive.foreach.max_loops). */
+    static final int MAX_SPLIT_PIECES = 100_000;
+
+    /**
+     * Pattern.split, throwing if it would return more than MAX_SPLIT_PIECES.
+     * With a limit of 0 split drops trailing empty pieces, so those are
+     * neither counted nor allocated: empty pieces are only added once a piece
+     * that isn't empty follows them.
+     */
+    private static String[] split(String s, Matcher m, int limit) {
+        List<String> pieces = new ArrayList<>();
+        int heldBack = 0;
+        int index = 0;
+        boolean last = false;
+        while (!last) {
+            last = limit > 0 && pieces.size() + heldBack == limit - 1 || 
!m.find();
+            if (!last && m.end() == 0) {
+                // an empty match at the start doesn't make an empty first 
piece
+                continue;
+            }
+            if (last && index == 0) {
+                // a string with no match is returned whole, even when empty
+                return new String[] {s};
+            }
+            int end = last ? s.length() : m.start();
+            if (limit == 0 && end == index) {
+                heldBack++;
+            } else {
+                if (pieces.size() + heldBack >= MAX_SPLIT_PIECES) {
+                    throw new RenderLimitException("split produced more than "
+                            + MAX_SPLIT_PIECES + " pieces");
+                }
+                for (; heldBack > 0; heldBack--) {
+                    pieces.add("");
+                }
+                pieces.add(s.substring(index, end));
+            }
+            index = last ? end : m.end();
+        }
+        return pieces.toArray(new String[0]);
+    }
+
+    private static Integer toInt(Object value) {
+        if (value instanceof Number) {
+            return ((Number) value).intValue();
+        }
+        if (value instanceof String) {
+            try {
+                return Integer.valueOf(((String) value).trim());
+            } catch (NumberFormatException ex) {
+                return null;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Matcher.replaceAll and replaceFirst, checking before each replacement
+     * that it can't take the result past the output limit: a replacement adds
+     * at most its own length plus, for each group reference ($1, ${name}) in
+     * it, the longest group of that match.
+     */
+    private static String replace(Matcher m, String replacement, boolean all) {
+        long references = replacement.chars().filter(c -> c == '$').count();
+        StringBuilder out = new StringBuilder();
+        int previousEnd = 0;
+        while (m.find()) {
+            long longestGroup = 0;
+            for (int g = 0; g <= m.groupCount(); g++) {
+                if (m.start(g) >= 0) {
+                    longestGroup = Math.max(longestGroup, m.end(g) - 
m.start(g));
+                }
+            }
+            RenderSession.checkLength((long) out.length() + (m.start() - 
previousEnd)
+                    + replacement.length() + references * longestGroup);
+            m.appendReplacement(out, replacement);
+            previousEnd = m.end();
+            if (!all) {
+                break;
+            }
+        }
+        m.appendTail(out);

Review Comment:
   **Important:** `appendTail(out)` can grow the replacement result past 
`maxOutputChars` before the following check runs. Check `out.length()` plus the 
remaining input length before appending the tail, and cover this case for 
`replaceFirst` and `replaceAll`.



-- 
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