Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

hcaptcha support (#9022)

Markus Kahl863d227d4b88
diff --git a/modules/recaptcha/app/views/recaptcha/request/perform.html.erb b/modules/recaptcha/app/views/recaptcha/request/perform.html.erb
index a23c7ed446d..2825596ae20 100644
--- a/modules/recaptcha/app/views/recaptcha/request/perform.html.erb
+++ b/modules/recaptcha/app/views/recaptcha/request/perform.html.erb
@@ -4,13 +4,27 @@
<%= styled_form_tag({ action: :verify }, { :autocomplete => "off", :id => 'submit_captcha' }) do %>
<h2><%= t 'recaptcha.verify_account' %></h2>
<% if recaptcha_settings[:recaptcha_type] == ::OpenProject::Recaptcha::TYPE_V2 %>
- <input type="hidden" name="g-recaptcha-response" />
- <%= recaptcha_tags nonce: content_security_policy_script_nonce,
- callback: 'submitRecaptchaForm',
- site_key: recaptcha_settings[:website_key] %>
+ <% input_name = "g-recaptcha-response" %>
+ <input type="hidden" name="<%= input_name %>" />
+ <%= recaptcha_tags(
+ nonce: content_security_policy_script_nonce,
+ callback: 'submitRecaptchaForm',
+ site_key: recaptcha_settings[:website_key]
+ ) %>
+
<%= nonced_javascript_tag do %>
function submitRecaptchaForm(val) {
- document.getElementById('g-recaptcha-response').value = val;
+ var input = document.getElementById('<%= input_name %>');
+
+ if (!input) { // hcaptcha uses name, not ID
+ var inputs = document.getElementsByName('<%= input_name %>');
+
+ if (inputs.length > 0) {
+ input = inputs[0];
+ }
+ }
+
+ input.value = val;
document.getElementById('submit_captcha').submit();
}
<% end %>
diff --git a/modules/recaptcha/config/initializers/recaptcha.rb b/modules/recaptcha/config/initializers/recaptcha.rb
new file mode 100644
index 00000000000..82ee22eb848
--- /dev/null
+++ b/modules/recaptcha/config/initializers/recaptcha.rb
@@ -0,0 +1,16 @@
+Recaptcha.configure do |config|
+ # site_key and secret_key are defined via ENV already (RECAPTCHA_SITE_KEY, RECAPTCHA_SECRET_KEY)
+
+ config.verify_url = OpenProject::Recaptcha.verify_url_override || config.verify_url
+ config.api_server_url = OpenProject::Recaptcha.api_server_url_override || config.api_server_url
+end
+
+module RecaptchaLimitOverride
+ def invalid_response?(resp)
+ return super unless OpenProject::Recaptcha::use_hcaptcha?
+
+ resp.empty? || resp.length > ::OpenProject::Recaptcha.hcaptcha_response_limit
+ end
+end
+
+Recaptcha.singleton_class.prepend RecaptchaLimitOverride
diff --git a/modules/recaptcha/lib/open_project/recaptcha.rb b/modules/recaptcha/lib/open_project/recaptcha.rb
index 8d6e3f12ecd..69b44c46914 100644
--- a/modules/recaptcha/lib/open_project/recaptcha.rb
+++ b/modules/recaptcha/lib/open_project/recaptcha.rb
@@ -5,5 +5,8 @@ module OpenProject
TYPE_V3 ||= 'v3'
require "open_project/recaptcha/engine"
+ require "open_project/recaptcha/configuration"
+
+ extend Configuration
end
end
diff --git a/modules/recaptcha/lib/open_project/recaptcha/configuration.rb b/modules/recaptcha/lib/open_project/recaptcha/configuration.rb
new file mode 100644
index 00000000000..94cfede8239
--- /dev/null
+++ b/modules/recaptcha/lib/open_project/recaptcha/configuration.rb
@@ -0,0 +1,31 @@
+module OpenProject
+ module Recaptcha
+ module Configuration
+ extend self
+
+ def use_hcaptcha?
+ OpenProject::Configuration['recaptcha_via_hcaptcha']
+ end
+
+ def hcaptcha_response_limit
+ @hcaptcha_response_limit ||= (ENV["RECAPTCHA_RESPONSE_LIMIT"].presence || 5000).to_i
+ end
+
+ def api_server_url_override
+ ENV["RECAPTCHA_API_SERVER_URL"].presence || ((use_hcaptcha? || nil) && hcaptcha_api_server_url)
+ end
+
+ def verify_url_override
+ ENV["RECAPTCHA_VERIFY_URL"].presence || ((use_hcaptcha? || nil) && hcaptcha_verify_url)
+ end
+
+ def hcaptcha_verify_url
+ "https://hcaptcha.com/siteverify"
+ end
+
+ def hcaptcha_api_server_url
+ "https://hcaptcha.com/1/api.js"
+ end
+ end
+ end
+end
diff --git a/modules/recaptcha/lib/open_project/recaptcha/engine.rb b/modules/recaptcha/lib/open_project/recaptcha/engine.rb
index 639c10caf2f..5f4bca60178 100644
--- a/modules/recaptcha/lib/open_project/recaptcha/engine.rb
+++ b/modules/recaptcha/lib/open_project/recaptcha/engine.rb
@@ -23,17 +23,29 @@ module OpenProject::Recaptcha
end
config.after_initialize do
- SecureHeaders::Configuration.named_append(:recaptcha) do |_request|
- { frame_src: %w(https://www.google.com/recaptcha/) }
+ SecureHeaders::Configuration.named_append(:recaptcha) do |request|
+ if OpenProject::Recaptcha.use_hcaptcha?
+ value = %w(https://*.hcaptcha.com)
+ keys = %i(frame_src script_src style_src connect_src)
+
+ keys.index_with value
+ else
+ {
+ frame_src: %w(https://www.google.com/recaptcha/)
+ }
+ end
end
- OpenProject::Authentication::Stage.register(:recaptcha,
- nil,
- run_after_activation: true,
- active: -> {
- type = Setting.plugin_openproject_recaptcha[:recaptcha_type]
- type.present? && type.to_s != ::OpenProject::Recaptcha::TYPE_DISABLED
- }) do
+ OpenProject::Authentication::Stage.register(
+ :recaptcha,
+ nil,
+ run_after_activation: true,
+ active: -> {
+ type = Setting.plugin_openproject_recaptcha[:recaptcha_type]
+
+ type.present? && type.to_s != ::OpenProject::Recaptcha::TYPE_DISABLED
+ }
+ ) do
recaptcha_request_path
end
end