Skip to content

Commit f972223

Browse files
committed
Improved DB handling
Fixes hestiacp#1045 hestiacp#1084 See hestiacp#1045 for notes
1 parent 2e05ab4 commit f972223

File tree

5 files changed

+73
-13
lines changed

5 files changed

+73
-13
lines changed

func/main.sh

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -724,9 +724,14 @@ is_common_format_valid() {
724724

725725
# Database format validator
726726
is_database_format_valid() {
727-
exclude="[!|@|#|$|^|&|*|(|)|+|=|{|}|:|,|<|>|?|/|\|\"|'|;|%|\`| ]"
728-
if [[ "$1" =~ $exclude ]] || [ 65 -le ${#1} ]; then
729-
check_result $E_INVALID "invalid $2 format :: $1"
727+
if [ "$3" == "mysql" ]; then
728+
if ! [[ "$1" =~ ^[0-9a-zA-Z_]{1,64}$ ]]; then
729+
check_result $E_INVALID "invalid $2 format :: $1"
730+
fi
731+
else
732+
if ! [[ "$1" =~ ^[0-9a-z_]{1,63}$ ]]; then
733+
check_result $E_INVALID "invalid $2 format :: $1"
734+
fi
730735
fi
731736
}
732737

@@ -739,12 +744,17 @@ is_date_format_valid() {
739744

740745
# Database user validator
741746
is_dbuser_format_valid() {
742-
exclude="[!|@|#|$|^|&|*|(|)|+|=|{|}|:|,|<|>|?|/|\|\"|'|;|%|\`| ]"
743-
if [ 31 -le ${#1} ]; then
744-
check_result $E_INVALID "mysql username can be up to 30 characters long"
745-
fi
746-
if [[ "$1" =~ $exclude ]]; then
747-
check_result $E_INVALID "invalid $2 format :: $1"
747+
if [ "$3" == "mysql" ]; then
748+
if [ 33 -le ${#1} ]; then
749+
check_result $E_INVALID "mysql username can be up to 32 characters long"
750+
fi
751+
if ! [[ "$1" =~ ^[0-9a-zA-Z_]{1,64}$ ]]; then
752+
check_result $E_INVALID "invalid $2 format :: $1"
753+
fi
754+
else
755+
if ! [[ "$1" =~ ^[0-9a-z_]{1,63}$ ]]; then
756+
check_result $E_INVALID "invalid $2 format :: $1"
757+
fi
748758
fi
749759
}
750760

@@ -931,10 +941,10 @@ is_format_valid() {
931941
charset) is_object_format_valid "$arg" "$arg_name" ;;
932942
charsets) is_common_format_valid "$arg" 'charsets' ;;
933943
comment) is_object_format_valid "$arg" 'comment' ;;
934-
database) is_database_format_valid "$arg" 'database';;
944+
database) is_database_format_valid "$arg" 'database' $type;;
935945
day) is_cron_format_valid "$arg" $arg_name ;;
936946
dbpass) is_password_format_valid "$arg" ;;
937-
dbuser) is_dbuser_format_valid "$arg" 'dbuser';;
947+
dbuser) is_dbuser_format_valid "$arg" 'dbuser' $type;;
938948
dkim) is_boolean_format_valid "$arg" 'dkim' ;;
939949
dkim_size) is_int_format_valid "$arg" ;;
940950
domain) is_domain_format_valid "$arg" ;;

web/add/db/index.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,39 @@
4040
}
4141
}
4242

