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

issue #74 PoisonPill regex fix #82

Merged
merged 1 commit into from
Apr 12, 2019
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 @@ -72,7 +72,7 @@ private static class PoisonPil {

private final static List<PoisonPil> POISON_PILLS = Arrays.asList(
// every 10th statements ended with semicolon put interrupt checking function
new PoisonPil(Pattern.compile("(([^;]+;){9}[^;]+(?<!break|continue);)\\n"),
new PoisonPil(Pattern.compile("(([^;]+;){9}[^;]+(?<!break|continue);\\n(?![\\W]*(\\/\\/.+[\\W]+)*else))"),
JS_INTERRUPTED_FUNCTION + "();\n"),
// every (except switch) block start brace put interrupt checking function
new PoisonPil(Pattern.compile("(\\s*for\\s*\\([^\\{]+\\)\\s*\\{)"), JS_INTERRUPTED_FUNCTION + "();"), // for
Expand Down
34 changes: 31 additions & 3 deletions src/test/java/delight/nashornsandbox/internal/JsSanitizerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,19 @@ public void testSecureJs_10statment() throws Exception {
final String js1 = "var i=0;\nvar i=0;\nvar i=0;\nvar i=0;\nvar i=0;\n"
+ "var i=0;\nvar i=0;\nvar i=0;\nvar i=0;\nvar i=10;\nvar i=11;";
final String bjs1 = jsSanitizer.secureJs(js1);
assertTrue(bjs1.contains("var i = 10;" + JsSanitizer.JS_INTERRUPTED_FUNCTION));
assertTrue(bjs1.contains("var i = 10;\n" + JsSanitizer.JS_INTERRUPTED_FUNCTION));
final String js2 = "var i=0;\nvar i=0;\nvar i=0;\nvar i=0;\nvar i=0;\n"
+ "var i=0;\nvar i=0;\nvar i=0;\nvar i=0;\n" + "for(var i=0; i<10; i++) {i--}" + "var i=10;\nvar i=11;";
final String bjs2 = jsSanitizer.secureJs(js2);
assertTrue(bjs2.contains("var i = 10;" + JsSanitizer.JS_INTERRUPTED_FUNCTION));
assertTrue(bjs2.contains("var i = 10;\n" + JsSanitizer.JS_INTERRUPTED_FUNCTION));
}

@Test
public void testSecureJs_10statment_break() throws Exception {
final String js1 = "var i=0;\nvar i=0;\nvar i=0;\nvar i=0;\nvar i=0;\n"
+ "var i=0;\nvar i=0;\nvar i=0;\nvar i=0;\nvar i=10;\nvar i=11;";
final String bjs1 = jsSanitizer.secureJs(js1);
assertTrue(bjs1.contains("var i = 10;" + JsSanitizer.JS_INTERRUPTED_FUNCTION));
assertTrue(bjs1.contains("var i = 10;\n" + JsSanitizer.JS_INTERRUPTED_FUNCTION));
final String js2 = "var i=0;\nvar i=0;\nvar i=0;\nvar i=0;\nvar i=0;\n"
+ "var i=0;\nvar i=0;\nvar i=0;\nvar i=0;\n" + "for(var i=0; i<10; i++) {break; continue; i--}\n"
+ "var i=10;\nvar i=11;";
Expand All @@ -87,6 +87,34 @@ public void testSecureJs_10statment_break() throws Exception {
assertFalse(bjs2.contains("continue;" + JsSanitizer.JS_INTERRUPTED_FUNCTION));
}

@Test
public void testSecureJs_10statment_elseif_comments() throws Exception {
final String js1 = "function FindProxyForURL(url, host)\n{\nvar i=1;\nvar i=2;\nvar i=3;\n"
+ "var i=4;\nvar i=5;\nvar i=6;\nvar i=7;\nif (dnsDomainIs(host, \"proxy8.com.net\"))\n"
+ "return \"PROXY http://proxy8.acme.com:8080\";\nelse if (dnsDomainIs(host, \"acme1.com.net\"))\n"
+ "return \"PROXY http://proxy9.acme.com:8080\";\nelse if (dnsDomainIs(host, \"initech.acme.com\"))\n"
+ "// would break if inserted interrupt function\nreturn \"PROXY http://acme10.com:8080\";\n"
+ "else if (dnsDomainIs(host, \"whymper.net\"))\nreturn \"PROXY http://acme2.com:8080\";\n"
+ "else if (dnsDomainIs(host, \"enough.acme.com\"))\nreturn \"PROXY http://one.proxy.acme.com:8080\";\n"
+ "else\nreturn \"DIRECT\";\n}"
;
final String bjs = jsSanitizer.secureJs(js1);
assertFalse(bjs.contains("\"PROXY http://acme10.com:8080\";\n" + JsSanitizer.JS_INTERRUPTED_FUNCTION));
assertTrue(bjs.contains("\"DIRECT\";\n" + JsSanitizer.JS_INTERRUPTED_FUNCTION));
final String js12 = "function FindProxyForURL(url, host)\n{\nvar i=1;\nvar i=2;\nvar i=3;\n"
+ "var i=4;\nvar i=5;\nvar i=6;\nvar i=7;\nif (dnsDomainIs(host, \"proxy8.com.net\"))\n"
+ "return \"PROXY http://proxy8.acme.com:8080\";\nelse if (dnsDomainIs(host, \"acme1.com.net\"))\n"
+ "return \"PROXY http://proxy9.acme.com:8080\";\nelse if (dnsDomainIs(host, \"skynet.acme.com\"))\n"
+ " // would break if inserted interrupt function\n return \"PROXY http://acme10.com:8080\";\n"
+ "else if (dnsDomainIs(host, \"whymper.net\"))\nreturn \"PROXY http://acme2.com:8080\";\n"
+ "else if (dnsDomainIs(host, \"enough.acme.com\"))\nreturn \"PROXY http://one.proxy.acme.com:8080\";\n"
+ "else\nreturn \"DIRECT\";\n}"
;
final String bjs2 = jsSanitizer.secureJs(js12);
assertFalse(bjs2.contains("\"PROXY http://acme10.com:8080\";\n" + JsSanitizer.JS_INTERRUPTED_FUNCTION));
assertTrue(bjs2.contains("\"DIRECT\";\n" + JsSanitizer.JS_INTERRUPTED_FUNCTION));
}

@Test
public void testSecureJs_simple() throws Exception {
final String js1 = "sum(a) + avg(b)";
Expand Down