Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert utf8->latin1 before decoding JSON-RPC payloads #353

Merged
merged 2 commits into from
Sep 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions apps/server/lib/lexical/server/transport/std_io.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ defmodule Lexical.Server.Transport.StdIO do

with {:ok, content_length} <-
header_value(headers, "content-length", &String.to_integer/1),
{:ok, data} <- read(device, content_length),
{:ok, data} <- read_body(device, content_length),
{:ok, message} <- JsonRpc.decode(data) do
callback.(message)
end
Expand All @@ -112,10 +112,16 @@ defmodule Lexical.Server.Transport.StdIO do
end
end

defp read(device, amount) do
defp read_body(device, amount) do
case IO.read(device, amount) do
data when is_binary(data) or is_list(data) -> {:ok, data}
other -> other
data when is_binary(data) or is_list(data) ->
# Ensure that incoming data is latin1 to prevent double-encoding to utf8 later
# See https://github.com/lexical-lsp/lexical/issues/287 for context.
data = :unicode.characters_to_binary(data, :utf8, :latin1)
{:ok, data}

other ->
other
end
end

Expand Down