|
24 | 24 | import org.jetbrains.annotations.Nullable;
|
25 | 25 | import org.jetbrains.annotations.Unmodifiable;
|
26 | 26 | import org.yaml.snakeyaml.DumperOptions;
|
| 27 | +import org.yaml.snakeyaml.LoaderOptions; |
27 | 28 | import org.yaml.snakeyaml.Yaml;
|
| 29 | +import org.yaml.snakeyaml.constructor.Constructor; |
28 | 30 | import org.yaml.snakeyaml.representer.Representer;
|
29 | 31 |
|
30 | 32 | import java.io.IOException;
|
|
51 | 53 | */
|
52 | 54 | public class YamlConfiguration extends AbstractFileConfiguration {
|
53 | 55 |
|
54 |
| - private static final Supplier<Yaml> DEFAULT_YAML_SUPPLIER; |
| 56 | + private static final Supplier<Yaml> DEFAULT_YAML_SUPPLIER = YamlConfiguration::createYaml; |
55 | 57 |
|
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); |
59 | 61 |
|
60 |
| - var representer = new Representer(); |
| 62 | + var representer = new Representer(dumperOptions); |
61 | 63 | representer.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
62 | 64 |
|
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); |
64 | 73 | }
|
65 | 74 |
|
66 | 75 | /**
|
@@ -121,9 +130,7 @@ public class YamlConfiguration extends AbstractFileConfiguration {
|
121 | 130 | */
|
122 | 131 | @SuppressWarnings("unchecked")
|
123 | 132 | 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); |
127 | 134 | return MappedConfiguration.create(map);
|
128 | 135 | }
|
129 | 136 |
|
|
0 commit comments