A platform for community discussion. Free, open, simple.
FIX: exclude 't' shortcut from min length bypass (#37440)
Sam20811cc9c08d
diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rbindex 53fbe95d770..d173bb6e612 100644--- a/app/controllers/search_controller.rb+++ b/app/controllers/search_controller.rb@@ -21,7 +21,7 @@ class SearchController < ApplicationControllerraise Discourse::InvalidParameters.new(:q) if params[:q].present? && !@search_term.present?if @search_term.present? && @search_term.length < SiteSetting.min_search_term_length &&- !Search.valid_search_shortcut?(@search_term)+ !Search.min_length_bypass?(@search_term)raise Discourse::InvalidParameters.new(:q)enddiff --git a/frontend/discourse/app/lib/search.js b/frontend/discourse/app/lib/search.jsindex d59a67ec6dd..0dc224cd6a9 100644--- a/frontend/discourse/app/lib/search.js+++ b/frontend/discourse/app/lib/search.js@@ -221,9 +221,12 @@ export function getSearchKey(args) {);}-// Patterns that indicate a valid search even without a traditional search term-const SEARCH_SYNTAX_PATTERNS =- /^(l|r|t)$|order:|category:|categories:|tags?:|before:|after:|status:|user:|group:|badge:|in:|with:|#|@/i;+// Patterns that bypass minimum search term length because they+// produce meaningful results on their own. Note: 't' (in:title) is+// intentionally excluded - it modifies where to search but still+// requires actual search terms.+const MIN_LENGTH_BYPASS_PATTERN =+ /^(l|r)$|order:|category:|categories:|tags?:|before:|after:|status:|user:|group:|badge:|in:|with:|#|@/i;export function isValidSearchTerm(searchTerm, siteSettings) {if (!searchTerm) {@@ -232,12 +235,10 @@ export function isValidSearchTerm(searchTerm, siteSettings) {const trimmed = searchTerm.trim();- // If the search contains filter syntax, it's valid regardless of length- if (SEARCH_SYNTAX_PATTERNS.test(trimmed)) {+ if (MIN_LENGTH_BYPASS_PATTERN.test(trimmed)) {return true;}- // Otherwise, apply minimum length requirementreturn trimmed.length >= siteSettings.min_search_term_length;}diff --git a/lib/search.rb b/lib/search.rbindex 9cf03c04de2..d09b7f9a189 100644--- a/lib/search.rb+++ b/lib/search.rb@@ -27,14 +27,16 @@ class Search%w[topic category user private_messages tags all_topics exclude_topics]end- # Patterns that indicate a valid search even without meeting minimum term length- # Includes shortcuts (l, r, t) and advanced filter syntax- VALID_SEARCH_SHORTCUT_PATTERN =- /\A[lrt]\z|order:|category:|categories:|tags?:|before:|after:|status:|user:|group:|badge:|in:|with:|#|@/i-- def self.valid_search_shortcut?(term)+ # Patterns that bypass minimum search term length because they+ # produce meaningful results on their own. Note: 't' (in:title) is+ # intentionally excluded - it modifies where to search but still+ # requires actual search terms.+ MIN_LENGTH_BYPASS_PATTERN =+ /\A[lr]\z|order:|category:|categories:|tags?:|before:|after:|status:|user:|group:|badge:|in:|with:|#|@/i++ def self.min_length_bypass?(term)return false if term.blank?- VALID_SEARCH_SHORTCUT_PATTERN.match?(term)+ MIN_LENGTH_BYPASS_PATTERN.match?(term)enddef self.ts_config(locale = SiteSetting.default_locale)diff --git a/spec/lib/search_spec.rb b/spec/lib/search_spec.rbindex e0b6e4d98b8..f5d8a73ee5c 100644--- a/spec/lib/search_spec.rb+++ b/spec/lib/search_spec.rb@@ -1543,6 +1543,13 @@ RSpec.describe Search doexpect(search.valid?).to eq(false)end++ it "marks 't' alone as an invalid search" do+ search = Search.new("t", type_filter: "topic")+ search.execute++ expect(search.valid?).to eq(false)+ endendcontext "with security" do