Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
21 changes: 13 additions & 8 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,19 @@
### 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. `#parse` and `#include` load only the weblog's
own templates. Error pages no longer show template ids (#367).
- **Macros defined in a template belong to that template.** A macro defined
inline in a template works in that page and in the templates it
`#parse`s, and one weblog's macro no longer replaces another's of the same
name. A custom theme that calls a macro defined in a different template,
which it doesn't `#parse`, now prints the call as text. Define the macro in
the page that calls it, or `#parse` the template that defines it. Library
macros (`roller.vm`, `weblog.vm` and a site's own `roller-custom.vm`) stay
global (#367).
- **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
@@ -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