Skip to content

Commit 201d1de

Browse files
authored
Merge pull request #25 from igorhrcek/issue-4.3
Fix sensitive directories/files functions
2 parents 0da90ca + dc9f3e2 commit 201d1de

8 files changed

+61
-33
lines changed

src/Exceptions/RuleAlreadyExist.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ class RuleAlreadyExist extends Exception {
88
/**
99
* @var string
1010
*/
11-
protected $message = 'The rule already exist in the file';
11+
protected $message = 'The rule already exists in the file';
1212
}

src/RuleContent.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ public function getContent() : array {
2929
$result = '';
3030
$templateContent = implode( PHP_EOL, $this->content );
3131

32-
3332
foreach ( $this->templateVars as $var => $replacements ) {
3433
$tmp_result = $templateContent;
3534
foreach ( $replacements as $key => $replacement ) {
36-
$tmp_result = str_replace( sprintf( '{{%s}}', $key ), $replacement, $tmp_result );
35+
if ( preg_match( '/.+\/.+/', $key ) ) {
36+
$tmp_result = implode( PHP_EOL, $replacement );
37+
$tmp_result = str_replace( '{{file}}', $key, $tmp_result );
38+
} else {
39+
$tmp_result = str_replace( sprintf( '{{%s}}', $key ), $replacement, $tmp_result );
40+
}
3741
}
3842
$result .= $tmp_result;
3943
}

src/SubCommands/BlockAccessToSensitiveDirectories.php

+6-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class BlockAccessToSensitiveDirectories extends SubCommand {
77
public string $ruleName = 'BLOCK ACCESS TO SENSITIVE DIRECTORIES';
88
public string $successMessage = 'Block Access to Sensitive Directories rule has been deployed.';
99
public string $removalMessage= 'Block Access to Sensitive Directories rule has been removed.';
10-
10+
1111
/**
1212
* @var string Default directories that we are going to protect
1313
*/
@@ -16,18 +16,15 @@ class BlockAccessToSensitiveDirectories extends SubCommand {
1616
/**
1717
* @return array
1818
*/
19-
public function getTemplateVars() : array {
19+
public function getTemplateVars() {
2020
$directories = $this->commandArguments['directories'] ?? $this->sensitiveDirectories;
21-
if (!empty($directories)) {
22-
$directories = explode(',', $directories);
23-
$directories = array_map('trim', $directories);
24-
$directories_array = [];
25-
21+
if ( ! empty( $directories ) ) {
22+
$directories = explode( ',', $directories );
23+
$directories = array_map( 'trim', $directories );
2624
return [
27-
['directories' => implode('|', array_map('preg_quote', $directories))]
25+
[ 'directories' => implode( '|', array_map( 'preg_quote', $directories ) ) ]
2826
];
2927
}
30-
3128
return [];
3229
}
3330
}

src/SubCommands/BlockAccessToSensitiveFiles.php

+25
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,29 @@ class BlockAccessToSensitiveFiles extends SubCommand {
77
public string $ruleName = 'BLOCK ACCESS TO SENSITIVE FILES';
88
public string $successMessage = 'Block Access to Sensitive Files rule has been deployed.';
99
public string $removalMessage= 'Block Access to Sensitive Files rule has been removed.';
10+
11+
public function getTemplateVars() {
12+
$files = isset( $this->commandArguments['files'] ) ? $this->commandArguments['files'] : 'readme.html,readme.txt,wp-config.php,nginx.conf,/wp-admin/install.php,/wp-admin/upgrade.php';
13+
if ( ! empty( $files ) ) {
14+
$files = explode( ',', $files );
15+
$files = array_map( 'trim', $files );
16+
$files_array = [];
17+
18+
foreach ( $files as $key => $value ) {
19+
if ( preg_match( '/.+\/.+/', $value ) ) {
20+
$file_with_directory = $this->setRuleContent( false, 'block_access_to_sensitive_files_with_directories' );
21+
if ( isset( $this->commandArguments['server'] ) && $this->commandArguments['server'] === 'nginx' ) {
22+
$file = $value;
23+
} else {
24+
$file = preg_quote( ltrim( $value, '/' ) );
25+
}
26+
$files_array[] = [ $file => $file_with_directory ];
27+
} else {
28+
$files_array[] = [ 'file' => isset( $this->commandArguments['server'] ) && $this->commandArguments['server'] === 'nginx' ? preg_quote( $value ) : $value ];
29+
}
30+
}
31+
return $files_array;
32+
}
33+
return [];
34+
}
1035
}

src/SubCommands/SubCommand.php

+15-8
Original file line numberDiff line numberDiff line change
@@ -101,30 +101,37 @@ private function setFilePath() : string {
101101
/**
102102
* Reads rule template file. Depending on output type, returns an array
103103
*
104-
* @return array
104+
* @param boolean $loadVars Whether to load the template vars or not.
105+
* @param boolean $template Template name to return instead of the loaded one.
106+
*
107+
* @return string|array
105108
*/
106-
private function setRuleContent() : array {
109+
protected function setRuleContent( bool $loadVars = true, bool|string $template = false ) : string|array {
107110
//Return an empty array in case when the executed command does not require a template
108-
if($this->ruleTemplate === '') {
111+
if($this->ruleTemplate === '' && ! $template ) {
109112
return [];
110113
}
111114

112115
$templateFilePath = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Templates' . DIRECTORY_SEPARATOR . $this->serverType . DIRECTORY_SEPARATOR .
113-
$this->ruleTemplate . '.tpl';
116+
( $template ? $template : $this->ruleTemplate ) . '.tpl';
114117

115118
$result = [];
116119
$file = new \SplFileObject($templateFilePath);
120+
117121
while(!$file->eof()) {
118122
$result[] = rtrim($file->current(), "\n");
119123
$file->next();
120124
}
121125
unset($file);
122126

123-
//Combine templates and command arguments, if any
124-
//This is used for block-access command
125-
$result = new RuleContent( $result, $this->getTemplateVars() );
127+
if ( $loadVars ) {
128+
//Combine templates and command arguments, if any
129+
//This is used for block-access command
130+
$result = new RuleContent( $result, $this->getTemplateVars() );
131+
$result = $result->getContent();
132+
}
126133

127-
return $result->getContent();
134+
return $result;
128135
}
129136

130137
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<IfModule mod_rewrite.c>
2+
RewriteEngine On
3+
RewriteRule ^{{file}}$ - [F]
4+
</IfModule>
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
location ~ ^.*/\.git/.*$ {
2-
deny all;
3-
}
4-
5-
location ~ ^.*/\.svn/.*$ {
6-
deny all;
7-
}
8-
9-
location ~ ^.*/vendors/.*$ {
10-
deny all;
11-
}
12-
13-
location ~ ^.*/cache/.*$ {
1+
location ~ ^.*/{{directories}}/.*$ {
142
deny all;
153
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
location = {{file}} {
2+
deny all;
3+
}

0 commit comments

Comments
 (0)