DEV: Sanitize integer site settings in front- and back-end (#23816)

Currently, if you set an integer site setting in the admin interface and include thousands separators, you will silently configure the wrong value.

This PR replaces TextField inputs for integer site settings with NumberField. It also cleans the numeric input of any non-digits in the backend in case any separators make it through.
This commit is contained in:
Ted Johansson 2023-10-06 19:21:01 +02:00 committed by GitHub
parent 484004fc5e
commit e113eff663
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 20 additions and 1 deletions

View File

@ -0,0 +1,4 @@
<NumberField @value={{this.value}} @classNames="input-setting-integer" />
<SettingValidationMessage @message={{this.validationMessage}} />
<div class="desc">{{html-safe this.setting.description}}</div>

View File

@ -0,0 +1,3 @@
import Component from "@ember/component";
export default class Integer extends Component {}

View File

@ -15,6 +15,7 @@ import SiteSettingDefaultCategoriesModal from "../components/modal/site-setting-
const CUSTOM_TYPES = [ const CUSTOM_TYPES = [
"bool", "bool",
"integer",
"enum", "enum",
"list", "list",
"url_list", "url_list",

View File

@ -58,6 +58,7 @@
float: left; float: left;
} }
.input-setting-string, .input-setting-string,
.input-setting-integer,
.input-setting-textarea { .input-setting-textarea {
width: 100%; width: 100%;
@media (max-width: $mobile-breakpoint) { @media (max-width: $mobile-breakpoint) {

View File

@ -31,7 +31,10 @@ class Admin::SiteSettingsController < Admin::AdminController
raise_access_hidden_setting(id) raise_access_hidden_setting(id)
if SiteSetting.type_supervisor.get_type(id) == :uploaded_image_list case SiteSetting.type_supervisor.get_type(id)
when :integer
value = value.gsub(/\D/, "")
when :uploaded_image_list
value = Upload.get_from_urls(value.split("|")).to_a value = Upload.get_from_urls(value.split("|")).to_a
end end

View File

@ -269,6 +269,13 @@ RSpec.describe Admin::SiteSettingsController do
expect(SiteSetting.title).to eq("") expect(SiteSetting.title).to eq("")
end end
it "sanitizes integer values" do
put "/admin/site_settings/suggested_topics.json", params: { suggested_topics: "1,000" }
expect(response.status).to eq(200)
expect(SiteSetting.suggested_topics).to eq(1000)
end
context "with default user options" do context "with default user options" do
let!(:user1) { Fabricate(:user) } let!(:user1) { Fabricate(:user) }
let!(:user2) { Fabricate(:user) } let!(:user2) { Fabricate(:user) }