mirror of
https://github.com/codeninjasllc/discourse.git
synced 2025-04-28 06:54:06 -04:00
FIX: validate poll parameters when type=multiple
This commit is contained in:
parent
72f7c26514
commit
f736b6face
5 changed files with 26 additions and 3 deletions
plugins/poll
|
@ -57,7 +57,7 @@
|
||||||
if (attributes[DATA_PREFIX + "type"] === "number") {
|
if (attributes[DATA_PREFIX + "type"] === "number") {
|
||||||
// default values
|
// default values
|
||||||
if (isNaN(min)) { min = 1; }
|
if (isNaN(min)) { min = 1; }
|
||||||
if (isNaN(max)) { max = 10; }
|
if (isNaN(max)) { max = Discourse.SiteSettings.poll_maximum_options; }
|
||||||
if (isNaN(step)) { step = 1; }
|
if (isNaN(step)) { step = 1; }
|
||||||
// dynamically generate options
|
// dynamically generate options
|
||||||
contents.push(["bulletlist"]);
|
contents.push(["bulletlist"]);
|
||||||
|
|
|
@ -21,9 +21,9 @@ en:
|
||||||
|
|
||||||
multiple:
|
multiple:
|
||||||
help:
|
help:
|
||||||
at_least_min_options: "You may choose at least <strong>%{count}</strong> options."
|
at_least_min_options: "You must choose at least <strong>%{count}</strong> options."
|
||||||
up_to_max_options: "You may choose up to <strong>%{count}</strong> options."
|
up_to_max_options: "You may choose up to <strong>%{count}</strong> options."
|
||||||
x_options: "You may choose <strong>%{count}</strong> options."
|
x_options: "You must choose <strong>%{count}</strong> options."
|
||||||
between_min_and_max_options: "You may choose between <strong>%{min}</strong> and <strong>%{max}</strong> options."
|
between_min_and_max_options: "You may choose between <strong>%{min}</strong> and <strong>%{max}</strong> options."
|
||||||
|
|
||||||
cast-votes:
|
cast-votes:
|
||||||
|
|
|
@ -23,6 +23,9 @@ en:
|
||||||
default_poll_must_have_different_options: "Poll must have different options."
|
default_poll_must_have_different_options: "Poll must have different options."
|
||||||
named_poll_must_have_different_options: "Poll named <strong>%{name}</strong> must have different options."
|
named_poll_must_have_different_options: "Poll named <strong>%{name}</strong> must have different options."
|
||||||
|
|
||||||
|
default_poll_with_multiple_choices_has_invalid_parameters: "Poll with multiple choices has invalid parameters."
|
||||||
|
named_poll_with_multiple_choices_has_invalid_parameters: "Poll named <strong>%{name}</strong> with multiple choice has invalid parameters."
|
||||||
|
|
||||||
requires_at_least_1_valid_option: "You must select at least 1 valid option."
|
requires_at_least_1_valid_option: "You must select at least 1 valid option."
|
||||||
|
|
||||||
cannot_change_polls_after_5_minutes: "You cannot add, remove or rename polls after the first 5 minutes."
|
cannot_change_polls_after_5_minutes: "You cannot add, remove or rename polls after the first 5 minutes."
|
||||||
|
|
|
@ -248,6 +248,19 @@ after_initialize do
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# poll with multiple choices
|
||||||
|
if poll["type"] == "multiple"
|
||||||
|
min = (poll["min"].presence || 1).to_i
|
||||||
|
max = (poll["max"].presence || poll["options"].size).to_i
|
||||||
|
|
||||||
|
if min > max || max <= 0 || max > poll["options"].size || min >= poll["options"].size
|
||||||
|
poll["name"] == DEFAULT_POLL_NAME ?
|
||||||
|
self.errors.add(:base, I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters")) :
|
||||||
|
self.errors.add(:base, I18n.t("poll.named_poll_with_multiple_choices_has_invalid_parameters", name: poll["name"]))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# store the valid poll
|
# store the valid poll
|
||||||
polls[poll["name"]] = poll
|
polls[poll["name"]] = poll
|
||||||
end
|
end
|
||||||
|
|
|
@ -49,6 +49,13 @@ describe PostsController do
|
||||||
expect(json["errors"][0]).to eq(I18n.t("poll.default_poll_must_have_less_options", max: SiteSetting.poll_maximum_options))
|
expect(json["errors"][0]).to eq(I18n.t("poll.default_poll_must_have_less_options", max: SiteSetting.poll_maximum_options))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should have valid parameters" do
|
||||||
|
xhr :post, :create, { title: title, raw: "[poll type=multiple min=5]\n- A\n- B[/poll]" }
|
||||||
|
expect(response).not_to be_success
|
||||||
|
json = ::JSON.parse(response.body)
|
||||||
|
expect(json["errors"][0]).to eq(I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters"))
|
||||||
|
end
|
||||||
|
|
||||||
it "prevents self-xss" do
|
it "prevents self-xss" do
|
||||||
xhr :post, :create, { title: title, raw: "[poll name=<script>alert('xss')</script>]\n- A\n- B\n[/poll]" }
|
xhr :post, :create, { title: title, raw: "[poll name=<script>alert('xss')</script>]\n- A\n- B\n[/poll]" }
|
||||||
expect(response).to be_success
|
expect(response).to be_success
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue