@@ -42,6 +42,21 @@ public AnchorContext(String anchor) {
42
42
}
43
43
}
44
44
45
+ /**
46
+ * the maximum number of events that can be replayed
47
+ */
48
+ public static final int MAX_EVENTS = 9999 ;
49
+
50
+ /**
51
+ * the maximum limit of anchors to remember
52
+ */
53
+ public static final int MAX_ANCHORS = 9999 ;
54
+
55
+ /**
56
+ * the maximum limit of merges to follow
57
+ */
58
+ public static final int MAX_MERGES = 9999 ;
59
+
45
60
/**
46
61
* Remembers when a merge has been started in order to skip the corresponding
47
62
* sequence end which needs to be excluded
@@ -73,9 +88,12 @@ public YAMLAnchorReplayingParser(IOContext ctxt, int parserFeatures, int formatF
73
88
}
74
89
75
90
private void finishContext (AnchorContext context ) {
91
+ if (referencedObjects .size () + 1 > MAX_REFS ) throw new IllegalStateException ("too many references in the document" );
76
92
referencedObjects .put (context .anchor , context .events );
77
93
if (!tokenStack .isEmpty ()) {
78
- tokenStack .peek ().events .addAll (context .events );
94
+ List <Event > events = tokenStack .peek ().events ;
95
+ if (events .size () + context .events .size () > MAX_EVENTS ) throw new IllegalStateException ("too many events to replay" );
96
+ events .addAll (context .events );
79
97
}
80
98
}
81
99
@@ -118,6 +136,7 @@ protected Event getEvent() {
118
136
AliasEvent alias = (AliasEvent ) event ;
119
137
List <Event > events = referencedObjects .get (alias .getAnchor ());
120
138
if (events != null ) {
139
+ if (refEvents .size () + events .size () > MAX_EVENTS ) throw new IllegalStateException ("too many events to replay" );
121
140
refEvents .addAll (events );
122
141
return refEvents .removeFirst ();
123
142
}
@@ -130,6 +149,7 @@ protected Event getEvent() {
130
149
AnchorContext context = new AnchorContext (anchor );
131
150
context .events .add (event );
132
151
if (event instanceof CollectionStartEvent ) {
152
+ if (tokenStack .size () + 1 > MAX_ANCHORS ) throw new IllegalStateException ("too many anchors in the document" );
133
153
tokenStack .push (context );
134
154
} else {
135
155
// directly store it
@@ -145,6 +165,7 @@ protected Event getEvent() {
145
165
// expect next node to be a map
146
166
Event next = getEvent ();
147
167
if (next instanceof MappingStartEvent ) {
168
+ if (mergeStack .size () + 1 > MAX_MERGES ) throw new IllegalStateException ("too many merges in the document" );
148
169
mergeStack .push (globalDepth );
149
170
return getEvent ();
150
171
}
@@ -154,6 +175,7 @@ protected Event getEvent() {
154
175
155
176
if (!tokenStack .isEmpty ()) {
156
177
AnchorContext context = tokenStack .peek ();
178
+ if (context .events .size () + 1 > MAX_EVENTS ) throw new IllegalStateException ("too many events to replay" );
157
179
context .events .add (event );
158
180
if (event instanceof CollectionStartEvent ) {
159
181
++context .depth ;
0 commit comments