-
Notifications
You must be signed in to change notification settings - Fork 8
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
feat(java): add EL injection rule (CWE-917) #200
Merged
elsapet
merged 1 commit into
main
from
feat/java/add-expression-language-injection-rule
Jan 29, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
imports: | ||
- java_shared_lang_instance | ||
- java_shared_lang_user_input | ||
patterns: | ||
- pattern: $<EXPRESSION_FACTORY>.$<METHOD>($<EL_CONTEXT>, $<STRING_PARAM>$<...>); | ||
filters: | ||
- variable: EXPRESSION_FACTORY | ||
detection: java_shared_lang_instance | ||
scope: cursor | ||
filters: | ||
- variable: JAVA_SHARED_LANG_INSTANCE_TYPE | ||
regex: \A(javax\.el\.)?ExpressionFactory\z | ||
- variable: METHOD | ||
values: | ||
- createValueExpression | ||
- createMethodExpression | ||
- variable: EL_CONTEXT | ||
detection: java_shared_lang_instance | ||
scope: cursor | ||
filters: | ||
- variable: JAVA_SHARED_LANG_INSTANCE_TYPE | ||
regex: \A(javax\.el\.)?ELContext\z | ||
- variable: STRING_PARAM | ||
detection: java_lang_expression_language_injection_string_method_arg | ||
auxiliary: | ||
- id: java_lang_expression_language_injection_string_method_arg | ||
patterns: | ||
- pattern: | | ||
class $<...>$<_> $<...>{ | ||
$<...>$<_> $<_>($<...>$<JAVA_SHARED_LANG_INSTANCE_TYPE> $<JAVA_SHARED_LANG_INSTANCE_VARIABLE>$<...>)$<...>{} | ||
} | ||
focus: JAVA_SHARED_LANG_INSTANCE_VARIABLE | ||
filters: | ||
- variable: JAVA_SHARED_LANG_INSTANCE_TYPE | ||
regex: \A(java\.lang\.)?String\z | ||
languages: | ||
- java | ||
severity: warning | ||
metadata: | ||
description: "Possible expression language (EL) injection detected." | ||
remediation_message: | | ||
## Description | ||
Using external input when building an expression language (EL) statement can lead to EL injection attacks and the evaluation of risky code. | ||
## Remediations | ||
✅ Always validate the source of external input or dynamic values before using them to construct EL statements | ||
## Resources | ||
- [OWASP Expression Language Injection](https://owasp.org/www-community/vulnerabilities/Expression_Language_Injection) | ||
cwe_id: | ||
- 917 | ||
- 94 | ||
id: java_lang_expression_language_injection | ||
documentation_url: https://docs.bearer.com/reference/rules/java_lang_expression_language_injection |
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,18 @@ | ||
const { | ||
createNewInvoker, | ||
getEnvironment, | ||
} = require("../../../helper.js") | ||
const { ruleId, ruleFile, testBase } = getEnvironment(__dirname) | ||
|
||
describe(ruleId, () => { | ||
const invoke = createNewInvoker(ruleId, ruleFile, testBase) | ||
|
||
test("expression_language_injection", () => { | ||
const testCase = "main.java" | ||
|
||
const results = invoke(testCase) | ||
|
||
expect(results.Missing).toEqual([]) | ||
expect(results.Extra).toEqual([]) | ||
}) | ||
}) |
30 changes: 30 additions & 0 deletions
30
tests/java/lang/expression_language_injection/testdata/main.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,30 @@ | ||
import javax.el.*; | ||
|
||
public class ELInjection { | ||
|
||
public String bad(String externalInput) { | ||
ExpressionFactory expressionFactory = context.getApplication().getExpressionFactory(); | ||
ELContext elContext = context.getELContext(); | ||
|
||
// bearer:expected java_lang_expression_language_injection | ||
ValueExpression valueExp = expressionFactory.createValueExpression(elContext, externalInput, String.class); | ||
// bearer:expected java_lang_expression_language_injection | ||
ValueExpression valueExp = expressionFactory.createValueExpression(elContext, externalInput); | ||
// bearer:expected java_lang_expression_language_injection | ||
MethodExpression methodExp = expressionFactory.createMethodExpression(elContext, externalInput, String.class, null); | ||
|
||
return (String) valEx.getValue(elContext); | ||
} | ||
|
||
public MethodInfo methodExpr(String expression) { | ||
FacesContext context = FacesContext.getCurrentInstance(); | ||
ExpressionFactory expressionFactory = context.getApplication().getExpressionFactory(); | ||
ELContext elContext = context.getELContext(); | ||
|
||
// ok | ||
String safe = "hello world"; | ||
MethodExpression ex = expressionFactory.createMethodExpression(elContext, safe, String.class, null); | ||
return ex.getMethodInfo(elContext); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't use
java_shared_lang_instance
here because we only want String instances that are method arguments