Skip to content

Commit d52d89c

Browse files
committed
Add a system property to configure YamlParser codepoint limits (#12301)
Signed-off-by: Andriy Redko <andriy.redko@aiven.io> (cherry picked from commit bff8eb7)
1 parent dfa41ea commit d52d89c

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
184184
- [Bug] Check phase name before SearchRequestOperationsListener onPhaseStart ([#12094](https://github.com/opensearch-project/OpenSearch/pull/12094))
185185
- Add advance(int) for numeric values in order to allow point based optimization to kick in ([#12089](https://github.com/opensearch-project/OpenSearch/pull/12089))
186186
- Fix Span operation names generated from RestActions ([#12005](https://github.com/opensearch-project/OpenSearch/pull/12005))
187+
- Add a system property to configure YamlParser codepoint limits ([#12298](https://github.com/opensearch-project/OpenSearch/pull/12298))
187188

188189
### Security
189190

libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentContraints.java

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020
@InternalApi
2121
public interface XContentContraints {
22+
final String DEFAULT_CODEPOINT_LIMIT_PROPERTY = "opensearch.xcontent.codepoint.max";
2223
final String DEFAULT_MAX_STRING_LEN_PROPERTY = "opensearch.xcontent.string.length.max";
2324
final String DEFAULT_MAX_NAME_LEN_PROPERTY = "opensearch.xcontent.name.length.max";
2425
final String DEFAULT_MAX_DEPTH_PROPERTY = "opensearch.xcontent.depth.max";
@@ -34,4 +35,6 @@ public interface XContentContraints {
3435
final int DEFAULT_MAX_DEPTH = Integer.parseInt(
3536
System.getProperty(DEFAULT_MAX_DEPTH_PROPERTY, Integer.toString(Integer.MAX_VALUE) /* no limit */ )
3637
);
38+
39+
final int DEFAULT_CODEPOINT_LIMIT = Integer.parseInt(System.getProperty(DEFAULT_CODEPOINT_LIMIT_PROPERTY, "52428800" /* ~50 Mb */));
3740
}

libs/x-content/src/main/java/org/opensearch/common/xcontent/yaml/YamlXContent.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.fasterxml.jackson.core.StreamWriteConstraints;
4040
import com.fasterxml.jackson.core.StreamWriteFeature;
4141
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
42+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactoryBuilder;
4243

4344
import org.opensearch.common.xcontent.XContentContraints;
4445
import org.opensearch.common.xcontent.XContentType;
@@ -56,6 +57,8 @@
5657
import java.io.Reader;
5758
import java.util.Set;
5859

60+
import org.yaml.snakeyaml.LoaderOptions;
61+
5962
/**
6063
* A YAML based content implementation using Jackson.
6164
*/
@@ -70,7 +73,9 @@ public static XContentBuilder contentBuilder() throws IOException {
7073
public static final YamlXContent yamlXContent;
7174

7275
static {
73-
yamlFactory = new YAMLFactory();
76+
final LoaderOptions loaderOptions = new LoaderOptions();
77+
loaderOptions.setCodePointLimit(DEFAULT_CODEPOINT_LIMIT);
78+
yamlFactory = new YAMLFactoryBuilder(new YAMLFactory()).loaderOptions(loaderOptions).build();
7479
yamlFactory.configure(JsonParser.Feature.STRICT_DUPLICATE_DETECTION, true);
7580
yamlFactory.setStreamWriteConstraints(StreamWriteConstraints.builder().maxNestingDepth(DEFAULT_MAX_DEPTH).build());
7681
yamlFactory.setStreamReadConstraints(

libs/x-content/src/test/java/org/opensearch/common/xcontent/XContentParserTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public class XContentParserTests extends OpenSearchTestCase {
7979
() -> randomAlphaOfLengthBetween(1, SmileXContent.DEFAULT_MAX_STRING_LEN / 10), /* limit to ~200Mb */
8080
/* YAML parser limitation */
8181
XContentType.YAML,
82-
() -> randomAlphaOfLengthBetween(1, 3140000)
82+
() -> randomRealisticUnicodeOfCodepointLengthBetween(1, YamlXContent.DEFAULT_CODEPOINT_LIMIT)
8383
);
8484

8585
private static final Map<XContentType, Supplier<String>> FIELD_NAME_GENERATORS = Map.of(
@@ -106,7 +106,7 @@ public class XContentParserTests extends OpenSearchTestCase {
106106

107107
public void testStringOffLimit() throws IOException {
108108
final String field = randomAlphaOfLengthBetween(1, 5);
109-
final String value = randomRealisticUnicodeOfCodepointLength(3145730);
109+
final String value = randomRealisticUnicodeOfCodepointLength(YamlXContent.DEFAULT_CODEPOINT_LIMIT + 1);
110110

111111
try (XContentBuilder builder = XContentBuilder.builder(XContentType.YAML.xContent())) {
112112
builder.startObject();

0 commit comments

Comments
 (0)