Skip to content

Commit 224ea57

Browse files
committed
feat(YamlParser): add SafeConstructor to enforce security
1 parent 55ca839 commit 224ea57

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

framework/src/play/deps/YamlParser.java

+17-18
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.apache.ivy.plugins.repository.Resource;
3636
import org.apache.ivy.plugins.repository.url.URLResource;
3737
import org.yaml.snakeyaml.Yaml;
38+
import org.yaml.snakeyaml.constructor.SafeConstructor;
3839

3940
import play.Logger;
4041
import play.Play;
@@ -53,15 +54,13 @@ public boolean accept(Resource rsrc) {
5354
return rsrc.exists() && rsrc.getName().endsWith(".yml");
5455
}
5556

56-
57-
5857
@Override
5958
public ModuleDescriptor parseDescriptor(ParserSettings ps, URL url, Resource rsrc, boolean bln) throws ParseException, IOException {
6059
try {
6160
InputStream srcStream = rsrc.openStream();
6261
long lastModified = (rsrc != null?rsrc.getLastModified():0L);
63-
64-
Yaml yaml = new Yaml();
62+
63+
Yaml yaml = new Yaml(new SafeConstructor(new LoaderOptions()));
6564
Object o = null;
6665

6766
// Try to parse the yaml
@@ -112,7 +111,7 @@ public ModuleDescriptorParser getParser() {
112111
descriptor.setLastModified(lastModified);
113112

114113
boolean transitiveDependencies = get(data, "transitiveDependencies", boolean.class, true);
115-
114+
116115
List<String> confs = new ArrayList<>();
117116
if (data.containsKey("configurations")) {
118117
if (data.get("configurations") instanceof List) {
@@ -121,7 +120,7 @@ public ModuleDescriptorParser getParser() {
121120
for (Object conf : configurations) {
122121
String confName;
123122
Map options;
124-
123+
125124
if (conf instanceof String) {
126125
confName = ((String) conf).trim();
127126
options = new HashMap();
@@ -135,7 +134,7 @@ public ModuleDescriptorParser getParser() {
135134
allExcludes &= exclude;
136135
confs.add((exclude ? "!" : "") + confName);
137136
}
138-
137+
139138
if (allExcludes) {
140139
confs.add(0, "*");
141140
}
@@ -145,7 +144,7 @@ public ModuleDescriptorParser getParser() {
145144
} else {
146145
confs.add("*");
147146
}
148-
147+
149148
if (data.containsKey("require")) {
150149
if (data.get("require") instanceof List) {
151150

@@ -304,7 +303,7 @@ public static Set<String> getOrderedModuleList(File file) throws ParseException,
304303
System.setProperty("application.path", Play.applicationPath.getAbsolutePath());
305304
return getOrderedModuleList(modules, file);
306305
}
307-
306+
308307
private static Set<String> getOrderedModuleList(Set<String> modules, File file) throws ParseException, IOException {
309308
if (file == null || !file.exists()) {
310309
throw new FileNotFoundException("There was a problem to find the file");
@@ -317,28 +316,28 @@ private static Set<String> getOrderedModuleList(Set<String> modules, File file)
317316
DependencyDescriptor[] rules = md.getDependencies();
318317
File localModules = Play.getFile("modules");
319318
for (DependencyDescriptor dep : rules) {
320-
ModuleRevisionId rev = dep.getDependencyRevisionId();
319+
ModuleRevisionId rev = dep.getDependencyRevisionId();
321320
String moduleName = filterModuleName(rev);
322-
321+
323322
// Check if the module was already load to avoid circular parsing
324323
if (moduleName != null && !modules.contains(moduleName)) {
325324
// Add the given module
326325
modules.add(moduleName);
327-
328-
// Need to load module dependencies of this given module
326+
327+
// Need to load module dependencies of this given module
329328
File module = new File(localModules, moduleName);
330-
if(module != null && module.isDirectory()) {
329+
if(module != null && module.isDirectory()) {
331330
File ivyModule = new File(module, "conf/dependencies.yml");
332331
if(ivyModule != null && ivyModule.exists()) {
333332
getOrderedModuleList(modules, ivyModule);
334-
}
333+
}
335334
} else {
336335
File modulePath = new File(IO.readContentAsString(module).trim());
337336
if (modulePath.exists() && modulePath.isDirectory()) {
338337
File ivyModule = new File(modulePath, "conf/dependencies.yml");
339338
if(ivyModule != null && ivyModule.exists()) {
340339
getOrderedModuleList(modules, ivyModule);
341-
}
340+
}
342341
}
343342
}
344343
} else if(moduleName == null && rev.getRevision().equals("->")){
@@ -347,8 +346,8 @@ private static Set<String> getOrderedModuleList(Set<String> modules, File file)
347346
}
348347
return modules;
349348
}
350-
351-
349+
350+
352351
private static String filterModuleName(ModuleRevisionId rev) {
353352
if (rev != null && !"play".equals(rev.getName())) {
354353
File moduleDir = new File(Play.applicationPath, "modules");

0 commit comments

Comments
 (0)