-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add detection of untrusted deserialization in snakeyaml library (#7406)
Adds instrumentation for snakeyaml library versions prior to 2.0
- Loading branch information
Showing
10 changed files
with
233 additions
and
1 deletion.
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
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
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,24 @@ | ||
muzzle { | ||
pass { | ||
name = 'snakeyaml-1.x' | ||
group = "org.yaml" | ||
module = "snakeyaml" | ||
versions = "[1.4, 2.0)" | ||
assertInverse = true | ||
} | ||
fail { | ||
group = "org.yaml" | ||
module = 'snakeyaml' | ||
versions = "[2.0,)" | ||
} | ||
} | ||
|
||
apply from: "$rootDir/gradle/java.gradle" | ||
addTestSuiteForDir('latestDepTest', 'test') | ||
|
||
dependencies { | ||
compileOnly group: 'org.yaml', name: 'snakeyaml', version: '1.33' | ||
testImplementation group: 'org.yaml', name: 'snakeyaml', version: '1.33' | ||
|
||
latestDepTestImplementation group: 'org.yaml', name: 'snakeyaml', version: '1.+' | ||
} |
39 changes: 39 additions & 0 deletions
39
...tion/snakeyaml/src/main/java/datadog/trace/instrumentation/snakeyaml/SnakeYamlHelper.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,39 @@ | ||
package datadog.trace.instrumentation.snakeyaml; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.UndeclaredThrowableException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.yaml.snakeyaml.Yaml; | ||
import org.yaml.snakeyaml.constructor.BaseConstructor; | ||
|
||
public final class SnakeYamlHelper { | ||
private SnakeYamlHelper() {} | ||
|
||
private static final Logger log = LoggerFactory.getLogger(SnakeYamlHelper.class); | ||
|
||
private static final Field CONSTRUCTOR = prepareConstructor(); | ||
|
||
private static Field prepareConstructor() { | ||
Field constructor = null; | ||
try { | ||
constructor = Yaml.class.getDeclaredField("constructor"); | ||
constructor.setAccessible(true); | ||
} catch (Throwable e) { | ||
log.debug("Failed to get Yaml constructor", e); | ||
return null; | ||
} | ||
return constructor; | ||
} | ||
|
||
public static BaseConstructor fetchConstructor(Yaml yaml) { | ||
if (CONSTRUCTOR == null) { | ||
return null; | ||
} | ||
try { | ||
return (BaseConstructor) CONSTRUCTOR.get(yaml); | ||
} catch (IllegalAccessException e) { | ||
throw new UndeclaredThrowableException(e); | ||
} | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
...nakeyaml/src/main/java/datadog/trace/instrumentation/snakeyaml/SnakeYamlInstrumenter.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,90 @@ | ||
package datadog.trace.instrumentation.snakeyaml; | ||
|
||
import static datadog.trace.agent.tooling.bytebuddy.matcher.ClassLoaderMatchers.hasClassNamed; | ||
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.not; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
||
import com.google.auto.service.AutoService; | ||
import datadog.trace.agent.tooling.Instrumenter; | ||
import datadog.trace.agent.tooling.InstrumenterModule; | ||
import datadog.trace.api.iast.InstrumentationBridge; | ||
import datadog.trace.api.iast.Sink; | ||
import datadog.trace.api.iast.VulnerabilityTypes; | ||
import datadog.trace.api.iast.sink.UntrustedDeserializationModule; | ||
import java.io.InputStream; | ||
import java.io.Reader; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import org.yaml.snakeyaml.Yaml; | ||
import org.yaml.snakeyaml.constructor.BaseConstructor; | ||
import org.yaml.snakeyaml.constructor.Constructor; | ||
|
||
@AutoService(InstrumenterModule.class) | ||
public class SnakeYamlInstrumenter extends InstrumenterModule.Iast | ||
implements Instrumenter.ForSingleType { | ||
|
||
public SnakeYamlInstrumenter() { | ||
super("snakeyaml", "snakeyaml"); | ||
} | ||
|
||
@Override | ||
public String muzzleDirective() { | ||
return "snakeyaml-1.x"; | ||
} | ||
|
||
static final ElementMatcher.Junction<ClassLoader> NOT_SNAKEYAML_2 = | ||
not(hasClassNamed("org.yaml.snakeyaml.inspector.TagInspector")); | ||
|
||
@Override | ||
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() { | ||
return NOT_SNAKEYAML_2; | ||
} | ||
|
||
@Override | ||
public String[] helperClassNames() { | ||
return new String[] { | ||
packageName + ".SnakeYamlHelper", | ||
}; | ||
} | ||
|
||
@Override | ||
public String instrumentedType() { | ||
return "org.yaml.snakeyaml.Yaml"; | ||
} | ||
|
||
@Override | ||
public void methodAdvice(MethodTransformer transformer) { | ||
transformer.applyAdvice( | ||
named("load") | ||
.and(isMethod()) | ||
.and( | ||
takesArguments(String.class) | ||
.or(takesArguments(InputStream.class)) | ||
.or(takesArguments(Reader.class))), | ||
SnakeYamlInstrumenter.class.getName() + "$LoadAdvice"); | ||
} | ||
|
||
public static class LoadAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
@Sink(VulnerabilityTypes.UNTRUSTED_DESERIALIZATION) | ||
public static void onEnter( | ||
@Advice.Argument(0) final Object data, @Advice.This final Yaml self) { | ||
if (data == null) { | ||
return; | ||
} | ||
final UntrustedDeserializationModule untrustedDeserialization = | ||
InstrumentationBridge.UNTRUSTED_DESERIALIZATION; | ||
if (untrustedDeserialization == null) { | ||
return; | ||
} | ||
final BaseConstructor constructor = SnakeYamlHelper.fetchConstructor(self); | ||
// For versions prior to 1.7 (not included), the constructor field is null | ||
if (constructor instanceof Constructor || constructor == null) { | ||
untrustedDeserialization.onObject(data); | ||
} | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
dd-java-agent/instrumentation/snakeyaml/src/test/groovy/SnakeYamlInstrumenterTest.groovy
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,54 @@ | ||
import datadog.trace.agent.test.AgentTestRunner | ||
import datadog.trace.api.iast.InstrumentationBridge | ||
import datadog.trace.api.iast.sink.UntrustedDeserializationModule | ||
import org.yaml.snakeyaml.Yaml | ||
|
||
class SnakeYamlInstrumenterTest extends AgentTestRunner { | ||
|
||
@Override | ||
protected void configurePreAgent() { | ||
injectSysConfig('dd.iast.enabled', 'true') | ||
} | ||
|
||
void 'test snakeyaml load with an input stream'() { | ||
given: | ||
final module = Mock(UntrustedDeserializationModule) | ||
InstrumentationBridge.registerIastModule(module) | ||
|
||
final InputStream inputStream = new ByteArrayInputStream("test".getBytes()) | ||
|
||
when: | ||
new Yaml().load(inputStream) | ||
|
||
then: | ||
1 * module.onObject(_) | ||
} | ||
|
||
void 'test snakeyaml load with a reader'() { | ||
given: | ||
final module = Mock(UntrustedDeserializationModule) | ||
InstrumentationBridge.registerIastModule(module) | ||
|
||
final Reader reader = new StringReader("test") | ||
|
||
when: | ||
new Yaml().load(reader) | ||
|
||
then: | ||
1 * module.onObject(_) | ||
} | ||
|
||
void 'test snakeyaml load with a string'() { | ||
given: | ||
final module = Mock(UntrustedDeserializationModule) | ||
InstrumentationBridge.registerIastModule(module) | ||
|
||
final String string = "test" | ||
|
||
when: | ||
new Yaml().load(string) | ||
|
||
then: | ||
1 * module.onObject(_) | ||
} | ||
} |
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
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
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
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