Skip to content

Commit

Permalink
Merge pull request #2 from rudy-on-rails/rudy-on-rails/sanitize-strin…
Browse files Browse the repository at this point in the history
…g-inputs

Sanitize double quoting strings inside of strings
  • Loading branch information
rudy-on-rails authored Jan 20, 2021
2 parents 5eea46c + 212a6d1 commit 099aec5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion core/src/main/kotlin/me/lazmaid/kraph/lang/GraphQLNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal abstract class GraphQLNode {
@Suppress("UNCHECKED_CAST")
private fun convertToDataEntry(value: Any?) =
when(value) {
is String -> DataEntry.StringData(value)
is String -> DataEntry.StringData(value.escapeQuotes())
is Int -> DataEntry.NonDecimalNumberData(value.toLong())
is Long -> DataEntry.NonDecimalNumberData(value)
is Float -> DataEntry.DecimalNumberData(value.toDouble())
Expand All @@ -42,6 +42,10 @@ internal abstract class GraphQLNode {
}
}

internal fun String.escapeQuotes() =
this.replace("\\s+".toRegex(), " ")
.replace("\"", "\\\\\\\"")

internal fun String.wrappedWithQuotes(shouldBeEscaped: Boolean) =
if (shouldBeEscaped) {
"\"$this\""
Expand Down
15 changes: 15 additions & 0 deletions core/src/test/kotlin/me/lazmaid/kraph/test/BuilderSpek.kt
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,21 @@ class BuilderSpek : Spek({
}
}
}

given("sample mutation with unescaped characters") {
val query = Kraph {
mutation {
field("someField",
args = mapOf(
"foo" to "some \"bar\" over"
)
)
}
}
it("should escape those characters") {
assertThat(query.toRequestString(), equalTo("{\"query\": \"mutation { someField (foo: \\\"some \\\\\\\"bar\\\\\\\" over\\\") }\", \"variables\": null, \"operationName\": null}"))
}
}
}
})

Expand Down

0 comments on commit 099aec5

Please sign in to comment.