Repository navigation
Add detection of untrusted deserialization in snakeyaml library #7406
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
Changes from all commits
f333392
771d7dc
0c7b2c9
8cb75b7
901960b
8676ca2
fc18416
f81e3ba
ae5cf34
a736193
9af04b3
9fccaf2
6ce6d15
03e9770
77ebfde
d92bee4
5eb116f
c3f4563
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,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.+' | ||
| } | ||
| 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); | ||
| } | ||
| } | ||
| } |
| 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); | ||
| } | ||
| } | ||
| } | ||
| } |
| 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'() { | ||
|
Member
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. We can simplify this test using one method with a where condition. For instance
Contributor
Author
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 this case I don't it could be possible as the load method only accepts a specific type which are InputStream, Reader or String. I cannot pass to it the Object method. One option is to do a casting previous to make the call but IMHO I think it is better to leave it as it is and don't try to simplify it as it will be hard to follow the test |
||
| 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(_) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.