From 04450e10866fc56890426525062fd5cdf56c4300 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Fri, 17 Jul 2026 19:30:43 +0200 Subject: Allowlist aggregate order columns and template names Page.aggregate interpolated order_by into SQL unchecked while already allowlisting order_direction; the column is now normalized and checked against the sortable columns, falling back to pages.id. Its values arrive from editor-authored aggregate shortcodes, so this was editor-gated, but the asymmetry was wrong regardless. template_name and default_template_name now validate inclusion in Page.custom_templates -- names render as filesystem paths, so only names actually present in the template directory are acceptable. Validated only on change: legacy rows whose template file has since vanished stay saveable, and valid_template's render-time fallback to standard_template continues to cover them. Two tests that wrote fabricated template names through the front door now arrange their state correctly (update_column for the stale-name fallback test, a real template for the update-persists test). --- app/models/node.rb | 5 +++++ app/models/page.rb | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) (limited to 'app/models') diff --git a/app/models/node.rb b/app/models/node.rb index 4f585e1..70ed4da 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -22,6 +22,11 @@ class Node < ApplicationRecord validates_uniqueness_of :slug, :scope => :parent_id, :unless => -> { parent_id.nil? } validates_presence_of :parent_id, :unless => -> { Node.root.nil? } + validates :default_template_name, + :inclusion => { :in => ->(_) { Page.custom_templates } }, + :allow_nil => true, + :if => :default_template_name_changed? + # Class methods # Returns a page for a given node. If no revision is supplied, it returns diff --git a/app/models/page.rb b/app/models/page.rb index e5a8d9d..be21ff7 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -11,6 +11,16 @@ class Page < ApplicationRecord translates :title, :abstract, :body # Globalize2 + # Template names render as filesystem paths; only names actually + # present in the public template directory are acceptable. Validated + # only on change so legacy rows whose template file has since + # vanished stay saveable -- valid_template already falls back to + # standard_template for those at render time. + validates :template_name, + :inclusion => { :in => ->(_) { Page.custom_templates } }, + :allow_nil => true, + :if => :template_name_changed? + # Associations belongs_to :node, optional: true belongs_to :user, optional: true @@ -77,7 +87,10 @@ class Page < ApplicationRecord .paginate(:page => page, :per_page => options[:limit]) end - scope.order("#{options[:order_by]} #{direction}") + column = options[:order_by].to_s.sub(/\Apages\./, "") + column = "id" unless %w[id published_at created_at updated_at].include?(column) + + scope.order("pages.#{column} #{direction}") .paginate(:page => page, :per_page => options[:limit]) end -- cgit v1.3