discourse/app/controllers/form_templates_controller.rb
Isaac Janzen ae5c9570fb SECURITY: Scope form template endpoints to accessible categories
Authenticated users could call `/form-templates` and `/form-templates/:id` to retrieve the name and full YAML body of templates attached to private/restricted categories they cannot access.

Both endpoints now scope results through `Category.secured(guardian)`, consistent with how category visibility is enforced elsewhere in the codebase.

---

**Security Advisory:** https://github.com/discourse/discourse/security/advisories/GHSA-w6g7-p2p9-2m5h
2026-05-19 00:26:04 +01:00

41 lines
1 KiB
Ruby
Vendored

# frozen_string_literal: true
class FormTemplatesController < ApplicationController
requires_login
before_action :ensure_form_templates_enabled
def index
form_templates = accessible_form_templates.order(:id)
render_serialized(form_templates, FormTemplateSerializer, root: "form_templates")
end
def show
params.require(:id)
template = accessible_form_templates.find_by(id: params[:id])
raise Discourse::NotFound if template.nil?
template.process!(guardian)
render_serialized(template, FormTemplateSerializer, root: "form_template")
end
private
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_enabled
raise Discourse::InvalidAccess.new unless SiteSetting.enable_form_templates
end
end