diff --git a/docs/services/api-gateway.md b/docs/services/api-gateway.md index 7358369a64..8dc9ec86f0 100644 --- a/docs/services/api-gateway.md +++ b/docs/services/api-gateway.md @@ -332,6 +332,19 @@ setups, and `floci:override-id` wins when both are present. Every other tag is p |---|---|---| | `FLOCI_SERVICES_APIGATEWAY_ENABLED` | `true` | Enable or disable API Gateway v1 (REST APIs) | | `FLOCI_SERVICES_APIGATEWAYV2_ENABLED` | `true` | Enable or disable API Gateway v2 (HTTP and WebSocket APIs) | +| `FLOCI_SERVICES_APIGATEWAY_VTL_MAX_LOOPS` | `10000` | Maximum `#foreach` iterations a VTL mapping template may execute | +| `FLOCI_SERVICES_APIGATEWAY_VTL_MAX_OUTPUT_CHARS` | `1048576` | Maximum characters a VTL mapping template may render | +| `FLOCI_SERVICES_APIGATEWAY_VTL_TIMEOUT_MILLIS` | `5000` | Maximum wall-clock time a VTL mapping template may spend evaluating | + +VTL (Velocity Template Language) mapping templates render inside a reflection-restricted sandbox +(`SecureUberspector`, with `Class`, `ClassLoader`, `Runtime`, `ProcessBuilder`, `System`, `Thread`, +`java.io.File` and related classes/packages blocked) and are subject to the three limits above. +The loop cap truncates a `#foreach` at the configured iteration count and lets the template finish +rendering with whatever output it produced up to that point; it does not fail the template. +Exceeding the output-size or execution-time limit does fail the template, the same way any other +Velocity evaluation error does; neither introduces a new error shape. This applies to both API +Gateway v1 (`AWS`/Lambda mapping templates) and the AppSync resolver templates described in +[appsync.md](appsync.md). ## API Gateway v2 (HTTP and WebSocket APIs) {#v2} diff --git a/docs/services/appsync.md b/docs/services/appsync.md index 9fcee65b56..72e398c3c8 100644 --- a/docs/services/appsync.md +++ b/docs/services/appsync.md @@ -286,6 +286,18 @@ These AWS AppSync capabilities are not yet implemented and are tracked in future | Variable | Default | Description | |---|---|---| | `FLOCI_SERVICES_APPSYNC_ENABLED` | `true` | Enable or disable the service | +| `FLOCI_SERVICES_APPSYNC_VTL_MAX_LOOPS` | `10000` | Maximum `#foreach` iterations a VTL resolver template may execute | +| `FLOCI_SERVICES_APPSYNC_VTL_MAX_OUTPUT_CHARS` | `1048576` | Maximum characters a VTL resolver template may render | +| `FLOCI_SERVICES_APPSYNC_VTL_TIMEOUT_MILLIS` | `5000` | Maximum wall-clock time a VTL resolver template may spend evaluating | + +Request/response mapping templates render inside the same VTL reflection sandbox described for +API Gateway in [api-gateway.md](api-gateway.md#configuration) (`SecureUberspector`, with `Class`, +`ClassLoader`, `Runtime`, `ProcessBuilder`, `System`, `Thread`, `java.io.File` and related +classes/packages blocked), and are subject to the same three limits above. The loop cap truncates +a `#foreach` at the configured iteration count and lets the template finish rendering with +whatever output it produced up to that point; it does not fail the resolver. Exceeding the +output-size or execution-time limit does fail the resolver, the same way any other VTL evaluation +error does. ## Examples diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java index 13a2bcdafd..e7890cb421 100644 --- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java +++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java @@ -1177,6 +1177,21 @@ public interface EmulatorConfig { interface ApiGatewayServiceConfig { @WithDefault("true") boolean enabled(); + + /** Maximum #foreach loop iterations allowed in a single VTL mapping template render, as a + * hard backstop against runaway loops. Env: FLOCI_SERVICES_APIGATEWAY_VTL_MAX_LOOPS */ + @WithDefault("10000") + int vtlMaxLoops(); + + /** Maximum rendered output size, in characters, for a single VTL mapping template render. + * Env: FLOCI_SERVICES_APIGATEWAY_VTL_MAX_OUTPUT_CHARS */ + @WithDefault("1048576") + int vtlMaxOutputChars(); + + /** Wall-clock execution budget, in milliseconds, for a single VTL mapping template render. + * Env: FLOCI_SERVICES_APIGATEWAY_VTL_TIMEOUT_MILLIS */ + @WithDefault("5000") + long vtlTimeoutMillis(); } interface IamServiceConfig { @@ -1968,6 +1983,21 @@ public interface EmulatorConfig { /** Seconds to wait for in-flight schema workers on shutdown. Env: FLOCI_SERVICES_APPSYNC_SCHEMA_WORKER_SHUTDOWN_TIMEOUT_SECONDS */ @WithDefault("30") int schemaWorkerShutdownTimeoutSeconds(); + + /** Maximum #foreach loop iterations allowed in a single VTL resolver template render, as a + * hard backstop against runaway loops. Env: FLOCI_SERVICES_APPSYNC_VTL_MAX_LOOPS */ + @WithDefault("10000") + int vtlMaxLoops(); + + /** Maximum rendered output size, in characters, for a single VTL resolver template render. + * Env: FLOCI_SERVICES_APPSYNC_VTL_MAX_OUTPUT_CHARS */ + @WithDefault("1048576") + int vtlMaxOutputChars(); + + /** Wall-clock execution budget, in milliseconds, for a single VTL resolver template render. + * Env: FLOCI_SERVICES_APPSYNC_VTL_TIMEOUT_MILLIS */ + @WithDefault("5000") + long vtlTimeoutMillis(); } interface OamServiceConfig { diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/BoundedWriter.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/BoundedWriter.java new file mode 100644 index 0000000000..539624111a --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/BoundedWriter.java @@ -0,0 +1,45 @@ +package io.github.hectorvent.floci.services.apigateway; + +import java.io.IOException; +import java.io.Writer; + +/** + * Wraps a delegate {@link Writer} and throws {@link VtlLimitExceededException} once the total + * number of characters written would exceed a configured budget. + * + *
This defends against unbounded VTL template output regardless of cause (a huge + * {@code #foreach} range, a single oversized reference, or repeated small writes that add up), + * complementing the loop-iteration cap and the execution-time deadline, neither of which observe + * output size directly. + */ +public class BoundedWriter extends Writer { + + private final Writer delegate; + private final long maxChars; + private long written; + + public BoundedWriter(Writer delegate, long maxChars) { + this.delegate = delegate; + this.maxChars = maxChars; + } + + @Override + public void write(char[] cbuf, int off, int len) throws IOException { + if (written + len > maxChars) { + throw new VtlLimitExceededException( + "VTL template output exceeded the configured size limit of " + maxChars + " characters"); + } + delegate.write(cbuf, off, len); + written += len; + } + + @Override + public void flush() throws IOException { + delegate.flush(); + } + + @Override + public void close() throws IOException { + delegate.close(); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/SandboxedForeach.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/SandboxedForeach.java new file mode 100644 index 0000000000..83c77a88c0 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/SandboxedForeach.java @@ -0,0 +1,26 @@ +package io.github.hectorvent.floci.services.apigateway; + +import org.apache.velocity.context.InternalContextAdapter; +import org.apache.velocity.runtime.directive.Foreach; +import org.apache.velocity.runtime.parser.node.Node; + +import java.io.IOException; +import java.io.Writer; + +/** + * Drop-in replacement for Velocity's built-in {@code #foreach} directive that checks the current + * thread's {@link VtlExecutionGuard} deadline before rendering each loop iteration. + * + *
Registered in place of {@code org.apache.velocity.runtime.directive.Foreach} by + * {@link VtlSandbox#installSandboxedForeach(org.apache.velocity.app.VelocityEngine)}, so it applies + * identically to every {@code #foreach} loop evaluated by either the API Gateway or the AppSync + * VTL engine. + */ +public class SandboxedForeach extends Foreach { + + @Override + protected void renderBlock(InternalContextAdapter context, Writer writer, Node node) throws IOException { + VtlExecutionGuard.checkDeadline(); + super.renderBlock(context, writer, node); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/VtlExecutionGuard.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/VtlExecutionGuard.java new file mode 100644 index 0000000000..edfdfc12fb --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/VtlExecutionGuard.java @@ -0,0 +1,50 @@ +package io.github.hectorvent.floci.services.apigateway; + +import java.time.Duration; +import java.time.Instant; + +/** + * Tracks a wall-clock execution deadline for the VTL template render currently in progress on + * the calling thread. + * + *
Both {@link VtlTemplateEngine} and the AppSync VTL engine call {@link #begin(Duration)} + * immediately before delegating to Velocity's {@code VelocityEngine.evaluate(...)}, and + * {@link #end()} in a {@code finally} block right after. {@link SandboxedForeach} calls + * {@link #checkDeadline()} before rendering each loop iteration's body, which is what actually + * stops a runaway {@code #foreach} (whether over a huge range, or one whose body does something + * increasingly expensive per iteration, such as {@code #set} string doubling): each iteration's + * own cost grows with the doubling, so the next iteration's check reliably trips shortly after + * the configured time budget is crossed, well before memory would otherwise be exhausted. + * + *
A {@link ThreadLocal} is used because template evaluation is synchronous and single-threaded
+ * per call, and Velocity does not offer a per-render context slot for deadline plumbing that both
+ * a custom writer and a custom directive can reach without changing Velocity's own APIs.
+ */
+public final class VtlExecutionGuard {
+
+ private static final ThreadLocal This is a plain unchecked exception, matching Velocity's own {@code VelocityException}
+ * hierarchy (which also extends {@link RuntimeException}), so it propagates through both the
+ * API Gateway and AppSync VTL engines exactly like any other template rendering failure already
+ * does today, without requiring any new catch or error-mapping logic at their call sites.
+ */
+public class VtlLimitExceededException extends RuntimeException {
+
+ public VtlLimitExceededException(String message) {
+ super(message);
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/VtlSandbox.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/VtlSandbox.java
new file mode 100644
index 0000000000..8b8926b515
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/VtlSandbox.java
@@ -0,0 +1,100 @@
+package io.github.hectorvent.floci.services.apigateway;
+
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.runtime.RuntimeConstants;
+import org.apache.velocity.util.introspection.SecureUberspector;
+
+/**
+ * Applies Floci's VTL reflection sandbox to a {@link VelocityEngine}, shared by both the
+ * API Gateway and AppSync VTL engines.
+ *
+ * Velocity normally reads {@code introspector.uberspect.class} and
+ * {@code introspector.restrict.*} from the classpath resource
+ * {@code org/apache/velocity/runtime/defaults/velocity.properties}, which this project shadows
+ * (see {@code src/main/resources/org/apache/velocity/runtime/defaults/velocity.properties}) to
+ * select {@link SecureUberspector} instead of Velocity's insecure default {@code UberspectImpl}.
+ * That shadowed file is kept for any embedding that resolves the resource normally, but it is
+ * not the actual enforcement point in this codebase: under Quarkus's custom
+ * test/runtime classloader, {@code ClassLoader#getResourceAsStream} for that exact path can
+ * resolve to the copy bundled inside the {@code velocity-engine-core} jar instead of this
+ * project's shadowed copy, silently leaving the insecure {@code UberspectImpl} active. This was
+ * confirmed empirically: with only the properties file changed, reflection-escape tests
+ * (e.g. {@code $obj.getClass().forName(...)}) still passed against the vulnerable engine.
+ *
+ * Setting these properties directly on the {@link VelocityEngine} via its Java API, before
+ * {@link VelocityEngine#init()}, is unaffected by that classloader ambiguity, so it is the real,
+ * verified enforcement mechanism. Both {@code VtlTemplateEngine} and {@code AppSyncVtlEngine}
+ * call {@link #restrictIntrospection(VelocityEngine)} before {@code init()} and
+ * {@link #installSandboxedForeach(VelocityEngine)} after {@code init()}.
+ */
+public final class VtlSandbox {
+
+ /**
+ * Packages blocked outright: any method call on an object from these packages is rejected,
+ * regardless of which method. {@code java.lang.reflect} and {@code java.lang.invoke} close
+ * off reflective/handle-based sandbox escapes; {@code javax.script} closes off the scripting
+ * engine escape; {@code java.nio.file} closes off filesystem access alongside
+ * {@code java.io.File} below.
+ */
+ private static final String[] RESTRICTED_PACKAGES = {
+ "java.lang.reflect",
+ "java.lang.invoke",
+ "javax.script",
+ "java.nio.file",
+ };
+
+ /**
+ * Individual classes blocked outright. Mirrors Velocity's own recommended secure-introspector
+ * defaults ({@code Class}, {@code ClassLoader}, {@code Runtime}, {@code ProcessBuilder},
+ * {@code System}, {@code Thread}, etc.), plus {@code java.io.File} so templates cannot read or
+ * write arbitrary files on the host.
+ */
+ private static final String[] RESTRICTED_CLASSES = {
+ "java.lang.Class",
+ "java.lang.ClassLoader",
+ "java.lang.Compiler",
+ "java.lang.InheritableThreadLocal",
+ "java.lang.Package",
+ "java.lang.Process",
+ "java.lang.ProcessBuilder",
+ "java.lang.Runtime",
+ "java.lang.RuntimePermission",
+ "java.lang.SecurityManager",
+ "java.lang.System",
+ "java.lang.Thread",
+ "java.lang.ThreadGroup",
+ "java.lang.ThreadLocal",
+ "java.net.Socket",
+ "javax.management.MBeanServer",
+ "javax.script.ScriptEngine",
+ "java.io.File",
+ };
+
+ private VtlSandbox() {
+ }
+
+ /**
+ * Configures {@code engine} to use {@link SecureUberspector} with the restricted
+ * packages/classes above. Must be called before {@link VelocityEngine#init()}.
+ */
+ public static void restrictIntrospection(VelocityEngine engine) {
+ engine.setProperty(RuntimeConstants.UBERSPECT_CLASSNAME, SecureUberspector.class.getName());
+ for (String pkg : RESTRICTED_PACKAGES) {
+ engine.addProperty(RuntimeConstants.INTROSPECTOR_RESTRICT_PACKAGES, pkg);
+ }
+ for (String cls : RESTRICTED_CLASSES) {
+ engine.addProperty(RuntimeConstants.INTROSPECTOR_RESTRICT_CLASSES, cls);
+ }
+ }
+
+ /**
+ * Replaces the built-in {@code #foreach} directive with {@link SandboxedForeach}, so every
+ * loop iteration is subject to {@link VtlExecutionGuard}'s execution deadline check. Must be
+ * called after {@link VelocityEngine#init()} (the built-in directive is only registered during
+ * {@code init()}, so calling this beforehand would have nothing to remove/replace).
+ */
+ public static void installSandboxedForeach(VelocityEngine engine) {
+ engine.removeDirective("foreach");
+ engine.loadDirective(SandboxedForeach.class.getName());
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/VtlTemplateEngine.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/VtlTemplateEngine.java
index 621eb95b5f..7fc6caa84d 100644
--- a/src/main/java/io/github/hectorvent/floci/services/apigateway/VtlTemplateEngine.java
+++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/VtlTemplateEngine.java
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.apigateway;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.vtl.VtlUtilFunctions;
+import io.github.hectorvent.floci.config.EmulatorConfig;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import io.quarkus.runtime.annotations.RegisterForReflection;
@@ -24,11 +25,13 @@ import org.apache.velocity.runtime.resource.ResourceManagerImpl;
import org.apache.velocity.runtime.resource.loader.FileResourceLoader;
import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
import org.apache.velocity.runtime.resource.util.StringResourceRepositoryImpl;
+import org.apache.velocity.util.introspection.SecureUberspector;
import org.apache.velocity.util.introspection.TypeConversionHandlerImpl;
import org.apache.velocity.util.introspection.UberspectImpl;
import java.io.StringWriter;
import java.util.AbstractMap;
+import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@@ -46,6 +49,7 @@ import java.util.Set;
VtlTemplateEngine.UtilVariable.class,
VtlTemplateEngine.ResponseOverride.class,
UberspectImpl.class,
+ SecureUberspector.class,
TypeConversionHandlerImpl.class,
ResourceManagerImpl.class,
ResourceCacheImpl.class,
@@ -54,6 +58,7 @@ import java.util.Set;
StringResourceLoader.class,
StringResourceRepositoryImpl.class,
Foreach.class,
+ SandboxedForeach.class,
Include.class,
Parse.class,
Macro.class,
@@ -67,17 +72,22 @@ public class VtlTemplateEngine {
private final VelocityEngine engine;
private final ObjectMapper objectMapper;
+ private final EmulatorConfig config;
@Inject
- public VtlTemplateEngine(ObjectMapper objectMapper) {
+ public VtlTemplateEngine(ObjectMapper objectMapper, EmulatorConfig config) {
this.objectMapper = objectMapper;
+ this.config = config;
this.engine = new VelocityEngine();
engine.setProperty(RuntimeConstants.INPUT_ENCODING, "UTF-8");
engine.setProperty(RuntimeConstants.RUNTIME_LOG_NAME, "io.github.hectorvent.floci.vtl");
engine.setProperty(RuntimeConstants.RESOURCE_LOADERS, "string");
engine.setProperty("resource.loader.string.class",
"org.apache.velocity.runtime.resource.loader.StringResourceLoader");
+ engine.setProperty(RuntimeConstants.MAX_NUMBER_LOOPS, config.services().apigateway().vtlMaxLoops());
+ VtlSandbox.restrictIntrospection(engine);
engine.init();
+ VtlSandbox.installSandboxedForeach(engine);
}
/**
@@ -110,10 +120,16 @@ public class VtlTemplateEngine {
vc.put("context", buildContextMap(ctx, override));
vc.put("stageVariables", ctx.stageVariables() != null ? ctx.stageVariables() : Map.of());
- StringWriter writer = new StringWriter();
- engine.evaluate(vc, writer, "apigw-template", template);
+ StringWriter rawWriter = new StringWriter();
+ BoundedWriter writer = new BoundedWriter(rawWriter, config.services().apigateway().vtlMaxOutputChars());
+ VtlExecutionGuard.begin(Duration.ofMillis(config.services().apigateway().vtlTimeoutMillis()));
+ try {
+ engine.evaluate(vc, writer, "apigw-template", template);
+ } finally {
+ VtlExecutionGuard.end();
+ }
return new EvaluateResult(
- writer.toString(),
+ rawWriter.toString(),
override.getStatus(),
override.getHeader().isEmpty() ? Map.of() : Map.copyOf(override.getHeader()));
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/appsync/graphql/AppSyncVtlEngine.java b/src/main/java/io/github/hectorvent/floci/services/appsync/graphql/AppSyncVtlEngine.java
index 07857842ec..ab5747b309 100644
--- a/src/main/java/io/github/hectorvent/floci/services/appsync/graphql/AppSyncVtlEngine.java
+++ b/src/main/java/io/github/hectorvent/floci/services/appsync/graphql/AppSyncVtlEngine.java
@@ -1,5 +1,9 @@
package io.github.hectorvent.floci.services.appsync.graphql;
+import io.github.hectorvent.floci.config.EmulatorConfig;
+import io.github.hectorvent.floci.services.apigateway.BoundedWriter;
+import io.github.hectorvent.floci.services.apigateway.VtlExecutionGuard;
+import io.github.hectorvent.floci.services.apigateway.VtlSandbox;
import io.github.hectorvent.floci.services.appsync.graphql.util.AppSyncUtil;
import io.github.hectorvent.floci.services.appsync.graphql.util.VtlErrorSignal;
import jakarta.enterprise.context.ApplicationScoped;
@@ -9,6 +13,7 @@ import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import java.io.StringWriter;
+import java.time.Duration;
import java.util.List;
import java.util.Map;
@@ -16,9 +21,11 @@ import java.util.Map;
public class AppSyncVtlEngine {
private final VelocityEngine engine;
+ private final EmulatorConfig config;
@Inject
- public AppSyncVtlEngine() {
+ public AppSyncVtlEngine(EmulatorConfig config) {
+ this.config = config;
this.engine = new VelocityEngine();
engine.setProperty(RuntimeConstants.INPUT_ENCODING, "UTF-8");
engine.setProperty(RuntimeConstants.RUNTIME_LOG_NAME,
@@ -29,7 +36,10 @@ public class AppSyncVtlEngine {
engine.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT, false);
engine.setProperty("userdirective",
"io.github.hectorvent.floci.services.appsync.graphql.ReturnDirective");
+ engine.setProperty(RuntimeConstants.MAX_NUMBER_LOOPS, config.services().appsync().vtlMaxLoops());
+ VtlSandbox.restrictIntrospection(engine);
engine.init();
+ VtlSandbox.installSandboxedForeach(engine);
}
public AppSyncVtlResult evaluate(String template, AppSyncVtlContext ctx) {
@@ -51,7 +61,9 @@ public class AppSyncVtlEngine {
vc.put("util", util);
vc.put("utils", util);
- StringWriter writer = new StringWriter();
+ StringWriter rawWriter = new StringWriter();
+ BoundedWriter writer = new BoundedWriter(rawWriter, config.services().appsync().vtlMaxOutputChars());
+ VtlExecutionGuard.begin(Duration.ofMillis(config.services().appsync().vtlTimeoutMillis()));
try {
engine.evaluate(vc, writer, "appsync-template", template);
} catch (ReturnSignal signal) {
@@ -65,9 +77,11 @@ public class AppSyncVtlEngine {
cause = cause.getCause();
}
throw new RuntimeException("VTL evaluation failed", e);
+ } finally {
+ VtlExecutionGuard.end();
}
- return new AppSyncVtlResult(writer.toString(), null, ctx.getAppendedErrors());
+ return new AppSyncVtlResult(rawWriter.toString(), null, ctx.getAppendedErrors());
}
}
diff --git a/src/main/resources/META-INF/native-image/reflect-config.json b/src/main/resources/META-INF/native-image/reflect-config.json
index e7563033db..2840004808 100644
--- a/src/main/resources/META-INF/native-image/reflect-config.json
+++ b/src/main/resources/META-INF/native-image/reflect-config.json
@@ -23,6 +23,18 @@
"allDeclaredMethods": true,
"allDeclaredFields": true
},
+ {
+ "name": "org.apache.velocity.util.introspection.SecureUberspector",
+ "allDeclaredConstructors": true,
+ "allDeclaredMethods": true,
+ "allDeclaredFields": true
+ },
+ {
+ "name": "io.github.hectorvent.floci.services.apigateway.SandboxedForeach",
+ "allDeclaredConstructors": true,
+ "allDeclaredMethods": true,
+ "allDeclaredFields": true
+ },
{
"name": "org.apache.velocity.util.introspection.TypeConversionHandlerImpl",
"allDeclaredConstructors": true,
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index 857288cb94..c2a8e788b0 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -319,6 +319,9 @@ floci:
enabled: false
apigateway:
enabled: true
+ vtl-max-loops: 10000 # Maximum #foreach iterations a VTL mapping template may execute (truncates the loop, does not fail the template)
+ vtl-max-output-chars: 1048576 # Maximum characters a VTL mapping template may render before it fails
+ vtl-timeout-millis: 5000 # Maximum wall-clock time a VTL mapping template may spend evaluating before it fails
apigatewayv2:
enabled: true
iam:
@@ -658,6 +661,9 @@ floci:
enabled: true
schema-worker-threads: 4
schema-worker-shutdown-timeout-seconds: 30
+ vtl-max-loops: 10000 # Maximum #foreach iterations a VTL resolver template may execute (truncates the loop, does not fail the template)
+ vtl-max-output-chars: 1048576 # Maximum characters a VTL resolver template may render before it fails
+ vtl-timeout-millis: 5000 # Maximum wall-clock time a VTL resolver template may spend evaluating before it fails
s3vectors:
enabled: true
s3tables:
diff --git a/src/main/resources/org/apache/velocity/runtime/defaults/velocity.properties b/src/main/resources/org/apache/velocity/runtime/defaults/velocity.properties
index e790f79069..0b17278a39 100644
--- a/src/main/resources/org/apache/velocity/runtime/defaults/velocity.properties
+++ b/src/main/resources/org/apache/velocity/runtime/defaults/velocity.properties
@@ -198,7 +198,7 @@ parser.pool.size = 20
# Allows alternative introspection and all that can of worms brings.
# ----------------------------------------------------------------------------
-introspector.uberspect.class = org.apache.velocity.util.introspection.UberspectImpl
+introspector.uberspect.class = org.apache.velocity.util.introspection.SecureUberspector
# ----------------------------------------------------------------------------
# CONVERSION HANDLER
@@ -242,6 +242,16 @@ introspector.restrict.classes = java.net.Socket
introspector.restrict.classes = javax.management.MBeanServer
introspector.restrict.classes = javax.script.ScriptEngine
+# Additional restrictions beyond Velocity's own defaults above. SecureIntrospectorImpl matches
+# package/class names by exact string equality (no wildcards), so sun.*/jdk.internal.* packages
+# are deliberately not enumerated here: the only realistic path to them from an arbitrary VTL
+# object graph is via java.lang.Class or a ClassLoader, both already fully blocked above.
+
+introspector.restrict.packages = java.lang.invoke
+introspector.restrict.packages = javax.script
+introspector.restrict.packages = java.nio.file
+introspector.restrict.classes = java.io.File
+
# ----------------------------------------------------------------------------
# SPACE GOBBLING
# ----------------------------------------------------------------------------
diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/BoundedWriterTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/BoundedWriterTest.java
new file mode 100644
index 0000000000..e1d0b9648a
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/BoundedWriterTest.java
@@ -0,0 +1,68 @@
+package io.github.hectorvent.floci.services.apigateway;
+
+import org.junit.jupiter.api.Test;
+
+import java.io.StringWriter;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+/**
+ * Isolated, non-CDI unit tests for {@link BoundedWriter}, exercising writes under, at, and over
+ * the configured character budget directly, without going through Velocity or Quarkus.
+ */
+class BoundedWriterTest {
+
+ @Test
+ void write_underLimit_passesThroughToDelegate() throws Exception {
+ StringWriter delegate = new StringWriter();
+ BoundedWriter writer = new BoundedWriter(delegate, 10);
+
+ writer.write("hello".toCharArray(), 0, 5);
+
+ assertEquals("hello", delegate.toString());
+ }
+
+ @Test
+ void write_exactlyAtLimit_isAllowed() throws Exception {
+ StringWriter delegate = new StringWriter();
+ BoundedWriter writer = new BoundedWriter(delegate, 5);
+
+ writer.write("hello".toCharArray(), 0, 5);
+
+ assertEquals("hello", delegate.toString());
+ }
+
+ @Test
+ void write_overLimit_throwsAndDoesNotWriteToDelegate() {
+ StringWriter delegate = new StringWriter();
+ BoundedWriter writer = new BoundedWriter(delegate, 4);
+
+ assertThrows(VtlLimitExceededException.class, () -> writer.write("hello".toCharArray(), 0, 5));
+ assertEquals("", delegate.toString(), "a write that exceeds the budget must not partially land");
+ }
+
+ @Test
+ void write_multipleWritesAccumulate_andTripOnceTotalExceedsLimit() throws Exception {
+ StringWriter delegate = new StringWriter();
+ BoundedWriter writer = new BoundedWriter(delegate, 6);
+
+ writer.write("abc".toCharArray(), 0, 3);
+ writer.write("def".toCharArray(), 0, 3);
+ assertEquals("abcdef", delegate.toString());
+
+ assertThrows(VtlLimitExceededException.class, () -> writer.write("g".toCharArray(), 0, 1));
+ }
+
+ @Test
+ void flushAndClose_delegateToUnderlyingWriter() throws Exception {
+ StringWriter delegate = new StringWriter();
+ BoundedWriter writer = new BoundedWriter(delegate, 10);
+
+ writer.write("ok".toCharArray(), 0, 2);
+ writer.flush();
+ writer.close();
+
+ assertEquals("ok", delegate.toString());
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/SandboxedForeachTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/SandboxedForeachTest.java
new file mode 100644
index 0000000000..e96cb11239
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/SandboxedForeachTest.java
@@ -0,0 +1,90 @@
+package io.github.hectorvent.floci.services.apigateway;
+
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.runtime.RuntimeConstants;
+import org.junit.jupiter.api.Test;
+
+import java.io.StringWriter;
+import java.time.Duration;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Isolated, non-CDI test proving that {@link VtlSandbox#installSandboxedForeach(VelocityEngine)}
+ * actually wires {@link SandboxedForeach}'s per-iteration {@link VtlExecutionGuard#checkDeadline()}
+ * call into a real {@code #foreach} render.
+ *
+ * This is deliberately distinct from the loop-count cap test in {@code VtlTemplateEngineTest}
+ * (which only proves Velocity's own native {@code directive.foreach.max_loops}) and from
+ * {@link BoundedWriterTest} (which only proves the output-size cap): the template below never
+ * writes its growing {@code $s} to the output writer at all, so a failure here can only come from
+ * the execution-time deadline tripping inside the loop body, which is exactly the "runaway
+ * {@code #set} string doubling that never reaches the output writer" scenario the deadline check
+ * exists to catch.
+ */
+class SandboxedForeachTest {
+
+ private VelocityEngine sandboxedEngine() {
+ VelocityEngine engine = new VelocityEngine();
+ engine.setProperty(RuntimeConstants.RESOURCE_LOADERS, "string");
+ engine.setProperty("resource.loader.string.class",
+ "org.apache.velocity.runtime.resource.loader.StringResourceLoader");
+ VtlSandbox.restrictIntrospection(engine);
+ engine.init();
+ VtlSandbox.installSandboxedForeach(engine);
+ return engine;
+ }
+
+ @Test
+ void runawayStringDoublingNeverWrittenToOutput_isStoppedByDeadline_notByOutputCap() {
+ VelocityEngine engine = sandboxedEngine();
+ // Doubling a 2-character seed 40 times would reach roughly 2 billion characters, well
+ // past what any sane loop-count cap (10,000) would prevent, and $s is never written to
+ // the output writer, so BoundedWriter cannot be what stops this either.
+ String template = "#set($s = \"xy\")#foreach($i in [1..40])#set($s = \"$s$s\")#end";
+ StringWriter writer = new StringWriter();
+
+ VtlExecutionGuard.begin(Duration.ofMillis(20));
+ try {
+ RuntimeException ex = assertThrows(RuntimeException.class,
+ () -> engine.evaluate(new VelocityContext(), writer, "runaway-doubling", template));
+ assertTrue(causedByDeadline(ex),
+ "expected the failure to be caused by VtlLimitExceededException (the deadline check), but was: "
+ + ex);
+ } finally {
+ VtlExecutionGuard.end();
+ }
+
+ assertEquals("", writer.toString(),
+ "nothing should have reached the output writer before the deadline tripped inside the loop");
+ }
+
+ @Test
+ void shortLoopWithinDeadline_completesNormally() {
+ VelocityEngine engine = sandboxedEngine();
+ String template = "#foreach($i in [1..5])x#end";
+ StringWriter writer = new StringWriter();
+
+ VtlExecutionGuard.begin(Duration.ofSeconds(30));
+ try {
+ engine.evaluate(new VelocityContext(), writer, "short-loop", template);
+ } finally {
+ VtlExecutionGuard.end();
+ }
+
+ assertEquals("xxxxx", writer.toString());
+ }
+
+ private static boolean causedByDeadline(Throwable t) {
+ while (t != null) {
+ if (t instanceof VtlLimitExceededException) {
+ return true;
+ }
+ t = t.getCause();
+ }
+ return false;
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/VtlExecutionGuardTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/VtlExecutionGuardTest.java
new file mode 100644
index 0000000000..525a7f4d12
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/VtlExecutionGuardTest.java
@@ -0,0 +1,67 @@
+package io.github.hectorvent.floci.services.apigateway;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import java.time.Duration;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+/**
+ * Isolated, non-CDI unit tests for {@link VtlExecutionGuard}, run directly against the thread's
+ * deadline state without going through Velocity or Quarkus, for fast deterministic coverage of
+ * the execution-time deadline mechanism itself.
+ */
+class VtlExecutionGuardTest {
+
+ @AfterEach
+ void clearAnyLeftoverDeadline() {
+ // Guard against a failed assertion mid-test leaking a deadline into later tests on the
+ // same thread (JUnit5 reuses threads across test methods by default).
+ VtlExecutionGuard.end();
+ }
+
+ @Test
+ void checkDeadline_withNoActiveDeadline_doesNothing() {
+ assertDoesNotThrow(VtlExecutionGuard::checkDeadline);
+ }
+
+ @Test
+ void checkDeadline_beforeDeadlinePasses_doesNothing() throws InterruptedException {
+ VtlExecutionGuard.begin(Duration.ofSeconds(30));
+ assertDoesNotThrow(VtlExecutionGuard::checkDeadline);
+ }
+
+ @Test
+ void checkDeadline_afterDeadlinePasses_throws() throws InterruptedException {
+ VtlExecutionGuard.begin(Duration.ofMillis(1));
+ Thread.sleep(20);
+ VtlLimitExceededException ex =
+ assertThrows(VtlLimitExceededException.class, VtlExecutionGuard::checkDeadline);
+ assertNotNullMessage(ex);
+ }
+
+ @Test
+ void end_clearsDeadline_soCheckDeadlineNoLongerThrows() throws InterruptedException {
+ VtlExecutionGuard.begin(Duration.ofMillis(1));
+ Thread.sleep(20);
+ VtlExecutionGuard.end();
+ assertDoesNotThrow(VtlExecutionGuard::checkDeadline);
+ }
+
+ @Test
+ void begin_replacesAnyPreviousDeadline() throws InterruptedException {
+ VtlExecutionGuard.begin(Duration.ofMillis(1));
+ Thread.sleep(20);
+ // A fresh begin() must reset the deadline, not extend the expired one.
+ VtlExecutionGuard.begin(Duration.ofSeconds(30));
+ assertDoesNotThrow(VtlExecutionGuard::checkDeadline);
+ }
+
+ private static void assertNotNullMessage(VtlLimitExceededException ex) {
+ if (ex.getMessage() == null || ex.getMessage().isBlank()) {
+ throw new AssertionError("expected a descriptive message on VtlLimitExceededException");
+ }
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/VtlTemplateEngineTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/VtlTemplateEngineTest.java
index 1ee6da07ce..9594002aee 100644
--- a/src/test/java/io/github/hectorvent/floci/services/apigateway/VtlTemplateEngineTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/VtlTemplateEngineTest.java
@@ -485,4 +485,77 @@ class VtlTemplateEngineTest {
assertNull(result.statusOverride());
assertTrue(result.headerOverrides().isEmpty());
}
+
+ // ────────── Sandbox: reflection escapes must be blocked ──────────
+
+ @Test
+ void security_getClassLoaderIsNotReachable() {
+ // When SecureUberspector blocks a method call, Velocity (in non-strict mode, the default
+ // here) renders the literal, unresolved reference text instead of throwing or evaluating
+ // it. Getting the raw template text back verbatim (rather than an actual ClassLoader
+ // instance's toString, e.g. "...ClassLoader@