Skip to content

Commit

Permalink
Add possibility to disable interruption calls injection
Browse files Browse the repository at this point in the history
  • Loading branch information
ark-1 committed Aug 9, 2018
1 parent 20fd81b commit 870bcbf
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
9 changes: 4 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.javadelight</groupId>
<artifactId>delight-nashorn-sandbox</artifactId>
<version>0.1.16-SNAPSHOT</version>
<version>0.1.16-SNAPSHOT-jb</version>
<description>A safe sandbox to execute JavaScript code from Nashorn.</description>
<url>https://github.com/javadelight/delight-nashorn-sandbox</url>

Expand Down Expand Up @@ -303,13 +303,12 @@

<distributionManagement>
<repository>
<id>bintray-javadelight-javadelight</id>
<name>javadelight-javadelight</name>
<url>https://api.bintray.com/maven/javadelight/javadelight/delight-nashorn-sandbox/;publish=1</url>
<id>bintray-ark1-delight-nashorn-sandbox</id>
<name>ark1-delight-nashorn-sandbox</name>
<url>https://api.bintray.com/maven/ark1/delight-nashorn-sandbox/delight-nashorn-sandbox/;publish=1</url>
</repository>
</distributionManagement>


<developers>
<developer>
<id>mxro</id>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/delight/nashornsandbox/NashornSandbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ public interface NashornSandbox {
* performed
*/
void allowNoBraces(boolean v);

void injectInterruptionCalls(boolean v);

/**
* The size of prepared statements LRU cache. Default 0 (disabled).
Expand Down
25 changes: 16 additions & 9 deletions src/main/java/delight/nashornsandbox/internal/JsSanitizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,23 @@ private static class PoisonPil {
/** <code>true</code> when lack of braces is allowed. */
private final boolean allowNoBraces;

JsSanitizer(final ScriptEngine scriptEngine, final int maxPreparedStatements, final boolean allowBraces) {
private final boolean injectInterruptionCalls;

JsSanitizer(final ScriptEngine scriptEngine, final int maxPreparedStatements, final boolean allowBraces, final boolean injectInterruptionCalls) {
this.scriptEngine = scriptEngine;
this.allowNoBraces = allowBraces;
this.securedJsCache = createSecuredJsCache(maxPreparedStatements);
assertScriptEngine();
this.injectInterruptionCalls = injectInterruptionCalls;
assertScriptEngine();
this.jsBeautify = getBeautifHandler(scriptEngine);
}

JsSanitizer(final ScriptEngine scriptEngine, final boolean allowBraces, SecuredJsCache cache) {
JsSanitizer(final ScriptEngine scriptEngine, final boolean allowBraces, SecuredJsCache cache, final boolean injectInterruptionCalls) {
this.scriptEngine = scriptEngine;
this.allowNoBraces = allowBraces;
this.securedJsCache = cache;
assertScriptEngine();
this.injectInterruptionCalls = injectInterruptionCalls;
assertScriptEngine();
this.jsBeautify = getBeautifHandler(scriptEngine);
}

Expand Down Expand Up @@ -186,24 +190,24 @@ void checkBraces(final String beautifiedJs) throws BracesException {
if (allowNoBraces) {
return;
}

for (final Pattern pattern : LACK_EXPECTED_BRACES) {
final Matcher matcher = pattern.matcher(RemoveComments.perform(beautifiedJs));
if (matcher.find()) {

String line = "";
int index = matcher.start();
while (index >= 0 && beautifiedJs.charAt(index) != '\n' ) {
line = beautifiedJs.charAt(index)+line;
index--;
}

int singleParaCount = line.length() - line.replace("'", "").length();
int doubleParaCount = line.length() - line.replace("\"", "").length();

if (singleParaCount % 2 != 0 || doubleParaCount % 2 != 0) {
// for in string

} else {
throw new BracesException("No block braces after function|for|while|do. Found ["+matcher.group()+"]");
}
Expand Down Expand Up @@ -264,6 +268,9 @@ private String secureJsImpl(final String js) throws BracesException {
checkJs(js);
final String beautifiedJs = beautifyJs(js);
checkBraces(beautifiedJs);
if (!injectInterruptionCalls) {
return beautifiedJs;
}
final String injectedJs = injectInterruptionCalls(beautifiedJs);
// if no injection, no need to add preamble
if (beautifiedJs.equals(injectedJs)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public class NashornSandboxImpl implements NashornSandbox {

protected SecuredJsCache suppliedCache;

private boolean injectInterruptionCalls = true;

public NashornSandboxImpl() {
this(new String[0]);
}
Expand Down Expand Up @@ -212,9 +214,9 @@ public void setMaxMemory(final long limit) {
JsSanitizer getSanitizer() {
if (sanitizer == null) {
if (suppliedCache == null) {
sanitizer = new JsSanitizer(scriptEngine, maxPreparedStatements, allowNoBraces);
sanitizer = new JsSanitizer(scriptEngine, maxPreparedStatements, allowNoBraces, injectInterruptionCalls);
} else {
sanitizer = new JsSanitizer(scriptEngine, allowNoBraces, suppliedCache);
sanitizer = new JsSanitizer(scriptEngine, allowNoBraces, suppliedCache, injectInterruptionCalls);
}
}
return sanitizer;
Expand Down Expand Up @@ -313,6 +315,14 @@ public void allowNoBraces(final boolean v) {
allowNoBraces = v;
}

@Override
public void injectInterruptionCalls(boolean v) {
if (injectInterruptionCalls != v) {
sanitizer = null;
}
injectInterruptionCalls = v;
}

@Override
public void setWriter(final Writer writer) {
scriptEngine.getContext().setWriter(writer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public JsSanitizerTest() {
@Before
public void setUp()
{
jsSanitizer = new JsSanitizer(scriptEngine, 0, false);
jsSanitizer = new JsSanitizer(scriptEngine, 0, false, true);
}

@Test
Expand Down

0 comments on commit 870bcbf

Please sign in to comment.