Skip to content

Commit 5e75221

Browse files
committed
fix(yaml): remove usage of deprecated constructor and comment about CVE
1 parent a9cf0c8 commit 5e75221

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

yaml/src/main/java/com/github/siroshun09/configapi/yaml/YamlConfiguration.java

+16-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
import org.jetbrains.annotations.Nullable;
2525
import org.jetbrains.annotations.Unmodifiable;
2626
import org.yaml.snakeyaml.DumperOptions;
27+
import org.yaml.snakeyaml.LoaderOptions;
2728
import org.yaml.snakeyaml.Yaml;
29+
import org.yaml.snakeyaml.constructor.Constructor;
2830
import org.yaml.snakeyaml.representer.Representer;
2931

3032
import java.io.IOException;
@@ -51,16 +53,23 @@
5153
*/
5254
public class YamlConfiguration extends AbstractFileConfiguration {
5355

54-
private static final Supplier<Yaml> DEFAULT_YAML_SUPPLIER;
56+
private static final Supplier<Yaml> DEFAULT_YAML_SUPPLIER = YamlConfiguration::createYaml;
5557

56-
static {
57-
var options = new DumperOptions();
58-
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
58+
private static @NotNull Yaml createYaml() {
59+
var dumperOptions = new DumperOptions();
60+
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
5961

60-
var representer = new Representer();
62+
var representer = new Representer(dumperOptions);
6163
representer.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
6264

63-
DEFAULT_YAML_SUPPLIER = () -> new Yaml(representer, options);
65+
var loaderOptions = new LoaderOptions();
66+
67+
// ConfigAPI is intended to load configuration files from trusted sources such as the local files.
68+
// It is NOT intended to load from untrusted sources.
69+
//noinspection VulnerableCodeUsages
70+
var constructor = new Constructor(LinkedHashMap.class, loaderOptions);
71+
72+
return new Yaml(constructor, representer, dumperOptions, loaderOptions);
6473
}
6574

6675
/**
@@ -121,9 +130,7 @@ public class YamlConfiguration extends AbstractFileConfiguration {
121130
*/
122131
@SuppressWarnings("unchecked")
123132
public static @NotNull Configuration loadFromInputStream(@NotNull InputStream input) {
124-
var yaml = new Yaml();
125-
var map = yaml.loadAs(input, LinkedHashMap.class);
126-
133+
var map = createYaml().loadAs(input, LinkedHashMap.class);
127134
return MappedConfiguration.create(map);
128135
}
129136

0 commit comments

Comments
 (0)