forked from databendlabs/databend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.rs
More file actions
503 lines (448 loc) · 17.5 KB
/
Copy pathstring.rs
File metadata and controls
503 lines (448 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// Copyright 2021 Datafuse Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::string::FromUtf8Error;
use std::sync::LazyLock;
use databend_common_exception::ErrorCode;
use databend_common_exception::Result;
use regex::Regex;
use unicode_segmentation::UnicodeSegmentation;
/// Function that escapes special characters in a string.
///
/// All characters except digit, alphabet and '_' are treated as special characters.
/// A special character will be converted into "%num" where num is the hexadecimal form of the character.
///
/// # Example
/// ```
/// let key = "data_bend!!";
/// let new_key = escape_for_key(&key);
/// assert_eq!(Ok("data_bend%21%21".to_string()), new_key);
/// ```
pub fn escape_for_key(key: &str) -> std::result::Result<String, FromUtf8Error> {
let mut new_key = Vec::with_capacity(key.len());
fn hex(num: u8) -> u8 {
match num {
0..=9 => b'0' + num,
10..=15 => b'a' + (num - 10),
unreachable => unreachable!("Unreachable branch num = {}", unreachable),
}
}
for char in key.as_bytes() {
match char {
b'0'..=b'9' => new_key.push(*char),
b'_' | b'a'..=b'z' | b'A'..=b'Z' => new_key.push(*char),
_other => {
new_key.push(b'%');
new_key.push(hex(*char / 16));
new_key.push(hex(*char % 16));
}
}
}
String::from_utf8(new_key)
}
/// The reverse function of escape_for_key.
///
/// # Example
/// ```
/// let key = "data_bend%21%21";
/// let original_key = unescape_for_key(&key);
/// assert_eq!(Ok("data_bend!!".to_string()), original_key);
/// ```
pub fn unescape_for_key(key: &str) -> std::result::Result<String, FromUtf8Error> {
let mut new_key = Vec::with_capacity(key.len());
fn unhex(num: u8) -> u8 {
match num {
b'0'..=b'9' => num - b'0',
b'a'..=b'f' => num - b'a' + 10,
unreachable => unreachable!("Unreachable branch num = {}", unreachable),
}
}
let bytes = key.as_bytes();
let mut index = 0;
while index < bytes.len() {
match bytes[index] {
b'%' => {
// The last byte of the string won't be '%'
let mut num = unhex(bytes[index + 1]) * 16;
num += unhex(bytes[index + 2]);
new_key.push(num);
index += 3;
}
other => {
new_key.push(other);
index += 1;
}
}
}
String::from_utf8(new_key)
}
/// Mask a string by "******", but keep `unmask_len` of suffix.
#[inline]
pub fn mask_string(s: &str, _unmask_len: usize) -> String {
let chars: Vec<char> = s.chars().collect();
if chars.len() <= 4 {
"***".to_string()
} else {
let head: String = chars[..2].iter().collect();
let tail: String = chars[chars.len() - 2..].iter().collect();
format!("{}***{}", head, tail)
}
}
/// Returns string after processing escapes.
/// This used for settings string unescape, like unescape format_field_delimiter from `\\x01` to `\x01`.
pub fn unescape_string(escape_str: &str) -> Result<String> {
enquote::unescape(escape_str, None)
.map_err(|e| ErrorCode::Internal(format!("unescape:{} error:{:?}", escape_str, e)))
}
pub fn format_byte_size(bytes: usize) -> String {
if bytes == 0 {
"0".to_string()
} else if bytes < 1024 {
"< 1 KiB".to_string()
} else {
convert_byte_size(bytes as f64)
}
}
pub fn convert_byte_size(num: f64) -> String {
let negative = if num.is_sign_positive() { "" } else { "-" };
let num = num.abs();
let units = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
if num < 1_f64 {
return format!("{}{:.02} {}", negative, num, "B");
}
let delimiter = 1024_f64;
let exponent = std::cmp::min(
(num.ln() / delimiter.ln()).floor() as i32,
(units.len() - 1) as i32,
);
let pretty_bytes = format!("{:.02}", num / delimiter.powi(exponent));
let unit = units[exponent as usize];
format!("{}{} {}", negative, pretty_bytes, unit)
}
pub fn convert_number_size(num: f64) -> String {
if num == 0.0 {
return String::from("0");
}
let negative = if num.is_sign_positive() { "" } else { "-" };
let num = num.abs();
let units = [
"",
" thousand",
" million",
" billion",
" trillion",
" quadrillion",
];
if num < 1_f64 {
return format!("{}{:.2}", negative, num);
}
let delimiter = 1000_f64;
let exponent = std::cmp::min(
(num.ln() / delimiter.ln()).floor() as i32,
(units.len() - 1) as i32,
);
let pretty_bytes = format!("{:.2}", num / delimiter.powi(exponent))
.parse::<f64>()
.unwrap()
* 1_f64;
let unit = units[exponent as usize];
format!("{}{}{}", negative, pretty_bytes, unit)
}
/// Mask a secret value, showing first 2 and last 2 characters.
/// For values with 4 or fewer characters, fully mask.
pub fn mask_partial(s: &str) -> String {
let chars: Vec<char> = s.chars().collect();
if chars.len() <= 4 {
"***".to_string()
} else {
let head: String = chars[..2].iter().collect();
let tail: String = chars[chars.len() - 2..].iter().collect();
format!("{}***{}", head, tail)
}
}
/// Mask the connection info in the sql.
pub fn mask_connection_info(sql: &str) -> String {
// Quoted string pattern: handles both '' (SQL doubled) and \' (backslash escaped) quotes
// Also handles other backslash escapes like \\
// Supports both single-quoted and double-quoted literals (MySQL/Hive dialects)
const SINGLE_QUOTED_STR: &str = r"'([^'\\]|''|\\.)*'";
const DOUBLE_QUOTED_STR: &str = r#""([^"\\]|""|\\.)*""#;
// Match individual secret key-value pairs
// Supports both single-quoted and double-quoted values
static RE_SECRET_KV: LazyLock<Regex> = LazyLock::new(|| {
let pattern = format!(
r"(?i)(ACCESS_KEY_ID|ACCESS_KEY_SECRET|SECRET_ACCESS_KEY|AWS_KEY_ID|AWS_KEY_SECRET|AWS_SECRET_KEY|AWS_ACCESS_KEY_ID|AWS_SECRET_ACCESS_KEY|AWS_TOKEN|AWS_SESSION_TOKEN|MASTER_KEY|ACCOUNT_KEY|ACCOUNT_NAME|PASSWORD|SECURITY_TOKEN|SESSION_TOKEN|SECRET_ID|SECRET_KEY|CREDENTIAL|EXTERNAL_ID|AWS_EXTERNAL_ID|DELEGATION)\s*=\s*({}|{})",
SINGLE_QUOTED_STR, DOUBLE_QUOTED_STR
);
Regex::new(&pattern).unwrap()
});
RE_SECRET_KV
.replace_all(sql, |caps: ®ex::Captures| {
let key = &caps[1];
let quoted_val = &caps[2];
let quote_char = "ed_val[..1];
let inner = "ed_val[1..quoted_val.len() - 1];
format!(
"{} = {}{}{}",
key,
quote_char,
mask_partial(inner),
quote_char
)
})
.to_string()
}
/// Maximum length of the SQL query to be displayed or log.
/// If the query exceeds this length and starts with keywords,
/// it will be truncated and appended with the remaining length.
pub fn short_sql(sql: String, max_length: u64) -> String {
let keywords = ["INSERT"];
fn starts_with_any(query: &str, keywords: &[&str]) -> bool {
keywords
.iter()
.any(|&keyword| query.to_uppercase().starts_with(keyword))
}
let query = sql.trim_start();
// Graphemes represent user-perceived characters, which might be composed
// of multiple Unicode code points.
// This ensures that we handle complex characters like emojis or
// accented characters properly.
if query.graphemes(true).count() > max_length as usize && starts_with_any(query, &keywords) {
let truncated: String = query.graphemes(true).take(max_length as usize).collect();
let original_length = query.graphemes(true).count();
let remaining_length = original_length.saturating_sub(max_length as usize);
// Append the remaining length indicator
truncated + &format!("...[{} more characters]", remaining_length)
} else {
query.to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_mask_connection_eq() {
let sql = "CREATE STAGE s URL='s3://b' CONNECTION = (ACCESS_KEY_ID = 'akid123' SECRET_ACCESS_KEY = 'secret456')";
let masked = mask_connection_info(sql);
assert_eq!(
masked,
"CREATE STAGE s URL='s3://b' CONNECTION = (ACCESS_KEY_ID = 'ak***23' SECRET_ACCESS_KEY = 'se***56')"
);
assert!(!masked.contains("akid123"));
assert!(!masked.contains("secret456"));
}
#[test]
fn test_mask_connection_arrow() {
let sql = "SELECT * FROM 's3://b/data.csv' (CONNECTION => (ACCESS_KEY_ID = 'akid123', SECRET_ACCESS_KEY = 'secret456'))";
let masked = mask_connection_info(sql);
assert!(masked.contains("ACCESS_KEY_ID = 'ak***23'"));
assert!(masked.contains("SECRET_ACCESS_KEY = 'se***56'"));
assert!(!masked.contains("akid123"));
assert!(!masked.contains("secret456"));
}
#[test]
fn test_mask_connection_with_parens_in_value() {
// Value contains ')' inside quotes — should not break the regex
let sql = "CONNECTION = (PASSWORD = 'p@ss(w)rd' ACCESS_KEY_ID = 'mykey123')";
let masked = mask_connection_info(sql);
assert!(masked.contains("PASSWORD = 'p@***rd'"));
assert!(masked.contains("ACCESS_KEY_ID = 'my***23'"));
assert!(!masked.contains("p@ss(w)rd"));
assert!(!masked.contains("mykey123"));
}
#[test]
fn test_mask_connection_with_escaped_quotes() {
// SQL-style escaped quotes ('') inside values
let sql = "CONNECTION = (PASSWORD = 'it''s_secret' ACCESS_KEY_ID = 'ak')";
let masked = mask_connection_info(sql);
assert!(masked.contains("PASSWORD = 'it***et'"));
assert!(masked.contains("ACCESS_KEY_ID = '***'"));
assert!(!masked.contains("it''s_secret"));
}
#[test]
fn test_mask_connection_with_backslash_escaped_quotes() {
// Backslash-escaped quotes (\') inside values
let sql = r"CONNECTION = (PASSWORD = 'abc\'tail' ACCESS_KEY_ID = 'mykey')";
let masked = mask_connection_info(sql);
assert!(!masked.contains("tail"));
assert!(!masked.contains("mykey"));
}
#[test]
fn test_mask_bare_key_with_backslash_escaped_quote() {
// Backslash-escaped quote in a bare key value
let sql = r"PASSWORD = 'p@ss\'word'";
let masked = mask_connection_info(sql);
assert!(!masked.contains("p@ss"));
}
#[test]
fn test_mask_connection_with_backslash_in_value() {
// Backslash-escaped backslash (\\) inside values
let sql = r"CONNECTION = (PASSWORD = 'path\\to\\secret' ACCESS_KEY_ID = 'key123')";
let masked = mask_connection_info(sql);
assert!(!masked.contains("path"));
assert!(!masked.contains("key123"));
}
#[test]
fn test_mask_secret_kv_standalone() {
let sql = "CREATE CONNECTION myconn STORAGE_TYPE = 's3' ACCESS_KEY_ID = 'AKID123' SECRET_ACCESS_KEY = 'secret456'";
let masked = mask_connection_info(sql);
assert!(masked.contains("ACCESS_KEY_ID = 'AK***23'"));
assert!(masked.contains("SECRET_ACCESS_KEY = 'se***56'"));
assert!(masked.contains("STORAGE_TYPE = 's3'"));
assert!(!masked.contains("AKID123"));
assert!(!masked.contains("secret456"));
}
#[test]
fn test_mask_password() {
let sql = "PASSWORD = 'my_password'";
let masked = mask_connection_info(sql);
assert_eq!(masked, "PASSWORD = 'my***rd'");
}
#[test]
fn test_mask_session_token() {
let sql = "SESSION_TOKEN = 'tok123'";
let masked = mask_connection_info(sql);
assert_eq!(masked, "SESSION_TOKEN = 'to***23'");
}
#[test]
fn test_mask_security_token() {
let sql = "SECURITY_TOKEN = 'sectok'";
let masked = mask_connection_info(sql);
assert_eq!(masked, "SECURITY_TOKEN = 'se***ok'");
}
#[test]
fn test_mask_secret_id_and_key() {
let sql = "SECRET_ID = 'sid' SECRET_KEY = 'skey'";
let masked = mask_connection_info(sql);
assert_eq!(masked, "SECRET_ID = '***' SECRET_KEY = '***'");
}
#[test]
fn test_mask_account_key() {
let sql = "ACCOUNT_KEY = 'azure_key_123'";
let masked = mask_connection_info(sql);
assert_eq!(masked, "ACCOUNT_KEY = 'az***23'");
assert!(!masked.contains("azure_key_123"));
}
#[test]
fn test_no_false_positive_on_token() {
// Plain 'token' column should NOT be masked (we only match specific prefixed tokens)
let sql = "WHERE token = 'abc'";
let masked = mask_connection_info(sql);
assert_eq!(masked, sql);
}
#[test]
fn test_no_false_positive_on_normal_settings() {
let sql = "SET max_threads = '8'";
let masked = mask_connection_info(sql);
assert_eq!(masked, sql);
}
#[test]
fn test_no_false_positive_on_select() {
let sql = "SELECT name FROM users WHERE id = '123'";
let masked = mask_connection_info(sql);
assert_eq!(masked, sql);
}
#[test]
fn test_mask_case_insensitive() {
let sql = "connection = (access_key_id = 'akid123' secret_access_key = 'secret456')";
let masked = mask_connection_info(sql);
assert!(masked.contains("access_key_id = 'ak***23'"));
assert!(masked.contains("secret_access_key = 'se***56'"));
}
#[test]
fn test_mask_mixed_connection_and_bare_keys() {
// Both CONNECTION block keys and bare keys get masked
let sql =
"COPY INTO t FROM 's3://b' CONNECTION = (ACCESS_KEY_ID = 'akid123') PASSWORD = 'pw123'";
let masked = mask_connection_info(sql);
assert!(masked.contains("ACCESS_KEY_ID = 'ak***23'"));
assert!(masked.contains("PASSWORD = 'pw***23'"));
assert!(!masked.contains("akid123"));
assert!(!masked.contains("pw123"));
}
#[test]
fn test_mask_empty_value() {
let sql = "PASSWORD = ''";
let masked = mask_connection_info(sql);
assert_eq!(masked, "PASSWORD = '***'");
}
#[test]
fn test_mask_no_space_around_eq() {
let sql = "CONNECTION=(ACCESS_KEY_ID='akid123')";
let masked = mask_connection_info(sql);
assert!(masked.contains("ACCESS_KEY_ID = 'ak***23'"));
}
#[test]
fn test_mask_aws_secret_key() {
let sql = "CREATE CONNECTION c STORAGE_TYPE='s3' AWS_SECRET_KEY='my_secret'";
let masked = mask_connection_info(sql);
assert!(masked.contains("AWS_SECRET_KEY = 'my***et'"));
assert!(masked.contains("STORAGE_TYPE='s3'"));
assert!(!masked.contains("my_secret"));
}
#[test]
fn test_mask_aws_token() {
let sql = "CREATE CONNECTION c STORAGE_TYPE='s3' AWS_TOKEN='tok_val99'";
let masked = mask_connection_info(sql);
assert!(masked.contains("AWS_TOKEN = 'to***99'"));
assert!(!masked.contains("tok_val99"));
}
#[test]
fn test_mask_aws_access_key_id() {
let sql = "AWS_ACCESS_KEY_ID='AKIA1234' AWS_SECRET_ACCESS_KEY='secret_val'";
let masked = mask_connection_info(sql);
assert!(masked.contains("AWS_ACCESS_KEY_ID = 'AK***34'"));
assert!(masked.contains("AWS_SECRET_ACCESS_KEY = 'se***al'"));
assert!(!masked.contains("AKIA1234"));
assert!(!masked.contains("secret_val"));
}
#[test]
fn test_mask_aws_session_token() {
let sql = "AWS_SESSION_TOKEN='session_tok_123'";
let masked = mask_connection_info(sql);
assert_eq!(masked, "AWS_SESSION_TOKEN = 'se***23'");
assert!(!masked.contains("session_tok_123"));
}
#[test]
fn test_mask_gcs_credential() {
let sql =
"CREATE CONNECTION c STORAGE_TYPE='gcs' CREDENTIAL='service-account-json-content'";
let masked = mask_connection_info(sql);
assert!(masked.contains("CREDENTIAL = 'se***nt'"));
assert!(masked.contains("STORAGE_TYPE='gcs'"));
assert!(!masked.contains("service-account-json-content"));
}
#[test]
fn test_mask_double_quoted_password() {
// MySQL/Hive dialect uses double-quoted string literals
let sql = r#"CREATE CONNECTION c STORAGE_TYPE="s3" PASSWORD="secret_val""#;
let masked = mask_connection_info(sql);
assert!(masked.contains(r#"PASSWORD = "se***al""#));
assert!(!masked.contains("secret_val"));
}
#[test]
fn test_mask_double_quoted_connection_block() {
let sql = r#"CONNECTION = (ACCESS_KEY_ID = "akid123" SECRET_ACCESS_KEY = "secret456")"#;
let masked = mask_connection_info(sql);
assert!(masked.contains(r#"ACCESS_KEY_ID = "ak***23""#));
assert!(masked.contains(r#"SECRET_ACCESS_KEY = "se***56""#));
assert!(!masked.contains("akid123"));
assert!(!masked.contains("secret456"));
}
#[test]
fn test_mask_double_quoted_with_backslash_escape() {
let sql = r#"PASSWORD = "pass\"word""#;
let masked = mask_connection_info(sql);
assert!(!masked.contains("pass"));
}
}