Skip to content

Commit ab1781d

Browse files
authored
Merge pull request #81 from tvdeyen/fix-80
Run CSRF protection before all other controller filters
2 parents 3140352 + cf0d606 commit ab1781d

10 files changed

Lines changed: 43 additions & 87 deletions

File tree

app/controllers/alchemy/user_sessions_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module Alchemy
22
class UserSessionsController < ::Devise::SessionsController
33
include Alchemy::Admin::Locale
44

5+
protect_from_forgery prepend: true
6+
57
before_action except: 'destroy' do
68
enforce_ssl if ssl_required? && !request.ssl?
79
end
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class ApplicationController < ActionController::Base
2-
# Prevent CSRF attacks by raising an exception.
3-
# For APIs, you may want to use :null_session instead.
4-
protect_from_forgery with: :exception
2+
# @See https://github.com/AlchemyCMS/alchemy-devise/issues/80
3+
before_action { current_user }
54
end

spec/dummy/config/application.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
module Dummy
2222
class Application < Rails::Application
2323
# Initialize configuration defaults for originally generated Rails version.
24-
config.load_defaults 5.1
24+
config.load_defaults 5.2
2525

2626
# Settings in config/environments/* take precedence over those specified here.
2727
# Application configuration can go into files in config/initializers

spec/dummy/config/environments/test.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@
2626
config.action_dispatch.show_exceptions = false
2727

2828
# Disable request forgery protection in test environment.
29-
config.action_controller.allow_forgery_protection = false
30-
31-
# Store uploaded files on the local file system in a temporary directory
32-
# config.active_storage.service = :test
29+
config.action_controller.allow_forgery_protection = true
3330

3431
config.action_mailer.perform_caching = false
3532

spec/dummy/config/initializers/new_framework_defaults.rb

Lines changed: 0 additions & 25 deletions
This file was deleted.

spec/dummy/config/initializers/new_framework_defaults_5_1.rb

Lines changed: 0 additions & 14 deletions
This file was deleted.

spec/dummy/config/initializers/new_framework_defaults_5_2.rb

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This migration comes from alchemy (originally 20180519204655)
2+
class AddFixedToAlchemyElements < ActiveRecord::Migration[5.0]
3+
def change
4+
add_column :alchemy_elements, :fixed, :boolean, default: false, null: false
5+
add_index :alchemy_elements, :fixed
6+
end
7+
end

spec/dummy/db/schema.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2018_03_01_103350) do
13+
ActiveRecord::Schema.define(version: 2019_04_24_075603) do
1414

1515
create_table "alchemy_attachments", force: :cascade do |t|
1616
t.string "name"
@@ -60,7 +60,9 @@
6060
t.integer "updater_id"
6161
t.integer "cell_id"
6262
t.integer "parent_element_id"
63+
t.boolean "fixed", default: false, null: false
6364
t.index ["cell_id"], name: "index_alchemy_elements_on_cell_id"
65+
t.index ["fixed"], name: "index_alchemy_elements_on_fixed"
6466
t.index ["page_id", "parent_element_id"], name: "index_alchemy_elements_on_page_id_and_parent_element_id"
6567
t.index ["page_id", "position"], name: "index_elements_on_page_id_and_position"
6668
end

spec/features/login_feature_spec.rb

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
require 'spec_helper'
22

33
describe "Login: " do
4-
context "If users present" do
4+
context "If user is present" do
5+
let!(:user) do
6+
Alchemy::User.create!(
7+
login: 'admin',
8+
email: 'admin@example.com',
9+
password: 's3cr3t',
10+
password_confirmation: 's3cr3t',
11+
alchemy_roles: %w[admin]
12+
)
13+
end
14+
515
let!(:default_key) { Devise.authentication_keys }
616

717
before do
@@ -13,6 +23,14 @@
1323
visit '/admin/login'
1424
expect(page).to have_field('user_login')
1525
end
26+
27+
it "works" do
28+
visit '/admin/login'
29+
fill_in 'user_login', with: user.login
30+
fill_in 'user_password', with: user.password
31+
click_button 'Login'
32+
expect(page).to have_content('Welcome back admin')
33+
end
1634
end
1735

1836
context "with default Devise configuration" do
@@ -25,6 +43,14 @@
2543
expect(page).to have_field('user_email')
2644
end
2745

46+
it "works" do
47+
visit '/admin/login'
48+
fill_in 'user_email', with: user.email
49+
fill_in 'user_password', with: user.password
50+
click_button 'Login'
51+
expect(page).to have_content('Welcome back admin')
52+
end
53+
2854
after do
2955
Devise.authentication_keys = default_key
3056
end

0 commit comments

Comments
 (0)