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

feat(java): add eval injection rule (CWE-95) #244

Merged
merged 1 commit into from
Feb 9, 2024
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
39 changes: 39 additions & 0 deletions rules/java/lang/eval_using_user_input.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
imports:
- java_shared_lang_instance
- java_shared_lang_user_input
patterns:
- pattern: |
$<ENGINE>.eval($<USER_INPUT>$<...>);
filters:
- variable: ENGINE
detection: java_lang_eval_using_user_input_script_engine
- variable: USER_INPUT
detection: java_shared_lang_user_input
auxiliary:
- id: java_lang_eval_using_user_input_script_engine
patterns:
- pattern: $<ENGINE_MANAGER>.getEngineByName();
filters:
- variable: ENGINE_MANAGER
detection: java_shared_lang_instance
scope: cursor
filters:
- variable: JAVA_SHARED_LANG_INSTANCE_TYPE
regex: \A(javax\.script\.)?(ScriptEngineManager)\z
languages:
- java
metadata:
description: "Unsanitized user input in 'eval' type function"
remediation_message: |
## Description
It is dangerous to use eval with user input, or to compile code with user-supplied data. Such practices can lead to command injection.

## Remediations
❌ Avoid using code execution methods with unsanitized user input.

## Resources
- [OWASP Code injection explained](https://owasp.org/www-community/attacks/Code_Injection)
cwe_id:
- 95
id: java_lang_eval_using_user_input
documentation_url: https://docs.bearer.com/reference/rules/java_lang_eval_using_user_input
18 changes: 18 additions & 0 deletions tests/java/lang/eval_using_user_input/test.js
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("eval_using_user_input", () => {
const testCase = "main.java"

const results = invoke(testCase)

expect(results.Missing).toEqual([])
expect(results.Extra).toEqual([])
})
})
24 changes: 24 additions & 0 deletions tests/java/lang/eval_using_user_input/testdata/main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class Foo {
public static void bad(HttpServletRequest req, HttpServletResponse res) {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");

String expression = "2 + 3 * " + req.getParameter("numUsers");

// bearer:expected java_lang_eval_using_user_input
Object result = engine.eval(expression);
}

public static void ok() {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");

String expression = "2 + 3 * 5";

Object result = engine.eval(expression);
}
}
Loading