diff --git a/Useful-Functions-for-Plugin-Theme-Security.md b/Useful-Functions-for-Plugin-Theme-Security.md new file mode 100644 index 0000000..d34f5d4 --- /dev/null +++ b/Useful-Functions-for-Plugin-Theme-Security.md @@ -0,0 +1,63 @@ +The snippets below are a collection of PHP functions to help WordPress Plugin/Theme developer secure their code. + + +## is_url_local() + +When using an arbitrary URL in functions such as `wp_remote_get`, `curl` etc (which is not really recommended but sometimes there is no other way), in addition to ensure that the URL is indeed an URL, it should be also be checked to make sure the URL is not a local one, to avoid issues such as SSRF (https://portswigger.net/web-security/ssrf, https://en.wikipedia.org/wiki/Server-side_request_forgery) + +```php + {$type}\n"; +} +``` + +## zip_only_contains_allowed_extensions() + +Before extracting a zip file uploaded by any user (including admin), its content should be checked to ensure that the archive only contains expected files (such as png) and no other ones (such as php etc) which could lead to severe security issues. + +```php +open($zip_path); + + for ($i = 0; $i < $zip->numFiles; $i++) { + $stat = $zip->statIndex( $i ); + + $ext = pathinfo($stat['name'], PATHINFO_EXTENSION); + + //print_r( "{$stat['name']} => {$ext}" . PHP_EOL ); + + if (!in_array(strtolower($ext), $allowed_extensions)) + return false; + } + return true; +} + +var_dump(zip_only_contains_allowed_extensions('midex.zip', ['png'])); +``` \ No newline at end of file