43+
if (empty($_SESSION['error_msg'])) {
44+
if($_POST['v_type'] == 'mysql'){
45+
if (strlen($user.'_'.$_POST['v_database']) > 64){
46+
$_SESSION['error_msg'] = __('Maximum length of database is %s characters', 64);
47+
}else if (!preg_match('/^[0-9a-zA-Z_]{1,64}$/',$user.'_'.$_POST['v_database'])){
48+
$_SESSION['error_msg'] = __('Database name may only contain lowercase/uppercase letters, numbers or a _');
49+
}
50+
}else{
51+
if (strlen($user.'_'.$_POST['v_database']) > 63){
52+
$_SESSION['error_msg'] = __('Maximum length of database is %s characters', 63);
53+
}else if (!preg_match('/^[0-9a-z_]{1,63}$/',$user.'_'.$_POST['v_database'])){
54+
var_dump(preg_match('/^[0-9a-z_]{1,63}$/',$user.'_'.$_POST['v_database']));
55+
$_SESSION['error_msg'] = __('Database name may only contain lowercase letters, numbers or a _');
56+
}
57+
}
58+
}
59+
60+
if (empty($_SESSION['error_msg'])) {
61+
if($_POST['v_type'] == 'mysql'){
62+
if (strlen($user.'_'.$_POST['v_dbuser']) > 32){
63+
$_SESSION['error_msg'] = __('Maximum length of database is %s characters', 32);
64+
}else if (!preg_match('/^[0-9a-zA-Z_]{1,32}$/',$user.'_'.$_POST['v_dbuser'])){
65+
$_SESSION['error_msg'] = __('Username may only contain lowercase/uppercase letters, numbers or a _');
66+
}
67+
}else{
68+
if (strlen($user.'_'.$_POST['v_dbuser']) > 63){
69+
$_SESSION['error_msg'] = __('Maximum length of database is %s characters', 63);
70+
}else if (!preg_match('/^[0-9a-z_]{1,63}$/',$user.'_'.$_POST['v_dbuser'])){
71+
$_SESSION['error_msg'] = __('Username may only contain lowercase letters, numbers or a _');
72+
}
73+
}
74+
}
75+
4376
// Check password length
4477
if (empty($_SESSION['error_msg'])) {
4578
if (!validate_password($_POST['v_password'])) { $_SESSION['error_msg'] = __('Password does not match the minimum requirements');}

web/edit/db/index.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,23 @@
5050
header('location: /login/');
5151
exit();
5252
}
53-
53+
54+
if (empty($_SESSION['error_msg'])) {
55+
if($_POST['v_type'] == 'mysql'){
56+
if (strlen($user.'_'.$_POST['v_dbuser']) > 32){
57+
$_SESSION['error_msg'] = __('Maximum length of database is %s characters', 32);
58+
}else if (!preg_match('/^[0-9a-zA-Z_]{1,32}$/',$user.'_'.$_POST['v_dbuser'])){
59+
$_SESSION['error_msg'] = __('Username may only contain lowercase/uppercase letters, numbers or a _');
60+
}
61+
}else{
62+
if (strlen($user.'_'.$_POST['v_dbuser']) > 63){
63+
$_SESSION['error_msg'] = __('Maximum length of database is %s characters', 63);
64+
}else if (!preg_match('/^[0-9a-z_]{1,63}$/',$user.'_'.$_POST['v_dbuser'])){
65+
$_SESSION['error_msg'] = __('Username may only contain lowercase letters, numbers or a _');
66+
}
67+
}
68+
}
69+
5470
// Change database user
5571
if (($v_dbuser != $_POST['v_dbuser']) && (empty($_SESSION['error_msg']))) {
5672
$v_dbuser = preg_replace("/^".$user."_/", "", $_POST['v_dbuser']);

web/templates/admin/add_db.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
<?php
7272
print __('User');
7373
// if (is_it_mysql_or_mariadb()=='mysql')
74-
echo "&nbsp;&nbsp;&nbsp;&nbsp;<em><small>(".__('maximum characters length, including prefix', 16).")</small></em>";
74+
echo "&nbsp;&nbsp;&nbsp;&nbsp;<em><small>(".__('maximum characters length, including prefix', 32).")</small></em>";
7575
?>
7676
</td>
7777
</tr>

web/templates/admin/edit_db.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<tr>
6969
<td class="vst-text input-label">
7070
<?php print __('User');?>
71+
<?php echo "&nbsp;&nbsp;&nbsp;&nbsp;<em><small>(".__('maximum characters length, including prefix', 32).")</small></em>"; ?>
7172
</td>
7273
</tr>
7374
<tr>

0 commit comments

Comments
 (0)