A platform for community discussion. Free, open, simple.
SECURITY: Scope form template endpoints to accessible categories
Isaac Janzenae5c9570fb91
diff --git a/app/controllers/form_templates_controller.rb b/app/controllers/form_templates_controller.rbindex 6eaf2a1768a..0c12b5f3fc8 100644--- a/app/controllers/form_templates_controller.rb+++ b/app/controllers/form_templates_controller.rb@@ -5,14 +5,14 @@ class FormTemplatesController < ApplicationControllerbefore_action :ensure_form_templates_enableddef index- form_templates = FormTemplate.all.order(:id)+ form_templates = accessible_form_templates.order(:id)render_serialized(form_templates, FormTemplateSerializer, root: "form_templates")enddef showparams.require(:id)- template = FormTemplate.find_by(id: params[:id])+ template = accessible_form_templates.find_by(id: params[:id])raise Discourse::NotFound if template.nil?@@ -23,6 +23,18 @@ class FormTemplatesController < ApplicationControllerprivate+ def accessible_form_templates+ unassigned = FormTemplate.where.not(id: CategoryFormTemplate.select(:form_template_id))+ accessible =+ FormTemplate.where(+ id:+ CategoryFormTemplate.where(category_id: Category.secured(guardian)).select(+ :form_template_id,+ ),+ )+ unassigned.or(accessible)+ end+def ensure_form_templates_enabledraise Discourse::InvalidAccess.new unless SiteSetting.enable_form_templatesenddiff --git a/spec/requests/form_templates_controller_spec.rb b/spec/requests/form_templates_controller_spec.rbindex 3ae0f71d167..6ae86134bc4 100644--- a/spec/requests/form_templates_controller_spec.rb+++ b/spec/requests/form_templates_controller_spec.rb@@ -23,6 +23,37 @@ RSpec.describe FormTemplatesController doexpect(templates).to eq(form_templates)end++ context "with private and public category templates" do+ fab!(:group)+ fab!(:private_category) { Fabricate(:private_category, group: group) }+ fab!(:private_template) do+ Fabricate(+ :form_template,+ name: "Private Template",+ template: "---\n- type: input\n id: secret\n",+ )+ end+ fab!(:public_category, :category)+ fab!(:public_template) { Fabricate(:form_template, name: "Public Template") }++ before do+ private_category.form_templates << private_template+ public_category.form_templates << public_template+ end++ it "does not return templates only associated with private categories" do+ get "/form-templates.json"+ expect(response.status).to eq(200)+ returned_templates = response.parsed_body["form_templates"]+ returned_ids = returned_templates.map { |t| t["id"] }+ expect(returned_ids).to include(public_template.id)+ expect(returned_ids).not_to include(private_template.id)+ expect(returned_templates.map { |t| t["template"] }).not_to include(+ private_template.template,+ )+ end+ endendcontext "when you are not logged in" do@@ -61,6 +92,19 @@ RSpec.describe FormTemplatesController doexpect(current_template["template"]).to eq(form_template.template)end+ context "with a template only in an inaccessible private category" do+ fab!(:group)+ fab!(:private_category) { Fabricate(:private_category, group: group) }+ fab!(:private_template) { Fabricate(:form_template, name: "Secret Template") }++ before { private_category.form_templates << private_template }++ it "returns 404" do+ get "/form-templates/#{private_template.id}.json"+ expect(response.status).to eq(404)+ end+ end+context "when using tag groups in a form template" dofab!(:admin)fab!(:tag1) { Fabricate(:tag, description: "A Tag 1 custom Translation") }