A platform for community discussion. Free, open, simple.
SECURITY: limit amount of links in custom sidebar section (#22543)
Blake Erickson52b003d91576
diff --git a/app/controllers/sidebar_sections_controller.rb b/app/controllers/sidebar_sections_controller.rbindex 0c9a919d18f..5e84793d841 100644--- a/app/controllers/sidebar_sections_controller.rb+++ b/app/controllers/sidebar_sections_controller.rb@@ -29,6 +29,8 @@ class SidebarSectionsController < ApplicationControllerrender_serialized(sidebar_section, SidebarSectionSerializer)rescue ActiveRecord::RecordInvalid => erender_json_error(e.record.errors.full_messages.first)+ rescue ActiveRecord::NestedAttributes::TooManyRecords => e+ render_json_error(e.message)enddef update@@ -62,6 +64,8 @@ class SidebarSectionsController < ApplicationControllerrender_serialized(sidebar_section.reload, SidebarSectionSerializer)rescue ActiveRecord::RecordInvalid => erender_json_error(e.record.errors.full_messages.first)+ rescue ActiveRecord::NestedAttributes::TooManyRecords => e+ render_json_error(e.message)rescue Discourse::InvalidAccessrender json: failed_json, status: 403enddiff --git a/app/models/sidebar_section.rb b/app/models/sidebar_section.rbindex 0162ca5fe4b..3b564c3faf0 100644--- a/app/models/sidebar_section.rb+++ b/app/models/sidebar_section.rb@@ -11,7 +11,9 @@ class SidebarSection < ActiveRecord::Basesource: :linkable,source_type: "SidebarUrl"- accepts_nested_attributes_for :sidebar_urls, allow_destroy: true+ accepts_nested_attributes_for :sidebar_urls,+ allow_destroy: true,+ limit: -> { SiteSetting.max_sidebar_section_links }before_save :set_system_user_for_public_sectiondiff --git a/config/site_settings.yml b/config/site_settings.ymlindex 52f857e45ad..e1d65ce5cea 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -2137,6 +2137,9 @@ developer:default: ""allow_any: falserefresh: true+ max_sidebar_section_links:+ default: 50+ hidden: truenavigation:navigation_menu:diff --git a/spec/requests/sidebar_sections_controller_spec.rb b/spec/requests/sidebar_sections_controller_spec.rbindex 61124d3e7e4..a8a0bb3764a 100644--- a/spec/requests/sidebar_sections_controller_spec.rb+++ b/spec/requests/sidebar_sections_controller_spec.rb@@ -87,6 +87,24 @@ RSpec.describe SidebarSectionsController doexpect(sidebar_section.sidebar_urls.fourth.external).to be falseend+ it "validates max number of links" do+ SiteSetting.max_sidebar_section_links = 5++ sign_in(user)++ links =+ 6.times.map do+ { icon: "external-link-alt", name: "My preferences", value: "/my/preferences" }+ end++ post "/sidebar_sections.json", params: { title: "custom section", links: links }++ expect(response.status).to eq(422)+ expect(response.parsed_body["errors"]).to eq(+ ["Maximum 5 records are allowed. Got 6 records instead."],+ )+ end+it "does not allow regular user to create public section" dosign_in(user)@@ -236,6 +254,28 @@ RSpec.describe SidebarSectionsController do)end+ it "validates limit of links" do+ SiteSetting.max_sidebar_section_links = 5++ sign_in(user)++ links =+ 6.times.map do+ { icon: "external-link-alt", name: "My preferences", value: "/my/preferences" }+ end++ put "/sidebar_sections/#{sidebar_section.id}.json",+ params: {+ title: "custom section",+ links: links,+ }++ expect(response.status).to eq(422)+ expect(response.parsed_body["errors"]).to eq(+ ["Maximum 5 records are allowed. Got 6 records instead."],+ )+ end+it "doesn't allow to edit other's sections" dosidebar_section_2 = Fabricate(:sidebar_section)sidebar_url_3 = Fabricate(:sidebar_url, name: "other_tags", value: "/tags")