2121import java .util .List ;
2222import java .util .Map ;
2323
24+ import org .yaml .snakeyaml .LoaderOptions ;
2425import org .yaml .snakeyaml .Yaml ;
2526import org .yaml .snakeyaml .constructor .SafeConstructor ;
2627
3233public class Parser {
3334
3435 private static final String REGEX_YAML_PATH = "/ua_parser/regexes.yaml" ;
36+
37+ public static final int CODE_POINT_LIMIT = 3455764 ;
3538 private UserAgentParser uaParser ;
3639 private OSParser osParser ;
3740 private DeviceParser deviceParser ;
@@ -41,8 +44,18 @@ public class Parser {
4144 * @throws RuntimeException if there's a problem reading the file from the classpath
4245 */
4346 public Parser () {
47+ this (getDefaultLoaderOptions ());
48+ }
49+
50+ /**
51+ * Creates a parser using the regular expression yaml file bundled in the jar.
52+ *
53+ * @param loaderOptions configuration for loading parser safe limits.
54+ * @throws RuntimeException if there's a problem reading the file from the classpath.
55+ */
56+ public Parser (LoaderOptions loaderOptions ) {
4457 try (InputStream is = Parser .class .getResourceAsStream (REGEX_YAML_PATH )) {
45- initialize (is );
58+ initialize (is , loaderOptions );
4659 } catch (IOException e ) {
4760 throw new RuntimeException ("failed to initialize parser from regexes.yaml bundled in jar" , e );
4861 }
@@ -54,7 +67,16 @@ public Parser() {
5467 * @param regexYaml the yaml file containing the regular expressions
5568 */
5669 public Parser (InputStream regexYaml ) {
57- initialize (regexYaml );
70+ this (regexYaml , getDefaultLoaderOptions ());
71+ }
72+ /**
73+ * Creates a parser using the supplied regular expression yaml file.
74+ * It is the responsibility of the caller to close the InputStream after construction.
75+ * @param regexYaml the yaml file containing the regular expressions
76+ * @param loaderOptions configuration for loading parser safe limits.
77+ */
78+ public Parser (InputStream regexYaml , LoaderOptions loaderOptions ) {
79+ initialize (regexYaml , loaderOptions );
5880 }
5981
6082 public Client parse (String agentString ) {
@@ -76,9 +98,15 @@ public OS parseOS(String agentString) {
7698 return osParser .parse (agentString );
7799 }
78100
79- private void initialize (InputStream regexYaml ) {
80- Yaml yaml = new Yaml (new SafeConstructor ());
81-
101+ public static LoaderOptions getDefaultLoaderOptions (){
102+ LoaderOptions options = new LoaderOptions ();
103+ options .setCodePointLimit (CODE_POINT_LIMIT );
104+ return options ;
105+ }
106+
107+ private void initialize (InputStream regexYaml , LoaderOptions loaderOptions ) {
108+ Yaml yaml = new Yaml (new SafeConstructor (loaderOptions ));
109+
82110 @ SuppressWarnings ("unchecked" )
83111 Map <String ,List <Map <String ,String >>> regexConfig = (Map <String ,List <Map <String ,String >>>) yaml .load (regexYaml );
84112
0 commit comments