From 5761e013f7e1bdb8e0cf3e6d60677acbb47107c8 Mon Sep 17 00:00:00 2001 From: baldarn <2814802+baldarn@users.noreply.github.com> Date: Fri, 30 Aug 2024 12:14:14 +0200 Subject: [PATCH] Security (#10) fidati di dio, ma prima lega il cavallo --- .env.example | 2 ++ Gemfile | 5 +++ Gemfile.lock | 25 ++++++++++++++ .../users/registrations_controller.rb | 10 +++++- app/models/user.rb | 2 +- app/views/devise/registrations/new.html.erb | 10 ++++++ config/deploy.yml | 13 ++++---- config/environments/development.rb | 6 +++- config/environments/production.rb | 4 ++- config/environments/test.rb | 2 ++ config/initializers/rack_attack.rb | 3 ++ config/initializers/rucaptcha.rb | 33 +++++++++++++++++++ docker-compose.yaml | 6 ++++ 13 files changed, 111 insertions(+), 10 deletions(-) create mode 100644 config/initializers/rack_attack.rb create mode 100644 config/initializers/rucaptcha.rb diff --git a/.env.example b/.env.example index 25f59f8..444b1f4 100644 --- a/.env.example +++ b/.env.example @@ -1,6 +1,8 @@ # conf for docker compose PG_PORT=8001 +REDIS_URL=redis://localhost +REDIS_PORT=8002 # kamal version hack for github deployment diff --git a/Gemfile b/Gemfile index 32bc740..63fdbde 100644 --- a/Gemfile +++ b/Gemfile @@ -8,6 +8,7 @@ gem 'rails', '~> 7.2.0' # Drivers gem 'pg', '~> 1.5.7' +gem 'redis', '~> 5.3' gem 'sqlite3', '~> 1.4' # Deployment @@ -33,6 +34,10 @@ gem 'simple_form', '~> 5.3' gem 'stimulus-rails' gem 'turbo-rails' +# Security +gem 'rack-attack' +gem 'rucaptcha' + # Pagination gem 'kaminari' gem 'kaminari-i18n' diff --git a/Gemfile.lock b/Gemfile.lock index 51095c8..e3c8193 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -295,6 +295,8 @@ GEM raabro (1.4.0) racc (1.8.1) rack (3.1.7) + rack-attack (6.7.0) + rack (>= 1.0, < 4) rack-session (2.0.0) rack (>= 3.0.0) rack-test (2.1.0) @@ -336,9 +338,14 @@ GEM zeitwerk (~> 2.6) rainbow (3.1.1) rake (13.2.1) + rb_sys (0.9.102) rdoc (6.7.0) psych (>= 4.0.0) redcarpet (3.6.0) + redis (5.3.0) + redis-client (>= 0.22.0) + redis-client (0.22.2) + connection_pool regexp_parser (2.9.2) reline (0.5.9) io-console (~> 0.5) @@ -377,6 +384,21 @@ GEM ruby-vips (2.2.1) ffi (~> 1.12) rubyzip (2.3.2) + rucaptcha (3.2.3) + railties (>= 3.2) + rb_sys (>= 0.9.86) + rucaptcha (3.2.3-aarch64-linux) + railties (>= 3.2) + rb_sys (>= 0.9.86) + rucaptcha (3.2.3-arm64-darwin) + railties (>= 3.2) + rb_sys (>= 0.9.86) + rucaptcha (3.2.3-x86_64-darwin) + railties (>= 3.2) + rb_sys (>= 0.9.86) + rucaptcha (3.2.3-x86_64-linux) + railties (>= 3.2) + rb_sys (>= 0.9.86) sassc (2.4.0) ffi (~> 1.9) sassc-rails (2.1.2) @@ -500,14 +522,17 @@ DEPENDENCIES mission_control-jobs (~> 0.3.1) pg (~> 1.5.7) puma (>= 5.0) + rack-attack rails (~> 7.2.0) rails-i18n (~> 7.0.0) redcarpet (~> 3.6) + redis (~> 5.3) rouge (~> 4.2) rqrcode rubocop (~> 1.65) rubocop-capybara rubocop-rails + rucaptcha sassc-rails selenium-webdriver simple_calendar diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index f20cab1..9968148 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -13,6 +13,14 @@ class RegistrationsController < Devise::RegistrationsController # POST /resource def create + build_resource(sign_up_params) + # TODO: fix this in tests + if !Rails.env.test? && !verify_rucaptcha?(nil, captcha: params[:user][:_rucaptcha]) + clean_up_passwords resource + resource.errors.add(:_rucaptcha, '') + return respond_with resource + end + params[:user][:registering] = true super @@ -65,7 +73,7 @@ def configure_sign_up_params devise_parameter_sanitizer.permit( :sign_up, keys: %i[first_name last_name registering club_name club_email club_address club_postal_code club_municipality - club_province club_tax_code club_telephone] + club_province club_tax_code club_telephone _rucaptcha] ) end diff --git a/app/models/user.rb b/app/models/user.rb index d41c3b5..a9219cc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -23,7 +23,7 @@ class User < ApplicationRecord -> { where(blsd_expires_at: Time.zone.today.beginning_of_day..6.months.from_now) } attr_accessor :registering, :club_name, :club_email, :club_address, :club_postal_code, - :club_municipality, :club_province, :club_tax_code, :club_telephone + :club_municipality, :club_province, :club_tax_code, :club_telephone, :_rucaptcha validates :club_name, :club_email, :club_address, :club_postal_code, :club_municipality, :club_province, :club_tax_code, presence: true, if: -> { registering == true } diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index 1d47b08..e13a178 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -15,6 +15,16 @@ <%= f.input :club_telephone, autofocus: true %> <%= f.input :password, required: true, hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length), input_html: { autocomplete: "new-password" } %> <%= f.input :password_confirmation, required: true, input_html: { autocomplete: "new-password" } %> + <%= f.input :_rucaptcha, required: true do %> +