Skip to content

Commit

Permalink
Use inspect/1 to safely encode bad binary samples (#1121)
Browse files Browse the repository at this point in the history
* Use inspect to safely encode bad binary samples

* Tweak test to better represent failure state

* Only inspect when necessary
  • Loading branch information
mattbaker authored Nov 22, 2021
1 parent d7837a7 commit 738f97b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/absinthe/phase/parse.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ defmodule Absinthe.Phase.Parse do
@spec format_raw_parse_error({:lexer, String.t(), {line :: pos_integer, column :: pos_integer}}) ::
Phase.Error.t()
defp format_raw_parse_error({:lexer, rest, {line, column}}) do
sample = String.slice(rest, 0, 10)
sample_slice = String.slice(rest, 0, 10)
sample = if String.valid?(sample_slice), do: sample_slice, else: inspect(sample_slice)

message = "Parsing failed at `#{sample}`"
%Phase.Error{message: message, locations: [%{line: line, column: column}], phase: __MODULE__}
Expand Down
15 changes: 15 additions & 0 deletions test/absinthe/phase/parse_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ defmodule Absinthe.Phase.ParseTest do
] == bp.execution.validation_errors
end

@graphql "test bad string" <> <<223>> <> "error"
test "coerces non-string binaries to strings" do
assert {:error, bp} = Absinthe.Phase.Parse.run(@graphql)

[parse_error] = bp.execution.validation_errors
assert String.valid?(parse_error.message)

assert %Absinthe.Phase.Error{
extra: %{},
locations: [%{column: 16, line: 1}],
message: "Parsing failed at `<<223, 101, 114, 114, 111, 114>>`",
phase: Absinthe.Phase.Parse
} == parse_error
end

@graphql ";"
test "should provide sample of parsing failure on very short query strings" do
assert {:error, bp} = Absinthe.Phase.Parse.run(@graphql, jump_phases: false)
Expand Down

0 comments on commit 738f97b

Please sign in to comment.