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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Unreleased
----------
- [Patch] Fix declarative webhook generator routes to match the configured webhook path
- [Patch] Validate host param in generated HomeController template to prevent open redirect
- [Patch] Fix sorbet errors in generated webhook handlers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,17 @@ def add_webhook_controller
end

def add_webhook_route
route = "\t\t\tpost '#{file_name}', to: '#{file_name}#receive'\n"
inject_into_file("config/routes.rb", route, after: /namespace :webhooks do\n/)
webhook_route = "post \"#{route_path}\", to: \"webhooks/#{file_name}#receive\""
routes = File.read("config/routes.rb")
return if routes.include?(webhook_route)

mount_engine_route = /^\s*mount\s+ShopifyApp::Engine,\s+at:\s+["']\/["'].*\n/

if routes.match?(mount_engine_route)
inject_into_file("config/routes.rb", " #{webhook_route}\n", before: mount_engine_route)
else
route(webhook_route)
end
end

private
Expand All @@ -41,6 +50,10 @@ def file_name
path.split("/").last
end

def route_path
"/#{path.delete_prefix("/")}"
end

def topic
options["topic"]
end
Expand Down
4 changes: 2 additions & 2 deletions test/generators/add_declarative_webhook_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ class AddDeclarativeWebhookGeneratorTest < Rails::Generators::TestCase
run_generator

assert_file "config/routes.rb" do |routes|
assert_match "namespace :webhooks do", routes
assert_match exisiting_webhook, routes
assert_match new_webhook, routes
assert_operator routes.index(new_webhook), :<, routes.index("mount ShopifyApp::Engine")
end
end

Expand All @@ -51,6 +51,6 @@ def exisiting_webhook
end

def new_webhook
"post 'product_update', to: 'product_update#receive'"
'post "/webhooks/product_update", to: "webhooks/product_update#receive"'
end
end
Loading