Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
1973f42
Bound weblog template rendering and keep templates and macros per weblog
mraible Nov 17, 2026
c3de58a
Stop string calls from templates that would build a string past the o…
mraible Nov 18, 2026
1879cd4
Keep strings built in memory, macro calls and regular expressions ins…
mraible Nov 18, 2026
a15d8c3
Check regex replaces against the output limit as they run, and drop t…
mraible Nov 18, 2026
e882061
Refuse a split that would return more pieces than a template loop can…
mraible Nov 18, 2026
439242b
Keep template locations on swapped-in nodes and count only the split …
mraible Nov 19, 2026
66d58c4
Bound expanding $utils calls before they run and stop over-counting c…
mraible Nov 20, 2026
b7b9c1a
Count only the characters escapeHTML and escapeXML expand
mraible Nov 20, 2026
29a8b9e
Merge remote-tracking branch 'origin/master' into template-limits
mraible Nov 20, 2026
9844fa5
Read stored templates as text instead of encoding and decoding them
mraible Nov 20, 2026
fdf4801
Parse templates with many nodes in linear time
mraible Nov 20, 2026
71c3d6a
Shorten long template error messages in the log and on the error page
mraible Nov 20, 2026
540cb37
Count only the characters a case change or an escape really grows
mraible Nov 20, 2026
3b3d5bd
Refuse regular expressions that repeat a part that can match an empty…
mraible Nov 20, 2026
cbf0c54
Fail the render when a #foreach runs past 100,000 passes
mraible Nov 20, 2026
5b823f2
Count the strings and list items a render builds, and check lists, ma…
mraible Nov 20, 2026
088841d
Bound $utils.addNofollow for its jsoup implementation
mraible Nov 20, 2026
9ad3bbe
Describe the render budget and the new template refusals in CHANGES
mraible Nov 20, 2026
ca9614f
Measure map entries and appended lists as text, and count copied list…
mraible Nov 20, 2026
694131f
Stop offering $utils.addNofollow to templates
mraible Nov 20, 2026
6ecc7d6
Drop the in-memory render budget and put template resource use out of…
mraible Nov 21, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,16 @@
### Behaviour changes worth reading before upgrading

