Skip to content

Commit 099aec5

Browse files
Merge pull request #2 from rudy-on-rails/rudy-on-rails/sanitize-string-inputs
Sanitize double quoting strings inside of strings
2 parents 5eea46c + 212a6d1 commit 099aec5

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

core/src/main/kotlin/me/lazmaid/kraph/lang/GraphQLNode.kt

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal abstract class GraphQLNode {
2727
@Suppress("UNCHECKED_CAST")
2828
private fun convertToDataEntry(value: Any?) =
2929
when(value) {
30-
is String -> DataEntry.StringData(value)
30+
is String -> DataEntry.StringData(value.escapeQuotes())
3131
is Int -> DataEntry.NonDecimalNumberData(value.toLong())
3232
is Long -> DataEntry.NonDecimalNumberData(value)
3333
is Float -> DataEntry.DecimalNumberData(value.toDouble())
@@ -42,6 +42,10 @@ internal abstract class GraphQLNode {
4242
}
4343
}
4444

45+
internal fun String.escapeQuotes() =
46+
this.replace("\\s+".toRegex(), " ")
47+
.replace("\"", "\\\\\\\"")
48+
4549
internal fun String.wrappedWithQuotes(shouldBeEscaped: Boolean) =
4650
if (shouldBeEscaped) {
4751
"\"$this\""

core/src/test/kotlin/me/lazmaid/kraph/test/BuilderSpek.kt

+15
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,21 @@ class BuilderSpek : Spek({
322322
}
323323
}
324324
}
325+
326+
given("sample mutation with unescaped characters") {
327+
val query = Kraph {
328+
mutation {
329+
field("someField",
330+
args = mapOf(
331+
"foo" to "some \"bar\" over"
332+
)
333+
)
334+
}
335+
}
336+
it("should escape those characters") {
337+
assertThat(query.toRequestString(), equalTo("{\"query\": \"mutation { someField (foo: \\\"some \\\\\\\"bar\\\\\\\" over\\\") }\", \"variables\": null, \"operationName\": null}"))
338+
}
339+
}
325340
}
326341
})
327342

0 commit comments

Comments
 (0)