Skip to content
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

SnakeYaml SafeConstructor restricting deserialization #6319

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.testcontainers.images.ParsedDockerfile;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;

import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -35,7 +37,7 @@ class ParsedDockerComposeFile {
private Map<String, Set<String>> serviceNameToImageNames = new HashMap<>();

ParsedDockerComposeFile(File composeFile) {
Yaml yaml = new Yaml();
Yaml yaml = new Yaml(new SafeConstructor(new LoaderOptions()));
try (FileInputStream fileInputStream = FileUtils.openInputStream(composeFile)) {
composeFileContent = yaml.load(fileInputStream);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.testcontainers.containers;

public class ParsedDockerComposeFileBean {

public String foo;

public ParsedDockerComposeFileBean(String foo) {
this.foo = foo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import lombok.SneakyThrows;
import org.junit.Test;

import java.io.File;
Expand Down Expand Up @@ -61,6 +62,22 @@ public void shouldIgnoreUnknownStructure() {
new ParsedDockerComposeFile(ImmutableMap.of("version", "9000"));
}

@Test
@SneakyThrows
public void shouldRejectDeserializationOfArbitraryClasses() {
// Reject deserialization gadget chain attacks: https://nvd.nist.gov/vuln/detail/CVE-2022-1471
// https://raw.githubusercontent.com/mbechler/marshalsec/master/marshalsec.pdf

File file = new File("src/test/resources/docker-compose-deserialization.yml");

// ParsedDockerComposeFile should reject deserialization of ParsedDockerComposeFileBean
assertThatThrownBy(() -> {
new ParsedDockerComposeFile(file);
})
.hasMessageContaining(file.getAbsolutePath())
.hasMessageContaining("Unable to parse YAML file");
}

@Test
public void shouldObtainImageNamesV1() {
File file = new File("src/test/resources/docker-compose-imagename-parsing-v1.yml");
Expand Down
3 changes: 3 additions & 0 deletions core/src/test/resources/docker-compose-deserialization.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
redis:
image: redis
key: !!org.testcontainers.containers.ParsedDockerComposeFileBean '{foo: bar}'