Skip to content

Commit 0bcbdd2

Browse files
Merge pull request ClickHouse#90105 from ClickHouse/backport/25.8/89942
Backport ClickHouse#89942 to 25.8: Fix escaping for some `SHOW` queries
2 parents f9a995b + 8ae5067 commit 0bcbdd2

9 files changed

+50
-7
lines changed

src/Databases/SQLite/DatabaseSQLite.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <Parsers/ASTFunction.h>
1717
#include <Storages/StorageSQLite.h>
1818
#include <Databases/SQLite/SQLiteUtils.h>
19+
#include <Common/quoteString.h>
1920

2021

2122
namespace DB
@@ -104,7 +105,7 @@ bool DatabaseSQLite::checkSQLiteTable(const String & table_name) const
104105
if (!sqlite_db)
105106
sqlite_db = openSQLiteDB(database_path, getContext(), /* throw_on_error */true);
106107

107-
const String query = fmt::format("SELECT name FROM sqlite_master WHERE type='table' AND name='{}';", table_name);
108+
const String query = "SELECT name FROM sqlite_master WHERE type = 'table' AND name = " + quoteStringSQLite(table_name) + ";";
108109

109110
auto callback_get_data = [](void * res, int, char **, char **) -> int
110111
{

src/Interpreters/InterpreterShowColumnsQuery.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ WITH map(
7676
'String', '{}',
7777
'FixedString', '{}') AS native_to_mysql_mapping,
7878
)",
79-
remap_string_as_text ? "TEXT" : "BLOB",
80-
remap_fixed_string_as_text ? "TEXT" : "BLOB");
79+
remap_string_as_text ? "TEXT" : "BLOB",
80+
remap_fixed_string_as_text ? "TEXT" : "BLOB");
8181

8282
rewritten_query += R"(
8383
splitByRegexp('\(|\)', type_) AS split,
@@ -127,7 +127,8 @@ SELECT
127127
'' AS privileges )";
128128
}
129129

130-
rewritten_query += fmt::format(R"(
130+
rewritten_query += fmt::format(
131+
R"(
131132
-- need to rename columns of the base table to avoid "CYCLIC_ALIASES" errors
132133
FROM (SELECT name AS name_,
133134
database AS database_,
@@ -141,7 +142,9 @@ FROM (SELECT name AS name_,
141142
FROM system.columns)
142143
WHERE
143144
database_ = '{}'
144-
AND table_ = '{}' )", database, table);
145+
AND table_ = '{}' )",
146+
database,
147+
table);
145148

146149
if (!query.like.empty())
147150
{
@@ -152,7 +155,7 @@ WHERE
152155
rewritten_query += "ILIKE ";
153156
else
154157
rewritten_query += "LIKE ";
155-
rewritten_query += fmt::format("'{}'", query.like);
158+
rewritten_query += quoteString(query.like);
156159
}
157160
else if (query.where_expression)
158161
rewritten_query += fmt::format(" AND ({})", query.where_expression->formatWithSecretsOneLine());

src/Interpreters/InterpreterShowFunctionsQuery.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <Interpreters/InterpreterShowFunctionsQuery.h>
66
#include <Interpreters/executeQuery.h>
77
#include <Parsers/ASTShowFunctionsQuery.h>
8+
#include <Common/quoteString.h>
89

910
namespace DB
1011
{
@@ -38,7 +39,7 @@ FROM {}.{})",
3839
{
3940
rewritten_query += " WHERE name ";
4041
rewritten_query += query.case_insensitive_like ? "ILIKE " : "LIKE ";
41-
rewritten_query += fmt::format("'{}'", query.like);
42+
rewritten_query += quoteString(query.like);
4243
}
4344

4445
return rewritten_query;

tests/queries/0_stateless/03714_queries_escaping_1.reference

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
show columns from a.b like 'a\' or 1=1;--'

tests/queries/0_stateless/03714_queries_escaping_2.reference

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
show functions like 'a\' or 1=1;--'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1
2+
0
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env bash
2+
# Tags: no-fasttest, no-parallel
3+
# no-parallel: dealing with an SQLite database makes concurrent SHOW TABLES queries fail sporadically with the "database is locked" error.
4+
5+
CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
6+
# shellcheck source=../shell_config.sh
7+
. "$CUR_DIR"/../shell_config.sh
8+
9+
export CURR_DATABASE="test_03714_sqllite_${CLICKHOUSE_DATABASE}"
10+
11+
DB_PATH=${USER_FILES_PATH}/${CURR_DATABASE}_db1
12+
13+
function cleanup()
14+
{
15+
${CLICKHOUSE_CLIENT} --query="DROP DATABASE IF EXISTS ${CURR_DATABASE}"
16+
}
17+
trap cleanup EXIT
18+
19+
20+
sqlite3 "${DB_PATH}" 'DROP TABLE IF EXISTS table1'
21+
22+
sqlite3 "${DB_PATH}" 'CREATE TABLE table1 (col1 text, col2 smallint);'
23+
24+
chmod ugo+w "${DB_PATH}"
25+
26+
sqlite3 "${DB_PATH}" "INSERT INTO table1 VALUES ('line1', 1), ('line2', 2), ('line3', 3)"
27+
28+
${CLICKHOUSE_CLIENT} --query="CREATE DATABASE ${CURR_DATABASE} ENGINE = SQLite('${DB_PATH}')"
29+
30+
${CLICKHOUSE_CLIENT} --query="EXISTS TABLE ${CURR_DATABASE}.table1;"
31+
${CLICKHOUSE_CLIENT} --query="EXISTS TABLE ${CURR_DATABASE}.\"a\' or name='table1\";"
32+
33+
34+
${CLICKHOUSE_CLIENT} --query="DROP DATABASE IF EXISTS ${CURR_DATABASE}"

0 commit comments

Comments
 (0)