Skip to content

Avoid interpreting path as route #150

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

tt
Copy link

@tt tt commented Feb 25, 2025

I was trying to upgrade Bypass in a project but ran into the issue reported in #122.

Bypass will currently interpret a path as a route by using Plug.Router.Utils.build_path_match to split it into path segments.

The simplest demonstration of that is by using /resources/:resource as both route pattern and path. The resource parameter is then supposed to be :resource but as both are interpreted as route, the parameter ends up being {:resource, [], nil}. This may not be a problem based on whether the expectation relies on this parameter.

However, another scenario is if your path contains multiple colons, e.g. /resources/a:b:c. With Bypass' current dependency on Plug 1.10.4, this fails with the following exception:

     ** (Plug.Router.InvalidSpecError) :identifier in routes must be made of letters, numbers and underscores
         (plug 1.10.4) lib/plug/router/utils.ex:209: Plug.Router.Utils.binary_to_identifier/2
         (plug 1.10.4) lib/plug/router/utils.ex:166: Plug.Router.Utils.segment_match/3
         (plug 1.10.4) lib/plug/router/utils.ex:131: Plug.Router.Utils.build_path_match/4
         (bypass 2.1.0) lib/bypass/instance.ex:300: Bypass.Instance.route_info/3
         (bypass 2.1.0) lib/bypass/instance.ex:162: Bypass.Instance.do_handle_call/3
         (stdlib 6.2) gen_server.erl:2381: :gen_server.try_handle_call/4
         (stdlib 6.2) gen_server.erl:2410: :gen_server.handle_msg/6
         (stdlib 6.2) proc_lib.erl:329: :proc_lib.init_p_do_apply/3
     Last message (from #PID<0.10094.0>): {:get_route, "POST", "/resources/a:b:c"}

Upgrading Plug to its latest release, version 1.16.1, changes the error but still fails:

     ** (Plug.Router.InvalidSpecError) only one dynamic entry (:var or *glob) per path segment is allowed, got: "a:b:c"
         (plug 1.16.1) lib/plug/router/utils.ex:236: Plug.Router.Utils.build_path_clause/7
         (plug 1.16.1) lib/plug/router/utils.ex:142: Plug.Router.Utils.build_path_clause/3
         (plug 1.16.1) lib/plug/router/utils.ex:84: Plug.Router.Utils.build_path_match/2
         (bypass 2.1.0) lib/bypass/instance.ex:300: Bypass.Instance.route_info/3
         (bypass 2.1.0) lib/bypass/instance.ex:162: Bypass.Instance.do_handle_call/3
         (stdlib 6.2) gen_server.erl:2381: :gen_server.try_handle_call/4
         (stdlib 6.2) gen_server.erl:2410: :gen_server.handle_msg/6
         (stdlib 6.2) proc_lib.erl:329: :proc_lib.init_p_do_apply/3
     Last message (from #PID<0.903.0>): {:get_route, "POST", "/resources/a:b:c"}

Switching the implementation to use Plug.Router.Utils.split resolves the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant