Commit 5f0ed57
Raise if there are too many redirects without a render (#3670)
* Raise if there are too many redirects without a render
In circumstances where handle_params does a patch redirect, it is
possible for a user to get stuck in an infinite loop. This can be
expensive if the application performs any querying inside of
handle_params/3
For a dead render, the browser will handle this, as it will give an
`ERR_TOO_MANY_REDIRECTS` - however for a patch, the browser never
actually handles the redirect.
This commit simply stores a counter every time a patch occurs, and if
there's no render between patches, then it will raise, giving the
developer a clue where to look for the loop. A limit of 20 was chosen to
match what chrome uses for its `ERR_TOO_MANY_REDIRECTS` implementation.
```
Mix.install([
{:phoenix_playground, "~> 0.1.6"},
{:phoenix_live_view, "~> 1.0.4"}
])
defmodule DemoLive do
use Phoenix.LiveView
def mount(_params, _session, socket) do
{:ok, assign(socket, permitted: true, count: 0)}
end
def handle_params(params, _uri, socket) do
socket = assign(socket, :count, socket.assigns.count + 1)
if socket.assigns.permitted do
{:noreply, socket}
else
{:noreply, push_patch(socket, to: "/?count=#{socket.assigns.count}")}
end
end
def handle_event("toggle", _params, socket) do
{:noreply, assign(socket, permitted: !socket.assigns.permitted)}
end
def handle_event("patch", _params, socket) do
{:noreply, push_patch(socket, to: "/?count=#{socket.assigns.count}")}
end
def render(assigns) do
~H"""
<div>
{if @permitted, do: "Permitted", else: "Not Permitted"}
<button phx-click="toggle">{if @permitted, do: "Disable", else: "Enable"}</button>
<button phx-click="patch">Patch Redirect</button>
</div>
"""
end
end
PhoenixPlayground.start(live: DemoLive, port: 4200)
```
* set low reload jitter for test
* Store redirect count on state instead of in process dictionary
---------
Co-authored-by: Steffen Deusch <[email protected]>1 parent abad050 commit 5f0ed57
File tree
4 files changed
+75
-1
lines changed- lib/phoenix_live_view
- test/e2e
- support
- tests
4 files changed
+75
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
812 | 812 | | |
813 | 813 | | |
814 | 814 | | |
| 815 | + | |
815 | 816 | | |
816 | 817 | | |
817 | 818 | | |
| |||
820 | 821 | | |
821 | 822 | | |
822 | 823 | | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
823 | 842 | | |
824 | 843 | | |
825 | 844 | | |
| |||
857 | 876 | | |
858 | 877 | | |
859 | 878 | | |
| 879 | + | |
860 | 880 | | |
861 | 881 | | |
862 | 882 | | |
| |||
1377 | 1397 | | |
1378 | 1398 | | |
1379 | 1399 | | |
| 1400 | + | |
1380 | 1401 | | |
1381 | 1402 | | |
1382 | 1403 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
13 | 17 | | |
14 | 18 | | |
15 | 19 | | |
| |||
185 | 189 | | |
186 | 190 | | |
187 | 191 | | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
188 | 222 | | |
189 | 223 | | |
190 | 224 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
172 | 172 | | |
173 | 173 | | |
174 | 174 | | |
| 175 | + | |
175 | 176 | | |
176 | 177 | | |
177 | 178 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
67 | 85 | | |
68 | 86 | | |
69 | 87 | | |
| |||
0 commit comments