Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:APIJSON APIJSON Function Sandboxing

From Leeroopedia
Knowledge Sources
Domains Script_Execution, Security, Sandboxing
Last Updated 2026-10-11 00:00 GMT

Overview

Nashorn ClassFilter-based script sandboxing that blocks all Java class access from remote script functions by default, preventing script injection attacks while allowing controlled class whitelisting via subclass override.

Description

APIJSON's JSR223ScriptExecutor executes server-side scripts (JavaScript, Lua, Python) that are loaded from the Script database table. Without sandboxing, a malicious script could access java.lang.Runtime, java.io.File, or other dangerous Java classes, leading to arbitrary code execution on the server.

The sandboxing mechanism works via Nashorn's ClassFilter interface. The createScriptEngine() method uses reflection to instantiate NashornScriptEngineFactory with a ClassFilter proxy that calls isClassExposureAllowed(className). The default implementation returns false for all class names, meaning scripts cannot import, access, or instantiate any Java class.

To allow specific classes, a subclass overrides isClassExposureAllowed():

@Override
protected boolean isClassExposureAllowed(String className) {
    return "java.lang.Math".equals(className);
}

Key safety properties:

  • Deny-by-default: All Java class access is blocked unless explicitly whitelisted.
  • Reflection-based: Uses Proxy.newProxyInstance to implement ClassFilter without a compile-time dependency on Nashorn (which is removed in JDK 14+).
  • Graceful fallback: If Nashorn classes are not found (JDK 14+), it falls back to ScriptEngineManager.getEngineByName() — at which point sandboxing is the external engine's responsibility.

Usage

Use this heuristic when:

  • Enabling ENABLE_SCRIPT_FUNCTION = true in production
  • Allowing user-uploaded scripts to execute on the server
  • Deciding which Java classes to whitelist for script access
  • Debugging script execution failures related to class access restrictions

The Insight (Rule of Thumb)

  • Action: Keep isClassExposureAllowed() returning false for all classes in production. Only override to whitelist specific, safe classes.
  • Value: Default: deny all. Whitelist example: java.lang.Math, java.util.Date.
  • Trade-off: Tighter sandbox = safer but less functional; looser sandbox = more capable but higher attack surface.
  • Compatibility: On JDK 14+ without Nashorn, the ClassFilter is not applied; use external engine sandboxing (e.g., GraalVM polyglot access controls).

Reasoning

Script injection is a critical security risk: if a client can submit JavaScript that runs on the server, they can potentially execute arbitrary code. Nashorn's ClassFilter is the primary defense — it intercepts every attempt to access a Java class from script code (Java.type(...), importPackage(...), etc.) and rejects it if the filter returns false.

The deny-by-default approach follows the principle of least privilege: scripts can compute values using pure JavaScript (math, string manipulation, JSON processing) but cannot touch the file system, network, or process execution. The few classes that might be useful in scripts (e.g., java.util.Date for date computation) can be individually whitelisted with minimal risk.

The reflection-based proxy approach avoids a hard dependency on jdk.nashorn.api.scripting.ClassFilter (which doesn't exist on JDK 14+), making the code compile and run on any JDK version.

Code Evidence

ClassFilter proxy creation in `JSR223ScriptExecutor.java:39-46`:

Class<?> factoryClass = Class.forName("jdk.nashorn.api.scripting.NashornScriptEngineFactory");
Class<?> filterClass = Class.forName("jdk.nashorn.api.scripting.ClassFilter");
Object filter = java.lang.reflect.Proxy.newProxyInstance(
    filterClass.getClassLoader(),
    new Class<?>[]{filterClass},
    (proxy, method, methodArgs) -> isClassExposureAllowed((String) methodArgs[0]));
Object factory = factoryClass.getDeclaredConstructor().newInstance();
return (ScriptEngine) factoryClass.getMethod("getScriptEngine", filterClass).invoke(factory, filter);

Default deny-all in `JSR223ScriptExecutor.java:54-56`:

protected boolean isClassExposureAllowed(String className) {
    return false;
}

Script enablement warning in `AbstractFunctionParser.java:34-38`:

// JDK 8~13 可用自带 Nashorn 这个 js 引擎,注意配置 ClassFilter 防脚本注入攻击;
// 其它语言及 JDK 14+ 都必须依赖外部脚本引擎,注意按对应引擎说明方式防脚本注入攻击,最好是沙箱环境。
public static boolean ENABLE_SCRIPT_FUNCTION = false;

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment