-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Protect against RegEx attacks in santising script input (#139)
* Adding test to assert system environment * Setting Java max memory for tests * Fixing java max memory allocations * Fixing upper memory limit * Adding comment to unit test * Adding protecting against regex attack * Increasing max operations on regex
- Loading branch information
Showing
5 changed files
with
101 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/delight/nashornsandbox/internal/SecureInterruptibleCharSequence.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package delight.nashornsandbox.internal; | ||
|
||
import delight.nashornsandbox.exceptions.ScriptCPUAbuseException; | ||
|
||
public class SecureInterruptibleCharSequence implements CharSequence { | ||
final CharSequence inner; | ||
final ThreadLocal<Integer> matchCount; | ||
|
||
public SecureInterruptibleCharSequence(CharSequence inner, ThreadLocal<Integer> matchCount) { | ||
super(); | ||
this.inner = inner; | ||
this.matchCount = matchCount; | ||
} | ||
|
||
@Override | ||
public char charAt(int index) { | ||
matchCount.set(matchCount.get()+1); | ||
if (matchCount.get() > 5000000){ | ||
Thread thread = Thread.currentThread(); | ||
thread.interrupt(); | ||
} | ||
if (Thread.currentThread().isInterrupted()) { | ||
throw new ScriptCPUAbuseException("Regular expression running for too many iterations.", true, null); | ||
} | ||
return inner.charAt(index); | ||
} | ||
|
||
@Override | ||
public int length() { | ||
return inner.length(); | ||
} | ||
|
||
@Override | ||
public CharSequence subSequence(int start, int end) { | ||
return new SecureInterruptibleCharSequence(inner.subSequence(start, end), matchCount); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return inner.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package delight.nashornsandbox; | ||
|
||
import javax.script.ScriptException; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import com.mifmif.common.regex.Generex; | ||
|
||
import delight.nashornsandbox.exceptions.ScriptCPUAbuseException; | ||
|
||
public class TestIssue117 { | ||
|
||
public static String getMatchStr(String regex, int repl) { | ||
Generex generex = new Generex(regex); | ||
|
||
String randomStr = generex.random(); | ||
StringBuffer sb = new StringBuffer(); | ||
sb.append(randomStr); | ||
sb.append(";\n"); | ||
for (int i = 0; i < repl; i++) { | ||
sb.append("/"); | ||
} | ||
|
||
return sb.toString(); | ||
} | ||
|
||
@Test | ||
public void test() throws ScriptCPUAbuseException, ScriptException, NoSuchMethodException { | ||
|
||
NashornSandbox sandbox = NashornSandboxes.create(); | ||
|
||
for (int i = 480; i <= 500; i++) { | ||
long startTime = System.currentTimeMillis(); | ||
String js_script = getMatchStr("(([^;]+;){9}[^;]+)", i); | ||
try { | ||
sandbox.eval(js_script); | ||
} catch (Exception e) { | ||
} | ||
long endTime = System.currentTimeMillis(); | ||
long costTime = endTime - startTime; | ||
Assert.assertTrue("RegEx attack successful. Took longer than 5000 ms to resolve script. Time required: "+costTime, costTime <= 5000); | ||
} | ||
} | ||
} |