Skip to content

Commit 0eaa3d2

Browse files
Stricter Parsing Shard Level Repository Metadata (#73269) (#73291)
Similar to #73268 we should be stricter here, especially when we are super-strict about additional fields anyway. Also, use our parser exception utils to get better exceptions if parsing fails.
1 parent 6671331 commit 0eaa3d2

File tree

2 files changed

+89
-93
lines changed

2 files changed

+89
-93
lines changed

server/src/main/java/org/elasticsearch/index/snapshots/blobstore/BlobStoreIndexShardSnapshot.java

Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -273,38 +273,37 @@ public static FileInfo fromXContent(XContentParser parser) throws IOException {
273273
Version writtenBy = null;
274274
String writtenByStr = null;
275275
BytesRef metaHash = new BytesRef();
276-
if (token == XContentParser.Token.START_OBJECT) {
277-
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
278-
if (token == XContentParser.Token.FIELD_NAME) {
279-
String currentFieldName = parser.currentName();
280-
token = parser.nextToken();
281-
if (token.isValue()) {
282-
if (NAME.equals(currentFieldName)) {
283-
name = parser.text();
284-
} else if (PHYSICAL_NAME.equals(currentFieldName)) {
285-
physicalName = parser.text();
286-
} else if (LENGTH.equals(currentFieldName)) {
287-
length = parser.longValue();
288-
} else if (CHECKSUM.equals(currentFieldName)) {
289-
checksum = parser.text();
290-
} else if (PART_SIZE.equals(currentFieldName)) {
291-
partSize = new ByteSizeValue(parser.longValue());
292-
} else if (WRITTEN_BY.equals(currentFieldName)) {
293-
writtenByStr = parser.text();
294-
writtenBy = Lucene.parseVersionLenient(writtenByStr, null);
295-
} else if (META_HASH.equals(currentFieldName)) {
296-
metaHash.bytes = parser.binaryValue();
297-
metaHash.offset = 0;
298-
metaHash.length = metaHash.bytes.length;
299-
} else {
300-
throw new ElasticsearchParseException("unknown parameter [{}]", currentFieldName);
301-
}
276+
XContentParserUtils.ensureExpectedToken(token, XContentParser.Token.START_OBJECT, parser);
277+
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
278+
if (token == XContentParser.Token.FIELD_NAME) {
279+
String currentFieldName = parser.currentName();
280+
token = parser.nextToken();
281+
if (token.isValue()) {
282+
if (NAME.equals(currentFieldName)) {
283+
name = parser.text();
284+
} else if (PHYSICAL_NAME.equals(currentFieldName)) {
285+
physicalName = parser.text();
286+
} else if (LENGTH.equals(currentFieldName)) {
287+
length = parser.longValue();
288+
} else if (CHECKSUM.equals(currentFieldName)) {
289+
checksum = parser.text();
290+
} else if (PART_SIZE.equals(currentFieldName)) {
291+
partSize = new ByteSizeValue(parser.longValue());
292+
} else if (WRITTEN_BY.equals(currentFieldName)) {
293+
writtenByStr = parser.text();
294+
writtenBy = Lucene.parseVersionLenient(writtenByStr, null);
295+
} else if (META_HASH.equals(currentFieldName)) {
296+
metaHash.bytes = parser.binaryValue();
297+
metaHash.offset = 0;
298+
metaHash.length = metaHash.bytes.length;
302299
} else {
303-
throw new ElasticsearchParseException("unexpected token [{}]", token);
300+
XContentParserUtils.throwUnknownField(currentFieldName, parser.getTokenLocation());
304301
}
305302
} else {
306-
throw new ElasticsearchParseException("unexpected token [{}]",token);
303+
XContentParserUtils.throwUnknownToken(token, parser.getTokenLocation());
307304
}
305+
} else {
306+
XContentParserUtils.throwUnknownToken(token, parser.getTokenLocation());
308307
}
309308
}
310309

@@ -513,39 +512,38 @@ public static BlobStoreIndexShardSnapshot fromXContent(XContentParser parser) th
513512
parser.nextToken();
514513
}
515514
XContentParser.Token token = parser.currentToken();
516-
if (token == XContentParser.Token.START_OBJECT) {
517-
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
518-
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);
519-
final String currentFieldName = parser.currentName();
520-
token = parser.nextToken();
521-
if (token.isValue()) {
522-
if (PARSE_NAME.match(currentFieldName, parser.getDeprecationHandler())) {
523-
snapshot = parser.text();
524-
} else if (PARSE_INDEX_VERSION.match(currentFieldName, parser.getDeprecationHandler())) {
525-
// The index-version is needed for backward compatibility with v 1.0
526-
indexVersion = parser.longValue();
527-
} else if (PARSE_START_TIME.match(currentFieldName, parser.getDeprecationHandler())) {
528-
startTime = parser.longValue();
529-
} else if (PARSE_TIME.match(currentFieldName, parser.getDeprecationHandler())) {
530-
time = parser.longValue();
531-
} else if (PARSE_INCREMENTAL_FILE_COUNT.match(currentFieldName, parser.getDeprecationHandler())) {
532-
incrementalFileCount = parser.intValue();
533-
} else if (PARSE_INCREMENTAL_SIZE.match(currentFieldName, parser.getDeprecationHandler())) {
534-
incrementalSize = parser.longValue();
535-
} else {
536-
throw new ElasticsearchParseException("unknown parameter [{}]", currentFieldName);
537-
}
538-
} else if (token == XContentParser.Token.START_ARRAY) {
539-
if (PARSE_FILES.match(currentFieldName, parser.getDeprecationHandler())) {
540-
while ((parser.nextToken()) != XContentParser.Token.END_ARRAY) {
541-
indexFiles.add(FileInfo.fromXContent(parser));
542-
}
543-
} else {
544-
throw new ElasticsearchParseException("unknown parameter [{}]", currentFieldName);
515+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser);
516+
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
517+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);
518+
final String currentFieldName = parser.currentName();
519+
token = parser.nextToken();
520+
if (token.isValue()) {
521+
if (PARSE_NAME.match(currentFieldName, parser.getDeprecationHandler())) {
522+
snapshot = parser.text();
523+
} else if (PARSE_INDEX_VERSION.match(currentFieldName, parser.getDeprecationHandler())) {
524+
// The index-version is needed for backward compatibility with v 1.0
525+
indexVersion = parser.longValue();
526+
} else if (PARSE_START_TIME.match(currentFieldName, parser.getDeprecationHandler())) {
527+
startTime = parser.longValue();
528+
} else if (PARSE_TIME.match(currentFieldName, parser.getDeprecationHandler())) {
529+
time = parser.longValue();
530+
} else if (PARSE_INCREMENTAL_FILE_COUNT.match(currentFieldName, parser.getDeprecationHandler())) {
531+
incrementalFileCount = parser.intValue();
532+
} else if (PARSE_INCREMENTAL_SIZE.match(currentFieldName, parser.getDeprecationHandler())) {
533+
incrementalSize = parser.longValue();
534+
} else {
535+
XContentParserUtils.throwUnknownField(currentFieldName, parser.getTokenLocation());
536+
}
537+
} else if (token == XContentParser.Token.START_ARRAY) {
538+
if (PARSE_FILES.match(currentFieldName, parser.getDeprecationHandler())) {
539+
while ((parser.nextToken()) != XContentParser.Token.END_ARRAY) {
540+
indexFiles.add(FileInfo.fromXContent(parser));
545541
}
546542
} else {
547-
throw new ElasticsearchParseException("unexpected token [{}]", token);
543+
XContentParserUtils.throwUnknownField(currentFieldName, parser.getTokenLocation());
548544
}
545+
} else {
546+
XContentParserUtils.throwUnknownToken(token, parser.getTokenLocation());
549547
}
550548
}
551549

server/src/main/java/org/elasticsearch/index/snapshots/blobstore/BlobStoreIndexShardSnapshots.java

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
package org.elasticsearch.index.snapshots.blobstore;
1010

11-
import org.elasticsearch.ElasticsearchParseException;
1211
import org.elasticsearch.common.ParseField;
1312
import org.elasticsearch.common.util.CollectionUtils;
1413
import org.elasticsearch.common.xcontent.ToXContentFragment;
@@ -248,47 +247,46 @@ public static BlobStoreIndexShardSnapshots fromXContent(XContentParser parser) t
248247
Map<String, List<String>> snapshotsMap = new HashMap<>();
249248
Map<String, String> historyUUIDs = new HashMap<>();
250249
Map<String, FileInfo> files = new HashMap<>();
251-
if (token == XContentParser.Token.START_OBJECT) {
252-
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
253-
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);
254-
String currentFieldName = parser.currentName();
255-
token = parser.nextToken();
256-
if (token == XContentParser.Token.START_ARRAY) {
257-
if (ParseFields.FILES.match(currentFieldName, parser.getDeprecationHandler()) == false) {
258-
throw new ElasticsearchParseException("unknown array [{}]", currentFieldName);
259-
}
260-
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
261-
FileInfo fileInfo = FileInfo.fromXContent(parser);
262-
files.put(fileInfo.name(), fileInfo);
263-
}
264-
} else if (token == XContentParser.Token.START_OBJECT) {
265-
if (ParseFields.SNAPSHOTS.match(currentFieldName, parser.getDeprecationHandler()) == false) {
266-
throw new ElasticsearchParseException("unknown object [{}]", currentFieldName);
267-
}
250+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser);
251+
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
252+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);
253+
String currentFieldName = parser.currentName();
254+
token = parser.nextToken();
255+
if (token == XContentParser.Token.START_ARRAY) {
256+
if (ParseFields.FILES.match(currentFieldName, parser.getDeprecationHandler()) == false) {
257+
XContentParserUtils.throwUnknownField(currentFieldName, parser.getTokenLocation());
258+
}
259+
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
260+
FileInfo fileInfo = FileInfo.fromXContent(parser);
261+
files.put(fileInfo.name(), fileInfo);
262+
}
263+
} else if (token == XContentParser.Token.START_OBJECT) {
264+
if (ParseFields.SNAPSHOTS.match(currentFieldName, parser.getDeprecationHandler()) == false) {
265+
XContentParserUtils.throwUnknownField(currentFieldName, parser.getTokenLocation());
266+
}
267+
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
268+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);
269+
String snapshot = parser.currentName();
270+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
268271
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
269-
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);
270-
String snapshot = parser.currentName();
271-
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
272-
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
273-
if (token == XContentParser.Token.FIELD_NAME) {
274-
currentFieldName = parser.currentName();
275-
if (ParseFields.FILES.match(currentFieldName, parser.getDeprecationHandler()) &&
272+
if (token == XContentParser.Token.FIELD_NAME) {
273+
currentFieldName = parser.currentName();
274+
if (ParseFields.FILES.match(currentFieldName, parser.getDeprecationHandler()) &&
276275
parser.nextToken() == XContentParser.Token.START_ARRAY) {
277-
List<String> fileNames = new ArrayList<>();
278-
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
279-
fileNames.add(parser.text());
280-
}
281-
snapshotsMap.put(snapshot, fileNames);
282-
} else if (ParseFields.SHARD_STATE_ID.match(currentFieldName, parser.getDeprecationHandler())) {
283-
parser.nextToken();
284-
historyUUIDs.put(snapshot, parser.text());
276+
List<String> fileNames = new ArrayList<>();
277+
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
278+
fileNames.add(parser.text());
285279
}
280+
snapshotsMap.put(snapshot, fileNames);
281+
} else if (ParseFields.SHARD_STATE_ID.match(currentFieldName, parser.getDeprecationHandler())) {
282+
parser.nextToken();
283+
historyUUIDs.put(snapshot, parser.text());
286284
}
287285
}
288286
}
289-
} else {
290-
throw new ElasticsearchParseException("unexpected token [{}]", token);
291287
}
288+
} else {
289+
XContentParserUtils.throwUnknownToken(token, parser.getTokenLocation());
292290
}
293291
}
294292

0 commit comments

Comments
 (0)