- **Weblog templates have render limits.** A page stops rendering, with a
logged error, once it writes more than `rendering.maxOutputChars` (10 MB)
or runs longer than `rendering.maxRenderSeconds` (30 seconds); `#foreach`
stops at 100,000 passes, and `String.repeat` isn't available. `#parse` and
`#include` load only the weblog's own templates, and macros defined inline
in a template stay with the page that defines them and the templates it
includes, so weblogs can't affect each other's macros.
Library macros such as `roller.vm` and a site's own `roller-custom.vm` stay
global. Error pages no longer show template ids (#317).
logged error, once it writes more than `rendering.maxOutputChars` (10 MB),
inserts a longer string, or runs longer than `rendering.maxRenderSeconds`
(30 seconds), and the page isn't cached. A `#foreach` of more than 100,000
passes fails the same way. Templates can no longer call
`$utils.addNofollow`, a breaking change. `#parse` and `#include` load only
the weblog's own templates, and macros defined inline in a template stay
with the page that defines them and the templates it includes, so weblogs
can't affect each other's macros. Library macros such as `roller.vm` and a
site's own `roller-custom.vm` stay global. Error pages no longer show
template ids (#317).
- **Entries with scripts or embeds open as HTML.** If an entry contains
scripts, frames, embedded objects, event-handler attributes or
`javascript:` links, the rich-text editor opens it in the HTML textarea
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,6 @@ public final String decode(String s) {
}
}

/**
* Code (stolen from Pebble) to add rel="nofollow" string to all links in HTML.
*/
public String addNofollow(String html) {
return Utilities.addNofollow(html);
}

/**
* Transforms the given String into a subset of HTML displayable on a web
* page. The subset includes <b>, <i>, <p>, <br>,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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;

/**
* Raised when a template goes past the size or time Roller allows for one
* render. It is unchecked so it passes through Velocity's directives and
* introspection unchanged; VelocityRenderer turns it into a failed render.
*/
public class RenderLimitException extends RuntimeException {

private static final long serialVersionUID = 1L;

public RenderLimitException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 org.apache.velocity.app.event.ReferenceInsertionEventHandler;
import org.apache.velocity.context.Context;

/**
* Checks every value a template inserts, including into a string literal such
* as "$s$s", so a string built up in memory stops at the output limit before
* it is ever written.
*/
public class RenderLimitInsertionHandler implements ReferenceInsertionEventHandler {

@Override
public Object referenceInsert(Context context, String reference, Object value) {
RenderSession.checkDeadline();
if (value instanceof CharSequence) {
RenderSession.checkLength(((CharSequence) value).length());
}
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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.io.IOException;
import java.io.Writer;

/**
* Passes rendered output through, failing the render once it has written more
* than the session allows or run past its deadline.
*/
class RenderLimitWriter extends Writer {

private final Writer out;
private final long maxChars;
private long written = 0;

RenderLimitWriter(Writer out, RenderSession session) {
this.out = out;
this.maxChars = session.getMaxOutputChars();
}

private void count(long len) {
written += len;
if (written > maxChars) {
throw new RenderLimitException("rendering produced more than "
+ maxChars + " characters");
}
RenderSession.checkDeadline();
}

@Override
public void write(int c) throws IOException {
count(1);
out.write(c);
}

@Override
public void write(char[] cbuf, int off, int len) throws IOException {
count(len);
out.write(cbuf, off, len);
}

@Override
public void write(String str, int off, int len) throws IOException {
count(len);
out.write(str, off, len);
}

@Override
public void flush() throws IOException {
out.flush();
}

@Override
public void close() throws IOException {
out.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* 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.concurrent.TimeUnit;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.roller.weblogger.config.WebloggerConfig;

/**
* What applies while one template is rendered on this thread: the weblog whose
* stored templates it may load, and how much output and time it may use.
*
* <p>Weblog templates are written by weblog authors, so the engine's hooks
* (the resource loader, the output writer, #foreach and reference
* insertion) check the current session rather than trusting the
* template. Outside a session those hooks apply no limits. A session started
* inside another one shares that one's limits and deadline.
*/
final class RenderSession {

private static final Log LOG = LogFactory.getLog(RenderSession.class);

/** Most characters one render may write, or put in one string. */
static final String MAX_OUTPUT_CHARS = "rendering.maxOutputChars";

/** Most seconds one render may take. */
static final String MAX_RENDER_SECONDS = "rendering.maxRenderSeconds";

private static final int DEFAULT_MAX_OUTPUT_CHARS = 10 * 1024 * 1024;
private static final int DEFAULT_MAX_RENDER_SECONDS = 30;

private static final ThreadLocal<RenderSession> CURRENT = new ThreadLocal<>();

private final String weblogId;
private final Limits limits;
private final RenderSession previous;

/** The limits of one render, shared by the sessions started inside it. */
private static final class Limits {
final long maxOutputChars;
final int maxRenderSeconds;
final long deadline;

Limits() {
int chars = intProperty(MAX_OUTPUT_CHARS, DEFAULT_MAX_OUTPUT_CHARS);
maxOutputChars = chars > 0 ? chars : Long.MAX_VALUE;
maxRenderSeconds = intProperty(MAX_RENDER_SECONDS, DEFAULT_MAX_RENDER_SECONDS);
deadline = maxRenderSeconds > 0
? System.nanoTime() + TimeUnit.SECONDS.toNanos(maxRenderSeconds)
: 0;
}
}

private RenderSession(String weblogId, RenderSession previous) {
this.weblogId = weblogId;
this.previous = previous;
this.limits = previous != null ? previous.limits : new Limits();
}

/** @return the property's value, or the default when it isn't a number */
private static int intProperty(String name, int defaultValue) {
String value = WebloggerConfig.getProperty(name);
if (value == null || value.isBlank()) {
return defaultValue;
}
try {
return Integer.parseInt(value.trim());
} catch (NumberFormatException ex) {
LOG.warn(name + " is not a number, using " + defaultValue + ": " + value);
return defaultValue;
}
}

/**
* Starts a session on this thread.
*
* @param weblogId the weblog being rendered, or null when there is none,
* in which case no stored weblog template may be loaded
*/
static RenderSession begin(String weblogId) {
RenderSession session = new RenderSession(weblogId, CURRENT.get());
CURRENT.set(session);
return session;
}

/** Ends this session, restoring any session it was started inside. */
void end() {
if (previous == null) {
CURRENT.remove();
} else {
CURRENT.set(previous);
}
}

/** @return the session on this thread, or null outside rendering */
static RenderSession current() {
return CURRENT.get();
}

String getWeblogId() {
return weblogId;
}

long getMaxOutputChars() {
return limits.maxOutputChars;
}

/** Throws once the current render has used up its time. */
static void checkDeadline() {
RenderSession session = CURRENT.get();
if (session != null && session.limits.maxRenderSeconds > 0
&& System.nanoTime() - session.limits.deadline > 0) {
throw new RenderLimitException("rendering took longer than "
+ session.limits.maxRenderSeconds + " seconds");
}
}

/** Throws if one string would be longer than one render may write. */
static void checkLength(long length) {
RenderSession session = CURRENT.get();
if (session != null && length > session.limits.maxOutputChars) {
throw new RenderLimitException("a string would be longer than "
+ session.limits.maxOutputChars + " characters");
}
}
}
Loading
Loading