Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add security to scripts by whitelisting the imports #220

Merged
merged 1 commit into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.atpfivt.jsyntrax.Configuration
import org.atpfivt.jsyntrax.units.Unit
import org.codehaus.groovy.control.CompilationFailedException
import org.codehaus.groovy.control.CompilerConfiguration
import org.codehaus.groovy.control.customizers.SecureASTCustomizer

class Parser {

Expand All @@ -13,8 +14,15 @@ class Parser {

Parser(String scriptText) throws CompilationFailedException {
this.scriptText = scriptText
final SecureASTCustomizer secure = new SecureASTCustomizer()
secure.with {
indirectImportCheckEnabled = true

importsWhitelist = ['java.lang.Object']
}
def config = new CompilerConfiguration()
config.setScriptBaseClass(SyntraxScript.class.name)
config.addCompilationCustomizers(secure)
def sharedData = new Binding()
def shell = new GroovyShell(this.class.classLoader, sharedData, config)
shell.setVariable("None", null)
Expand Down
19 changes: 19 additions & 0 deletions jsyntrax/src/test/java/org/atpfivt/jsyntrax/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,24 @@ void testPngByDefault() throws IOException, URISyntaxException {
}
}

@ParameterizedTest
@ValueSource(strings = {
"sysexit.spec",
"sysexitreflection.spec"})
void testSysexitNotAllowed(String filename) throws URISyntaxException, IOException {
Path inputPath = Paths.get(MainTest.class.getResource(filename).toURI());
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
PrintStream originalOut = System.out;
try {
System.setOut(new PrintStream(outContent));
Main.main(inputPath.toString());
assertTrue(outContent.toString().contains("Something is wrong with input script"));
} finally {
System.setOut(originalOut);
System.out.println(outContent);
}
}


private static void validatePng(byte[] png) {
//check for png header
Expand All @@ -137,6 +155,7 @@ private static void validatePng(byte[] png) {
private static void validateSVG(String svg) {
assertTrue(svg.startsWith("<?xml"));
assertTrue(svg.contains("JSYNTRAX"));
assertTrue(svg.contains("diagram title"));
assertTrue(svg.contains("href=\"https://github.com/atp-mipt/jsyntrax\""));
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
title('diagram title')
jsyntrax(optx('JSYNTRAX'), ['JSYNTRAX': 'https://github.com/atp-mipt/jsyntrax'])

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
System.exit(42)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def systemClass = Class.forName("java.lang.System")
def exitMethod = systemClass.getDeclaredMethod("exit", Integer.TYPE)
exitMethod.invoke(null, 0)
Loading