Skip to content

Commit 9e06681

Browse files
committedAug 19, 2020
*looks at nashorn builtins* good lord what is happening in there
1 parent df7a24c commit 9e06681

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed
 

‎src/main/java/io/github/cottonmc/parchment/impl/NashornScriptInitializer.java

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.cottonmc.parchment.impl;
22

3+
import javax.script.ScriptContext;
34
import javax.script.ScriptEngine;
45
import javax.script.ScriptEngineFactory;
56

@@ -17,7 +18,25 @@ public Class<? extends ScriptEngineFactory> getEngineFactory() {
1718
//necessary thanks to Nashorn's *incredible* compliance with JSR223 /s
1819
@Override
1920
public ScriptEngine initialize(ScriptEngine engine) {
20-
return NASHORN_FACTORY.getScriptEngine(name -> !name.startsWith("java.io") && !name.startsWith("java.nio")
21-
&& !name.startsWith("java.net"));
21+
//lock off access to IO, NIO, and networking from scripts
22+
ScriptEngine newEngine = NASHORN_FACTORY.getScriptEngine(name -> !name.startsWith("java.io") &&
23+
!name.startsWith("java.nio") && !name.startsWith("java.net"));
24+
//lock off the nashorn functions we don't want to support
25+
ScriptContext ctx = newEngine.getContext();
26+
//quits the game! no!
27+
ctx.removeAttribute("quit", ctx.getAttributesScope("quit"));
28+
ctx.removeAttribute("exit", ctx.getAttributesScope("exit"));
29+
//loads code from a mystery file or the internet! no!!!
30+
ctx.removeAttribute("load", ctx.getAttributesScope("load"));
31+
ctx.removeAttribute("loadWithNewGlobal", ctx.getAttributesScope("loadWithNewGlobal"));
32+
//reads from the console or a text file! Please do not do this!!!!!
33+
ctx.removeAttribute("readLine", ctx.getAttributesScope("readLine"));
34+
ctx.removeAttribute("readFully", ctx.getAttributesScope("readFully"));
35+
//prints to the command line on its own! technically fine but just use `log.info` please!
36+
ctx.removeAttribute("print", ctx.getAttributesScope("print"));
37+
ctx.removeAttribute("echo", ctx.getAttributesScope("echo"));
38+
//we're safe now, right?
39+
//...right?
40+
return newEngine;
2241
}
2342
}

0 commit comments

Comments
 (0)
Please sign in to comment.