mirror of
https://github.com/discourse/discourse.git
synced 2025-05-31 21:08:57 +08:00
FIX: restrict a href protocols on form template description (#27472)
This commit is contained in:
@ -3,7 +3,17 @@
|
|||||||
class FormTemplateYamlValidator < ActiveModel::Validator
|
class FormTemplateYamlValidator < ActiveModel::Validator
|
||||||
RESERVED_KEYWORDS = %w[title body category category_id tags]
|
RESERVED_KEYWORDS = %w[title body category category_id tags]
|
||||||
ALLOWED_TYPES = %w[checkbox dropdown input multi-select textarea upload]
|
ALLOWED_TYPES = %w[checkbox dropdown input multi-select textarea upload]
|
||||||
HTML_SANITIZATION_OPTIONS = { elements: ["a"], attributes: { "a" => %w[href target] } }
|
HTML_SANITIZATION_OPTIONS = {
|
||||||
|
elements: ["a"],
|
||||||
|
attributes: {
|
||||||
|
"a" => %w[href target],
|
||||||
|
},
|
||||||
|
protocols: {
|
||||||
|
"a" => {
|
||||||
|
"href" => %w[http https mailto],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
def validate(record)
|
def validate(record)
|
||||||
begin
|
begin
|
||||||
|
@ -136,6 +136,23 @@ RSpec.describe FormTemplateYamlValidator, type: :validator do
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "when description field has unsafe anchor href" do
|
||||||
|
let(:yaml_content) { <<~YAML }
|
||||||
|
- type: input
|
||||||
|
id: name
|
||||||
|
attributes:
|
||||||
|
label: "Full name"
|
||||||
|
description: "What is your full name? Details <a href='javascript:alert()'>here</a>."
|
||||||
|
YAML
|
||||||
|
|
||||||
|
it "adds a validation error" do
|
||||||
|
validator.validate(form_template)
|
||||||
|
expect(form_template.errors[:template]).to include(
|
||||||
|
I18n.t("form_templates.errors.unsafe_description"),
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#check_ids" do
|
describe "#check_ids" do
|
||||||
|
Reference in New Issue
Block a user