-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathPgSQL_Variables_Validator.cpp
More file actions
671 lines (578 loc) · 19.6 KB
/
PgSQL_Variables_Validator.cpp
File metadata and controls
671 lines (578 loc) · 19.6 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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
#include "PgSQL_Variables_Validator.h"
#include "PgSQL_Session.h"
#include "cpp.h"
/**
* @brief Validates a boolean variable for PostgreSQL.
*
* This function checks if the provided value is a valid boolean representation
* and transforms it if necessary. The valid representations for false are "0", "f", "false", and "off".
* The valid representations for true are "1", "t", "true", and "on".
*
* @param value The value to validate.
* @param params Unused parameter.
* @param session Unused parameter.
* @param transformed_value If not null, will be set to the transformed value ("on" for false inputs, "off" for true inputs).
* @return true if the value is a valid boolean representation, false otherwise.
*/
bool pgsql_variable_validate_bool(const char* value, const params_t* params, PgSQL_Session* session, char** transformed_value) {
(void)params;
(void)session;
bool result = false;
if (transformed_value) *transformed_value = nullptr;
if (
(strcasecmp(value, (char*)"0") == 0) ||
(strcasecmp(value, (char*)"f") == 0) ||
(strcasecmp(value, (char*)"false") == 0) ||
(strcasecmp(value, (char*)"off") == 0) ||
(strcasecmp(value, (char*)"no") == 0)) {
if (transformed_value)
*transformed_value = strdup("off");
result = true;
} else if (
(strcasecmp(value, (char*)"1") == 0) ||
(strcasecmp(value, (char*)"t") == 0) ||
(strcasecmp(value, (char*)"true") == 0) ||
(strcasecmp(value, (char*)"on") == 0) ||
(strcasecmp(value, (char*)"yes") == 0)) {
if (transformed_value)
*transformed_value = strdup("on");
result = true;
}
return result;
}
/**
* @brief Validates an float variable for PostgreSQL.
*
* This function checks if the provided value is a valid float representation
* and falls within the specified range. The range is defined by the params
* parameter.
*
* @param value The value to validate.
* @param params The parameter structure containing the float range.
* @param session Unused parameter.
* @param transformed_value If not null, will be set to null.
* @return true if the value is a valid float representation within the specified range, false otherwise.
*/
bool pgsql_variable_validate_float(const char* value, const params_t* params, PgSQL_Session* session, char** transformed_value) {
(void)session;
if (transformed_value) *transformed_value = nullptr;
char* end = nullptr;
//long num = strtol(value, &end, 10);
double num = strtod(value, &end);
if (end == value || *end != '\0') return false;
if (num < params->float_range.min || num > params->float_range.max) return false;
return true;
}
/**
* @brief Validates a string variable for PostgreSQL.
*
* This function checks if the provided value is a valid string representation
* based on the allowed strings specified in the params parameter.
*
* @param value The value to validate.
* @param params The parameter structure containing the allowed strings.
* @param session Unused parameter.
* @param transformed_value If not null, will be set to the validated value.
* @return true if the value is a valid string representation, false otherwise.
*/
bool pgsql_variable_validate_string(const char* value, const params_t* params, PgSQL_Session* session, char** transformed_value) {
(void)session;
if (transformed_value) *transformed_value = nullptr;
if (params->string_allowed) {
const char** allowed_ptr = params->string_allowed;
while (*allowed_ptr) {
if (strcasecmp(value, *allowed_ptr) == 0) {
if (transformed_value)
*transformed_value = strdup(*allowed_ptr);
return true;
}
allowed_ptr++;
}
}
return false;
}
/**
* @brief Validates a DateStyle variable for PostgreSQL.
*
* This function checks if the provided value is a valid DateStyle representation.
* If the session is null, it parses the DateStyle value directly. If the session is not null,
* it uses the current DateStyle from the session to fill in any missing parts of the provided value.
*
* @param value The value to validate.
* @param params Unused parameter.
* @param session The current session, which holds the current DateStyle.
* @param transformed_value If not null, will be set to the transformed value.
* @return true if the value is a valid DateStyle representation, false otherwise.
*/
bool pgsql_variable_validate_datestyle(const char* value, const params_t* params, PgSQL_Session* session, char** transformed_value) {
(void)params;
if (transformed_value) *transformed_value = nullptr;
if (session == nullptr) {
PgSQL_DateStyle_t datestyle = PgSQL_DateStyle_Util::parse_datestyle(value);
if (datestyle.format == DATESTYLE_FORMAT_NONE || datestyle.order == DATESTYLE_ORDER_NONE) {
return false;
}
if (transformed_value) {
PgSQL_DateStyle_t current_datestyle = { .format = DATESTYLE_FORMAT_NONE, .order = DATESTYLE_ORDER_NONE };
const std::string& value_tmp = PgSQL_DateStyle_Util::datestyle_to_string(value, current_datestyle);
// if something goes wrong, the value will be empty
if (value_tmp.empty()) {
return false;
}
*transformed_value = strdup(value_tmp.c_str());
}
return true;
}
const PgSQL_DateStyle_t& current_datestyle = session->current_datestyle;
assert(current_datestyle.format != DATESTYLE_FORMAT_NONE);
assert(current_datestyle.order != DATESTYLE_ORDER_NONE);
// Especial case:
// PostgreSQL strangely accepts an empty value for datestyle, but it does not alter previously set value.
if (strlen(value) == 0) {
if (transformed_value) *transformed_value = strdup("");
return true;
}
//------------------------------------------------------------------------------------------------------
// Convert DateStyle to a string. Any missing parts will be filled using the current DateStyle value.
// For example:
// If current DateStyle is 'ISO, MDY' and the user sets 'DMY' the resulting DateStyle will be 'ISO, DMY'
const std::string& value_tmp = PgSQL_DateStyle_Util::datestyle_to_string(value, current_datestyle);
// if something goes wrong, the value will be empty
if (value_tmp.empty()) {
return false;
}
if (transformed_value)
*transformed_value = strdup(value_tmp.c_str());
return true;
}
/**
* @brief Validates the maintenance_work_mem variable for PostgreSQL.
*
* This function checks if the provided value is a valid representation of memory size
* with optional units. The valid units are 'kB', 'MB', 'GB', and 'TB'. If no unit is
* specified, 'kB' is assumed by default. The function also normalizes the value to
* always include the unit in lowercase.
*
* @param value The value to validate.
* @param params Unused parameter.
* @param session Unused parameter.
* @param transformed_value If not null, will be set to the normalized value.
* @return true if the value is a valid memory size representation, false otherwise.
*/
bool pgsql_variable_validate_maintenance_work_mem(const char* value, const params_t* params, PgSQL_Session* session, char** transformed_value) {
(void)params;
(void)session;
const char* p = value;
char* endptr;
long long num;
char unit = 'k'; // Default unit is 'k' (kB)
bool has_unit = false;
if (transformed_value) *transformed_value = nullptr;
// Skip leading whitespace
while (fast_isspace((unsigned char)*p)) p++;
// Parse numeric part
num = strtoll(p, &endptr, 10);
if (p == endptr || num <= 0) return false;
if (errno == ERANGE && (num == LLONG_MAX || num == LLONG_MIN)) {
return false;
}
p = endptr;
// Skip whitespace after number
while (fast_isspace((unsigned char)*p)) p++;
// Parse unit
if (*p != '\0') {
char tmp_unit = ::tolower(*p);
switch (tmp_unit) {
case 'k':
case 'm':
case 'g':
case 't':
if (tmp_unit != 'k')
unit = toupper(*p++);
has_unit = true;
// Check optional 'b'/'B'
if (::tolower(*p) == 'b') p++;
break;
default:
return false;
}
}
// Skip trailing whitespace
while (fast_isspace((unsigned char)*p)) p++;
// Validate entire string consumed
if (*p != '\0') return false;
char output[128];
// Format normalized string (always show unit in lowercase)
int written = snprintf(output, sizeof(output), has_unit ? "%lld%cB" : "%lldkB",
num, unit);
if (written < 0 || written >= (int)sizeof(output)) return false;
if (transformed_value)
*transformed_value = strdup(output);
return true;
}
bool pgsql_variable_validate_maintenance_work_mem_v2(const char* value, const params_t* params, PgSQL_Session* session, char** transformed_value) {
(void)session;
const char* input = value;
/* Trim leading whitespace */
while (fast_isspace((unsigned char)*input)) input++;
/* Parse numeric part */
uint64_t number;
char* endptr;
//size_t num_len = 0;
errno = 0;
number = strtoull(input, &endptr, 10);
if (endptr == input || errno == ERANGE || number == 0)
return false;
//num_len = endptr - input;
// Skip whitespace after number
while (fast_isspace((unsigned char)*endptr)) endptr++;
/* Parse unit part */
const char* unit_ptr = endptr;
uint64_t multiplier;
char unit[3] = { 0 };
size_t unit_len = strlen(unit_ptr);
/* Handle default unit (kB) if no unit specified */
if (unit_len == 0) {
strcpy(unit, "kB");
multiplier = 1024;
}
else {
/* Convert unit to lowercase for validation */
char u[3] = { 0 };
for (int i = 0; i < 2 && unit_ptr[i]; i++)
u[i] = ::tolower((unsigned char)unit_ptr[i]);
/* Validate unit and set multiplier */
if (unit_len == 1 && u[0] == 'b') {
strcpy(unit, "B");
multiplier = 1;
}
else if (strcmp(u, "kb") == 0) {
strcpy(unit, "kB");
multiplier = 1024;
}
else if (strcmp(u, "mb") == 0) {
strcpy(unit, "MB");
multiplier = 1024 * 1024;
}
else if (strcmp(u, "gb") == 0) {
strcpy(unit, "GB");
multiplier = 1024ULL * 1024 * 1024;
}
else if (strcmp(u, "tb") == 0) {
strcpy(unit, "TB");
multiplier = 1024ULL * 1024 * 1024 * 1024;
}
else {
return false;
}
/* Validate unit length matches parsed characters */
size_t actual_unit_len = (unit[1] == 'B') ? 2 : (unit[0] == 'B') ? 1 : 0;
if (strlen(unit_ptr) != actual_unit_len)
return false;
}
/* Check for multiplication overflow */
if (number > UINT64_MAX / multiplier)
return false;
uint64_t total_bytes = number * multiplier;
/* Validate PostgreSQL's requirements */
if ((total_bytes / 1024ULL) < params->uint_range.min || (total_bytes / 1024ULL) > params->uint_range.max)
return false;
char output[128];
/* Format output without leading zeros */
int needed = snprintf(output, sizeof(output), "%lu%s", number, unit);
if (needed < 0 || needed >= (int)sizeof(output)) return false;
if (transformed_value)
*transformed_value = strdup(output);
return true;
}
bool pgsql_variable_validate_maintenance_work_mem_v3(const char* value, const params_t* params, PgSQL_Session* session, char** transformed_value) {
(void)session;
// Trim leading whitespace
while (fast_isspace((unsigned char)*value)) value++;
char* endptr;
const char* num_start = value;
errno = 0;
double number = strtod(value, &endptr);
// Basic numeric validation
if (endptr == num_start || errno == ERANGE || number <= 0)
return false;
// Validate numeric format (digits and single decimal point)
int dot_count = 0;
const char* p = num_start;
while (p < endptr) {
if (*p == '.') {
if (++dot_count > 1) return false;
}
else if (!isdigit((unsigned char)*p)) {
return false;
}
p++;
}
// Parse unit
const char* unit_ptr = endptr;
uint64_t multiplier;
char unit[3] = { 0 };
size_t unit_len = strlen(unit_ptr);
// Default to kB if no unit specified
if (unit_len == 0) {
strcpy(unit, "kB");
multiplier = 1024;
}
else {
// Convert unit to lowercase for validation
char u[3] = { 0 };
for (int i = 0; i < 2 && unit_ptr[i]; i++)
u[i] = ::tolower((unsigned char)unit_ptr[i]);
// Validate units and set multipliers
if (unit_len == 1 && u[0] == 'b') {
strcpy(unit, "B");
multiplier = 1;
}
else if (strcmp(u, "kb") == 0) {
strcpy(unit, "kB");
multiplier = 1024;
}
else if (strcmp(u, "mb") == 0) {
strcpy(unit, "MB");
multiplier = 1024 * 1024;
}
else if (strcmp(u, "gb") == 0) {
strcpy(unit, "GB");
multiplier = 1024ULL * 1024 * 1024;
}
else if (strcmp(u, "tb") == 0) {
strcpy(unit, "TB");
multiplier = 1024ULL * 1024 * 1024 * 1024;
}
else {
return false;
}
// Validate unit length matches parsed characters
size_t expected_len = (unit[1] == 'B') ? 2 : (unit[0] == 'B') ? 1 : 0;
if (strlen(unit_ptr) != expected_len)
return false;
}
// Calculate total bytes with floating point
uint64_t total_bytes = (uint64_t)number * multiplier;
/* Validate PostgreSQL's requirements */
if ((total_bytes / 1024ULL) < params->uint_range.min || (total_bytes / 1024ULL) > params->uint_range.max)
return false;
char output[128];
/* Format output without leading zeros */
int needed = snprintf(output, sizeof(output), "%.15g%s", number, unit);
if (needed < 0 || needed >= (int)sizeof(output)) return false;
if (transformed_value)
*transformed_value = strdup(output);
return true;
}
bool pgsql_variable_validate_client_encoding(const char* value, const params_t* params, PgSQL_Session* session, char** transformed_value) {
(void)params;
if (transformed_value) *transformed_value = nullptr;
int charset_encoding = PgSQL_Connection::char_to_encoding(value);
if (charset_encoding == -1) {
return false;
}
if (transformed_value) {
*transformed_value = strdup(value);
if (*transformed_value) { // Ensure strdup succeeded
char* tmp_val = *transformed_value;
while (*tmp_val) {
*tmp_val = toupper((unsigned char)*tmp_val);
tmp_val++;
}
}
}
return true;
}
bool pgsql_variable_validate_search_path(const char* value, const params_t* params, PgSQL_Session* session, char** transformed_value) {
(void)params;
(void)session;
if (transformed_value) *transformed_value = nullptr;
if (value == nullptr) return false;
// NOSONAR: strlen is safe here
size_t value_len = strlen(value); // NOSONAR
if (value_len > SIZE_MAX - 1) return false;
char* normalized = (char*)malloc(value_len + 1);
if (normalized == nullptr) return false;
normalized[0] = '\0';
size_t norm_pos = 0;
bool first = true;
bool result = true;
const char* token = value;
while (*token && result) {
/* skip leading whitespace */
while (*token && fast_isspace((unsigned char)*token)) token++;
if (*token == '\0') break;
const char* part_start = token;
size_t part_len = 0;
int effective_len = 0; // for length check after de-escaping
if (*token == '"' || *token == '\'') {
// quoted identifier (preserve quotes in normalized output)
char quote = *token++;
const char* search = token;
while (*search) {
if (*search == quote) {
if (*(search + 1) == quote) {
search += 2;
effective_len++; // doubled quote counts as one char
continue;
} else {
break;
}
}
search++;
effective_len++;
}
if (*search != quote) {
result = false;
break; // unclosed quote
}
part_len = (size_t)(search - part_start + 1);
token = search + 1; // skip closing quote
if (effective_len > 63) {
result = false;
break;
}
} else {
// unquoted identifier or $user
while (*token && *token != ',' && !fast_isspace(*token)) token++;
part_len = (size_t)(token - part_start);
if (part_len == 0 || part_len > 63) {
result = false;
break;
}
// validate as identifier
if (!isalpha(part_start[0]) && part_start[0] != '_') {
result = false;
break;
}
for (size_t i = 1; i < part_len; ++i) {
if (!isalnum(part_start[i]) && part_start[i] != '_' && part_start[i] != '$') {
result = false;
break;
}
}
if (!result) break;
}
// add to normalized
if (!first) {
normalized[norm_pos++] = ',';
}
first = false;
// append the part bytes
if (part_len > 0) {
memcpy(normalized + norm_pos, part_start, part_len);
norm_pos += part_len;
}
normalized[norm_pos] = '\0';
// skip whitespace after part
while (*token && fast_isspace(*token)) token++;
// expect comma or end
if (*token == ',') {
token++;
} else if (*token != '\0') {
// if there's more content without comma, invalid
result = false;
break;
}
}
if (result) {
if (transformed_value) {
*transformed_value = normalized;
} else {
free(normalized);
}
} else {
free(normalized);
}
return result;
}
const pgsql_variable_validator pgsql_variable_validator_bool = {
.type = VARIABLE_TYPE_BOOL,
.validate = &pgsql_variable_validate_bool,
.params = {}
};
const pgsql_variable_validator pgsql_variable_validator_extra_float_digits = {
.type = VARIABLE_TYPE_FLOAT,
.validate = &pgsql_variable_validate_float,
.params = {
.float_range = { .min = -15.0, .max = 3.0 }
}
};
const pgsql_variable_validator pgsql_variable_validator_intervalstyle = {
.type = VARIABLE_TYPE_STRING,
.validate = &pgsql_variable_validate_string,
.params = {
.string_allowed = (const char* []){ "postgres", "sql_standard", "postgres_verbose", "iso_8601", nullptr }
}
};
const pgsql_variable_validator pgsql_variable_validator_synchronous_commit = {
.type = VARIABLE_TYPE_STRING,
.validate = &pgsql_variable_validate_string,
.params = {
.string_allowed = (const char* []){ "local", "remote_write", "remote_apply", "on", "off", nullptr }
}
};
const pgsql_variable_validator pgsql_variable_validator_client_min_messages = {
.type = VARIABLE_TYPE_STRING,
.validate = &pgsql_variable_validate_string,
.params = {
.string_allowed = (const char* []){ "debug5", "debug4", "debug3", "debug2", "debug1", "log", "notice", "warning", "error", nullptr }
}
};
const pgsql_variable_validator pgsql_variable_validator_bytea_output = {
.type = VARIABLE_TYPE_STRING,
.validate = &pgsql_variable_validate_string,
.params = {
.string_allowed = (const char* []){ "hex", "escape", nullptr }
}
};
const pgsql_variable_validator pgsql_variable_validator_datestyle = {
.type = VARIABLE_TYPE_DATESTYLE,
.validate = &pgsql_variable_validate_datestyle,
.params = {}
};
const pgsql_variable_validator pgsql_variable_validator_maintenance_work_mem = {
.type = VARIABLE_TYPE_MAINTENANCE_WORK_MEM,
.validate = &pgsql_variable_validate_maintenance_work_mem_v3,
.params = {
.uint_range = {.min = 1024, .max = 2147483647 } // this range is in kB
}
};
const pgsql_variable_validator pgsql_variable_validator_client_encoding = {
.type = VARIABLE_TYPE_CLIENT_ENCODING,
.validate = &pgsql_variable_validate_client_encoding,
.params = {}
};
const pgsql_variable_validator pgsql_variable_validator_search_path = {
.type = VARIABLE_TYPE_STRING,
.validate = &pgsql_variable_validate_search_path,
.params = {}
};
/**
* @brief Validates an integer variable for PostgreSQL.
*
* This function checks if the provided value is a valid integer representation
* and falls within the specified range. The range is defined by the params
* parameter.
*
* @param value The value to validate.
* @param params The parameter structure containing the integer range.
* @param session Unused parameter.
* @param transformed_value If not null, will be set to null.
* @return true if the value is a valid integer representation within the specified range, false otherwise.
*/
bool pgsql_variable_validate_integer(const char* value, const params_t* params, PgSQL_Session* session, char** transformed_value) {
(void)session;
if (transformed_value) *transformed_value = nullptr;
char* end = nullptr;
long num = strtol(value, &end, 10);
if (end == value || *end != '\0') return false;
if (num < params->int_range.min || num > params->int_range.max) return false;
return true;
}
const pgsql_variable_validator pgsql_variable_validator_integer = {
.type = VARIABLE_TYPE_INT,
.validate = &pgsql_variable_validate_integer,
.params = {}
};