-
Notifications
You must be signed in to change notification settings - Fork 349
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
7
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
7 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 8351883
migrate groovy test file to junit
claponcet 8150f1b
formatting
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
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); | ||
| } | ||
| } |
249 changes: 249 additions & 0 deletions
249
dd-java-agent/agent-iast/src/test/java/com/datadog/iast/sink/CodeInjectionModuleTest.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,249 @@ | ||
| package com.datadog.iast.sink; | ||
|
|
||
| import static com.datadog.iast.model.VulnerabilityType.CODE_INJECTION; | ||
| import static datadog.trace.api.iast.IastContext.Mode.GLOBAL; | ||
| import static datadog.trace.api.iast.SourceTypes.REQUEST_PARAMETER_VALUE; | ||
| import static datadog.trace.api.iast.VulnerabilityMarks.CODE_INJECTION_MARK; | ||
| import static org.junit.jupiter.params.provider.Arguments.arguments; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.argThat; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.verifyNoInteractions; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import com.datadog.iast.Dependencies; | ||
| import com.datadog.iast.IastGlobalContext; | ||
| import com.datadog.iast.IastRequestContext; | ||
| import com.datadog.iast.Reporter; | ||
| import com.datadog.iast.model.Range; | ||
| import com.datadog.iast.model.Source; | ||
| import com.datadog.iast.overhead.OverheadController; | ||
| import com.datadog.iast.taint.Ranges; | ||
| import datadog.trace.api.Config; | ||
| import datadog.trace.api.gateway.RequestContext; | ||
| import datadog.trace.api.gateway.RequestContextSlot; | ||
| import datadog.trace.api.iast.IastContext; | ||
| import datadog.trace.api.iast.sink.CodeInjectionModule; | ||
| import datadog.trace.api.internal.TraceSegment; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentTracer; | ||
| import datadog.trace.util.stacktrace.StackWalkerFactory; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.io.CharArrayReader; | ||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| import java.io.Reader; | ||
| import java.io.StringReader; | ||
| import java.lang.reflect.Field; | ||
| import java.util.stream.Stream; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.Arguments; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
| import org.junit.jupiter.params.provider.NullAndEmptySource; | ||
|
|
||
| class CodeInjectionModuleTest { | ||
|
|
||
| private static final AgentTracer.TracerAPI ORIGINAL_TRACER = AgentTracer.get(); | ||
| private static final IastContext.Provider ORIGINAL_CONTEXT_PROVIDER = readContextProvider(); | ||
|
|
||
| private IastContext.Provider contextProvider; | ||
| private IastRequestContext ctx; | ||
| private AgentSpan span; | ||
| private AgentTracer.TracerAPI tracer; | ||
| private Reporter reporter; | ||
| private OverheadController overheadController; | ||
| private CodeInjectionModule module; | ||
|
|
||
| @BeforeEach | ||
| void setup() { | ||
| contextProvider = | ||
| Config.get().getIastContextMode() == GLOBAL | ||
| ? new IastGlobalContext.Provider() | ||
| : new IastRequestContext.Provider(); | ||
| ctx = (IastRequestContext) contextProvider.buildRequestContext(); | ||
|
|
||
| TraceSegment traceSegment = mock(TraceSegment.class); | ||
| RequestContext reqCtx = mock(RequestContext.class); | ||
| when(reqCtx.getData(RequestContextSlot.IAST)).thenReturn(ctx); | ||
| when(reqCtx.getTraceSegment()).thenReturn(traceSegment); | ||
|
|
||
| span = mock(AgentSpan.class); | ||
| when(span.getSpanId()).thenReturn(123456L); | ||
| when(span.getRequestContext()).thenReturn(reqCtx); | ||
|
|
||
| tracer = mock(AgentTracer.TracerAPI.class); | ||
| when(tracer.activeSpan()).thenReturn(span); | ||
| when(tracer.getTraceSegment()).thenReturn(traceSegment); | ||
|
|
||
| reporter = mock(Reporter.class); | ||
|
|
||
| overheadController = mock(OverheadController.class); | ||
| when(overheadController.acquireRequest()).thenReturn(true); | ||
| when(overheadController.consumeQuota(any(), any(), any())).thenReturn(true); | ||
|
|
||
| Dependencies dependencies = | ||
| new Dependencies( | ||
| Config.get(), | ||
| reporter, | ||
| overheadController, | ||
| StackWalkerFactory.INSTANCE, | ||
| contextProvider); | ||
|
|
||
| AgentTracer.forceRegister(tracer); | ||
| IastContext.Provider.register(contextProvider); | ||
|
|
||
| module = new CodeInjectionModuleImpl(dependencies); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void cleanup() { | ||
| contextProvider.releaseRequestContext(ctx); | ||
| AgentTracer.forceRegister(ORIGINAL_TRACER); | ||
| writeContextProvider(ORIGINAL_CONTEXT_PROVIDER); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @NullAndEmptySource | ||
| void nullOrEmptyScriptIsIgnored(String script) { | ||
| // a String-typed argument selects the onEval(String) overload, no cast needed | ||
| module.onEval(script); | ||
|
|
||
| // mirrors the Groovy original's `0 * _`: nothing is touched on the early-return path | ||
| verifyNoInteractions(reporter, overheadController, tracer); | ||
| } | ||
|
|
||
| @Test | ||
| void codeInjectionDetectionOnString() { | ||
| String script = "2 + 2"; | ||
|
|
||
| // report is not called if the script is not tainted | ||
| module.onEval(script); | ||
| verify(reporter, never()).report(any(), any()); | ||
|
|
||
| // report is not called if no active span, even when the script is tainted | ||
| taint(script); | ||
| when(tracer.activeSpan()).thenReturn(null); | ||
| module.onEval(script); | ||
| verify(reporter, never()).report(any(), any()); | ||
|
|
||
| // report is called when the script is tainted and there is an active span | ||
| when(tracer.activeSpan()).thenReturn(span); | ||
| module.onEval(script); | ||
| verify(reporter).report(eq(span), argThat(vul -> vul.getType() == CODE_INJECTION)); | ||
| } | ||
|
|
||
| @Test | ||
| void codeInjectionDetectionOnStringReader() { | ||
| StringReader reader = new StringReader("2 + 2"); | ||
|
|
||
| // report is not called if the reader is not tainted | ||
| module.onEval(reader); | ||
| verify(reporter, never()).report(any(), any()); | ||
|
|
||
| // report is called when the reader is tainted | ||
| taint(reader); | ||
| module.onEval(reader); | ||
| verify(reporter).report(eq(span), argThat(vul -> vul.getType() == CODE_INJECTION)); | ||
| } | ||
|
|
||
| @Test | ||
| void codeInjectionDetectionOnInputStreamReader() throws IOException { | ||
| try (InputStreamReader reader = | ||
| new InputStreamReader(new ByteArrayInputStream("2 + 2".getBytes()))) { | ||
| // report is not called if the reader is not tainted | ||
| module.onEval(reader); | ||
| verify(reporter, never()).report(any(), any()); | ||
|
|
||
| // report is called when the reader is tainted | ||
| taint(reader); | ||
| module.onEval(reader); | ||
| verify(reporter).report(eq(span), argThat(vul -> vul.getType() == CODE_INJECTION)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void unsupportedReaderTypeIsIgnoredEvenWhenTainted() { | ||
| // only StringReader and InputStreamReader are inspected (see CodeInjectionModuleImpl.onEval) | ||
| try (CharArrayReader reader = new CharArrayReader("2 + 2".toCharArray())) { | ||
| taint(reader); | ||
|
|
||
| module.onEval(reader); | ||
|
|
||
| // mirrors the Groovy original's `0 * _`: the unsupported-reader path touches no mock | ||
| verifyNoInteractions(reporter, overheadController, tracer); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void allRangesWithMarkOnScriptAreNotReported() { | ||
| String script = "2 + 2"; | ||
| Range[] ranges = markedRanges(); | ||
| ctx.getTaintedObjects().taint(script, ranges); | ||
|
|
||
| module.onEval(script); | ||
|
|
||
| verify(reporter, never()).report(any(), any()); | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "{0}") | ||
| @MethodSource("allRangesWithMarkOnReaderAreNotReportedArguments") | ||
| void allRangesWithMarkOnReaderAreNotReported(String scenario, Reader reader) throws IOException { | ||
| Range[] ranges = markedRanges(); | ||
| ctx.getTaintedObjects().taint(reader, ranges); | ||
|
|
||
| module.onEval(reader); | ||
|
|
||
| verify(reporter, never()).report(any(), any()); | ||
|
|
||
| reader.close(); | ||
| } | ||
|
|
||
| static Stream<Arguments> allRangesWithMarkOnReaderAreNotReportedArguments() { | ||
| return Stream.of( | ||
| arguments("StringReader", new StringReader("2 + 2")), | ||
| arguments( | ||
| "InputStreamReader", | ||
| new InputStreamReader(new ByteArrayInputStream("2 + 2".getBytes())))); | ||
| } | ||
|
|
||
| private Range[] markedRanges() { | ||
| return new Range[] { | ||
| new Range(0, 1, new Source(REQUEST_PARAMETER_VALUE, "name", "value"), CODE_INJECTION_MARK) | ||
| }; | ||
| } | ||
|
|
||
| private void taint(Object value) { | ||
| ctx.getTaintedObjects() | ||
| .taint( | ||
| value, Ranges.forObject(new Source(REQUEST_PARAMETER_VALUE, "name", value.toString()))); | ||
| } | ||
|
|
||
| // IastContext.Provider.INSTANCE is a private static field; the Groovy base read and restored it | ||
| // directly, which Java cannot, so snapshot and restore it reflectively to preserve test | ||
| // isolation. | ||
| private static IastContext.Provider readContextProvider() { | ||
| try { | ||
| Field field = IastContext.Provider.class.getDeclaredField("INSTANCE"); | ||
| field.setAccessible(true); | ||
| return (IastContext.Provider) field.get(null); | ||
| } catch (ReflectiveOperationException e) { | ||
| throw new IllegalStateException(e); | ||
| } | ||
| } | ||
|
|
||
| private static void writeContextProvider(IastContext.Provider provider) { | ||
| try { | ||
| Field field = IastContext.Provider.class.getDeclaredField("INSTANCE"); | ||
| field.setAccessible(true); | ||
| field.set(null, provider); | ||
| } catch (ReflectiveOperationException e) { | ||
| throw new IllegalStateException(e); | ||
| } | ||
| } | ||
| } |
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 | ||
| } | ||
| 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.
Uh oh!
There was an error while loading. Please reload this page.