Skip to content

Commit

Permalink
Merge pull request mastodon#1665 from ClearlyClaire/glitch-soc/featur…
Browse files Browse the repository at this point in the history
…es/hcaptcha

Add optional hCaptcha support
  • Loading branch information
ClearlyClaire committed Jan 25, 2022
2 parents e58e0eb + b7cf394 commit 129bc42
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .env.production.sample
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,7 @@ MAX_POLL_OPTION_CHARS=100
# Units are in bytes
MAX_EMOJI_SIZE=51200
MAX_REMOTE_EMOJI_SIZE=204800

# Optional hCaptcha support
# HCAPTCHA_SECRET_KEY=
# HCAPTCHA_SITE_KEY=
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,5 @@ gem 'concurrent-ruby', require: false
gem 'connection_pool', require: false

gem 'xorcist', '~> 1.1'

gem 'hcaptcha', '~> 7.1'
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ GEM
railties (>= 4.0.1)
hashdiff (1.0.1)
hashie (4.1.0)
hcaptcha (7.1.0)
json
highline (2.0.3)
hiredis (0.6.3)
hkdf (0.3.0)
Expand Down Expand Up @@ -719,6 +721,7 @@ DEPENDENCIES
fog-openstack (~> 0.3)
fuubar (~> 2.5)
hamlit-rails (~> 0.2)
hcaptcha (~> 7.1)
hiredis (~> 0.6)
htmlentities (~> 4.3)
http (~> 5.0)
Expand Down
44 changes: 44 additions & 0 deletions app/controllers/auth/confirmations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
# frozen_string_literal: true

class Auth::ConfirmationsController < Devise::ConfirmationsController
include CaptchaConcern

layout 'auth'

before_action :set_body_classes
before_action :set_pack
before_action :set_confirmation_user!, only: [:show, :confirm_captcha]
before_action :require_unconfirmed!

before_action :extend_csp_for_captcha!, only: [:show, :confirm_captcha]
before_action :require_captcha_if_needed!, only: [:show]

skip_before_action :require_functional!

def new
Expand All @@ -15,8 +21,46 @@ def new
resource.email = current_user.unconfirmed_email || current_user.email if user_signed_in?
end

def show
old_session_values = session.to_hash
reset_session
session.update old_session_values.except('session_id')

super
end

def confirm_captcha
check_captcha! do |message|
flash.now[:alert] = message
render :captcha
return
end

show
end

private

def require_captcha_if_needed!
render :captcha if captcha_required?
end

def set_confirmation_user!
# We need to reimplement looking up the user because
# Devise::ConfirmationsController#show looks up and confirms in one
# step.
confirmation_token = params[:confirmation_token]
return if confirmation_token.nil?
@confirmation_user = User.find_first_by_auth_conditions(confirmation_token: confirmation_token)
end

def captcha_user_bypass?
return true if @confirmation_user.nil? || @confirmation_user.confirmed?

invite = Invite.find(@confirmation_user.invite_id) if @confirmation_user.invite_id.present?
invite.present? && !invite.max_uses.nil?
end

def set_pack
use_pack 'auth'
end
Expand Down
59 changes: 59 additions & 0 deletions app/controllers/concerns/captcha_concern.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

module CaptchaConcern
extend ActiveSupport::Concern
include Hcaptcha::Adapters::ViewMethods

included do
helper_method :render_captcha
end

def captcha_available?
ENV['HCAPTCHA_SECRET_KEY'].present? && ENV['HCAPTCHA_SITE_KEY'].present?
end

def captcha_enabled?
captcha_available? && Setting.captcha_enabled
end

def captcha_user_bypass?
false
end

def captcha_required?
captcha_enabled? && !captcha_user_bypass?
end

def check_captcha!
return true unless captcha_required?

if verify_hcaptcha
true
else
if block_given?
message = flash[:hcaptcha_error]
flash.delete(:hcaptcha_error)
yield message
end
false
end
end

def extend_csp_for_captcha!
policy = request.content_security_policy
return unless captcha_required? && policy.present?

