From 164e9ccc4028b88003baea691e32a4a1c7bd6125 Mon Sep 17 00:00:00 2001 From: Daniel O'Connell Date: Wed, 6 May 2026 21:21:19 +1000 Subject: [PATCH] fix: escape full `@deprecated` reason correctly render_deprecated/1 only escaped `\` and `"` before now it escapes all characters from the graphql spec for StringValue --- .../schema/remote/introspection.ex | 25 ++++++- .../schema/remote/introspection_test.exs | 75 +++++++++++++++++++ 2 files changed, 98 insertions(+), 2 deletions(-) diff --git a/lib/graphql_query/schema/remote/introspection.ex b/lib/graphql_query/schema/remote/introspection.ex index b9a4f15..ea78196 100644 --- a/lib/graphql_query/schema/remote/introspection.ex +++ b/lib/graphql_query/schema/remote/introspection.ex @@ -409,8 +409,7 @@ defmodule GraphqlQuery.Schema.Remote.Introspection do # Deprecation defp render_deprecated(%{"isDeprecated" => true, "deprecationReason" => reason}) when is_binary(reason) and reason != "" do - escaped = reason |> String.replace("\\", "\\\\") |> String.replace("\"", "\\\"") - " @deprecated(reason: \"#{escaped}\")" + " @deprecated(reason: \"#{escape_string_value(reason)}\")" end defp render_deprecated(%{"isDeprecated" => true}) do @@ -419,6 +418,28 @@ defmodule GraphqlQuery.Schema.Remote.Introspection do defp render_deprecated(_), do: "" + # escape StringValue following spec at + # https://spec.graphql.org/September2025/#sec-String) + defp escape_string_value(reason) do + reason + |> String.to_charlist() + |> Enum.map_join(&escape_codepoint/1) + end + + defp escape_codepoint(?\\), do: "\\\\" + defp escape_codepoint(?"), do: "\\\"" + defp escape_codepoint(?\b), do: "\\b" + defp escape_codepoint(?\f), do: "\\f" + defp escape_codepoint(?\n), do: "\\n" + defp escape_codepoint(?\r), do: "\\r" + defp escape_codepoint(?\t), do: "\\t" + + defp escape_codepoint(c) when c < 0x20 or (c >= 0x7F and c <= 0x9F) do + "\\u" <> (c |> Integer.to_string(16) |> String.pad_leading(4, "0")) + end + + defp escape_codepoint(c), do: <> + # Default values — stored as JSON-encoded strings in introspection defp render_default_value(nil), do: "" defp render_default_value(value), do: " = #{value}" diff --git a/test/graphql_query/schema/remote/introspection_test.exs b/test/graphql_query/schema/remote/introspection_test.exs index 2f65fde..feeb9f2 100644 --- a/test/graphql_query/schema/remote/introspection_test.exs +++ b/test/graphql_query/schema/remote/introspection_test.exs @@ -178,6 +178,81 @@ defmodule GraphqlQuery.Schema.Remote.IntrospectionTest do assert sdl =~ "directive @deprecated" end + test "escapes control characters in deprecation reasons so the SDL parses" do + schema = %{ + "queryType" => %{"name" => "Query"}, + "mutationType" => nil, + "subscriptionType" => nil, + "directives" => [], + "types" => [ + %{ + "kind" => "OBJECT", + "name" => "Query", + "description" => nil, + "interfaces" => [], + "fields" => [ + %{ + "name" => "old", + "description" => nil, + "args" => [], + "type" => %{"kind" => "SCALAR", "name" => "String", "ofType" => nil}, + "isDeprecated" => true, + "deprecationReason" => "Use `new` instead.\nSee the docs for details.\n" + } + ], + "inputFields" => nil, + "enumValues" => nil, + "possibleTypes" => nil + } + ] + } + + {:ok, sdl} = Introspection.to_sdl(schema) + assert sdl =~ ~S|@deprecated(reason: "Use `new` instead.\nSee the docs for details.\n")| + assert {:ok, _} = GraphqlQuery.Native.validate_schema(sdl, "test.graphql") + end + + test "escapes the full set of GraphQL StringValue escapes in deprecation reasons" do + reason = + "quote:\" backslash:\\ bs:\b ff:\f lf:\n cr:\r tab:\t soh:\x01 nel:\u0085 unicode:café" + + schema = %{ + "queryType" => %{"name" => "Query"}, + "mutationType" => nil, + "subscriptionType" => nil, + "directives" => [], + "types" => [ + %{ + "kind" => "OBJECT", + "name" => "Query", + "description" => nil, + "interfaces" => [], + "fields" => [ + %{ + "name" => "old", + "description" => nil, + "args" => [], + "type" => %{"kind" => "SCALAR", "name" => "String", "ofType" => nil}, + "isDeprecated" => true, + "deprecationReason" => reason + } + ], + "inputFields" => nil, + "enumValues" => nil, + "possibleTypes" => nil + } + ] + } + + {:ok, sdl} = Introspection.to_sdl(schema) + + expected = + "@deprecated(reason: \"quote:\\\" backslash:\\\\ bs:\\b ff:\\f lf:\\n cr:\\r tab:\\t soh:\\u0001 nel:\\u0085 unicode:café\")" + + assert sdl =~ expected + assert {:ok, _} = GraphqlQuery.Native.validate_schema(sdl, "test.graphql") + end + test "does not emit schema definition for standard root type names" do json = load_fixture() {:ok, sdl} = Introspection.to_sdl(json)