Skip to content

Commit 4aa7b5d

Browse files
Kajori BanerjeeAkhil Guliani
Kajori Banerjee
authored and
Akhil Guliani
committed
WL#16214 Export Query Results to Object Storage (Part 3/12)
Bug#37069723 - WL16214 OPTIMIZER_TRACE shows query text with PAR ID Bug#37035000 - WL#16214: PAR leaked in show processlist during export data Bug#37069770 - WL16214 SELECT EXPORT using PAR ID seen in slow_query_log Bug#37069714 - WL16214 General Log shows PAR id with SELECT EXPORT Bug#37041886 - WL#16214: PAR leaked to audit log AIM ============= Ensure SQL queries containing sensitive information like PAR IDs are safely logged without exposing sensitive data (specifically the PAR ID). If the query requires exporting of query result to object storage, then any pattern matching the /p/<parID>/n/ is replaced with /p/<redacted>/n/ Moreover, we should refer to the rewritten query instead of the user supplied one in such cases. Change-Id: I4bb5f07fa3c7b9cf83a9ef6ba9e42154e75a44cb
1 parent 174c1bb commit 4aa7b5d

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

sql/parse_tree_nodes.cc

+6
Original file line numberDiff line numberDiff line change
@@ -4252,6 +4252,12 @@ bool PT_into_destination_outfile::do_contextualize(Parse_context *pc) {
42524252
LEX *lex = pc->thd->lex;
42534253
lex->set_uncacheable(pc->select, UNCACHEABLE_SIDEEFFECT);
42544254
if (dumpfile_dest == OBJECT_STORE_DEST) {
4255+
/*
4256+
To ensure SQL queries containing sensitive information like PAR IDs are
4257+
safely logged without exposing sensitive data, we need to redact the
4258+
relevant portions of the query.
4259+
*/
4260+
lex->set_rewrite_required();
42554261
lex->set_execute_only_in_secondary_engine(true, OUTFILE_OBJECT_STORE);
42564262
lex->result = new (pc->mem_root) Query_result_to_object_store(&m_exchange);
42574263
} else {

sql/sql_rewrite.cc

+53
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,12 @@ bool rewrite_query(THD *thd, Consumer_type type, const Rewrite_params *params,
288288
case SQLCOM_ALTER_SERVER:
289289
rw.reset(new Rewriter_alter_server(thd, type));
290290
break;
291+
case SQLCOM_SELECT: {
292+
if (thd->lex->export_result_to_object_storage()) {
293+
rw.reset(new Rewriter_select_query(thd, type));
294+
}
295+
break;
296+
}
291297

292298
/*
293299
PREPARE stmt FROM <string> is rewritten so that <string> is
@@ -1819,3 +1825,50 @@ bool Rewriter_start_group_replication::rewrite(String &rlb) const {
18191825

18201826
return true;
18211827
}
1828+
1829+
Rewriter_select_query::Rewriter_select_query(THD *thd, Consumer_type type)
1830+
: I_rewriter(thd, type) {}
1831+
1832+
/**
1833+
Rewrite the query with the PAR id being redacted if the query exports query
1834+
result to the object storage.
1835+
Any pattern like "/p/.*?/n/" is replaced with "/p/<redacted>/n/"
1836+
@param[in,out] rlb Buffer to return the rewritten query in.
1837+
1838+
@retval true the query is rewritten
1839+
*/
1840+
bool Rewriter_select_query::rewrite(String &rlb) const {
1841+
assert(m_thd->lex->export_result_to_object_storage());
1842+
assert(m_thd->query().length);
1843+
String original_query_str(m_thd->query().str, m_thd->query().length,
1844+
system_charset_info);
1845+
String pattern_start("/p/", system_charset_info);
1846+
String pattern_end("/n/", system_charset_info);
1847+
1848+
size_t search_offset = 0;
1849+
while (search_offset <= original_query_str.length()) {
1850+
auto first_index = original_query_str.strstr(pattern_start, search_offset);
1851+
if (first_index == -1) {
1852+
// we could not find any other "/p/"
1853+
break;
1854+
}
1855+
1856+
// search for the "/n/" after the "/p/" location
1857+
auto second_index = original_query_str.strstr(pattern_end, first_index);
1858+
if (second_index == -1) {
1859+
// we could not find any other "/n/"
1860+
break;
1861+
}
1862+
1863+
// Copy from the original string from the search_offset till first_index
1864+
rlb.append(original_query_str.c_ptr() + search_offset,
1865+
first_index - search_offset);
1866+
rlb.append(STRING_WITH_LEN("/p/<redacted>"));
1867+
search_offset = second_index;
1868+
}
1869+
1870+
// Copy the remaining string
1871+
rlb.append(original_query_str.c_ptr() + search_offset,
1872+
original_query_str.length() - search_offset);
1873+
return true;
1874+
}

sql/sql_rewrite.h

+7
Original file line numberDiff line numberDiff line change
@@ -352,4 +352,11 @@ class Rewriter_start_group_replication final : public I_rewriter {
352352
bool rewrite(String &rlb) const override;
353353
};
354354

355+
/** Rewrites the SELECT statement. */
356+
class Rewriter_select_query final : public I_rewriter {
357+
public:
358+
Rewriter_select_query(THD *thd, Consumer_type type);
359+
bool rewrite(String &rlb) const override;
360+
};
361+
355362
#endif /* SQL_REWRITE_INCLUDED */

0 commit comments

Comments
 (0)