← All changes
|
src/component/security-tweaks/servers/class-apache.php
+67
-22
5.7.1
→
6.3.0
View file →
| @@ -84,9 +84,9 @@ | ||
| 84 | 84 | public function get_error_by_file_path( $file_path ) { |
| 85 | 85 | return new WP_Error( |
| 86 | 86 | 'defender_file_not_writable', |
| 87 | 87 | sprintf( |
| 88 | - /* translators: %s - file path */ | |
| 88 | + /* translators: %s: file path */ | |
| 89 | 89 | esc_html__( 'The file %s is not writable', 'defender-security' ), |
| 90 | 90 | $file_path |
| 91 | 91 | ) |
| 92 | 92 | ); |
| @@ -107,9 +107,9 @@ | ||
| 107 | 107 | if ( 'protect-information' === $this->type ) { |
| 108 | 108 | $ht_access_path = ABSPATH . '.htaccess'; |
| 109 | 109 | // Create a htaccess-file if it doesn't exist. |
| 110 | 110 | if ( ! is_file( $ht_access_path ) |
| 111 | - && false === (bool) file_put_contents( $ht_access_path, '', LOCK_EX ) // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents | |
| 111 | + && false === file_put_contents( $ht_access_path, '', LOCK_EX ) // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents | |
| 112 | 112 | ) { |
| 113 | 113 | return $this->get_error_by_file_path( $ht_access_path ); |
| 114 | 114 | } |
| 115 | 115 | // If the htaccess-file is existed. |
| @@ -180,8 +180,10 @@ | ||
| 180 | 180 | update_site_option( "defender_security_tweeks_{$this->type}", $settings ); |
| 181 | 181 | |
| 182 | 182 | return true; |
| 183 | 183 | } |
| 184 | + | |
| 185 | + return true; | |
| 184 | 186 | } |
| 185 | 187 | |
| 186 | 188 | /** |
| 187 | 189 | * Revert the rules. |
| @@ -246,8 +248,10 @@ | ||
| 246 | 248 | } |
| 247 | 249 | |
| 248 | 250 | return delete_site_option( "defender_security_tweeks_{$this->type}" ); |
| 249 | 251 | } |
| 252 | + | |
| 253 | + return true; | |
| 250 | 254 | } |
| 251 | 255 | |
| 252 | 256 | /** |
| 253 | 257 | * Get Apache rule depending on the version. |
| @@ -256,9 +260,9 @@ | ||
| 256 | 260 | */ |
| 257 | 261 | public function get_rules() { |
| 258 | 262 | $rules = array( |
| 259 | 263 | PHP_EOL . '## WP Defender - Prevent information disclosure ##' . PHP_EOL, |
| 260 | - '<FilesMatch "\.(md|exe|sh|bak|inc|pot|po|mo|log|sql)$">' . PHP_EOL . | |
| 264 | + '<FilesMatch "(?i)\.(md|exe|sh|bak|inc|pot|po|mo|log|sql)$">' . PHP_EOL . | |
| 261 | 265 | 'Require all denied' . PHP_EOL . |
| 262 | 266 | '</FilesMatch>' . PHP_EOL, |
| 263 | 267 | '<Files robots.txt>' . PHP_EOL . |
| 264 | 268 | 'Require all granted' . PHP_EOL . |
| @@ -271,9 +275,9 @@ | ||
| 271 | 275 | |
| 272 | 276 | if ( version_compare( $this->get_version(), '2.4', '<' ) ) { |
| 273 | 277 | $rules = array( |
| 274 | 278 | PHP_EOL . '## WP Defender - Prevent information disclosure ##' . PHP_EOL, |
| 275 | - '<FilesMatch "\.(md|exe|sh|bak|inc|pot|po|mo|log|sql)$">' . PHP_EOL . | |
| 279 | + '<FilesMatch "(?i)\.(md|exe|sh|bak|inc|pot|po|mo|log|sql)$">' . PHP_EOL . | |
| 276 | 280 | 'Order allow,deny' . PHP_EOL . |
| 277 | 281 | 'Deny from all' . PHP_EOL . |
| 278 | 282 | '</FilesMatch>' . PHP_EOL, |
| 279 | 283 | '<Files robots.txt>' . PHP_EOL . |
| @@ -289,8 +293,27 @@ | ||
| 289 | 293 | return $rules; |
| 290 | 294 | } |
| 291 | 295 | |
| 292 | 296 | /** |
| 297 | + * Get LiteSpeed .htaccess rules for display in the browser, independent of the current server detection. | |
| 298 | + * | |
| 299 | + * @return string | |
| 300 | + */ | |
| 301 | + public function get_litespeed_rules_for_instruction(): string { | |
| 302 | + if ( 'prevent-php-executed' !== $this->type ) { | |
| 303 | + return ''; | |
| 304 | + } | |
| 305 | + | |
| 306 | + $rules = '## WP Defender - Protect PHP Executed ##' . PHP_EOL; | |
| 307 | + $rules .= '<Files *.php>' . PHP_EOL; | |
| 308 | + $rules .= 'Require all denied' . PHP_EOL; | |
| 309 | + $rules .= '</Files>' . PHP_EOL; | |
| 310 | + $rules .= '## WP Defender - End ##' . PHP_EOL; | |
| 311 | + | |
| 312 | + return $rules; | |
| 313 | + } | |
| 314 | + | |
| 315 | + /** | |
| 293 | 316 | * Get Apache rule depending on the version for instruction on browser. |
| 294 | 317 | * |
| 295 | 318 | * @return string |
| 296 | 319 | */ |
| @@ -305,9 +328,9 @@ | ||
| 305 | 328 | $rules .= '</Files>' . PHP_EOL; |
| 306 | 329 | $rules .= PHP_EOL; |
| 307 | 330 | $rules .= '## WP Defender - End ##' . PHP_EOL; |
| 308 | 331 | |
| 309 | - if ( version_compare( $this->get_version(), '2.4', '<' ) ) { | |
| 332 | + if ( 'apache' === Server::get_current_server() && version_compare( $this->get_version(), '2.4', '<' ) ) { | |
| 310 | 333 | $rules = '## WP Defender - Protect PHP Executed ##' . PHP_EOL; |
| 311 | 334 | $rules .= PHP_EOL; |
| 312 | 335 | $rules .= '<Files *.php>' . PHP_EOL; |
| 313 | 336 | $rules .= 'Order allow,deny' . PHP_EOL; |
| @@ -314,8 +337,13 @@ | ||
| 314 | 337 | $rules .= 'Deny from all' . PHP_EOL; |
| 315 | 338 | $rules .= '</Files>' . PHP_EOL; |
| 316 | 339 | $rules .= PHP_EOL; |
| 317 | 340 | $rules .= '## WP Defender - End ##' . PHP_EOL; |
| 341 | + } elseif ( 'litespeed' === Server::get_current_server() ) { | |
| 342 | + $rules = '## WP Defender - Protect PHP Executed ##' . PHP_EOL; | |
| 343 | + $rules .= 'RewriteEngine On' . PHP_EOL; | |
| 344 | + $rules .= 'RewriteRule ^.*\.php$ - [F,L,NC]' . PHP_EOL; | |
| 345 | + $rules .= '## WP Defender - End ##' . PHP_EOL; | |
| 318 | 346 | } |
| 319 | 347 | } |
| 320 | 348 | |
| 321 | 349 | if ( 'protect-information' === $this->type ) { |
| @@ -320,9 +348,9 @@ | ||
| 320 | 348 | |
| 321 | 349 | if ( 'protect-information' === $this->type ) { |
| 322 | 350 | $rules = '## WP Defender - Prevent information disclosure ##' . PHP_EOL; |
| 323 | 351 | $rules .= PHP_EOL; |
| 324 | - $rules .= '<FilesMatch "\.(md|exe|sh|bak|inc|pot|po|mo|log|sql)$">' . PHP_EOL; | |
| 352 | + $rules .= '<FilesMatch "(?i)\.(md|exe|sh|bak|inc|pot|po|mo|log|sql)$">' . PHP_EOL; | |
| 325 | 353 | $rules .= 'Require all denied' . PHP_EOL; |
| 326 | 354 | $rules .= '</FilesMatch>' . PHP_EOL; |
| 327 | 355 | $rules .= PHP_EOL; |
| 328 | 356 | $rules .= '<Files robots.txt>' . PHP_EOL; |
| @@ -337,9 +365,9 @@ | ||
| 337 | 365 | |
| 338 | 366 | if ( version_compare( $this->get_version(), '2.4', '<' ) ) { |
| 339 | 367 | $rules = '## WP Defender - Prevent information disclosure ##' . PHP_EOL; |
| 340 | 368 | $rules .= PHP_EOL; |
| 341 | - $rules .= '<FilesMatch "\.(md|exe|sh|bak|inc|pot|po|mo|log|sql)$">' . PHP_EOL; | |
| 369 | + $rules .= '<FilesMatch "(?i)\.(md|exe|sh|bak|inc|pot|po|mo|log|sql)$">' . PHP_EOL; | |
| 342 | 370 | $rules .= 'Order allow,deny' . PHP_EOL; |
| 343 | 371 | $rules .= 'Deny from all' . PHP_EOL; |
| 344 | 372 | $rules .= '</FilesMatch>' . PHP_EOL; |
| 345 | 373 | $rules .= PHP_EOL; |
| @@ -374,9 +402,9 @@ | ||
| 374 | 402 | if ( ! is_array( $apache_version ) ) { |
| 375 | 403 | $apache_version = array(); |
| 376 | 404 | } |
| 377 | 405 | |
| 378 | - if ( isset( $apache_version[ $url ] ) && ! empty( $apache_version[ $url ] ) ) { | |
| 406 | + if ( isset( $apache_version[ $url ] ) && is_string( $apache_version[ $url ] ) && '' !== $apache_version[ $url ] ) { | |
| 379 | 407 | return strtolower( $apache_version[ $url ] ); |
| 380 | 408 | } |
| 381 | 409 | // The default support is 2.2. |
| 382 | 410 | $apache_version[ $url ] = $version; |
| @@ -397,9 +425,9 @@ | ||
| 397 | 425 | set_site_transient( self::CACHE_APACHE_VERSION, $apache_version, 3600 ); |
| 398 | 426 | } else { |
| 399 | 427 | $version = apache_get_version(); |
| 400 | 428 | $version = explode( '/', $version ); |
| 401 | - $version = ! empty( $version[1] ) ? $version[1] : $version[0]; | |
| 429 | + $version = isset( $version[1] ) && is_string( $version[1] ) && '' !== $version[1] ? $version[1] : $version[0]; | |
| 402 | 430 | } |
| 403 | 431 | |
| 404 | 432 | return $version; |
| 405 | 433 | } |
| @@ -417,14 +445,14 @@ | ||
| 417 | 445 | WP_Filesystem(); |
| 418 | 446 | } |
| 419 | 447 | $ht_access_path = $this->contentdir_path; |
| 420 | 448 | |
| 421 | - if ( empty( $ht_access_path ) ) { | |
| 449 | + if ( ! is_string( $ht_access_path ) || '' === $ht_access_path ) { | |
| 422 | 450 | $ht_access_path = WP_CONTENT_DIR . '/.htaccess'; |
| 423 | 451 | } |
| 424 | 452 | // Create a htaccess-file if it doesn't exist. |
| 425 | 453 | if ( ! is_file( $ht_access_path ) |
| 426 | - && false === (bool) file_put_contents( $ht_access_path, '', LOCK_EX ) // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents | |
| 454 | + && false === file_put_contents( $ht_access_path, '', LOCK_EX ) // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents | |
| 427 | 455 | ) { |
| 428 | 456 | return $this->get_error_by_file_path( $ht_access_path ); |
| 429 | 457 | } |
| 430 | 458 | // If the htaccess-file is existed. |
| @@ -457,14 +485,14 @@ | ||
| 457 | 485 | WP_Filesystem(); |
| 458 | 486 | } |
| 459 | 487 | $ht_access_path = $this->includedir_path; |
| 460 | 488 | |
| 461 | - if ( empty( $ht_access_path ) ) { | |
| 489 | + if ( ! is_string( $ht_access_path ) || '' === $ht_access_path ) { | |
| 462 | 490 | $ht_access_path = ABSPATH . WPINC . '/.htaccess'; |
| 463 | 491 | } |
| 464 | 492 | // Create a htaccess-file if it doesn't exist. |
| 465 | 493 | if ( ! is_file( $ht_access_path ) |
| 466 | - && false === (bool) file_put_contents( $ht_access_path, '', LOCK_EX ) // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents | |
| 494 | + && false === file_put_contents( $ht_access_path, '', LOCK_EX ) // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents | |
| 467 | 495 | ) { |
| 468 | 496 | return $this->get_error_by_file_path( $ht_access_path ); |
| 469 | 497 | } |
| 470 | 498 | // If the htaccess-file is existed. |
| @@ -519,10 +547,11 @@ | ||
| 519 | 547 | * |
| 520 | 548 | * @return void|WP_Error |
| 521 | 549 | */ |
| 522 | 550 | public function protect_uploads_directory() { |
| 523 | - if ( defined( 'UPLOADS' ) ) { | |
| 524 | - $this->contentdir_path = ABSPATH . UPLOADS . '/.htaccess'; | |
| 551 | + $path = $this->get_custom_upload_dir(); | |
| 552 | + if ( '' !== $path ) { | |
| 553 | + $this->contentdir_path = $path; | |
| 525 | 554 | |
| 526 | 555 | return $this->protect_content_directory(); |
| 527 | 556 | } |
| 528 | 557 | } |
| @@ -540,9 +569,9 @@ | ||
| 540 | 569 | WP_Filesystem(); |
| 541 | 570 | } |
| 542 | 571 | $ht_access_path = $this->contentdir_path; |
| 543 | 572 | |
| 544 | - if ( empty( $ht_access_path ) ) { | |
| 573 | + if ( ! is_string( $ht_access_path ) || '' === $ht_access_path ) { | |
| 545 | 574 | $ht_access_path = WP_CONTENT_DIR . '/.htaccess'; |
| 546 | 575 | } |
| 547 | 576 | // Quick exit if the file does not exist. |
| 548 | 577 | if ( ! is_file( $ht_access_path ) ) { |
| @@ -573,9 +602,9 @@ | ||
| 573 | 602 | WP_Filesystem(); |
| 574 | 603 | } |
| 575 | 604 | $ht_access_path = $this->includedir_path; |
| 576 | 605 | |
| 577 | - if ( empty( $ht_access_path ) ) { | |
| 606 | + if ( ! is_string( $ht_access_path ) || '' === $ht_access_path ) { | |
| 578 | 607 | $ht_access_path = ABSPATH . WPINC . '/.htaccess'; |
| 579 | 608 | } |
| 580 | 609 | // Quick exit if the file does not exist. |
| 581 | 610 | if ( ! is_file( $ht_access_path ) ) { |
| @@ -592,14 +621,16 @@ | ||
| 592 | 621 | |
| 593 | 622 | /** |
| 594 | 623 | * Unprotect upload directory. |
| 595 | 624 | * |
| 596 | - * @return void | |
| 625 | + * @return void|WP_Error | |
| 597 | 626 | */ |
| 598 | 627 | public function unprotect_upload_directory() { |
| 599 | - if ( defined( 'UPLOADS' ) ) { | |
| 600 | - $this->contentdir_path = ABSPATH . UPLOADS . '/.htaccess'; | |
| 601 | - $this->unprotect_content_directory(); | |
| 628 | + $path = $this->get_custom_upload_dir(); | |
| 629 | + if ( '' !== $path ) { | |
| 630 | + $this->contentdir_path = $path; | |
| 631 | + | |
| 632 | + return $this->unprotect_content_directory(); | |
| 602 | 633 | } |
| 603 | 634 | } |
| 604 | 635 | |
| 605 | 636 | /** |
| @@ -607,9 +638,9 @@ | ||
| 607 | 638 | * |
| 608 | 639 | * @param array $config to set. |
| 609 | 640 | */ |
| 610 | 641 | public function set_new_htaccess_config( $config = array() ) { |
| 611 | - if ( ! empty( $config ) ) { | |
| 642 | + if ( is_array( $config ) && array() !== $config ) { | |
| 612 | 643 | $this->new_htaccess_config = $config; |
| 613 | 644 | } |
| 614 | 645 | } |
| 615 | 646 | |
| @@ -642,6 +673,20 @@ | ||
| 642 | 673 | $exists_rules .= PHP_EOL; |
| 643 | 674 | } |
| 644 | 675 | |
| 645 | 676 | return $exists_rules; |
| 677 | + } | |
| 678 | + | |
| 679 | + /** | |
| 680 | + * Get custom upload dir path. | |
| 681 | + * | |
| 682 | + * @return string | |
| 683 | + */ | |
| 684 | + private function get_custom_upload_dir(): string { | |
| 685 | + if ( defined( 'UPLOADS' ) ) { | |
| 686 | + $upload_dir = wp_upload_dir( null, false ); | |
| 687 | + return $upload_dir['basedir'] . '/.htaccess'; | |
| 688 | + } | |
| 689 | + | |
| 690 | + return ''; | |
| 646 | 691 | } |
| 647 | 692 | } |