Skip to content

A platform for community discussion. Free, open, simple.

SECURITY: Strip SQL comments and use non-recursive parameter interpolation in Data Explorer

Sam3dc7f0da7aa1
diff --git a/plugins/discourse-data-explorer/lib/discourse_data_explorer/data_explorer.rb b/plugins/discourse-data-explorer/lib/discourse_data_explorer/data_explorer.rb
index 7dda903f59c..7347b112fe8 100644
--- a/plugins/discourse-data-explorer/lib/discourse_data_explorer/data_explorer.rb
+++ b/plugins/discourse-data-explorer/lib/discourse_data_explorer/data_explorer.rb
@@ -1,7 +1,12 @@
# frozen_string_literal: true
+require "strscan"
+
module DiscourseDataExplorer
module DataExplorer
+ # the lookbehind skips `::type` casts
+ PARAM_REGEX = /(?<!:):([a-zA-Z_][a-zA-Z0-9_]*)/
+
# Used for ftype calls, see https://www.rubydoc.info/gems/pg/0.17.1/PG%2FResult:ftype
# and /usr/include/postgresql/server/catalog/pg_type_d.h
PG_TYPE_OID_JSON = 114
@@ -42,6 +47,9 @@ module DiscourseDataExplorer
return { error: e, duration_nanos: 0 }
end
+ # a parameter value could otherwise break out of a comment
+ executable_sql = interpolate_params(strip_comments(query.sql), query_args)
+
time_start, time_end, explain, err, result = nil
begin
ActiveRecord::Base.connection.transaction do
@@ -51,7 +59,7 @@ module DiscourseDataExplorer
# Set a statement timeout so we can't tie up the server
DB.exec "SET LOCAL statement_timeout = 10000"
- # SQL comments are for the benefits of the slow queries log
+ # Add trusted instrumentation after removing comments from the stored query.
started_by = opts[:current_user]&.username
sql = <<~SQL
/*
@@ -60,30 +68,20 @@ module DiscourseDataExplorer
#{"* Started by: #{started_by}" if started_by}
*/
WITH query AS (
- #{query.sql}
+ #{executable_sql}
) SELECT * FROM query
LIMIT #{opts[:limit] || SiteSetting.data_explorer_query_result_limit}
SQL
time_start = Time.now
- # Using MiniSql::InlineParamEncoder directly instead of DB.param_encoder because current implementation of
- # DB.param_encoder is meant for SQL fragments and not an entire SQL string.
- sql =
- MiniSql::InlineParamEncoder.new(ActiveRecord::Base.connection.raw_connection).encode(
- sql,
- query_args,
- )
-
result = ActiveRecord::Base.connection.raw_connection.async_exec(sql)
result.check # make sure it's done
time_end = Time.now
if opts[:explain]
explain =
- DB
- .query_hash("EXPLAIN #{query.sql}", query_args)
- .map { |row| row["QUERY PLAN"] }.join "\n"
+ DB.query_hash("EXPLAIN #{executable_sql}").map { |row| row["QUERY PLAN"] }.join "\n"
end
# All done. Issue a rollback anyways, just in case
@@ -104,6 +102,53 @@ module DiscourseDataExplorer
}
end
+ # Single pass, so that a substituted value is never rescanned.
+ def self.interpolate_params(sql, params)
+ return sql if params.blank?
+
+ # not DB.param_encoder, which quotes some types differently
+ encoder = MiniSql::InlineParamEncoder.new(ActiveRecord::Base.connection.raw_connection)
+ values = params.transform_keys(&:to_s)
+ sql.gsub(PARAM_REGEX) { values.key?($1) ? encoder.quote_val(values[$1]) : $& }
+ end
+
+ # Literals, quoted identifiers, and dollar-quoted strings are left untouched,
+ # as are unterminated constructs, so that PostgreSQL reports the syntax error.
+ def self.strip_comments(sql)
+ scanner = StringScanner.new(sql)
+ result = +""
+
+ until scanner.eos?
+ if scanner.scan(/--[^\n]*/)
+ # dropped, the newline is kept
+ elsif scanner.scan(%r{/\*})
+ depth = 1
+ while depth > 0 && scanner.skip_until(%r{\*/|/\*})
+ depth += scanner.matched == "/*" ? 1 : -1
+ end
+ scanner.terminate if depth > 0
+ result << " "
+ elsif scanner.match?(/'/)
+ # backslash escapes only apply to E'' strings
+ pattern = result.match?(/(?<![\w"])[eE]\z/) ? /'(?:''|[^'\\]|\\.)*'/m : /'(?:''|[^'])*'/
+ result << (scanner.scan(pattern) || scan_rest(scanner))
+ elsif scanner.match?(/"/)
+ result << (scanner.scan(/"(?:""|[^"])*"/) || scan_rest(scanner))
+ elsif scanner.match?(/\$\w*\$/)
+ result << (scanner.scan(/\$(\w*)\$.*?\$\1\$/m) || scan_rest(scanner))
+ else
+ result << (scanner.scan(%r{[^-/'"$]+}) || scanner.getch)
+ end
+ end
+
+ result
+ end
+
+ def self.scan_rest(scanner)
+ scanner.rest.tap { scanner.terminate }
+ end
+ private_class_method :scan_rest
+
def self.extra_data_pluck_fields
@extra_data_pluck_fields ||= {
user: {
diff --git a/plugins/discourse-data-explorer/lib/discourse_data_explorer/workflows/sql_action/v1.rb b/plugins/discourse-data-explorer/lib/discourse_data_explorer/workflows/sql_action/v1.rb
index 764ed4bc519..f99d41f2999 100644
--- a/plugins/discourse-data-explorer/lib/discourse_data_explorer/workflows/sql_action/v1.rb
+++ b/plugins/discourse-data-explorer/lib/discourse_data_explorer/workflows/sql_action/v1.rb
@@ -195,13 +195,8 @@ module DiscourseDataExplorer
req_params[param["name"].to_sym] = param["value"] if param["name"].present?
end
- if req_params.present?
- sql =
- MiniSql::InlineParamEncoder.new(ActiveRecord::Base.connection.raw_connection).encode(
- sql,
- req_params,
- )
- end
+ sql = DiscourseDataExplorer::DataExplorer.strip_comments(sql)
+ sql = DiscourseDataExplorer::DataExplorer.interpolate_params(sql, req_params)
query = DiscourseDataExplorer::Query.new(name: "workflow", sql: sql)
result =
diff --git a/plugins/discourse-data-explorer/spec/data_explorer_spec.rb b/plugins/discourse-data-explorer/spec/data_explorer_spec.rb
index ef252f02dda..0acc35c1152 100644
--- a/plugins/discourse-data-explorer/spec/data_explorer_spec.rb
+++ b/plugins/discourse-data-explorer/spec/data_explorer_spec.rb
@@ -1,6 +1,32 @@
# frozen_string_literal: true
describe DiscourseDataExplorer::DataExplorer do
+ describe ".strip_comments" do
+ it "removes line comments, keeping the newline" do
+ expect(described_class.strip_comments("SELECT 1 -- hi\nFROM t")).to eq("SELECT 1 \nFROM t")
+ end
+
+ it "removes nested block comments" do
+ expect(described_class.strip_comments("SELECT /* a /* b */ c */ 1")).to eq("SELECT 1")
+ end
+
+ it "keeps comment markers inside quoted identifiers" do
+ expect(described_class.strip_comments(%{SELECT 1 AS "-- a"})).to eq(%{SELECT 1 AS "-- a"})
+ end
+
+ it "keeps comment markers inside dollar-quoted strings" do
+ expect(described_class.strip_comments("SELECT $q$-- a$q$")).to eq("SELECT $q$-- a$q$")
+ end
+
+ it "does not let an escaped quote in an E'' string swallow a comment" do
+ expect(described_class.strip_comments("SELECT E'a\\'b' -- c")).to eq("SELECT E'a\\'b' ")
+ end
+
+ it "passes unterminated literals through" do
+ expect(described_class.strip_comments("SELECT 'a -- b")).to eq("SELECT 'a -- b")
+ end
+ end
+
describe ".run_query" do
fab!(:topic)
@@ -58,6 +84,102 @@ describe DiscourseDataExplorer::DataExplorer do
expect(result[:pg_result][0]["id"]).to eq(topic2.id)
end
+ it "adds query instrumentation after removing stored comments" do
+ user = Fabricate(:user)
+ query =
+ DiscourseDataExplorer::Query.create!(
+ name: "instrumented query",
+ sql: "-- removable annotation\nSELECT current_query() AS sql",
+ )
+
+ result = described_class.run_query(query, {}, { current_user: user })
+ executed_sql = result[:pg_result][0]["sql"]
+
+ expect(result[:error]).to eq(nil)
+ expect(executed_sql).to include(
+ "DiscourseDataExplorer Query",
+ "/admin/plugins/discourse-data-explorer/queries/#{query.id}",
+ "Started by: #{user.username}",
+ )
+ expect(executed_sql).not_to include("removable annotation")
+ end
+
+ it "keeps comment markers that are part of a string literal" do
+ query =
+ DiscourseDataExplorer::Query.create!(
+ name: "some query",
+ sql: "SELECT '-- not /* a comment' AS value",
+ )
+
+ result = described_class.run_query(query)
+
+ expect(result[:error]).to eq(nil)
+ expect(result[:pg_result][0]["value"]).to eq("-- not /* a comment")
+ end
+
+ it "interpolates each parameter once" do
+ query = DiscourseDataExplorer::Query.create!(name: "parameterized query", sql: <<~SQL)
+ -- [params]
+ -- string :selected
+ -- string :other
+ SELECT :selected AS value
+ SQL
+
+ result =
+ described_class.run_query(query, { "selected" => ":other", "other" => "other value" })
+
+ expect(result[:error]).to eq(nil)
+ expect(result[:pg_result][0]["value"]).to eq(":other")
+ end
+
+ it "interpolates a parameter whose name is a prefix of another" do
+ query = DiscourseDataExplorer::Query.create!(name: "parameterized query", sql: <<~SQL)
+ -- [params]
+ -- int :topic
+ -- int :topic_id
+ SELECT :topic AS a, :topic_id AS b
+ SQL
+
+ result = described_class.run_query(query, { "topic" => "1", "topic_id" => "2" })
+
+ expect(result[:error]).to eq(nil)
+ expect(result[:pg_result].to_a).to eq([{ "a" => 1, "b" => 2 }])
+ end
+
+ it "leaves undeclared parameters alone" do
+ query = DiscourseDataExplorer::Query.create!(name: "parameterized query", sql: <<~SQL)
+ -- [params]
+ -- string :declared
+ SELECT :declared AS a, ':undeclared' AS b
+ SQL
+
+ result = described_class.run_query(query, { "declared" => "value" })
+
+ expect(result[:error]).to eq(nil)
+ expect(result[:pg_result].to_a).to eq([{ "a" => "value", "b" => ":undeclared" }])
+ end
+
+ it "does not let a parameter break out of a string literal via another parameter" do
+ query = DiscourseDataExplorer::Query.create!(name: "parameterized query", sql: <<~SQL)
+ -- [params]
+ -- string :selected
+ -- string :other
+ SELECT :selected AS value
+ SQL
+
+ result =
+ described_class.run_query(
+ query,
+ {
+ "selected" => "x:other",
+ "other" => "'||(SELECT users.username FROM users LIMIT 1)||'",
+ },
+ )
+
+ expect(result[:error]).to eq(nil)
+ expect(result[:pg_result][0]["value"]).to eq("x:other")
+ end
+
describe "current_user_id parameter" do
fab!(:user)
diff --git a/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb b/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb
index 6ab17d20226..0535e935120 100644
--- a/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb
+++ b/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb
@@ -883,6 +883,50 @@ describe DiscourseDataExplorer::QueryController do
expect(response.body).not_to include(private_topic.title)
end
+ it "prevents nested params from injecting SQL" do
+ victim = Fabricate(:user, email: "[email protected]")
+ query = make_query(<<~SQL, { name: "Parameterized Query" }, [group.id.to_s])
+ -- [params]
+ -- string :first_value
+ -- string :second
+ SELECT :first_value AS value
+ SQL
+
+ post "/g/#{group.name}/reports/#{query.id}/run.json",
+ params: {
+ params: {
+ first_value: ":second",
+ second: "UNION SELECT email FROM user_emails --",
+ },
+ }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["success"]).to eq(true)
+ expect(response.parsed_body["rows"]).to eq([[":second"]])
+ expect(response.body).not_to include(victim.email)
+ end
+
+ it "prevents params from injecting SQL through parameter declaration comments" do
+ victim = Fabricate(:user, email: "[email protected]")
+ query = make_query(<<~SQL, { name: "Parameterized Query" }, [group.id.to_s])
+ -- [params]
+ -- string :injection
+ SELECT 'safe' AS value
+ SQL
+
+ post "/g/#{group.name}/reports/#{query.id}/run.json",
+ params: {
+ params: {
+ injection: "ignored\nSELECT email FROM user_emails UNION ALL --",
+ },
+ }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["success"]).to eq(true)
+ expect(response.parsed_body["rows"]).to eq([["safe"]])
+ expect(response.body).not_to include(victim.email)
+ end
+
it "can accept parameters as a hash" do
query_string = <<~SQL
-- [params]