Skip to content

Commit

Permalink
Merge pull request from GHSA-g56x-7j6w-g8r8
Browse files Browse the repository at this point in the history
Add limits to parser; improve variable and fragment validation
  • Loading branch information
armanbilge authored Dec 18, 2023
2 parents 581f278 + 05dde88 commit 56e244b
Show file tree
Hide file tree
Showing 17 changed files with 1,854 additions and 939 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ ThisBuild / scalaVersion := Scala2
ThisBuild / crossScalaVersions := Seq(Scala2, Scala3)
ThisBuild / tlJdkRelease := Some(11)

ThisBuild / tlBaseVersion := "0.17"
ThisBuild / tlBaseVersion := "0.18"
ThisBuild / startYear := Some(2019)
ThisBuild / licenses := Seq(License.Apache2)
ThisBuild / developers := List(
Expand Down
18 changes: 11 additions & 7 deletions modules/core/src/main/scala-2/syntax2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import cats.data.NonEmptyChain
import cats.syntax.all._
import org.typelevel.literally.Literally
import grackle.Ast.Document
import grackle.GraphQLParser.Document.parseAll
import grackle.Schema

trait VersionSpecificSyntax {
Expand All @@ -32,26 +31,31 @@ class StringContextOps(val sc: StringContext) extends AnyVal {
def doc(args: Any*): Document = macro DocumentLiteral.make
}

object SchemaLiteral extends Literally[Schema] {
private object SchemaLiteral extends Literally[Schema] {
def validate(c: Context)(s: String): Either[String,c.Expr[Schema]] = {
import c.universe._
def mkError(err: Either[Throwable, NonEmptyChain[Problem]]) =
err.fold(
t => s"Internal error: ${t.getMessage}",
ps => s"Invalid schema: ${ps.toList.distinct.mkString("\n 🐞 ", "\n 🐞 ", "\n")}",
)
Schema(s).toEither.bimap(mkError, _ => c.Expr(q"_root_.grackle.Schema($s).toOption.get"))
Schema(s, CompiletimeParsers.schemaParser).toEither.bimap(mkError, _ => c.Expr(q"_root_.grackle.Schema($s, _root_.grackle.CompiletimeParsers.schemaParser).toOption.get"))
}
def make(c: Context)(args: c.Expr[Any]*): c.Expr[Schema] = apply(c)(args: _*)
}

object DocumentLiteral extends Literally[Document] {
private object DocumentLiteral extends Literally[Document] {
def validate(c: Context)(s: String): Either[String,c.Expr[Document]] = {
import c.universe._
parseAll(s).bimap(
pf => show"Invalid document: $pf",
_ => c.Expr(q"_root_.grackle.GraphQLParser.Document.parseAll($s).toOption.get"),
CompiletimeParsers.parser.parseText(s).toEither.bimap(
_.fold(thr => show"Invalid document: ${thr.getMessage}", _.toList.mkString("\n 🐞 ", "\n 🐞 ", "\n")),
_ => c.Expr(q"_root_.grackle.CompiletimeParsers.parser.parseText($s).toOption.get"),
)
}
def make(c: Context)(args: c.Expr[Any]*): c.Expr[Document] = apply(c)(args: _*)
}

object CompiletimeParsers {
val parser: GraphQLParser = GraphQLParser(GraphQLParser.defaultConfig)
val schemaParser: SchemaParser = SchemaParser(parser)
}
17 changes: 10 additions & 7 deletions modules/core/src/main/scala-3/syntax3.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,27 @@ package grackle
import cats.syntax.all._
import org.typelevel.literally.Literally
import grackle.Ast.Document
import grackle.GraphQLParser.Document.parseAll

trait VersionSpecificSyntax:

extension (inline ctx: StringContext)
inline def schema(inline args: Any*): Schema = ${SchemaLiteral('ctx, 'args)}
inline def doc(inline args: Any*): Document = ${ DocumentLiteral('ctx, 'args) }
inline def doc(inline args: Any*): Document = ${DocumentLiteral('ctx, 'args) }

object SchemaLiteral extends Literally[Schema]:
def validate(s: String)(using Quotes) =
Schema(s).toEither.bimap(
Schema(s, CompiletimeParsers.schemaParser).toEither.bimap(
nec => s"Invalid schema:${nec.toList.distinct.mkString("\n 🐞 ", "\n 🐞 ", "\n")}",
_ => '{Schema(${Expr(s)}).toOption.get}
_ => '{Schema(${Expr(s)}, CompiletimeParsers.schemaParser).toOption.get}
)

object DocumentLiteral extends Literally[Document]:
def validate(s: String)(using Quotes) =
parseAll(s).bimap(
pf => show"Invalid document: $pf",
_ => '{parseAll(${Expr(s)}).toOption.get}
CompiletimeParsers.parser.parseText(s).toEither.bimap(
_.fold(thr => show"Invalid document: ${thr.getMessage}", _.toList.mkString("\n 🐞 ", "\n 🐞 ", "\n")),
_ => '{CompiletimeParsers.parser.parseText(${Expr(s)}).toOption.get}
)

object CompiletimeParsers:
val parser: GraphQLParser = GraphQLParser(GraphQLParser.defaultConfig)
val schemaParser: SchemaParser = SchemaParser(parser)
Loading

0 comments on commit 56e244b

Please sign in to comment.