Last active
February 17, 2021 12:36
-
-
Save artem-smotrakov/aadd6b7d91bfa330fdafbab3ea22e87d to your computer and use it in GitHub Desktop.
Run JEXL expressions in a sandbox implemented with JexlUberspect
This file contains hidden or 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
| void runJexl(String jexlExpr) { | |
| JexlUberspect sandbox = new JexlUberspectSandbox(); | |
| JexlEngine jexl = new JexlBuilder().uberspect(sandbox).create(); | |
| JexlExpression expression = jexl.createExpression(jexlExpr); | |
| JexlContext context = new MapContext(); | |
| expression.evaluate(context); | |
| } | |
| private static class JexlUberspectSandbox implements JexlUberspect { | |
| private final List<String> allowedClasses = | |
| Arrays.asList("java.lang.Math", "java.util.Random"); | |
| private final JexlUberspect uberspect = new JexlBuilder().create().getUberspect(); | |
| private void checkAccess(String className) { | |
| if (!allowedClasses.contains(className)) { | |
| throw new AccessControlException("Not allowed"); | |
| } | |
| } | |
| @Override | |
| public JexlMethod getMethod(Object obj, String method, Object... args) { | |
| checkAccess(obj.getClass().getCanonicalName()); | |
| return uberspect.getMethod(obj, method, args); | |
| } | |
| // Implement other methods from JexlUberspect | |
| // Don't forget to call checkAccess() method |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment