Skip to content

Commit 998a13f

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

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

framework/src/play/deps/YamlParser.java

+18-18
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
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;
39+
import org.yaml.snakeyaml.LoaderOptions;
3840

3941
import play.Logger;
4042
import play.Play;
@@ -53,15 +55,13 @@ public boolean accept(Resource rsrc) {
5355
return rsrc.exists() && rsrc.getName().endsWith(".yml");
5456
}
5557

56-
57-
5858
@Override
5959
public ModuleDescriptor parseDescriptor(ParserSettings ps, URL url, Resource rsrc, boolean bln) throws ParseException, IOException {
6060
try {
6161
InputStream srcStream = rsrc.openStream();
6262
long lastModified = (rsrc != null?rsrc.getLastModified():0L);
63-
64-
Yaml yaml = new Yaml();
63+
64+
Yaml yaml = new Yaml(new SafeConstructor(new LoaderOptions()));
6565
Object o = null;
6666

6767
// Try to parse the yaml
@@ -112,7 +112,7 @@ public ModuleDescriptorParser getParser() {
112112
descriptor.setLastModified(lastModified);
113113

114114
boolean transitiveDependencies = get(data, "transitiveDependencies", boolean.class, true);
115-
115+
116116
List<String> confs = new ArrayList<>();
117117
if (data.containsKey("configurations")) {
118118
if (data.get("configurations") instanceof List) {
@@ -121,7 +121,7 @@ public ModuleDescriptorParser getParser() {
121121
for (Object conf : configurations) {
122122
String confName;
123123
Map options;
124-
124+
125125
if (conf instanceof String) {
126126
confName = ((String) conf).trim();
127127
options = new HashMap();
@@ -135,7 +135,7 @@ public ModuleDescriptorParser getParser() {
135135
allExcludes &= exclude;
136136
confs.add((exclude ? "!" : "") + confName);
137137
}
138-
138+
139139
if (allExcludes) {
140140
confs.add(0, "*");
141141
}
@@ -145,7 +145,7 @@ public ModuleDescriptorParser getParser() {
145145
} else {
146146
confs.add("*");
147147
}
148-
148+
149149
if (data.containsKey("require")) {
150150
if (data.get("require") instanceof List) {
151151

@@ -304,7 +304,7 @@ public static Set<String> getOrderedModuleList(File file) throws ParseException,
304304
System.setProperty("application.path", Play.applicationPath.getAbsolutePath());
305305
return getOrderedModuleList(modules, file);
306306
}
307-
307+
308308
private static Set<String> getOrderedModuleList(Set<String> modules, File file) throws ParseException, IOException {
309309
if (file == null || !file.exists()) {
310310
throw new FileNotFoundException("There was a problem to find the file");
@@ -317,28 +317,28 @@ private static Set<String> getOrderedModuleList(Set<String> modules, File file)
317317
DependencyDescriptor[] rules = md.getDependencies();
318318
File localModules = Play.getFile("modules");
319319
for (DependencyDescriptor dep : rules) {
320-
ModuleRevisionId rev = dep.getDependencyRevisionId();
320+
ModuleRevisionId rev = dep.getDependencyRevisionId();
321321
String moduleName = filterModuleName(rev);
322-
322+
323323
// Check if the module was already load to avoid circular parsing
324324
if (moduleName != null && !modules.contains(moduleName)) {
325325
// Add the given module
326326
modules.add(moduleName);
327-
328-
// Need to load module dependencies of this given module
327+
328+
// Need to load module dependencies of this given module
329329
File module = new File(localModules, moduleName);
330-
if(module != null && module.isDirectory()) {
330+
if(module != null && module.isDirectory()) {
331331
File ivyModule = new File(module, "conf/dependencies.yml");
332332
if(ivyModule != null && ivyModule.exists()) {
333333
getOrderedModuleList(modules, ivyModule);
334-
}
334+
}
335335
} else {
336336
File modulePath = new File(IO.readContentAsString(module).trim());
337337
if (modulePath.exists() && modulePath.isDirectory()) {
338338
File ivyModule = new File(modulePath, "conf/dependencies.yml");
339339
if(ivyModule != null && ivyModule.exists()) {
340340
getOrderedModuleList(modules, ivyModule);
341-
}
341+
}
342342
}
343343
}
344344
} else if(moduleName == null && rev.getRevision().equals("->")){
@@ -347,8 +347,8 @@ private static Set<String> getOrderedModuleList(Set<String> modules, File file)
347347
}
348348
return modules;
349349
}
350-
351-
350+
351+
352352
private static String filterModuleName(ModuleRevisionId rev) {
353353
if (rev != null && !"play".equals(rev.getName())) {
354354
File moduleDir = new File(Play.applicationPath, "modules");

0 commit comments

Comments
 (0)