From 2835ab9125c215a09f9cc466facd98baaa76c52d Mon Sep 17 00:00:00 2001 From: Rahul Ramesh <121226043+rahu1ramesh@users.noreply.github.com> Date: Wed, 16 Oct 2024 07:36:02 +0000 Subject: [PATCH 1/2] [Rahul] | Fix. Host Extraction Logic To Handle original_url Safely --- app/controllers/api/v1/templates_controller.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/templates_controller.rb b/app/controllers/api/v1/templates_controller.rb index 5d85f5f..29660bc 100644 --- a/app/controllers/api/v1/templates_controller.rb +++ b/app/controllers/api/v1/templates_controller.rb @@ -25,7 +25,11 @@ def find_by_domain private def find_host(request) - host = request.origin.match(%r{https?://([^/]+)}).captures[0] rescue nil + host = nil + + if request.original_url && (match_data = request.original_url.match(%r{https?://([^/]+)})) + host = match_data[1] + end return host if host From 78e5c91e5f11524d9fe869c246b6ee2b3948fd19 Mon Sep 17 00:00:00 2001 From: Bodhish Thomas Date: Wed, 16 Oct 2024 06:27:08 -0500 Subject: [PATCH 2/2] Update the logic for find_host --- app/controllers/api/v1/templates_controller.rb | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/app/controllers/api/v1/templates_controller.rb b/app/controllers/api/v1/templates_controller.rb index 29660bc..d23ae05 100644 --- a/app/controllers/api/v1/templates_controller.rb +++ b/app/controllers/api/v1/templates_controller.rb @@ -25,16 +25,10 @@ def find_by_domain private def find_host(request) - host = nil - - if request.original_url && (match_data = request.original_url.match(%r{https?://([^/]+)})) - host = match_data[1] - end - - return host if host - - return "www.medispeak.in" if request.referer&.include?("medispeak.in") + extract_host(request.origin) || extract_host(request.original_url) + end - nil + def extract_host(url) + url&.match(%r{https?://([^/]+)}).captures[0] rescue nil end end