From 66953b5f15405ae7fe7827c4c261b7577ad2d374 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Wed, 10 Apr 2024 12:55:02 +0200 Subject: [PATCH] Make SnakeYAML's code point limit configurable Resolves #517. --- src/commonMain/kotlin/com/charleskorn/kaml/Yaml.kt | 2 +- .../kotlin/com/charleskorn/kaml/YamlConfiguration.kt | 2 ++ src/commonMain/kotlin/com/charleskorn/kaml/YamlParser.kt | 7 +++++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/commonMain/kotlin/com/charleskorn/kaml/Yaml.kt b/src/commonMain/kotlin/com/charleskorn/kaml/Yaml.kt index a67f3131..c2081362 100644 --- a/src/commonMain/kotlin/com/charleskorn/kaml/Yaml.kt +++ b/src/commonMain/kotlin/com/charleskorn/kaml/Yaml.kt @@ -69,7 +69,7 @@ public class Yaml( parseToYamlNode(string.bufferedSource()) internal fun parseToYamlNode(source: Source): YamlNode { - val parser = YamlParser(source) + val parser = YamlParser(source, configuration.codePointLimit) val reader = YamlNodeReader(parser, configuration.extensionDefinitionPrefix, configuration.allowAnchorsAndAliases) val node = reader.read() diff --git a/src/commonMain/kotlin/com/charleskorn/kaml/YamlConfiguration.kt b/src/commonMain/kotlin/com/charleskorn/kaml/YamlConfiguration.kt index 3ffc5b3f..84a46cff 100644 --- a/src/commonMain/kotlin/com/charleskorn/kaml/YamlConfiguration.kt +++ b/src/commonMain/kotlin/com/charleskorn/kaml/YamlConfiguration.kt @@ -34,6 +34,7 @@ package com.charleskorn.kaml * * [ambiguousQuoteStyle]: how strings should be escaped when [singleLineStringStyle] is [SingleLineStringStyle.PlainExceptAmbiguous] and the value is ambiguous * * [sequenceBlockIndent]: number of spaces to use as indentation for sequences, if [sequenceStyle] set to [SequenceStyle.Block] * * [allowAnchorsAndAliases]: set to true to allow anchors and aliases when decoding YAML (defaults to `false`) + * * [codePointLimit]: the maximum amount of code points allowed in the input YAML document (defaults to 3 MB) */ public data class YamlConfiguration( internal val encodeDefaults: Boolean = true, @@ -50,6 +51,7 @@ public data class YamlConfiguration( internal val sequenceBlockIndent: Int = 0, internal val allowAnchorsAndAliases: Boolean = false, internal val yamlNamingStrategy: YamlNamingStrategy? = null, + internal val codePointLimit: Int? = null, ) public enum class PolymorphismStyle { diff --git a/src/commonMain/kotlin/com/charleskorn/kaml/YamlParser.kt b/src/commonMain/kotlin/com/charleskorn/kaml/YamlParser.kt index 30584e61..052cd436 100644 --- a/src/commonMain/kotlin/com/charleskorn/kaml/YamlParser.kt +++ b/src/commonMain/kotlin/com/charleskorn/kaml/YamlParser.kt @@ -26,11 +26,14 @@ import org.snakeyaml.engine.v2.exceptions.MarkedYamlEngineException import org.snakeyaml.engine.v2.parser.ParserImpl import org.snakeyaml.engine.v2.scanner.StreamReader -internal class YamlParser(reader: Source) { +internal class YamlParser(reader: Source, codePointLimit: Int? = null) { internal constructor(source: String) : this(source.bufferedSource()) private val dummyFileName = "DUMMY_FILE_NAME" - private val loadSettings = LoadSettings.builder().setLabel(dummyFileName).build() + private val loadSettings = LoadSettings.builder().apply { + if (codePointLimit != null) setCodePointLimit(codePointLimit) + setLabel(dummyFileName) + }.build() private val streamReader = StreamReader(loadSettings, reader) private val events = ParserImpl(loadSettings, streamReader)