1
1
package io .github .cottonmc .parchment .impl ;
2
2
3
+ import javax .script .ScriptContext ;
3
4
import javax .script .ScriptEngine ;
4
5
import javax .script .ScriptEngineFactory ;
5
6
@@ -17,7 +18,25 @@ public Class<? extends ScriptEngineFactory> getEngineFactory() {
17
18
//necessary thanks to Nashorn's *incredible* compliance with JSR223 /s
18
19
@ Override
19
20
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 ;
22
41
}
23
42
}
0 commit comments