-
Notifications
You must be signed in to change notification settings - Fork 347
Add IAST code injection detection for BeanShell #12113
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
Open
claponcet
wants to merge
5
commits into
master
Choose a base branch
from
clara.poncet/iast-code-injection-beanshell
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5547282
beanshell instrumentation + new code_injection vulnerability
claponcet e6bd5f6
Add beanshell to agent jar integrations golden file
claponcet a748317
add muzzle references
claponcet 668f41e
fix review
claponcet 53d77e6
codeowners
claponcet 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 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
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
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
35 changes: 35 additions & 0 deletions
35
dd-java-agent/agent-iast/src/main/java/com/datadog/iast/sink/CodeInjectionModuleImpl.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package com.datadog.iast.sink; | ||
|
|
||
| import static com.datadog.iast.taint.Tainteds.canBeTainted; | ||
|
|
||
| import com.datadog.iast.Dependencies; | ||
| import com.datadog.iast.model.VulnerabilityType; | ||
| import datadog.trace.api.iast.sink.CodeInjectionModule; | ||
| import java.io.InputStreamReader; | ||
| import java.io.Reader; | ||
| import java.io.StringReader; | ||
| import javax.annotation.Nonnull; | ||
|
|
||
| public class CodeInjectionModuleImpl extends SinkModuleBase implements CodeInjectionModule { | ||
|
|
||
| public CodeInjectionModuleImpl(final Dependencies dependencies) { | ||
| super(dependencies); | ||
| } | ||
|
|
||
| @Override | ||
| public void onEval(@Nonnull Reader reader) { | ||
| // IAST propagates taint only to StringReader and InputStreamReader. | ||
| if (!(reader instanceof StringReader) && !(reader instanceof InputStreamReader)) { | ||
| return; | ||
| } | ||
| checkInjection(VulnerabilityType.CODE_INJECTION, reader); | ||
| } | ||
|
|
||
| @Override | ||
| public void onEval(@Nonnull String script) { | ||
| if (!canBeTainted(script)) { | ||
| return; | ||
| } | ||
| checkInjection(VulnerabilityType.CODE_INJECTION, script); | ||
| } | ||
| } |
168 changes: 168 additions & 0 deletions
168
...ava-agent/agent-iast/src/test/groovy/com/datadog/iast/sink/CodeInjectionModuleTest.groovy
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| package com.datadog.iast.sink | ||
|
|
||
| import com.datadog.iast.IastModuleImplTestBase | ||
| import com.datadog.iast.Reporter | ||
| import com.datadog.iast.model.Range | ||
| import com.datadog.iast.model.Source | ||
| import com.datadog.iast.model.Vulnerability | ||
| import com.datadog.iast.model.VulnerabilityType | ||
| import com.datadog.iast.taint.Ranges | ||
| import datadog.trace.api.iast.SourceTypes | ||
| import datadog.trace.api.iast.VulnerabilityMarks | ||
| import datadog.trace.api.iast.sink.CodeInjectionModule | ||
|
|
||
| class CodeInjectionModuleTest extends IastModuleImplTestBase { | ||
|
|
||
| private CodeInjectionModule module | ||
|
|
||
| def setup() { | ||
| module = new CodeInjectionModuleImpl(dependencies) | ||
| } | ||
|
|
||
| @Override | ||
| protected Reporter buildReporter() { | ||
| return Mock(Reporter) | ||
| } | ||
|
|
||
| void 'test null or empty script is ignored'() { | ||
| when: 'cast disambiguates the onEval(String) / onEval(Reader) overloads for a null argument' | ||
| module.onEval(script as String) | ||
|
|
||
| then: | ||
| 0 * _ | ||
|
|
||
| where: | ||
| script | _ | ||
| null | _ | ||
| '' | _ | ||
| } | ||
|
|
||
| void 'test code injection detection on string'() { | ||
| when: | ||
| module.onEval(script) | ||
|
|
||
| then: 'report is not called if no active span' | ||
| tracer.activeSpan() >> null | ||
| 0 * reporter.report(_, _) | ||
|
|
||
| when: | ||
| module.onEval(script) | ||
|
|
||
| then: 'report is not called if the script is not tainted' | ||
| tracer.activeSpan() >> span | ||
| 0 * reporter.report(_, _) | ||
|
|
||
| when: | ||
| taint(script) | ||
| module.onEval(script) | ||
|
|
||
| then: 'report is called when the script is tainted' | ||
| tracer.activeSpan() >> span | ||
| 1 * reporter.report(span, { Vulnerability vul -> vul.type == VulnerabilityType.CODE_INJECTION }) | ||
|
|
||
| where: | ||
| script = '2 + 2' | ||
| } | ||
|
|
||
| void 'test code injection detection on StringReader'() { | ||
| given: | ||
| final reader = new StringReader('2 + 2') | ||
|
|
||
| when: | ||
| module.onEval(reader) | ||
|
|
||
| then: 'report is not called if the reader is not tainted' | ||
| tracer.activeSpan() >> span | ||
| 0 * reporter.report(_, _) | ||
|
|
||
| when: | ||
| taint(reader) | ||
| module.onEval(reader) | ||
|
|
||
| then: 'report is called when the reader is tainted' | ||
| tracer.activeSpan() >> span | ||
| 1 * reporter.report(span, { Vulnerability vul -> vul.type == VulnerabilityType.CODE_INJECTION }) | ||
| } | ||
|
|
||
| void 'test code injection detection on InputStreamReader'() { | ||
| given: | ||
| final reader = new InputStreamReader(new ByteArrayInputStream('2 + 2'.bytes)) | ||
|
|
||
| when: | ||
| module.onEval(reader) | ||
|
|
||
| then: 'report is not called if the reader is not tainted' | ||
| tracer.activeSpan() >> span | ||
| 0 * reporter.report(_, _) | ||
|
|
||
| when: | ||
| taint(reader) | ||
| module.onEval(reader) | ||
|
|
||
| then: 'report is called when the reader is tainted' | ||
| tracer.activeSpan() >> span | ||
| 1 * reporter.report(span, { Vulnerability vul -> vul.type == VulnerabilityType.CODE_INJECTION }) | ||
|
|
||
| cleanup: | ||
| reader.close() | ||
| } | ||
|
|
||
| void 'test unsupported Reader type is ignored even when tainted'() { | ||
| given: 'only StringReader and InputStreamReader are inspected (see CodeInjectionModuleImpl.onEval)' | ||
| final reader = new CharArrayReader('2 + 2'.toCharArray()) | ||
| taint(reader) | ||
|
|
||
| when: | ||
| module.onEval(reader) | ||
|
|
||
| then: | ||
| 0 * _ | ||
|
|
||
| cleanup: | ||
| reader.close() | ||
| } | ||
|
|
||
| void 'if all ranges of the tainted script have the code injection mark it is not reported'() { | ||
| given: | ||
| final script = '2 + 2' | ||
| final Range[] ranges = [ | ||
| new Range(0, 1, new Source(SourceTypes.REQUEST_PARAMETER_VALUE, 'name', 'value'), VulnerabilityMarks.CODE_INJECTION_MARK) | ||
| ] | ||
| ctx.getTaintedObjects().taint(script, ranges) | ||
|
|
||
| when: | ||
| module.onEval(script) | ||
|
|
||
| then: | ||
| tracer.activeSpan() >> span | ||
| 0 * reporter.report(_, _) | ||
| } | ||
|
|
||
| void 'if all ranges of the tainted reader have the code injection mark it is not reported'() { | ||
| given: | ||
| final Range[] ranges = [ | ||
| new Range(0, 1, new Source(SourceTypes.REQUEST_PARAMETER_VALUE, 'name', 'value'), VulnerabilityMarks.CODE_INJECTION_MARK) | ||
| ] | ||
| ctx.getTaintedObjects().taint(reader, ranges) | ||
|
|
||
| when: | ||
| module.onEval(reader) | ||
|
|
||
| then: | ||
| tracer.activeSpan() >> span | ||
| 0 * reporter.report(_, _) | ||
|
|
||
| cleanup: | ||
| reader.close() | ||
|
|
||
| where: | ||
| reader << [ | ||
| new StringReader('2 + 2'), | ||
| new InputStreamReader(new ByteArrayInputStream('2 + 2'.bytes)) | ||
| ] | ||
| } | ||
|
|
||
| private taint(final Object value) { | ||
| ctx.getTaintedObjects().taint(value, Ranges.forObject(new Source(SourceTypes.REQUEST_PARAMETER_VALUE, 'name', value.toString()))) | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| muzzle { | ||
| // BeanShell ships under two coordinates over its lifetime: the original org.beanshell:bsh | ||
| // (up to 2.0b5) and the later org.apache-extras.beanshell:bsh (2.0b5, 2.0b6). Cover both. | ||
| // 2.0b4 is the supported floor. | ||
| pass { | ||
| group = 'org.beanshell' | ||
| module = 'bsh' | ||
| versions = '[2.0b4,]' | ||
| assertInverse = true | ||
|
claponcet marked this conversation as resolved.
|
||
| } | ||
| pass { | ||
| group = 'org.apache-extras.beanshell' | ||
| module = 'bsh' | ||
| versions = '[2.0b4,]' | ||
| assertInverse = true | ||
| } | ||
| } | ||
|
|
||
| apply from: "$rootDir/gradle/java.gradle" | ||
|
|
||
| addTestSuiteForDir('latestDepTest', 'test') | ||
|
|
||
| // latestDepTest extends test (see gradle/test-suites.gradle), which drags in the org.beanshell:bsh | ||
| // 2.0b4 floor. Drop that coordinate here so its duplicate bsh.* classes don't collide with — or | ||
| // shadow on the classpath — the org.apache-extras.beanshell:bsh artifact we actually want to validate. | ||
| configurations.latestDepTestImplementation { | ||
| exclude group: 'org.beanshell', module: 'bsh' | ||
| } | ||
|
|
||
| dependencies { | ||
| compileOnly group: 'org.beanshell', name: 'bsh', version: '2.0b4' | ||
|
|
||
| // Unit/instrumentation tests run against the supported floor (2.0b4); latestDepTest re-runs the | ||
| // same suite against the newest published version to guard against future regressions. | ||
| testImplementation group: 'org.beanshell', name: 'bsh', version: '2.0b4' | ||
|
|
||
| testRuntimeOnly project(':dd-java-agent:instrumentation:datadog:asm:iast-instrumenter') | ||
|
|
||
| latestDepTestImplementation group: 'org.apache-extras.beanshell', name: 'bsh', version: '+' | ||
|
claponcet marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
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.
This is a new .groovy test file, we should try to write them in Java/JUnit 5 (see docs/how_to_test_with_junit.md), or add the tag: override groovy enforcement label if an exception is needed