Skip to content

Commit

Permalink
Fix template alias parsing livelock (elastic#112217) (elastic#112245)
Browse files Browse the repository at this point in the history
* Fix template alias parsing livelock

This commit fixes an issue with templates parsing alias definitions that can cause the ES thread to
hang indefinitely. Due to the malformed alias definition, the parsing gets into a loop which never
exits. In this commit a null check in both the component template and alias parsing code is added,
which prevents the looping.
  • Loading branch information
dakrone authored Aug 27, 2024
1 parent 7b3d73c commit 6fc00ad
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/changelog/112217.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 112217
summary: Fix template alias parsing livelock
area: Indices APIs
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ public static AliasMetadata fromXContent(XContentParser parser) throws IOExcepti
} else if ("is_hidden".equals(currentFieldName)) {
builder.isHidden(parser.booleanValue());
}
} else if (token == null) {
throw new IllegalArgumentException("unexpected null token while parsing alias");
}
}
return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ public class Template implements SimpleDiffable<Template>, ToXContentObject {
}, MAPPINGS, ObjectParser.ValueType.VALUE_OBJECT_ARRAY);
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> {
Map<String, AliasMetadata> aliasMap = new HashMap<>();
while ((p.nextToken()) != XContentParser.Token.END_OBJECT) {
XContentParser.Token token;
while ((token = p.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == null) {
break;
}
AliasMetadata alias = AliasMetadata.Builder.fromXContent(p);
aliasMap.put(alias.alias(), alias);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentFactory;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentParserConfiguration;
import org.elasticsearch.xcontent.XContentType;

import java.io.IOException;
Expand Down Expand Up @@ -307,4 +308,22 @@ public void testXContentSerializationWithRolloverAndEffectiveRetention() throws
assertThat(serialized, not(containsString("effective_retention")));
}
}

public void testHangingParsing() throws IOException {
String cutDown = """
{
"template": {
"aliases": {
"foo": "bar"
},
"food": "eggplant"
},
"potato": true
}
""";

try (XContentParser parser = XContentType.JSON.xContent().createParser(XContentParserConfiguration.EMPTY, cutDown)) {
expectThrows(Exception.class, () -> ComponentTemplate.parse(parser));
}
}
}

0 comments on commit 6fc00ad

Please sign in to comment.