Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow tuning loader options #333

Merged
merged 19 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions src/main/scala/io/circe/yaml/Parser.scala
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package io.circe.yaml

import Parser._
import cats.syntax.either._
import io.circe._
import io.circe.yaml.Parser._
import java.io.{ Reader, StringReader }
import org.yaml.snakeyaml.LoaderOptions
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.{ LoaderOptions, Yaml }
import org.yaml.snakeyaml.constructor.SafeConstructor
import org.yaml.snakeyaml.nodes._
import scala.collection.JavaConverters._

final case class Parser(
maxAliasesForCollections: Int = 50
maxAliasesForCollections: Int = Parser.defaultMaxAliasesForCollections,
nestingDepthLimit: Int = Parser.defaultNestingDepthLimit,
codePointLimit: Int = Parser.defaultCodePointLimit
) {

private val loaderOptions = {
val options = new LoaderOptions()
options.setMaxAliasesForCollections(maxAliasesForCollections)
options.setNestingDepthLimit(nestingDepthLimit)
options.setCodePointLimit(codePointLimit)
options
}

Expand All @@ -40,19 +43,43 @@ final case class Parser(

private[this] def parseStream(reader: Reader): Stream[Node] =
new Yaml(loaderOptions).composeAll(reader).asScala.toStream

def copy(
maxAliasesForCollections: Int = this.maxAliasesForCollections,
nestingDepthLimit: Int = this.nestingDepthLimit,
codePointLimit: Int = this.codePointLimit
): Parser = new Parser(maxAliasesForCollections, nestingDepthLimit, codePointLimit)

def copy(maxAliasesForCollections: Int): Parser = new Parser(
maxAliasesForCollections = maxAliasesForCollections,
nestingDepthLimit = this.nestingDepthLimit,
codePointLimit = this.codePointLimit
)

def this(maxAliasesForCollections: Int) =
this(maxAliasesForCollections, Parser.defaultNestingDepthLimit, Parser.defaultCodePointLimit)
}

object Parser {
val defaultMaxAliasesForCollections: Int = 50 // to prevent YAML at
// https://en.wikipedia.org/wiki/Billion_laughs_attack
val defaultNestingDepthLimit: Int = 50
val defaultCodePointLimit: Int = 3 * 1024 * 1024 // 3MB

val default: Parser = Parser()

def apply(maxAliasesForCollections: Int): Parser =
new Parser(maxAliasesForCollections = maxAliasesForCollections)

private[yaml] object CustomTag {
def unapply(tag: Tag): Option[String] = if (!tag.startsWith(Tag.PREFIX))
Some(tag.getValue)
else
None
}

private[yaml] class FlatteningConstructor extends SafeConstructor {
private[yaml] class FlatteningConstructor(val loaderOptions: LoaderOptions = new LoaderOptions)
extends SafeConstructor(loaderOptions) {
lucaviolanti marked this conversation as resolved.
Show resolved Hide resolved
def flatten(node: MappingNode): MappingNode = {
flattenMapping(node)
node
Expand Down
57 changes: 55 additions & 2 deletions src/test/scala/io/circe/yaml/ParserTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ class ParserTests extends AnyFlatSpec with Matchers with EitherValues {
.parse(
""
)
.right
.value == Json.False
)
}
Expand All @@ -82,7 +81,6 @@ class ParserTests extends AnyFlatSpec with Matchers with EitherValues {
.parse(
" "
)
.right
.value == Json.False
)
}
Expand Down Expand Up @@ -122,4 +120,59 @@ class ParserTests extends AnyFlatSpec with Matchers with EitherValues {
.isLeft
)
}

it should "parse when within depth limits" in {
assert(
Parser(nestingDepthLimit = 3)
.parse(
"""
| foo:
| bar:
| baz
|""".stripMargin
)
.isRight
)
}

it should "fail to parse when depth limit is exceeded" in {
assert(
Parser(nestingDepthLimit = 1)
.parse(
"""
| foo:
| bar:
| baz
|""".stripMargin
)
.isLeft
)
}

it should "parse when within code point limit" in {
assert(
Parser(codePointLimit = 1 * 1024 * 1024) // 1MB
.parse(
"""
| foo:
| bar:
| baz
|""".stripMargin
)
.isRight
)
}

it should "fail to parse when code point limit is exceeded" in {
assert(
Parser(codePointLimit = 13) // 13B
.parse(
"""
| foo:
| bar
|""".stripMargin
)
.isLeft
)
}
}