Skip to content

Commit dd5a0bb

Browse files
Fix Metadata and IndexMetadata XContent Parsing not Draining Parser (#74844) (#75277)
Exactly the same as #73268 but for metadata and index metadata which can run into the same bug.
1 parent 736c91e commit dd5a0bb

File tree

3 files changed

+25
-32
lines changed

3 files changed

+25
-32
lines changed

server/src/main/java/org/elasticsearch/cluster/metadata/IndexMetadata.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323
import org.elasticsearch.cluster.block.ClusterBlockLevel;
2424
import org.elasticsearch.cluster.node.DiscoveryNodeFilters;
2525
import org.elasticsearch.cluster.routing.allocation.IndexMetadataUpdater;
26+
import org.elasticsearch.common.xcontent.ToXContent;
27+
import org.elasticsearch.common.xcontent.ToXContentFragment;
28+
import org.elasticsearch.common.xcontent.XContentBuilder;
29+
import org.elasticsearch.common.xcontent.XContentFactory;
30+
import org.elasticsearch.common.xcontent.XContentHelper;
31+
import org.elasticsearch.common.xcontent.XContentParser;
32+
import org.elasticsearch.common.xcontent.XContentParserUtils;
2633
import org.elasticsearch.core.Nullable;
2734
import org.elasticsearch.common.collect.ImmutableOpenIntMap;
2835
import org.elasticsearch.common.collect.ImmutableOpenMap;
@@ -34,12 +41,6 @@
3441
import org.elasticsearch.common.settings.Setting;
3542
import org.elasticsearch.common.settings.Setting.Property;
3643
import org.elasticsearch.common.settings.Settings;
37-
import org.elasticsearch.common.xcontent.ToXContent;
38-
import org.elasticsearch.common.xcontent.ToXContentFragment;
39-
import org.elasticsearch.common.xcontent.XContentBuilder;
40-
import org.elasticsearch.common.xcontent.XContentFactory;
41-
import org.elasticsearch.common.xcontent.XContentHelper;
42-
import org.elasticsearch.common.xcontent.XContentParser;
4344
import org.elasticsearch.gateway.MetadataStateFormat;
4445
import org.elasticsearch.index.Index;
4546
import org.elasticsearch.index.mapper.MapperService;
@@ -1502,16 +1503,12 @@ public static IndexMetadata fromXContent(XContentParser parser) throws IOExcepti
15021503
if (parser.currentToken() == XContentParser.Token.START_OBJECT) { // on a start object move to next token
15031504
parser.nextToken();
15041505
}
1505-
if (parser.currentToken() != XContentParser.Token.FIELD_NAME) {
1506-
throw new IllegalArgumentException("expected field name but got a " + parser.currentToken());
1507-
}
1506+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.currentToken(), parser);
15081507
Builder builder = new Builder(parser.currentName());
15091508

