Skip to content

Commit a78f897

Browse files
authored
Ci4 enhancement use writable backup (#3815)
* Backup .env to writable dir For security the base URL directory must not be writable by the web server, so when making a backup of .env it must go to a new 'backup' directory under the writable directory. * Revert "Backup .env to writable dir" This reverts commit e980b88. * Update security_helper.php For security the base URL directory must not be writable by the web server, so when making a backup of .env it must go to a new 'backup' directory under the writable directory. Creation of this directory should be automated by the build script. * Updade security_helper.php CI4: enhancement This code significantly rewritten to use CI4's WRITEPATH variable and to wrap file management in defensive code, as well as other changes. * CI4 Enhancement: Upgrade security_helper.php This version removes surplus comments, removes surplus empty lines and improves responses to fwrite() errors * CI4 Enhancement: Upgrade security_helper.php Correct file permissions and revise some if tests
1 parent 1067e68 commit a78f897

File tree

1 file changed

+48
-17
lines changed

1 file changed

+48
-17
lines changed

app/Helpers/security_helper.php

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function check_encryption(): bool
99
{
1010
$old_key = config('Encryption')->key;
1111

12-
if(strlen($old_key) < 64)
12+
if((empty($old_key)) || (strlen($old_key) < 64))
1313
{
1414
//Create Key
1515
$encryption = new Encryption();
@@ -18,12 +18,19 @@ function check_encryption(): bool
1818

1919
//Write to .env
2020
$config_path = ROOTPATH . '.env';
21-
$backup_path = $config_path . '.bak';
21+
$new_config_path = WRITEPATH . '/backup/.env' ;
22+
$backup_path = WRITEPATH . '/backup/.env.bak';
2223

23-
copy($config_path, $backup_path);
24-
$config_file = file_get_contents($config_path);
25-
@chmod($config_path, 0440);
24+
//Copy to backup
25+
if(!copy($config_path, $backup_path))
26+
{
27+
log_message('error', "Unable to copy $config_path to $backup_path");
28+
}
2629

30+
@chmod($config_path, 0660);
31+
@chmod($backup_path, 0660);
32+
33+
$config_file = file_get_contents($config_path);
2734
$config_file = preg_replace("/(encryption\.key.*=.*)('.*')/", "$1'$key'", $config_file);
2835

2936
if(!empty($old_key))
@@ -33,19 +40,24 @@ function check_encryption(): bool
3340
$config_file = substr_replace($config_file, $old_line, $insertion_point,0);
3441
}
3542

36-
@chmod($config_path, 0770);
37-
38-
if(is_writable($config_path))
43+
$handle = @fopen($config_path, 'w+');
44+
45+
if(empty($handle))
3946
{
40-
// Write the new config.php file
41-
$handle = @fopen($config_path, 'w+');
42-
fwrite($handle, $config_file) === FALSE;
43-
fclose($handle);
47+
log_message('error', "Unable to open $config_path for updating");
48+
return false;
4449
}
45-
else
50+
51+
@chmod($config_path, 0660);
52+
$write_failed = !fwrite($handle, $config_file);
53+
fclose($handle);
54+
55+
if($write_failed)
4656
{
47-
return false;
57+
log_message('error', "Unable to write to $config_path for updating.");
58+
return false;
4859
}
60+
log_message('info', "File $config_path has been updated.");
4961
}
5062

5163
return true;
@@ -54,8 +66,27 @@ function check_encryption(): bool
5466
function abort_encryption_conversion()
5567
{
5668
$config_path = ROOTPATH . '.env';
57-
$backup_path = $config_path . '.bak';
69+
$backup_path = WRITEPATH . '/backup/.env.bak';
5870

59-
unlink($config_path);
60-
rename($backup_path, $config_path);
71+
$config_file = file_get_contents($backup_path);
72+
73+
$handle = @fopen($config_path, 'w+');
74+
75+
if(empty($handle))
76+
{
77+
log_message('error', "Unable to open $config_path to undo encryption conversion");
78+
}
79+
else
80+
{
81+
@chmod($config_path, 0660);
82+
$write_failed = !fwrite($handle, $config_file);
83+
fclose($handle);
84+
85+
if($write_failed)
86+
{
87+
log_message('error', "Unable to write to $config_path to undo encryption conversion.");
88+
return;
89+
}
90+
log_message('info', "File $config_path has been updated to undo encryption conversion");
91+
}
6192
}

0 commit comments

Comments
 (0)