• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2008, SnakeYAML
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package org.yaml.snakeyaml;
15 
16 public class LoaderOptions {
17 
18   private boolean allowDuplicateKeys = true;
19   private boolean wrappedToRootException = false;
20   private int maxAliasesForCollections = 50; // to prevent YAML at
21   // https://en.wikipedia.org/wiki/Billion_laughs_attack
22   private boolean allowRecursiveKeys = false;
23   private boolean processComments = false;
24   private boolean enumCaseSensitive = true;
25   private int nestingDepthLimit = 50;
26   private int codePointLimit = 3 * 1024 * 1024; // 3 MB
27 
isAllowDuplicateKeys()28   public final boolean isAllowDuplicateKeys() {
29     return allowDuplicateKeys;
30   }
31 
32   /**
33    * Allow/Reject duplicate map keys in the YAML file.
34    *
35    * Default is to allow.
36    *
37    * YAML 1.1 is slightly vague around duplicate entries in the YAML file. The best reference is
38    * <a href="http://www.yaml.org/spec/1.1/#id862121"> 3.2.1.3. Nodes Comparison</a> where it hints
39    * that a duplicate map key is an error.
40    *
41    * For future reference, YAML spec 1.2 is clear. The keys MUST be unique.
42    * <a href="http://www.yaml.org/spec/1.2/spec.html#id2759572">1.3. Relation to JSON</a>
43    *
44    * @param allowDuplicateKeys false to reject duplicate mapping keys
45    */
setAllowDuplicateKeys(boolean allowDuplicateKeys)46   public void setAllowDuplicateKeys(boolean allowDuplicateKeys) {
47     this.allowDuplicateKeys = allowDuplicateKeys;
48   }
49 
isWrappedToRootException()50   public final boolean isWrappedToRootException() {
51     return wrappedToRootException;
52   }
53 
54   /**
55    * Wrap runtime exception to YAMLException during parsing or leave them as they are
56    *
57    * Default is to leave original exceptions
58    *
59    * @param wrappedToRootException - true to convert runtime exception to YAMLException
60    */
setWrappedToRootException(boolean wrappedToRootException)61   public void setWrappedToRootException(boolean wrappedToRootException) {
62     this.wrappedToRootException = wrappedToRootException;
63   }
64 
getMaxAliasesForCollections()65   public final int getMaxAliasesForCollections() {
66     return maxAliasesForCollections;
67   }
68 
69   /**
70    * Restrict the amount of aliases for collections (sequences and mappings) to avoid
71    * https://en.wikipedia.org/wiki/Billion_laughs_attack
72    *
73    * @param maxAliasesForCollections set max allowed value (50 by default)
74    */
setMaxAliasesForCollections(int maxAliasesForCollections)75   public void setMaxAliasesForCollections(int maxAliasesForCollections) {
76     this.maxAliasesForCollections = maxAliasesForCollections;
77   }
78 
79   /**
80    * Allow recursive keys for mappings. By default, it is not allowed. This setting only prevents
81    * the case when the key is the value. If the key is only a part of the value (the value is a
82    * sequence or a mapping) then this case is not recognized and always allowed.
83    *
84    * @param allowRecursiveKeys - false to disable recursive keys
85    */
setAllowRecursiveKeys(boolean allowRecursiveKeys)86   public void setAllowRecursiveKeys(boolean allowRecursiveKeys) {
87     this.allowRecursiveKeys = allowRecursiveKeys;
88   }
89 
getAllowRecursiveKeys()90   public final boolean getAllowRecursiveKeys() {
91     return allowRecursiveKeys;
92   }
93 
94   /**
95    * Set the comment processing. By default, comments are ignored.
96    *
97    * @param processComments <code>true</code> to process; <code>false</code> to ignore
98    */
setProcessComments(boolean processComments)99   public LoaderOptions setProcessComments(boolean processComments) {
100     this.processComments = processComments;
101     return this;
102   }
103 
isProcessComments()104   public final boolean isProcessComments() {
105     return processComments;
106   }
107 
isEnumCaseSensitive()108   public final boolean isEnumCaseSensitive() {
109     return enumCaseSensitive;
110   }
111 
112   /**
113    * Disables or enables case sensitivity during construct enum constant from string value Default
114    * is false.
115    *
116    * @param enumCaseSensitive - true to set enum case sensitive, false the reverse
117    */
setEnumCaseSensitive(boolean enumCaseSensitive)118   public void setEnumCaseSensitive(boolean enumCaseSensitive) {
119     this.enumCaseSensitive = enumCaseSensitive;
120   }
121 
getNestingDepthLimit()122   public final int getNestingDepthLimit() {
123     return nestingDepthLimit;
124   }
125 
126   /**
127    * Set max depth of nested collections. When the limit is exceeded an exception is thrown.
128    * Aliases/Anchors are not counted. This is to prevent a DoS attack
129    *
130    * @param nestingDepthLimit - depth to be accepted (50 by default)
131    */
setNestingDepthLimit(int nestingDepthLimit)132   public void setNestingDepthLimit(int nestingDepthLimit) {
133     this.nestingDepthLimit = nestingDepthLimit;
134   }
135 
getCodePointLimit()136   public final int getCodePointLimit() {
137     return codePointLimit;
138   }
139 
140   /**
141    * The max amount of code points in the input YAML document. Please be aware that byte limit
142    * depends on the encoding.
143    *
144    * @param codePointLimit - the max allowed size of the YAML data
145    */
setCodePointLimit(int codePointLimit)146   public void setCodePointLimit(int codePointLimit) {
147     this.codePointLimit = codePointLimit;
148   }
149 }
150