# Java Javascript Security

> Best practices for securing JavaScript execution in Java applications using ScriptEngines like Rhino or Nashorn.

- Skill: `cxcscmu/java-javascript-security` (Agent Skill)
- Install (CLI): `npx get-skill add cxcscmu/java-javascript-security`
- Raw SKILL.md: https://api.skillmd.com/api/skills/cxcscmu/java-javascript-security/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Security
- Author: cxcscmu (https://skillmd.com/u/cxcscmu)
- Updated: 2026-11-24
- Page: https://skillmd.com/skills/cxcscmu/java-javascript-security

---


# Java JavaScript Security

When executing JavaScript in a Java application, it is crucial to restrict the capabilities of the script to prevent Arbitrary Code Execution (ACE).

## Vulnerability: Access to Java Classes

By default, many JavaScript engines (like Rhino, which Druid uses) allow the script to access Java classes.

```javascript
var Runtime = java.lang.Runtime;
Runtime.getRuntime().exec("rm -rf /");
```

## Mitigation: Disabling JavaScript if not needed

The best defense is to disable JavaScript execution entirely if it is not required. Apache Druid provides a configuration `druid.javascript.enabled` to control this.

## Mitigation: Sandboxing

If JavaScript must be used, it should be sandboxed.

### Rhino Sandboxing

In Rhino, you can use a `ClassShutter` to restrict which Java classes the script can access.

```java
Context cx = Context.enter();
try {
    cx.setClassShutter(new ClassShutter() {
        @Override
        public boolean visibleToScripts(String fullClassName) {
            return fullClassName.startsWith("org.mycompany.safe.");
        }
    });
    // ...
} finally {
    Context.exit();
}
```

## Mitigation: Input Validation

Always validate the JSON structure and ensure that security-critical configurations (like whether JS is enabled) cannot be overridden by user input. In Druid, this often involves ensuring `@JacksonInject` values are not overridable by JSON properties.

