snoopdave commented on code in PR #335: URL: https://github.com/apache/roller/pull/335#discussion_r4609257010
########## app/src/main/java/org/apache/roller/weblogger/ui/rendering/velocity/DeadlineRegex.java: ########## @@ -0,0 +1,151 @@ +/* + * 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.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": + if (args.length == 1) { + return Pattern.compile(regex).split(text, 0); Review Comment: 🐞Codex Issue: **Blocking:** `String.split` still bypasses the memory limit. A template can build a string just under `rendering.maxOutputChars` (10 MiB by default) and call `$s.split('')`; `Pattern.split` allocates an array with roughly one `String` per character before returning. `RenderLimitUberspector.checked()` only checks `CharSequence` results, so the array is never size-checked, and the deadline checks cannot cap the allocation. Limit the number of split results before allocating them (or refuse `split` in templates), and add a regression test that exercises this path under the shipped render configuration. -- 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]
