-
Notifications
You must be signed in to change notification settings - Fork 953
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
runtime checks for socket access in async functions (#3161)
* runtime checks for socket access in async functions * use stacktrace for runtime case * Update lib/phoenix_live_view/async.ex Co-authored-by: José Valim <[email protected]> * only use Function.info if enable_expensive_runtime_checks is true * add warning for accessing an assigns map * set enable_expensive_runtime_checks in test config * add eval_quoted test variants --------- Co-authored-by: José Valim <[email protected]>
- Loading branch information
Showing
12 changed files
with
445 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import Config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import Config | ||
|
||
config :logger, :level, :error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Config | ||
|
||
config :logger, :level, :debug | ||
config :logger, :backends, [] | ||
|
||
config :phoenix_live_view, enable_expensive_runtime_checks: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
defmodule Phoenix.LiveView.AsyncTest do | ||
# run with async: false to prevent other messages from being captured | ||
use ExUnit.Case, async: false | ||
|
||
import ExUnit.CaptureIO | ||
|
||
describe "async operations - eval_quoted" do | ||
for fun <- [:assign_async, :start_async] do | ||
test "warns when passing socket to #{fun} function" do | ||
warnings = | ||
capture_io(:stderr, fn -> | ||
fun = unquote(fun) | ||
|
||
Code.eval_quoted(quote do | ||
require Phoenix.LiveView | ||
|
||
socket = %Phoenix.LiveView.Socket{assigns: %{__changed__: %{}, bar: :baz}} | ||
|
||
Phoenix.LiveView.unquote(fun)(socket, :foo, fn -> | ||
socket.assigns.bar | ||
end) | ||
end) | ||
end) | ||
|
||
assert warnings =~ | ||
"you are accessing the LiveView Socket inside a function given to #{unquote(fun)}" | ||
end | ||
|
||
test "does not warn when accessing socket outside of function passed to #{fun}" do | ||
warnings = | ||
capture_io(:stderr, fn -> | ||
fun = unquote(fun) | ||
|
||
Code.eval_quoted(quote do | ||
require Phoenix.LiveView | ||
|
||
socket = %Phoenix.LiveView.Socket{assigns: %{__changed__: %{}, bar: :baz}} | ||
bar = socket.assigns.bar | ||
|
||
Phoenix.LiveView.unquote(fun)(socket, :foo, fn -> | ||
bar | ||
end) | ||
end) | ||
end) | ||
|
||
refute warnings =~ | ||
"you are accessing the LiveView Socket inside a function given to #{unquote(fun)}" | ||
end | ||
end | ||
end | ||
|
||
describe "async operations" do | ||
for fun <- [:assign_async, :start_async] do | ||
test "warns when passing socket to #{fun} function", %{test: test} do | ||
warnings = | ||
capture_io(:stderr, fn -> | ||
defmodule Module.concat(AssignAsyncSocket, "Test#{:erlang.phash2(test)}") do | ||
use Phoenix.LiveView | ||
|
||
def mount(_params, _session, socket) do | ||
{:ok, unquote(fun)(socket, :foo, fn -> | ||
do_something(socket.assigns) | ||
end)} | ||
end | ||
|
||
defp do_something(_socket), do: :ok | ||
end | ||
end) | ||
|
||
assert warnings =~ | ||
"you are accessing the LiveView Socket inside a function given to #{unquote(fun)}" | ||
end | ||
|
||
test "does not warn when accessing socket outside of function passed to #{fun}", %{test: test} do | ||
warnings = | ||
capture_io(:stderr, fn -> | ||
defmodule Module.concat(AssignAsyncSocket, "Test#{:erlang.phash2(test)}") do | ||
use Phoenix.LiveView | ||
|
||
def mount(_params, _session, socket) do | ||
socket = assign(socket, :foo, :bar) | ||
foo = socket.assigns.foo | ||
|
||
{:ok, unquote(fun)(socket, :foo, fn -> | ||
do_something(foo) | ||
end)} | ||
end | ||
|
||
defp do_something(assigns), do: :ok | ||
end | ||
end) | ||
|
||
refute warnings =~ | ||
"you are accessing the LiveView Socket inside a function given to #{unquote(fun)}" | ||
end | ||
|
||
test "does not warn when argument is not a function (#{fun})", %{test: test} do | ||
warnings = | ||
capture_io(:stderr, fn -> | ||
defmodule Module.concat(AssignAsyncSocket, "Test#{:erlang.phash2(test)}") do | ||
use Phoenix.LiveView | ||
|
||
def mount(_params, _session, socket) do | ||
socket = assign(socket, :foo, :bar) | ||
|
||
{:ok, unquote(fun)(socket, :foo, function_that_returns_the_func(socket))} | ||
end | ||
|
||
defp function_that_returns_the_func(socket) do | ||
foo = socket.assigns.foo | ||
|
||
fn -> | ||
do_something(foo) | ||
end | ||
end | ||
|
||
defp do_something(assigns), do: :ok | ||
end | ||
end) | ||
|
||
refute warnings =~ | ||
"you are accessing the LiveView Socket inside a function given to #{unquote(fun)}" | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.