Skip to content

Commit 097758b

Browse files
asomovbclozel
authored andcommitted
Upgrade to SnakeYAML 2.0
This commit raises the SnakeYAML baseline version to 2.0. While most Spring applications are not affected by CVE-2022-1471, upgrading this version should prevent automated tools from raising this as a security issue. Such tools usually do not understand that YAML parsing in Spring is about reading configuration, not parsing untrusted content. Closes gh-30048
1 parent 96a429a commit 097758b

File tree

3 files changed

+14
-23
lines changed

3 files changed

+14
-23
lines changed

framework-platform/framework-platform.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,6 @@ dependencies {
144144
api("org.webjars:webjars-locator-core:0.52")
145145
api("org.xmlunit:xmlunit-assertj:2.9.1")
146146
api("org.xmlunit:xmlunit-matchers:2.9.1")
147-
api("org.yaml:snakeyaml:1.33")
147+
api("org.yaml:snakeyaml:2.0")
148148
}
149149
}

spring-beans/src/main/java/org/springframework/beans/factory/config/YamlProcessor.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import org.yaml.snakeyaml.LoaderOptions;
3535
import org.yaml.snakeyaml.Yaml;
3636
import org.yaml.snakeyaml.constructor.Constructor;
37+
import org.yaml.snakeyaml.inspector.TagInspector;
38+
import org.yaml.snakeyaml.nodes.Tag;
3739
import org.yaml.snakeyaml.reader.UnicodeReader;
3840
import org.yaml.snakeyaml.representer.Representer;
3941

@@ -184,8 +186,9 @@ protected void process(MatchCallback callback) {
184186
protected Yaml createYaml() {
185187
LoaderOptions loaderOptions = new LoaderOptions();
186188
loaderOptions.setAllowDuplicateKeys(false);
189+
loaderOptions.setTagInspector(new SupportedTagInspector());
187190
DumperOptions dumperOptions = new DumperOptions();
188-
return new Yaml(new FilteringConstructor(loaderOptions), new Representer(dumperOptions),
191+
return new Yaml(new Constructor(loaderOptions), new Representer(dumperOptions),
189192
dumperOptions, loaderOptions);
190193
}
191194

@@ -425,23 +428,11 @@ public enum ResolutionMethod {
425428
FIRST_FOUND
426429
}
427430

428-
429-
/**
430-
* {@link Constructor} that supports filtering of unsupported types.
431-
* <p>If an unsupported type is encountered in a YAML document, an
432-
* {@link IllegalStateException} will be thrown from {@link #getClassForName}.
433-
*/
434-
private class FilteringConstructor extends Constructor {
435-
436-
FilteringConstructor(LoaderOptions loaderOptions) {
437-
super(loaderOptions);
438-
}
431+
private class SupportedTagInspector implements TagInspector {
439432

440433
@Override
441-
protected Class<?> getClassForName(String name) throws ClassNotFoundException {
442-
Assert.state(YamlProcessor.this.supportedTypes.contains(name),
443-
() -> "Unsupported type encountered in YAML document: " + name);
444-
return super.getClassForName(name);
434+
public boolean isGlobalTagAllowed(Tag tag) {
435+
return supportedTypes.contains(tag.getClassName());
445436
}
446437
}
447438

spring-beans/src/test/java/org/springframework/beans/factory/config/YamlProcessorTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,7 +24,7 @@
2424
import java.util.Set;
2525

2626
import org.junit.jupiter.api.Test;
27-
import org.yaml.snakeyaml.constructor.ConstructorException;
27+
import org.yaml.snakeyaml.composer.ComposerException;
2828
import org.yaml.snakeyaml.parser.ParserException;
2929
import org.yaml.snakeyaml.scanner.ScannerException;
3030

@@ -156,9 +156,9 @@ void standardTypesSupportedByDefault() throws Exception {
156156
void customTypeNotSupportedByDefault() throws Exception {
157157
URL url = new URL("https://localhost:9000/");
158158
setYaml("value: !!java.net.URL [\"" + url + "\"]");
159-
assertThatExceptionOfType(ConstructorException.class)
159+
assertThatExceptionOfType(ComposerException.class)
160160
.isThrownBy(() -> this.processor.process((properties, map) -> {}))
161-
.withMessageContaining("Unsupported type encountered in YAML document: java.net.URL");
161+
.withMessageContaining("Global tag is not allowed: tag:yaml.org,2002:java.net.URL");
162162
}
163163

164164
@Test
@@ -180,9 +180,9 @@ void customTypeNotSupportedDueToExplicitConfiguration() {
180180

181181
setYaml("value: !!java.net.URL [\"https://localhost:9000/\"]");
182182

183-
assertThatExceptionOfType(ConstructorException.class)
183+
assertThatExceptionOfType(ComposerException.class)
184184
.isThrownBy(() -> this.processor.process((properties, map) -> {}))
185-
.withMessageContaining("Unsupported type encountered in YAML document: java.net.URL");
185+
.withMessageContaining("Global tag is not allowed: tag:yaml.org,2002:java.net.URL");
186186
}
187187

188188
private void setYaml(String yaml) {

0 commit comments

Comments
 (0)