From da7db790c49c764cc09916002f0b2a694b030968 Mon Sep 17 00:00:00 2001 From: ARAYN7011 <88572149+ARAYN7011@users.noreply.github.com> Date: Wed, 18 Nov 2026 18:33:40 +0530 Subject: [PATCH] Encode interpolated values in static ~p urls url/1,2,3 passed the original ~p route to static_url/2 for static paths, skipping the percent-encoding of interpolated values that ~p paths and non-static urls get. Pass the rewritten route instead. Co-Authored-By: Claude Sonnet 5.5 --- lib/phoenix/verified_routes.ex | 19 +++++++++++-------- test/phoenix/verified_routes_test.exs | 11 +++++++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/phoenix/verified_routes.ex b/lib/phoenix/verified_routes.ex index 15e22e0163..3b1281b141 100644 --- a/lib/phoenix/verified_routes.ex +++ b/lib/phoenix/verified_routes.ex @@ -381,7 +381,7 @@ defmodule Phoenix.VerifiedRoutes do end defp inject_path( - {%__MODULE__{} = route, static?, _endpoint_ctx, _route_ast, path_ast, static_ast}, + {%__MODULE__{} = route, static?, _endpoint_ctx, path_ast, static_ast, _static_url_ast}, env ) do if static? do @@ -393,13 +393,11 @@ defmodule Phoenix.VerifiedRoutes do end defp inject_url( - {%__MODULE__{} = route, static?, endpoint_ctx, route_ast, path_ast, _static_ast}, + {%__MODULE__{} = route, static?, endpoint_ctx, path_ast, _static_ast, static_url_ast}, env ) do if static? do - quote do - unquote(__MODULE__).static_url(unquote_splicing([endpoint_ctx, route_ast])) - end + static_url_ast else Module.put_attribute(env.module, :phoenix_verified_routes, route) @@ -974,7 +972,7 @@ defmodule Phoenix.VerifiedRoutes do """ end - {static?, meta, test_path, path_ast, static_ast} = + {static?, meta, test_path, path_ast, static_ast, static_url_ast} = rewrite_path(route_ast, endpoint_ctx, router, config) route = %__MODULE__{ @@ -984,7 +982,7 @@ defmodule Phoenix.VerifiedRoutes do test_path: test_path } - {route, static?, endpoint_ctx, route_ast, path_ast, static_ast} + {route, static?, endpoint_ctx, path_ast, static_ast, static_url_ast} end defp warn_location(meta, %{line: line, file: file, function: function, module: module}) do @@ -1037,7 +1035,12 @@ defmodule Phoenix.VerifiedRoutes do unquote(__MODULE__).static_path(unquote_splicing([endpoint, rewrite_route])) end - {static?, meta, test_path, path_ast, static_ast} + static_url_ast = + quote generated: true do + unquote(__MODULE__).static_url(unquote_splicing([endpoint, rewrite_route])) + end + + {static?, meta, test_path, path_ast, static_ast, static_url_ast} end defp materialize_path(path) do diff --git a/test/phoenix/verified_routes_test.exs b/test/phoenix/verified_routes_test.exs index 8eaf56e42f..90c108c48b 100644 --- a/test/phoenix/verified_routes_test.exs +++ b/test/phoenix/verified_routes_test.exs @@ -403,6 +403,17 @@ defmodule Phoenix.VerifiedRoutesTest do assert path(socket_with_endpoint(), ~p"/images/foo.png") == "/images/foo.png" end + test "static urls encode interpolated values like paths" do + name = "a/b?x=1#" + + assert ~p"/images/#{name}" == "/images/a%2Fb%3Fx%3D1%23" + assert path(Endpoint, ~p"/images/#{name}") == "/images/a%2Fb%3Fx%3D1%23" + assert url(~p"/images/#{name}") == "https://static.example.com/images/a%2Fb%3Fx%3D1%23" + + assert url(Endpoint, ~p"/images/#{name}") == + "https://static.example.com/images/a%2Fb%3Fx%3D1%23" + end + test "~p dict query strings" do assert ~p"/posts/5?#{[id: 3]}" == "/posts/5?id=3" assert ~p"/posts/5?#{[foo: "bar"]}" == "/posts/5?foo=bar"