Skip to content

Commit 667f95b

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

File tree

3 files changed

+25
-31
lines changed

3 files changed

+25
-31
lines changed

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

Lines changed: 12 additions & 17 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;
@@ -1422,16 +1423,12 @@ public static IndexMetadata fromXContent(XContentParser parser) throws IOExcepti
14221423
if (parser.currentToken() == XContentParser.Token.START_OBJECT) { // on a start object move to next token
14231424
parser.nextToken();
14241425
}
1425-
if (parser.currentToken() != XContentParser.Token.FIELD_NAME) {
1426-
throw new IllegalArgumentException("expected field name but got a " + parser.currentToken());
1427-
}
1426+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.currentToken(), parser);
14281427
Builder builder = new Builder(parser.currentName());
14291428

14301429
String currentFieldName = null;
14311430
XContentParser.Token token = parser.nextToken();
1432-
if (token != XContentParser.Token.START_OBJECT) {
1433-
throw new IllegalArgumentException("expected object but got a " + token);
1434-
}
1431+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser);
14351432
boolean mappingVersion = false;
14361433
boolean settingsVersion = false;
14371434
boolean aliasesVersion = false;
@@ -1514,11 +1511,8 @@ public static IndexMetadata fromXContent(XContentParser parser) throws IOExcepti
15141511
} else if (KEY_PRIMARY_TERMS.equals(currentFieldName)) {
15151512
LongArrayList list = new LongArrayList();
15161513
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
1517-
if (token == XContentParser.Token.VALUE_NUMBER) {
1518-
list.add(parser.longValue());
1519-
} else {
1520-
throw new IllegalStateException("found a non-numeric value under [" + KEY_PRIMARY_TERMS + "]");
1521-
}
1514+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, parser);
1515+
list.add(parser.longValue());
15221516
}
15231517
builder.primaryTerms(list.toArray());
15241518
} else {
@@ -1549,6 +1543,7 @@ public static IndexMetadata fromXContent(XContentParser parser) throws IOExcepti
15491543
throw new IllegalArgumentException("Unexpected token " + token);
15501544
}
15511545
}
1546+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.END_OBJECT, parser.nextToken(), parser);
15521547
if (Assertions.ENABLED) {
15531548
assert mappingVersion : "mapping version should be present for indices created on or after 6.5.0";
15541549
}

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525
import org.elasticsearch.cluster.block.ClusterBlock;
2626
import org.elasticsearch.cluster.block.ClusterBlockLevel;
2727
import org.elasticsearch.cluster.coordination.CoordinationMetadata;
28+
import org.elasticsearch.common.xcontent.NamedObjectNotFoundException;
29+
import org.elasticsearch.common.xcontent.ToXContent;
30+
import org.elasticsearch.common.xcontent.ToXContentFragment;
31+
import org.elasticsearch.common.xcontent.XContentBuilder;
32+
import org.elasticsearch.common.xcontent.XContentHelper;
33+
import org.elasticsearch.common.xcontent.XContentParser;
34+
import org.elasticsearch.common.xcontent.XContentParserUtils;
2835
import org.elasticsearch.core.Nullable;
2936
import org.elasticsearch.common.Strings;
3037
import org.elasticsearch.common.UUIDs;
@@ -37,12 +44,6 @@
3744
import org.elasticsearch.common.settings.Setting;
3845
import org.elasticsearch.common.settings.Setting.Property;
3946
import org.elasticsearch.common.settings.Settings;
40-
import org.elasticsearch.common.xcontent.NamedObjectNotFoundException;
41-
import org.elasticsearch.common.xcontent.ToXContent;
42-
import org.elasticsearch.common.xcontent.ToXContentFragment;
43-
import org.elasticsearch.common.xcontent.XContentBuilder;
44-
import org.elasticsearch.common.xcontent.XContentHelper;
45-
import org.elasticsearch.common.xcontent.XContentParser;
4647
import org.elasticsearch.gateway.MetadataStateFormat;
4748
import org.elasticsearch.index.Index;
4849
import org.elasticsearch.index.IndexNotFoundException;
@@ -1653,22 +1654,17 @@ public static Metadata fromXContent(XContentParser parser) throws IOException {
16531654
token = parser.nextToken();
16541655
if (token == XContentParser.Token.START_OBJECT) {
16551656
// move to the field name (meta-data)
1656-
token = parser.nextToken();
1657-
if (token != XContentParser.Token.FIELD_NAME) {
1658-
throw new IllegalArgumentException("Expected a field name but got " + token);
1659-
}
1657+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser);
16601658
// move to the next object
16611659
token = parser.nextToken();
16621660
}
16631661
currentFieldName = parser.currentName();
16641662
}
16651663

1666-
if ("meta-data".equals(parser.currentName()) == false) {
1664+
if ("meta-data".equals(currentFieldName) == false) {
16671665
throw new IllegalArgumentException("Expected [meta-data] as a field name but got " + currentFieldName);
16681666
}
1669-
if (token != XContentParser.Token.START_OBJECT) {
1670-
throw new IllegalArgumentException("Expected a START_OBJECT but got " + token);
1671-
}
1667+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser);
16721668

16731669
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
16741670
if (token == XContentParser.Token.FIELD_NAME) {
@@ -1711,6 +1707,7 @@ public static Metadata fromXContent(XContentParser parser) throws IOException {
17111707
throw new IllegalArgumentException("Unexpected token " + token);
17121708
}
17131709
}
1710+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.END_OBJECT, parser.nextToken(), parser);
17141711
return builder.build();
17151712
}
17161713
}

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)