1818package org .apache .commons .configuration2 ;
1919
2020import org .apache .commons .configuration2 .ex .ConfigurationException ;
21+ import org .apache .commons .configuration2 .ex .ConfigurationRuntimeException ;
2122import org .apache .commons .configuration2 .io .InputStreamSupport ;
2223import org .apache .commons .configuration2 .tree .ImmutableNode ;
2324import org .yaml .snakeyaml .DumperOptions ;
2425import org .yaml .snakeyaml .LoaderOptions ;
2526import org .yaml .snakeyaml .Yaml ;
27+ import org .yaml .snakeyaml .constructor .Constructor ;
28+ import org .yaml .snakeyaml .representer .Representer ;
2629
2730import java .io .IOException ;
2831import java .io .InputStream ;
@@ -65,7 +68,7 @@ public void read(final Reader in) throws ConfigurationException
6568 {
6669 try
6770 {
68- final Yaml yaml = new Yaml ( );
71+ final Yaml yaml = createYamlForReading ( new LoaderOptions () );
6972 final Map <String , Object > map = (Map ) yaml .load (in );
7073 load (map );
7174 }
@@ -80,7 +83,7 @@ public void read(final Reader in, final LoaderOptions options)
8083 {
8184 try
8285 {
83- final Yaml yaml = new Yaml (options );
86+ final Yaml yaml = createYamlForReading (options );
8487 final Map <String , Object > map = (Map ) yaml .load (in );
8588 load (map );
8689 }
@@ -117,7 +120,7 @@ public void read(final InputStream in) throws ConfigurationException
117120 {
118121 try
119122 {
120- final Yaml yaml = new Yaml ( );
123+ final Yaml yaml = createYamlForReading ( new LoaderOptions () );
121124 final Map <String , Object > map = (Map ) yaml .load (in );
122125 load (map );
123126 }
@@ -132,7 +135,7 @@ public void read(final InputStream in, final LoaderOptions options)
132135 {
133136 try
134137 {
135- final Yaml yaml = new Yaml (options );
138+ final Yaml yaml = createYamlForReading (options );
136139 final Map <String , Object > map = (Map ) yaml .load (in );
137140 load (map );
138141 }
@@ -142,4 +145,34 @@ public void read(final InputStream in, final LoaderOptions options)
142145 }
143146 }
144147
148+ /**
149+ * Creates a {@code Yaml} object for reading a Yaml file. The object is
150+ * configured with some default settings.
151+ *
152+ * @param options options for loading the file
153+ * @return the {@code Yaml} instance for loading a file
154+ */
155+ private static Yaml createYamlForReading (LoaderOptions options )
156+ {
157+ return new Yaml (createClassLoadingDisablingConstructor (), new Representer (), new DumperOptions (), options );
158+ }
159+
160+ /**
161+ * Returns a {@code Constructor} object for the YAML parser that prevents
162+ * all classes from being loaded. This effectively disables the dynamic
163+ * creation of Java objects that are declared in YAML files to be loaded.
164+ *
165+ * @return the {@code Constructor} preventing object creation
166+ */
167+ private static Constructor createClassLoadingDisablingConstructor ()
168+ {
169+ return new Constructor ()
170+ {
171+ @ Override
172+ protected Class <?> getClassForName (String name )
173+ {
174+ throw new ConfigurationRuntimeException ("Class loading is disabled." );
175+ }
176+ };
177+ }
145178}
0 commit comments