Skip to content

Commit 161d924

Browse files
committed
RANGER-3959: restrict scripts from accessing some built-in methods - #2
Signed-off-by: Abhay Kulkarni <[email protected]>
1 parent 73f1a3b commit 161d924

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

agents-common/src/main/java/org/apache/ranger/plugin/policyengine/RangerRequestScriptEvaluator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public final class RangerRequestScriptEvaluator {
5555
private static final String DEFAULT_RANGER_TAG_ATTRIBUTE_DATE_FORMAT = "yyyy/MM/dd";
5656
private static final String DEFAULT_ATLAS_TAG_ATTRIBUTE_DATE_FORMAT_NAME = "ATLAS_DATE_FORMAT";
5757
private static final String DEFAULT_ATLAS_TAG_ATTRIBUTE_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
58+
private static final String SCRIPT_SAFE_PREEXEC = "exit=null;quit=null;";
5859
private static final String SCRIPT_PREEXEC = SCRIPT_VAR__CTX + "=JSON.parse(" + SCRIPT_VAR__CTX_JSON + "); J=JSON.stringify;" +
5960
SCRIPT_VAR_REQ + "=" + SCRIPT_VAR__CTX + "." + SCRIPT_FIELD_REQUEST + ";" +
6061
SCRIPT_VAR_RES + "=" + SCRIPT_VAR_REQ + "." + SCRIPT_FIELD_RESOURCE + ";" +
@@ -214,6 +215,8 @@ private Object evaluateScript(ScriptEngine scriptEngine, String script, boolean
214215
bindings.put(SCRIPT_VAR_tag, currentTag);
215216
bindings.put(SCRIPT_VAR_tagAttr, tagAttribs);
216217

218+
script = SCRIPT_SAFE_PREEXEC + script;
219+
217220
if (enableJsonCtx) {
218221
bindings.put(SCRIPT_VAR__CTX_JSON, this.toJson());
219222

agents-common/src/main/java/org/apache/ranger/plugin/util/ScriptEngineUtil.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
public class ScriptEngineUtil {
3333
private static final Logger LOG = LoggerFactory.getLogger(RangerScriptConditionEvaluator.class);
3434

35-
private static final String[] SCRIPT_ENGINE_ARGS = new String[0];
35+
private static final String[] SCRIPT_ENGINE_ARGS = new String[] { "--no-java", "--no-syntax-extensions" };
3636

3737
// for backward compatibility with any plugin that might use this API
3838
public static ScriptEngine createScriptEngine(String engineName, String serviceType) {
@@ -86,11 +86,11 @@ private static ScriptEngine getScriptEngine(ClassLoader clsLoader) {
8686
try {
8787
final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
8888

89-
if (clsLoader != null) {
90-
ret = factory.getScriptEngine(SCRIPT_ENGINE_ARGS, clsLoader, RangerClassFilter.INSTANCE);
91-
} else {
92-
ret = factory.getScriptEngine(RangerClassFilter.INSTANCE);
89+
if (clsLoader == null) {
90+
clsLoader = Thread.currentThread().getContextClassLoader();
9391
}
92+
93+
ret = factory.getScriptEngine(SCRIPT_ENGINE_ARGS, clsLoader, RangerClassFilter.INSTANCE);
9494
} catch (Throwable t) {
9595
if (LOG.isDebugEnabled()) {
9696
LOG.debug("ScriptEngineUtil.getScriptEngine(clsLoader={}): failed", clsLoader, t);

0 commit comments

Comments
 (0)