Skip to content

Commit

Permalink
Feature/add sanitize to prevent xss (LINCnil#169)
Browse files Browse the repository at this point in the history
* feat: configure sanitize and add example

* feat: sanitize name, author, evaluator, validator

* feat: add some sanitize overwrite

* fix: fix for l.38 on evoluations_controller
  • Loading branch information
syl-p authored May 9, 2022
1 parent c989a97 commit 201883e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .env-example
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ DEVISE_SECRET_KEY=
DEVISE_PEPPER=
MAILER_SENDER=
DEFAULT_URL=
DEFAULT_PORT=
DEFAULT_PORT=
SANITIZED_ALLOWED_TAGS="strong em a ul li"
SANITIZED_ALLOWED_ATTRIBUTES="href title"
2 changes: 1 addition & 1 deletion app/controllers/evaluations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def create
# PATCH/PUT /evaluations/1
def update
if @evaluation.update(evaluation_params)
@evaluation.evaluation_infos = JSON.parse(params['evaluation']["evaluation_infos"]) if params['evaluation']["evaluation_infos"]
@evaluation.evaluation_infos = JSON.parse(params['evaluation']["evaluation_infos"]) if params.dig("evaluation", "evaluation_infos")
@evaluation.global_status = 0 if @evaluation.status == 1 && evaluation_params["global_status"].blank?
@evaluation.save
render json: serialize(@evaluation)
Expand Down
7 changes: 7 additions & 0 deletions app/models/evaluation.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
class Evaluation < ApplicationRecord
include ActionView::Helpers::SanitizeHelper
belongs_to :pia, inverse_of: :evaluations
validates :reference_to, presence: true
attr_accessor :evaluation_infos

after_create :email_for_evaluation! if ENV['ENABLE_AUTHENTICATION'].present?
after_update :email_for_validation! if ENV['ENABLE_AUTHENTICATION'].present?

after_initialize :overwrite_to_safety_values

def email_for_evaluation!
return unless self.evaluation_infos.present?

Expand All @@ -32,4 +35,8 @@ def email_for_validation!

UserMailer.with(validator: validator, pia: self.pia).section_ready_for_validation.deliver_now
end

def overwrite_to_safety_values
self.evaluation_comment = sanitize read_attribute(:evaluation_comment)
end
end
11 changes: 11 additions & 0 deletions app/models/pia.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Pia < ApplicationRecord
include ActionView::Helpers::SanitizeHelper
has_many :answers, inverse_of: :pia, dependent: :destroy
has_many :comments, inverse_of: :pia, dependent: :destroy
has_many :evaluations, inverse_of: :pia, dependent: :destroy
Expand All @@ -10,6 +11,8 @@ class Pia < ApplicationRecord
belongs_to :structure, optional: true
validates :name, presence: true

after_initialize :overwrite_to_safety_values

def self.import(json_string)
json = JSON.parse(json_string)
json.each do |pia_in|
Expand Down Expand Up @@ -48,6 +51,14 @@ def duplicate
@clone
end

def overwrite_to_safety_values
self.name = sanitize read_attribute(:name)
self.author_name = sanitize read_attribute(:author_name)
self.evaluator_name = sanitize read_attribute(:evaluator_name)
self.validator_name = sanitize read_attribute(:validator_name)
self.category = sanitize read_attribute(:category)
end

private

def duplicate_self
Expand Down
7 changes: 7 additions & 0 deletions app/models/structure.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
class Structure < ApplicationRecord
include ActionView::Helpers::SanitizeHelper
has_many :pias, dependent: :nullify
after_initialize :overwrite_to_safety_values

def overwrite_to_safety_values
self.name = sanitize read_attribute(:name)
self.sector_name = sanitize read_attribute(:sector_name)
end
end
5 changes: 5 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,10 @@ class Application < Rails::Application
config.i18n.available_locales = [:bg, :cs, :da, :de, :el, :en, :es, :et,
:fi, :fr, :hr, :hu, :it, :lt, :lv, :nl,
:no, :pl, :pt, :ro, :sl, :sv]

tags_allowed = ENV['SANITIZED_ALLOWED_TAGS'] ? ENV['SANITIZED_ALLOWED_TAGS'].split(' ') : []
config.action_view.sanitized_allowed_tags = tags_allowed
attributes_allowed = ENV['SANITIZED_ALLOWED_ATTRIBUTES'] ? ENV['SANITIZED_ALLOWED_ATTRIBUTES'].split(' ') : []
config.action_view.sanitized_allowed_attributes = attributes_allowed
end
end

0 comments on commit 201883e

Please sign in to comment.