Skip to content
Open
Show file tree
Hide file tree
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
18 changes: 1 addition & 17 deletions lib/ex_json_parser_web/live/home_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ defmodule ExJsonParserWeb.HomeLive do
end
end

defp format_json(
%{"json" => new_json, "elixir_map" => new_elixir_map},
%{assigns: %{json: prev_json, elixir_map: prev_elixir_map}} = socket
) do
defp format_json(%{"json" => new_json}, %{assigns: %{json: prev_json}} = socket) do
cond do
new_json != prev_json ->
with {:ok, new_elixir_map} <- Jason.decode(new_json) do
Expand All @@ -45,19 +42,6 @@ defmodule ExJsonParserWeb.HomeLive do
|> assign(elixir_map: inspect(new_elixir_map))}
end

new_elixir_map != prev_elixir_map ->
with {:ok, {:%{}, _, _}} <- Code.string_to_quoted(new_elixir_map),
{new_elixir_map, []} <- Code.eval_string(new_elixir_map),
{:ok, new_json} <- Jason.encode(new_elixir_map, pretty: true) do
{:ok,
socket
|> assign(json: new_json)
|> assign(error: nil)
|> assign(elixir_map: inspect(new_elixir_map))}
else
_ -> {:error, "Error when decoding Json to Elixir Map"}
end

true ->
{:ok, socket}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ex_json_parser_web/live/home_live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<div class="col-span-4">
<span class="block sm:inline">Elixir map</span>
<pre class="whitespace-pre-wrap h-96">
<textarea name="elixir_map" class="resize rounded-md w-full h-full"><%= @elixir_map %></textarea>
<textarea name="elixir_map" readonly class="resize rounded-md w-full h-full bg-gray-100 cursor-not-allowed"><%= @elixir_map %></textarea>
</pre>
<button phx-hook="CopyToClipboard" id="elixir-map-copy-to-clipboard" class="mt-8 w-full bg-indigo-600 hover:bg-indigo-800 text-white font-bold py-2 px-4 rounded-full">
Copy to Clipboard
Expand Down
30 changes: 10 additions & 20 deletions test/ex_json_parser_web/live/home_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,20 @@ defmodule ExJsonParserWeb.HomeLiveTest do
end
end

describe "testing the elixir map to json submit" do
test "should transform to elixir map because json is right", %{conn: conn} do
describe "security around user input handling" do
test "should ignore elixir_map input changes to prevent code execution", %{conn: conn} do
{:ok, view, _html} = live(conn, "/")

assert view
|> element("form")
|> render_submit(%{"json" => ~s({"ok": "test"})}) =~
~s(%{&amp;quot;ok&amp;quot; =&amp;gt; &amp;quot;test&amp;quot;})
end
malicious_input = ~s(%{"ok" => System.cmd("touch", ["/tmp/evil"])})

test "should not have an Error because json is right", %{conn: conn} do
{:ok, view, _html} = live(conn, "/")
html =
view
|> element("form")
|> render_submit(%{"json" => "", "elixir_map" => malicious_input})

refute view
|> element("form")
|> render_submit(%{"json" => ~s({"ok": "test"})}) =~ "Error"
end

test "should return an error because json is wrong", %{conn: conn} do
{:ok, view, _html} = live(conn, "/")

assert view
|> element("form")
|> render_submit(%{"json" => ~s({"ok" "test"})}) =~ "Error"
refute html =~ ~s(%{&amp;quot;ok&amp;quot; =&amp;gt; &amp;quot;test&amp;quot;})
refute html =~ malicious_input
refute html =~ "Error"
end
end
end
Loading