Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit 9ee4396

Browse files
authored
Use SafeConstuctor with SnakeYaml (#1056)
Fixes #1055
1 parent 54d21d2 commit 9ee4396

File tree

9 files changed

+44
-20
lines changed

9 files changed

+44
-20
lines changed

spring-cloud-skipper-server-core/src/main/java/org/springframework/cloud/skipper/server/service/ReleaseReportService.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2018 the original author or authors.
2+
* Copyright 2017-2022 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.
@@ -13,12 +13,14 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.cloud.skipper.server.service;
1718

1819
import java.util.Map;
1920

2021
import org.yaml.snakeyaml.DumperOptions;
2122
import org.yaml.snakeyaml.Yaml;
23+
import org.yaml.snakeyaml.constructor.SafeConstructor;
2224

2325
import org.springframework.cloud.skipper.SkipperException;
2426
import org.springframework.cloud.skipper.domain.ConfigValues;
@@ -44,6 +46,7 @@
4446

4547
/**
4648
* @author Mark Pollack
49+
* @author Chris Bono
4750
*/
4851
public class ReleaseReportService {
4952

@@ -136,7 +139,7 @@ private Release updateReplacingReleaseConfigValues(Release targetRelease, Releas
136139
}
137140

138141
private Map<String, Object> getConfigValuesAsMap(ConfigValues configValues) {
139-
Yaml yaml = new Yaml();
142+
Yaml yaml = new Yaml(new SafeConstructor());
140143
if (StringUtils.hasText(configValues.getRaw())) {
141144
Object data = yaml.load(configValues.getRaw());
142145
if (data instanceof Map) {

spring-cloud-skipper-server-core/src/main/java/org/springframework/cloud/skipper/server/util/ConfigValueUtils.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 the original author or authors.
2+
* Copyright 2017-2022 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.
@@ -20,6 +20,7 @@
2020
import java.util.TreeMap;
2121

2222
import org.yaml.snakeyaml.Yaml;
23+
import org.yaml.snakeyaml.constructor.SafeConstructor;
2324

2425
import org.springframework.cloud.skipper.SkipperException;
2526
import org.springframework.cloud.skipper.domain.ConfigValues;
@@ -29,7 +30,9 @@
2930

3031
/**
3132
* Utility methods for merging of configuration values.
33+
*
3234
* @author Mark Pollack
35+
* @author Chris Bono
3336
*/
3437
@SuppressWarnings("unchecked")
3538
public class ConfigValueUtils {
@@ -50,7 +53,7 @@ public class ConfigValueUtils {
5053
*/
5154
public static Map<String, Object> mergeConfigValues(Package pkg, ConfigValues overrideValues) {
5255
// parse ConfigValues to a map.
53-
Yaml yaml = new Yaml();
56+
Yaml yaml = new Yaml(new SafeConstructor());
5457
Map<String, Object> mergedValues;
5558
// merge top level override values on top level package values
5659
if (StringUtils.hasText(overrideValues.getRaw())) {
@@ -86,7 +89,7 @@ public static Map<String, Object> mergeOverrideMap(Package pkg, Map<String, Obje
8689
return overrideMap;
8790
}
8891
// load the package values
89-
Yaml yaml = new Yaml();
92+
Yaml yaml = new Yaml(new SafeConstructor());
9093
Object object = yaml.load(pkg.getConfigValues().getRaw());
9194
if (object == null) {
9295
// Config Values could have been file with comments only, no data.
@@ -160,7 +163,7 @@ private static Map<String, Object> mergePackagesIncludingDependencies(Package pk
160163
}
161164

162165
private static Map<String, Object> convertConfigValuesToMap(Package pkg) {
163-
Yaml yaml = new Yaml();
166+
Yaml yaml = new Yaml(new SafeConstructor());
164167
Map<String, Object> currentPackageValueMap = new TreeMap<>();
165168
if (pkg.getConfigValues() != null && StringUtils.hasText(pkg.getConfigValues().getRaw())) {
166169
currentPackageValueMap = (Map<String, Object>) yaml.load(pkg.getConfigValues().getRaw());

spring-cloud-skipper-server-core/src/main/java/org/springframework/cloud/skipper/server/util/ManifestUtils.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2018 the original author or authors.
2+
* Copyright 2017-2022 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.
@@ -26,6 +26,7 @@
2626
import com.samskivert.mustache.Mustache;
2727
import org.yaml.snakeyaml.DumperOptions;
2828
import org.yaml.snakeyaml.Yaml;
29+
import org.yaml.snakeyaml.constructor.SafeConstructor;
2930
import org.yaml.snakeyaml.nodes.Node;
3031
import org.yaml.snakeyaml.nodes.Tag;
3132
import org.yaml.snakeyaml.representer.Representer;
@@ -38,6 +39,7 @@
3839
* Utility functions for manifest file processing.
3940
* @author Mark Pollack
4041
* @author Christian Tzolov
42+
* @author Chris Bono
4143
*/
4244
public class ManifestUtils {
4345

@@ -60,7 +62,7 @@ public static String resolveKind(String manifest) {
6062
if (!StringUtils.hasText(manifest)) {
6163
return null;
6264
}
63-
Yaml yaml = new Yaml();
65+
Yaml yaml = new Yaml(new SafeConstructor());
6466
Iterable<Object> object = yaml.loadAll(manifest);
6567
for (Object o : object) {
6668
if (o != null && o instanceof Map) {

spring-cloud-skipper-server-core/src/test/java/org/springframework/cloud/skipper/server/service/PackageServiceTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2018 the original author or authors.
2+
* Copyright 2017-2022 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.
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.cloud.skipper.server.service;
1718

1819
import java.io.IOException;
@@ -24,6 +25,7 @@
2425
import org.slf4j.Logger;
2526
import org.slf4j.LoggerFactory;
2627
import org.yaml.snakeyaml.Yaml;
28+
import org.yaml.snakeyaml.constructor.SafeConstructor;
2729

2830
import org.springframework.beans.factory.annotation.Autowired;
2931
import org.springframework.cloud.skipper.SkipperException;
@@ -53,6 +55,7 @@
5355
* managed test method transaction
5456
* @author Mark Pollack
5557
* @author Ilayaperumal Gopinathan
58+
* @author Chris Bono
5659
*/
5760
@ActiveProfiles("repo-test")
5861
@Transactional
@@ -272,7 +275,7 @@ private void assertPackageContent(Package pkgContent) {
272275
protected void assertConfigValues(Package pkg) {
273276
// Note same config values for both time and log
274277
ConfigValues configValues = pkg.getConfigValues();
275-
Yaml yaml = new Yaml();
278+
Yaml yaml = new Yaml(new SafeConstructor());
276279
Map<String, Object> logConfigValueMap = (Map<String, Object>) yaml.load(configValues.getRaw());
277280
assertThat(logConfigValueMap).containsKeys("version", "spec");
278281
if (pkg.getMetadata().getName().equals("log")) {

spring-cloud-skipper-server-core/src/test/java/org/springframework/cloud/skipper/server/templates/PackageTemplateTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2019 the original author or authors.
2+
* Copyright 2017-2022 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.
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.cloud.skipper.server.templates;
1718

1819
import java.io.IOException;
@@ -26,6 +27,7 @@
2627
import org.slf4j.Logger;
2728
import org.slf4j.LoggerFactory;
2829
import org.yaml.snakeyaml.Yaml;
30+
import org.yaml.snakeyaml.constructor.SafeConstructor;
2931

3032
import org.springframework.beans.factory.annotation.Value;
3133
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
@@ -49,6 +51,7 @@
4951

5052
/**
5153
* @author Mark Pollack
54+
* @author Chris Bono
5255
*/
5356
@RunWith(SpringRunner.class)
5457
@SpringBootTest(classes = TestConfig.class, properties = "spring.main.allow-bean-definition-overriding=true")
@@ -65,7 +68,7 @@ public class PackageTemplateTests {
6568
@Test
6669
@SuppressWarnings("unchecked")
6770
public void testMustasche() throws IOException {
68-
Yaml yaml = new Yaml();
71+
Yaml yaml = new Yaml(new SafeConstructor());
6972
Map model = (Map) yaml.load(valuesResource.getInputStream());
7073
String templateAsString = StreamUtils.copyToString(nestedMapResource.getInputStream(),
7174
Charset.defaultCharset());

spring-cloud-skipper-shell-commands/src/main/java/org/springframework/cloud/skipper/shell/command/support/YmlUtils.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2018 the original author or authors.
2+
* Copyright 2017-2022 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.
@@ -13,13 +13,15 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.cloud.skipper.shell.command.support;
1718

1819
import java.io.File;
1920
import java.io.FileInputStream;
2021
import java.io.FileNotFoundException;
2122

2223
import org.yaml.snakeyaml.Yaml;
24+
import org.yaml.snakeyaml.constructor.SafeConstructor;
2325

2426
import org.springframework.cloud.skipper.SkipperException;
2527
import org.springframework.cloud.skipper.support.DeploymentPropertiesUtils;
@@ -33,13 +35,14 @@
3335
*
3436
* @author Ilayaperumal Gopinathan
3537
* @author Mark Pollack
38+
* @author Chris Bono
3639
*/
3740
public abstract class YmlUtils {
3841

3942
public static String getYamlConfigValues(File yamlFile, String properties) {
4043
String configValuesYML = null;
4144
if (yamlFile != null) {
42-
Yaml yaml = new Yaml();
45+
Yaml yaml = new Yaml(new SafeConstructor());
4346
// Validate it is yaml formatted.
4447
try {
4548
configValuesYML = yaml.dump(yaml.load(new FileInputStream(yamlFile)));

spring-cloud-skipper/src/main/java/org/springframework/cloud/skipper/domain/CloudFoundryApplicationManifestReader.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 the original author or authors.
2+
* Copyright 2017-2022 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.
@@ -31,6 +31,7 @@
3131
import org.slf4j.Logger;
3232
import org.slf4j.LoggerFactory;
3333
import org.yaml.snakeyaml.Yaml;
34+
import org.yaml.snakeyaml.constructor.SafeConstructor;
3435

3536
import org.springframework.cloud.skipper.SkipperException;
3637

@@ -41,6 +42,7 @@
4142
* will not throw an exception in the deserialization process.
4243
*
4344
* @author Ilayaperumal Gopinathan
45+
* @author Chris Bono
4446
*/
4547
public class CloudFoundryApplicationManifestReader implements SkipperManifestReader {
4648

@@ -78,7 +80,7 @@ public List<CloudFoundryApplicationSkipperManifest> read(String manifest) {
7880
}
7981

8082
public boolean canSupport(String manifest) {
81-
Yaml yaml = new Yaml();
83+
Yaml yaml = new Yaml(new SafeConstructor());
8284
Iterable<Object> object = yaml.loadAll(manifest);
8385
for (Object o : object) {
8486
boolean supportKind = assertSupportedKind(o);

spring-cloud-skipper/src/main/java/org/springframework/cloud/skipper/domain/SpringCloudDeployerApplicationManifestReader.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2018 the original author or authors.
2+
* Copyright 2017-2022 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.
@@ -30,6 +30,7 @@
3030
import org.slf4j.Logger;
3131
import org.slf4j.LoggerFactory;
3232
import org.yaml.snakeyaml.Yaml;
33+
import org.yaml.snakeyaml.constructor.SafeConstructor;
3334

3435
import org.springframework.cloud.skipper.SkipperException;
3536

@@ -41,6 +42,7 @@
4142
*
4243
* @author Mark Pollack
4344
* @author Ilayaperumal Gopinathan
45+
* @author Chris Bono
4446
*/
4547
public class SpringCloudDeployerApplicationManifestReader implements SkipperManifestReader {
4648

@@ -79,7 +81,7 @@ public List<SpringCloudDeployerApplicationManifest> read(String manifest) {
7981
}
8082

8183
public boolean canSupport(String manifest) {
82-
Yaml yaml = new Yaml();
84+
Yaml yaml = new Yaml(new SafeConstructor());
8385
Iterable<Object> object = yaml.loadAll(manifest);
8486
for (Object o : object) {
8587
boolean supportKind = assertSupportedKind(o);

spring-cloud-skipper/src/test/java/org/springframework/cloud/skipper/io/PackageReaderTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 the original author or authors.
2+
* Copyright 2017-2022 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.
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.cloud.skipper.io;
1718

1819
import java.io.IOException;
@@ -22,6 +23,7 @@
2223

2324
import org.junit.Test;
2425
import org.yaml.snakeyaml.Yaml;
26+
import org.yaml.snakeyaml.constructor.SafeConstructor;
2527

2628
import org.springframework.cloud.skipper.domain.Package;
2729
import org.springframework.cloud.skipper.domain.PackageMetadata;
@@ -33,6 +35,7 @@
3335

3436
/**
3537
* @author Mark Pollack
38+
* @author Chris Bono
3639
*/
3740
public class PackageReaderTests {
3841

@@ -60,7 +63,7 @@ private void assertTickTockPackage(Package pkg) {
6063
assertThat(metadata.getMaintainer()).isEqualTo("https://github.com/markpollack");
6164
assertThat(metadata.getDescription()).isEqualTo("The ticktock stream sends a time stamp and logs the value.");
6265
String rawYamlString = pkg.getConfigValues().getRaw();
63-
Yaml yaml = new Yaml();
66+
Yaml yaml = new Yaml(new SafeConstructor());
6467
Map<String, String> valuesAsMap = (Map<String, String>) yaml.load(rawYamlString);
6568
assertThat(valuesAsMap).hasSize(2).containsEntry("foo", "bar").containsEntry("biz", "baz");
6669

0 commit comments

Comments
 (0)