15101509
String currentFieldName = null;
15111510
XContentParser.Token token = parser.nextToken();
1512-
if (token != XContentParser.Token.START_OBJECT) {
1513-
throw new IllegalArgumentException("expected object but got a " + token);
1514-
}
1511+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser);
15151512
boolean mappingVersion = false;
15161513
boolean settingsVersion = false;
15171514
boolean aliasesVersion = false;
@@ -1594,11 +1591,8 @@ public static IndexMetadata fromXContent(XContentParser parser) throws IOExcepti
15941591
} else if (KEY_PRIMARY_TERMS.equals(currentFieldName)) {
15951592
LongArrayList list = new LongArrayList();
15961593
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
1597-
if (token == XContentParser.Token.VALUE_NUMBER) {
1598-
list.add(parser.longValue());
1599-
} else {
1600-
throw new IllegalStateException("found a non-numeric value under [" + KEY_PRIMARY_TERMS + "]");
1601-
}
1594+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, parser);
1595+
list.add(parser.longValue());
16021596
}
16031597
builder.primaryTerms(list.toArray());
16041598
} else {
@@ -1629,7 +1623,7 @@ public static IndexMetadata fromXContent(XContentParser parser) throws IOExcepti
16291623
throw new IllegalArgumentException("Unexpected token " + token);
16301624
}
16311625
}
1632-
1626+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.END_OBJECT, parser.nextToken(), parser);
16331627
Version indexVersion = indexCreatedVersion(builder.settings);
16341628
if (Assertions.ENABLED && indexVersion.onOrAfter(Version.V_6_5_0)) {
16351629
assert mappingVersion : "mapping version should be present for indices created on or after 6.5.0";

server/src/main/java/org/elasticsearch/cluster/metadata/Metadata.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
import org.elasticsearch.cluster.block.ClusterBlock;
2929
import org.elasticsearch.cluster.block.ClusterBlockLevel;
3030
import org.elasticsearch.cluster.coordination.CoordinationMetadata;
31+
import org.elasticsearch.common.xcontent.NamedObjectNotFoundException;
32+
import org.elasticsearch.common.xcontent.ToXContent;
33+
import org.elasticsearch.common.xcontent.ToXContentFragment;
34+
import org.elasticsearch.common.xcontent.XContentBuilder;
35+
import org.elasticsearch.common.xcontent.XContentHelper;
36+
import org.elasticsearch.common.xcontent.XContentParser;
37+
import org.elasticsearch.common.xcontent.XContentParserUtils;
3138
import org.elasticsearch.core.Nullable;
3239
import org.elasticsearch.common.Strings;
3340
import org.elasticsearch.common.UUIDs;
@@ -40,12 +47,6 @@
4047
import org.elasticsearch.common.settings.Setting;
4148
import org.elasticsearch.common.settings.Setting.Property;
4249
import org.elasticsearch.common.settings.Settings;
43-
import org.elasticsearch.common.xcontent.NamedObjectNotFoundException;
44-
import org.elasticsearch.common.xcontent.ToXContent;
45-
import org.elasticsearch.common.xcontent.ToXContentFragment;
46-
import org.elasticsearch.common.xcontent.XContentBuilder;
47-
import org.elasticsearch.common.xcontent.XContentHelper;
48-
import org.elasticsearch.common.xcontent.XContentParser;
4950
import org.elasticsearch.gateway.MetadataStateFormat;
5051
import org.elasticsearch.index.Index;
5152
import org.elasticsearch.index.IndexNotFoundException;
@@ -1787,22 +1788,17 @@ public static Metadata fromXContent(XContentParser parser) throws IOException {
17871788
token = parser.nextToken();
17881789
if (token == XContentParser.Token.START_OBJECT) {
17891790
// move to the field name (meta-data)
1790-
token = parser.nextToken();
1791-
if (token != XContentParser.Token.FIELD_NAME) {
1792-
throw new IllegalArgumentException("Expected a field name but got " + token);
1793-
}
1791+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser);
17941792
// move to the next object
17951793
token = parser.nextToken();
17961794
}
17971795
currentFieldName = parser.currentName();
17981796
}
17991797

1800-
if ("meta-data".equals(parser.currentName()) == false) {
1798+
if ("meta-data".equals(currentFieldName) == false) {
18011799
throw new IllegalArgumentException("Expected [meta-data] as a field name but got " + currentFieldName);
18021800
}
1803-
if (token != XContentParser.Token.START_OBJECT) {
1804-
throw new IllegalArgumentException("Expected a START_OBJECT but got " + token);
1805-
}
1801+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser);
18061802

18071803
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
18081804
if (token == XContentParser.Token.FIELD_NAME) {
@@ -1845,6 +1841,7 @@ public static Metadata fromXContent(XContentParser parser) throws IOException {
18451841
throw new IllegalArgumentException("Unexpected token " + token);
18461842
}
18471843
}
1844+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.END_OBJECT, parser.nextToken(), parser);
18481845
return builder.build();
18491846
}
18501847
}

server/src/main/java/org/elasticsearch/repositories/blobstore/ChecksumBlobStoreFormat.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.elasticsearch.common.xcontent.XContentBuilder;
2929
import org.elasticsearch.common.xcontent.XContentFactory;
3030
import org.elasticsearch.common.xcontent.XContentParser;
31+
import org.elasticsearch.common.xcontent.XContentParserUtils;
3132
import org.elasticsearch.common.xcontent.XContentType;
3233
import org.elasticsearch.gateway.CorruptStateException;
3334

@@ -108,6 +109,7 @@ public T deserialize(String repoName, NamedXContentRegistry namedXContentRegistr
108109
.createParser(namedXContentRegistry, LoggingDeprecationHandler.INSTANCE, wrappedStream)
109110
) {
110111
result = reader.apply(repoName, parser);
112+
XContentParserUtils.ensureExpectedToken(null, parser.nextToken(), parser);
111113
}
112114
deserializeMetaBlobInputStream.verifyFooter();
113115
return result;

0 commit comments

Comments
 (0)