Skip to content

Commit

Permalink
security: prevent xss in pagination params (#2990)
Browse files Browse the repository at this point in the history
* security: prevent xss in turbo frames

* wip

* wip

* wip

* use CGI.escapeHTML

* test and fix when params[:turbo_frame] is nil

---------

Co-authored-by: Paul Bob <[email protected]>
  • Loading branch information
adrianthedev and Paul-Bob authored Jul 15, 2024
1 parent 3ce52b9 commit 99beb29
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/avo/concerns/pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ def apply_pagination(index_params:, query:)
extra_pagy_params[:keep_filters_panel_open] = "0"
end

data_turbo_frame = "data-turbo-frame=\"#{CGI.escapeHTML(params[:turbo_frame]) if params[:turbo_frame]}\""

send PAGINATION_METHOD[pagination_type.to_sym],
query,
items: index_params[:per_page],
link_extra: "data-turbo-frame=\"#{params[:turbo_frame]}\"", # Add extra arguments in pagy 7.
anchor_string: "data-turbo-frame=\"#{params[:turbo_frame]}\"", # Add extra arguments in pagy 8.
link_extra: data_turbo_frame, # Add extra arguments in pagy 7.
anchor_string: data_turbo_frame, # Add extra arguments in pagy 8.
params: extra_pagy_params,
size: pagination_hash[:size]
end
Expand Down
14 changes: 14 additions & 0 deletions spec/system/avo/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,18 @@
}.to change(Project, :count).by(-1)
end
end

describe "security", js: true do
let!(:projects) { create_list :project, 2 }

it "xss in turbo frames 1" do
visit "/admin/resources/projects?per_page=1&turbo_frame=has_many_field_show_test_xgc2pf%22%3e%3cscript%3ealert(1)%3c%2fscript%3ep9sk5"
expect { accept_alert }.to raise_error(Capybara::ModalNotFound)
end

it "xss in turbo frames 2" do
visit '/admin/resources/projects?per_page=1&turbo_frame=has_many_field_show_test_xgc2pf><script>alert("XSS")<%2Fscript>p9sk5'
expect { accept_alert }.to raise_error(Capybara::ModalNotFound)
end
end
end

0 comments on commit 99beb29

Please sign in to comment.