%w(script_src frame_src style_src connect_src).each do |directive|
values = policy.send(directive)
values << 'https://hcaptcha.com' unless values.include?('https://hcaptcha.com') || values.include?('https:')
values << 'https://*.hcaptcha.com' unless values.include?('https://*.hcaptcha.com') || values.include?('https:')
policy.send(directive, *values)
end
end

def render_captcha
return unless captcha_required?

hcaptcha_tags
end
end
4 changes: 4 additions & 0 deletions app/helpers/admin/settings_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ def site_upload_delete_hint(hint, var)
link = link_to t('admin.site_uploads.delete'), admin_site_upload_path(upload), data: { method: :delete }
safe_join([hint, link], '<br/>'.html_safe)
end

def captcha_available?
ENV['HCAPTCHA_SECRET_KEY'].present? && ENV['HCAPTCHA_SITE_KEY'].present?
end
end
4 changes: 4 additions & 0 deletions app/javascript/flavours/glitch/styles/forms.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1058,3 +1058,7 @@ code {
display: none;
}
}

.simple_form .h-captcha {
text-align: center;
}
2 changes: 2 additions & 0 deletions app/models/form/admin_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Form::AdminSettings
noindex
outgoing_spoilers
require_invite_text
captcha_enabled
).freeze

BOOLEAN_KEYS = %i(
Expand All @@ -58,6 +59,7 @@ class Form::AdminSettings
trendable_by_default
noindex
require_invite_text
captcha_enabled
).freeze

UPLOAD_KEYS = %i(
Expand Down
5 changes: 4 additions & 1 deletion app/views/admin/settings/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@

.fields-group
= f.input :require_invite_text, as: :boolean, wrapper: :with_label, label: t('admin.settings.registrations.require_invite_text.title'), hint: t('admin.settings.registrations.require_invite_text.desc_html'), disabled: !approved_registrations?
.fields-group

- if captcha_available?
.fields-group
= f.input :captcha_enabled, as: :boolean, wrapper: :with_label, label: t('admin.settings.captcha_enabled.title'), hint: t('admin.settings.captcha_enabled.desc_html')

%hr.spacer/

Expand Down
11 changes: 11 additions & 0 deletions app/views/auth/confirmations/captcha.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- content_for :page_title do
= t('auth.confirm_captcha')

= form_tag auth_captcha_confirmation_url, method: 'POST', class: 'simple_form' do
= hidden_field_tag :confirmation_token, params[:confirmation_token]

.field-group
= render_captcha

.actions
%button.button= t('challenge.continue')
5 changes: 5 additions & 0 deletions config/locales-glitch/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
en:
admin:
settings:
captcha_enabled:
desc_html: Enable hCaptcha integration, requiring new users to solve a challenge when confirming their email address. This requires third-party scripts from hCaptcha to be embedded in the email verification page, which may have security and privacy concerns. Users that have been invited through a limited-use invite will not need to solve a CAPTCHA challenge.
title: Require new users to go through a CAPTCHA to confirm their account
enable_keybase:
desc_html: Allow your users to prove their identity via keybase
title: Enable keybase integration
Expand All @@ -17,6 +20,8 @@ en:
show_replies_in_public_timelines:
desc_html: In addition to public self-replies (threads), show public replies in local and public timelines.
title: Show replies in public timelines
auth:
confirm_captcha: User verification
generic:
use_this: Use this
settings:
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
resource :setup, only: [:show, :update], controller: :setup
resource :challenge, only: [:create], controller: :challenges
get 'sessions/security_key_options', to: 'sessions#webauthn_options'
post 'captcha_confirmation', to: 'confirmations#confirm_captcha', as: :captcha_confirmation
end
end

Expand Down
1 change: 1 addition & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ defaults: &defaults
show_domain_blocks_rationale: 'disabled'
outgoing_spoilers: ''
require_invite_text: false
captcha_enabled: false

development:
<<: *defaults
Expand Down

0 comments on commit 129bc42

Please sign in to comment.