-
Notifications
You must be signed in to change notification settings - Fork 0
Implement endpoints with potential tainted code execution #5
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,68 @@ | ||||||||||||||||||
| package test; | ||||||||||||||||||
|
|
||||||||||||||||||
| import org.springframework.web.bind.annotation.RequestParam; | ||||||||||||||||||
| import org.springframework.web.bind.annotation.GetMapping; | ||||||||||||||||||
|
|
||||||||||||||||||
| class Test { | ||||||||||||||||||
| @GetMapping("/api/bad1") | ||||||||||||||||||
| @ResponseBody | ||||||||||||||||||
| public String bad1(@RequestParam String input) { | ||||||||||||||||||
| ExpressionParser expressionParser = new SpelExpressionParser(); | ||||||||||||||||||
| // ruleid: spring-tainted-code-execution | ||||||||||||||||||
| Expression expression = expressionParser.parseExpression(input).getValue(); | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Semgrep identified a blocking 🔴 issue in your code: Dataflow graphflowchart LR
classDef invis fill:white, stroke: none
classDef default fill:#e7f5ff, color:#1c7fd6, stroke: none
subgraph File0["<b>vuln15.java</b>"]
direction LR
%% Source
subgraph Source
direction LR
v0["<a href=https://github.com/kyle-semgrep/bad-python-app-kyle-managed/blob/795966a770f03a0790876a78bd54c4674598810b/vuln15.java#L9 target=_blank style='text-decoration:none; color:#1c7fd6'>[Line: 9] input</a>"]
end
%% Intermediate
subgraph Traces0[Traces]
direction TB
v2["<a href=https://github.com/kyle-semgrep/bad-python-app-kyle-managed/blob/795966a770f03a0790876a78bd54c4674598810b/vuln15.java#L9 target=_blank style='text-decoration:none; color:#1c7fd6'>[Line: 9] input</a>"]
end
%% Sink
subgraph Sink
direction LR
v1["<a href=https://github.com/kyle-semgrep/bad-python-app-kyle-managed/blob/795966a770f03a0790876a78bd54c4674598810b/vuln15.java#L12 target=_blank style='text-decoration:none; color:#1c7fd6'>[Line: 12] input</a>"]
end
end
%% Class Assignment
Source:::invis
Sink:::invis
Traces0:::invis
File0:::invis
%% Connections
Source --> Traces0
Traces0 --> Sink
To resolve this comment: ✨ Commit Assistant fix suggestion
Suggested change
View step-by-step instructions
Passing untrusted user input to expression or script evaluators such as SpEL is dangerous because these APIs are designed to execute arbitrary code and can lead to severe security vulnerabilities. 💬 Ignore this findingReply with Semgrep commands to ignore this finding.
Alternatively, triage in Semgrep AppSec Platform to ignore the finding created by spring-tainted-code-execution. You can view more details about this finding in the Semgrep AppSec Platform. |
||||||||||||||||||
| String result = (String) expression.getValue(); | ||||||||||||||||||
|
Comment on lines
+9
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Method Useful? React with 👍 / 👎.
Comment on lines
+12
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In Useful? React with 👍 / 👎.
Comment on lines
+10
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In Useful? React with 👍 / 👎. |
||||||||||||||||||
| System.out.println(result); | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+13
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||
| @GetMapping("/api/bad1") | ||||||||||||||||||
| @ResponseBody | ||||||||||||||||||
| public String bad2(@RequestParam String input) { | ||||||||||||||||||
|
Comment on lines
+16
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||
| if (expression == null) { | ||||||||||||||||||
| return null; | ||||||||||||||||||
|
Comment on lines
+19
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||
| } | ||||||||||||||||||
| FacesContext context = getFacesContext(); | ||||||||||||||||||
|
Comment on lines
+18
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||
| ELContext elContext = context.getELContext(); | ||||||||||||||||||
| String expressionString = input; | ||||||||||||||||||
| ExpressionFactory factory = getExpressionFactory(); | ||||||||||||||||||
| // old syntax compatibility | ||||||||||||||||||
| // #{car[column.property]} | ||||||||||||||||||
| // new syntax is: | ||||||||||||||||||
| // #{column.property} or even a method call | ||||||||||||||||||
| if (expressionString.startsWith("#{" + getVar() + "[")) { | ||||||||||||||||||
| expressionString = expressionString.substring(expressionString.indexOf("[") + 1, expressionString.indexOf("]")); | ||||||||||||||||||
| expressionString = "#{" + expressionString + "}"; | ||||||||||||||||||
| // ruleid: spring-tainted-code-execution | ||||||||||||||||||
| ValueExpression dynaVE = factory.createValueExpression(elContext, expressionString, String.class); | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Semgrep identified a blocking 🔴 issue in your code: Dataflow graphflowchart LR
classDef invis fill:white, stroke: none
classDef default fill:#e7f5ff, color:#1c7fd6, stroke: none
subgraph File0["<b>vuln15.java</b>"]
direction LR
%% Source
subgraph Source
direction LR
v0["<a href=https://github.com/kyle-semgrep/bad-python-app-kyle-managed/blob/795966a770f03a0790876a78bd54c4674598810b/vuln15.java#L18 target=_blank style='text-decoration:none; color:#1c7fd6'>[Line: 18] input</a>"]
end
%% Intermediate
subgraph Traces0[Traces]
direction TB
v2["<a href=https://github.com/kyle-semgrep/bad-python-app-kyle-managed/blob/795966a770f03a0790876a78bd54c4674598810b/vuln15.java#L18 target=_blank style='text-decoration:none; color:#1c7fd6'>[Line: 18] input</a>"]
v3["<a href=https://github.com/kyle-semgrep/bad-python-app-kyle-managed/blob/795966a770f03a0790876a78bd54c4674598810b/vuln15.java#L24 target=_blank style='text-decoration:none; color:#1c7fd6'>[Line: 24] expressionString</a>"]
end
v2 --> v3
%% Sink
subgraph Sink
direction LR
v1["<a href=https://github.com/kyle-semgrep/bad-python-app-kyle-managed/blob/795966a770f03a0790876a78bd54c4674598810b/vuln15.java#L34 target=_blank style='text-decoration:none; color:#1c7fd6'>[Line: 34] expressionString</a>"]
end
end
%% Class Assignment
Source:::invis
Sink:::invis
Traces0:::invis
File0:::invis
%% Connections
Source --> Traces0
Traces0 --> Sink
To resolve this comment: ✨ Commit Assistant fix suggestion
Suggested change
View step-by-step instructions
Expression injection enables attackers to execute arbitrary code, so any user input included in expressions must be validated and restricted to known-safe values. 💬 Ignore this findingReply with Semgrep commands to ignore this finding.
Alternatively, triage in Semgrep AppSec Platform to ignore the finding created by spring-tainted-code-execution. You can view more details about this finding in the Semgrep AppSec Platform. |
||||||||||||||||||
| return (String) dynaVE.getValue(elContext); | ||||||||||||||||||
| } | ||||||||||||||||||
| return (String) expression.getValue(elContext); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| @GetMapping("/api/bad3") | ||||||||||||||||||
| @ResponseBody | ||||||||||||||||||
| public String bad3(Model model, @RequestParam String input) { | ||||||||||||||||||
| ScriptEngineManager mgr = new ScriptEngineManager(); | ||||||||||||||||||
| ScriptEngine engine = mgr.getEngineByName("JavaScript"); | ||||||||||||||||||
| String foo = "40+"+input; | ||||||||||||||||||
| // ruleid: spring-tainted-code-execution | ||||||||||||||||||
| System.out.println(engine.eval(foo)); | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Semgrep identified a blocking 🔴 issue in your code: Dataflow graphflowchart LR
classDef invis fill:white, stroke: none
classDef default fill:#e7f5ff, color:#1c7fd6, stroke: none
subgraph File0["<b>vuln15.java</b>"]
direction LR
%% Source
subgraph Source
direction LR
v0["<a href=https://github.com/kyle-semgrep/bad-python-app-kyle-managed/blob/795966a770f03a0790876a78bd54c4674598810b/vuln15.java#L42 target=_blank style='text-decoration:none; color:#1c7fd6'>[Line: 42] input</a>"]
end
%% Intermediate
subgraph Traces0[Traces]
direction TB
v2["<a href=https://github.com/kyle-semgrep/bad-python-app-kyle-managed/blob/795966a770f03a0790876a78bd54c4674598810b/vuln15.java#L42 target=_blank style='text-decoration:none; color:#1c7fd6'>[Line: 42] input</a>"]
v3["<a href=https://github.com/kyle-semgrep/bad-python-app-kyle-managed/blob/795966a770f03a0790876a78bd54c4674598810b/vuln15.java#L45 target=_blank style='text-decoration:none; color:#1c7fd6'>[Line: 45] foo</a>"]
end
v2 --> v3
%% Sink
subgraph Sink
direction LR
v1["<a href=https://github.com/kyle-semgrep/bad-python-app-kyle-managed/blob/795966a770f03a0790876a78bd54c4674598810b/vuln15.java#L47 target=_blank style='text-decoration:none; color:#1c7fd6'>[Line: 47] foo</a>"]
end
end
%% Class Assignment
Source:::invis
Sink:::invis
Traces0:::invis
File0:::invis
%% Connections
Source --> Traces0
Traces0 --> Sink
To resolve this comment: ✨ Commit Assistant fix suggestion
Suggested change
View step-by-step instructions
Allowing user input to be executed as code is highly dangerous and can allow attackers to run arbitrary commands on the server. Use only strict, whitelisted data and avoid dynamic code execution wherever possible. 💬 Ignore this findingReply with Semgrep commands to ignore this finding.
Alternatively, triage in Semgrep AppSec Platform to ignore the finding created by spring-tainted-code-execution. You can view more details about this finding in the Semgrep AppSec Platform. |
||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+42
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||
|
|
||||||||||||||||||
| @GetMapping("/api/ok1") | ||||||||||||||||||
| @ResponseBody | ||||||||||||||||||
| public String ok1(Model model, @RequestParam String input) { | ||||||||||||||||||
| ScriptEngineManager mgr = new ScriptEngineManager(); | ||||||||||||||||||
| ScriptEngine engine = mgr.getEngineByName("JavaScript"); | ||||||||||||||||||
| String foo = "40+3"; | ||||||||||||||||||
| // ok: spring-tainted-code-execution | ||||||||||||||||||
| System.out.println(engine.eval(foo)); | ||||||||||||||||||
|
Comment on lines
+52
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| @SuppressWarnings("unchecked") // Forced cast on T | ||||||||||||||||||
| @Override | ||||||||||||||||||
| public <T> T evaluateExpressionGet(FacesContext context, String expression, Class<? extends T> expectedType) | ||||||||||||||||||
| throws ELException { | ||||||||||||||||||
|
Comment on lines
+60
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The Useful? React with 👍 / 👎. |
||||||||||||||||||
| // ok: spring-tainted-code-execution | ||||||||||||||||||
| ValueExpression ve = getExpressionFactory().createValueExpression(context.getELContext(), expression, expectedType); | ||||||||||||||||||
| return (T) (ve.getValue(context.getELContext())); | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
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.
Two methods (
bad1andbad2) are both annotated with@GetMapping("/api/bad1"), which will cause Spring to fail startup with an ambiguous mapping error once compilation issues are fixed.Useful? React with 👍 / 👎.