DEV: Use safer SQL functions for string queries when looking for tags (#26838)

This commit is contained in:
Daniel Waterworth 2024-05-02 10:13:45 -05:00 committed by GitHub
parent 9db5eafb15
commit a9ca35b671
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 4 additions and 6 deletions

View File

@ -510,16 +510,14 @@ module DiscourseTagging
term = opts[:term]
if term.present?
builder_params[:cleaned_term] = term
term = term.gsub("_", "\\_").downcase
if opts[:term_type] == DiscourseTagging.term_types[:starts_with]
builder_params[:term] = "#{term}%"
builder.where("starts_with(LOWER(name), LOWER(:cleaned_term))")
sql.gsub!("/*and_name_like*/", "AND starts_with(LOWER(t.name), LOWER(:cleaned_term))")
else
builder_params[:term] = "%#{term}%"
builder.where("position(LOWER(:cleaned_term) IN LOWER(t.name)) <> 0")
sql.gsub!("/*and_name_like*/", "AND position(LOWER(:cleaned_term) IN LOWER(t.name)) <> 0")
end
builder.where("LOWER(name) LIKE :term")
sql.gsub!("/*and_name_like*/", "AND LOWER(t.name) LIKE :term")
else
sql.gsub!("/*and_name_like*/", "")
end