Skip to content

Instantly share code, notes, and snippets.

@artem-smotrakov
Last active April 14, 2021 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artem-smotrakov/e2a76babb90de87d3646b3a501521ed3 to your computer and use it in GitHub Desktop.
Save artem-smotrakov/e2a76babb90de87d3646b3a501521ed3 to your computer and use it in GitHub Desktop.
Restricting Jakarta EL expressions to avoid RCE
void handle(HttpRequest request) {
if (request.hasParameter("questionToBackend")) {
String input = request.getParameter("questionToBackend"));
String pattern = "(inside|outside)\\.(temperature|humidity)";
if (!input.matches(pattern)) {
throw new IllegalArgumentException("Unexpected expression");
}
String expression = "${" + input + "}";
ExpressionFactory factory = new de.odysseus.el.ExpressionFactoryImpl();
ELContext context = new de.odysseus.el.util.SimpleContext();
ValueExpression e = factory.createValueExpression(context, expression, Object.class);
Object object = e.getValue(context);
handleResult(object);
} else {
callNextHandler(request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment