Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanitize name and description attrs of TermTaxonomy classes to prevent XSS attacks #3

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ Metrics/MethodLength:
Exclude:
- spec/**/*

RSpec/AnyInstance:
Enabled: false

RSpec/ExampleLength:
Enabled: false

Expand Down
13 changes: 13 additions & 0 deletions app/models/camaleon_cms/term_taxonomy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ def self.inherited(subclass)
# attr_accessible :data_options
# attr_accessible :data_metas

# TODO: Remove the 1st branch when support will be dropped of Rails < 7.1
if ::Rails::VERSION::STRING < '7.1.0'
before_validation(on: %i[create update]) do
%i[name description].each do |attr|
next unless new_record? || attribute_changed?(attr)

self[attr] = ActionController::Base.helpers.sanitize(__send__(attr))
end
end
else
normalizes :name, :description, with: ->(field) { ActionController::Base.helpers.sanitize(field) }
end

# callbacks
before_validation :before_validating
before_destroy :destroy_dependencies
Expand Down
10 changes: 10 additions & 0 deletions spec/models/category_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

require 'rails_helper'
require 'shared_specs/sanitize_attrs'

RSpec.describe CamaleonCms::Category, type: :model do
before { allow_any_instance_of(described_class).to receive(:set_site) }

it_behaves_like 'sanitize attrs', model: described_class, attrs_to_sanitize: %i[name description]
end
10 changes: 10 additions & 0 deletions spec/models/nav_menu_item_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

require 'rails_helper'
require 'shared_specs/sanitize_attrs'

RSpec.describe CamaleonCms::NavMenuItem, type: :model do
before { allow_any_instance_of(described_class).to receive(:update_count) }

it_behaves_like 'sanitize attrs', model: described_class, attrs_to_sanitize: %i[name description]
end
8 changes: 8 additions & 0 deletions spec/models/nav_menu_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

require 'rails_helper'
require 'shared_specs/sanitize_attrs'

RSpec.describe CamaleonCms::NavMenu, type: :model do
it_behaves_like 'sanitize attrs', model: described_class, attrs_to_sanitize: %i[name description]
end
8 changes: 8 additions & 0 deletions spec/models/plugin_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

require 'rails_helper'
require 'shared_specs/sanitize_attrs'

RSpec.describe CamaleonCms::Plugin, type: :model do
it_behaves_like 'sanitize attrs', model: described_class, attrs_to_sanitize: %i[name description]
end
8 changes: 8 additions & 0 deletions spec/models/post_tag_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

require 'rails_helper'
require 'shared_specs/sanitize_attrs'

RSpec.describe CamaleonCms::PostTag, type: :model do
it_behaves_like 'sanitize attrs', model: described_class, attrs_to_sanitize: %i[name description]
end
10 changes: 10 additions & 0 deletions spec/models/post_type_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

require 'rails_helper'
require 'shared_specs/sanitize_attrs'

RSpec.describe CamaleonCms::PostType, type: :model do
init_site

it_behaves_like 'sanitize attrs', model: described_class, attrs_to_sanitize: %i[name description]
end
3 changes: 3 additions & 0 deletions spec/models/site_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# frozen_string_literal: true

require 'rails_helper'
require 'shared_specs/sanitize_attrs'

RSpec.describe CamaleonCms::Site, type: :model do
it_behaves_like 'sanitize attrs', model: described_class, attrs_to_sanitize: %i[name description]

describe 'check metas relationships' do
let!(:site) { create(:site).decorate }

Expand Down
8 changes: 8 additions & 0 deletions spec/models/term_taxonomy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

require 'rails_helper'
require 'shared_specs/sanitize_attrs'

RSpec.describe CamaleonCms::TermTaxonomy, type: :model do
it_behaves_like 'sanitize attrs', model: described_class, attrs_to_sanitize: %i[name description]
end
8 changes: 8 additions & 0 deletions spec/models/theme_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

require 'rails_helper'
require 'shared_specs/sanitize_attrs'

RSpec.describe CamaleonCms::Theme, type: :model do
it_behaves_like 'sanitize attrs', model: described_class, attrs_to_sanitize: %i[name description]
end
8 changes: 8 additions & 0 deletions spec/models/user_role_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

require 'rails_helper'
require 'shared_specs/sanitize_attrs'

RSpec.describe CamaleonCms::UserRole, type: :model do
it_behaves_like 'sanitize attrs', model: described_class, attrs_to_sanitize: %i[name description]
end
8 changes: 8 additions & 0 deletions spec/models/widget/widget_main_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

require 'rails_helper'
require 'shared_specs/sanitize_attrs'

RSpec.describe CamaleonCms::Widget::Main, type: :model do
it_behaves_like 'sanitize attrs', model: described_class, attrs_to_sanitize: %i[name description]
end
8 changes: 8 additions & 0 deletions spec/models/widget/widget_sidebar_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

require 'rails_helper'
require 'shared_specs/sanitize_attrs'

RSpec.describe CamaleonCms::Widget::Sidebar, type: :model do
it_behaves_like 'sanitize attrs', model: described_class, attrs_to_sanitize: %i[name description]
end
23 changes: 23 additions & 0 deletions spec/shared_specs/sanitize_attrs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

RSpec.shared_examples 'sanitize attrs' do |model:, attrs_to_sanitize:|
attrs_to_sanitize.each do |attr|
it 'sanitizes name attribute on create' do
attrs_for_creation = { attr => '"><script>alert(1)</script>' }
attrs_for_creation.merge!(site: @site) if defined?(@site)
model_instance = model.create(attrs_for_creation)

expect(model_instance.__send__(attr)).to eql('"&gt;alert(1)')
end

it 'sanitizes name attribute on update' do
attrs_for_creation = { attr => 'Legit text' }
attrs_for_creation.merge!(site: @site) if defined?(@site)
model_instance = model.create(attrs_for_creation)
# attrs_for_creation = { attr => '"><script>alert(1)</script>' }
model_instance.update(attr => '"><script>alert(1)</script>')

expect(model_instance.__send__(attr)).to eql('"&gt;alert(1)')
end
end
end
Loading