prevent_csrf.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. $check_csrf = true;
  3. if ($_SERVER['SCRIPT_FILENAME'] == '/usr/local/hestia/web/inc/mail-wrapper.php' || $_SERVER['SCRIPT_FILENAME'] == '/usr/local/hestia//web/inc/mail-wrapper.php') {
  4. $check_csrf=false;
  5. } // execute only from CLI
  6. if ($_SERVER['SCRIPT_FILENAME'] == '/usr/local/hestia/web/reset/mail/index.php' || $_SERVER['SCRIPT_FILENAME'] == '/usr/local/hestia/web//reset/mail/index.php') {
  7. $check_csrf=false;
  8. } // Localhost only
  9. if ($_SERVER['SCRIPT_FILENAME'] == '/usr/local/hestia/web/api/index.php' || $_SERVER['SCRIPT_FILENAME'] == '/usr/local/hestia/web//api/index.php') {
  10. $check_csrf=false;
  11. } // Own check
  12. if (substr($_SERVER['SCRIPT_FILENAME'], 0, 22)=='/usr/local/hestia/bin/') {
  13. $check_csrf=false;
  14. }
  15. function checkStrictness($level)
  16. {
  17. if ($level >= $_SESSION['POLICY_CSRF_STRICTNESS']) {
  18. return true;
  19. } else {
  20. http_response_code(400);
  21. echo "<h1>Potential use CSRF detected</h1>\n".
  22. "<p>Please disable any plugins/add-ons inside your browser or contact your system administrator. If you are the system administrator you can run v-change-sys-config-value 'POLICY_CSRF_STRICTNESS' '0' as root to disable this check.<p>".
  23. "<p>If you folowed a bookmark or an static link <a href='/'>please click here</a>";
  24. die();
  25. }
  26. }
  27. function prevent_post_csrf()
  28. {
  29. if ($_SERVER['REQUEST_METHOD']==='POST') {
  30. $hostname = explode(':', $_SERVER['HTTP_HOST']);
  31. $port=$hostname[1];
  32. $hostname=$hostname[0];
  33. if (strpos($_SERVER['HTTP_ORIGIN'], gethostname()) !== false && in_array($port, array('443',$_SERVER['SERVER_PORT']))) {
  34. return checkStrictness(2);
  35. } else {
  36. if (strpos($_SERVER['HTTP_ORIGIN'], $hostname) !== false && in_array($port, array('443',$_SERVER['SERVER_PORT']))) {
  37. return checkStrictness(1);
  38. } else {
  39. return checkStrictness(0);
  40. }
  41. }
  42. }
  43. }
  44. function prevent_get_csrf()
  45. {
  46. if ($_SERVER['REQUEST_METHOD']==='GET') {
  47. $hostname = explode(':', $_SERVER['HTTP_HOST']);
  48. $port=$hostname[1];
  49. $hostname=$hostname[0];
  50. //list of possible entries route and these should never be blocked
  51. if (in_array($_SERVER['DOCUMENT_URI'], array('/list/user/index.php', '/login/index.php','/list/web/index.php','/list/dns/index.php','/list/mail/index.php','/list/db/index.php','/list/cron/index.php','/list/backup/index.php','/reset/index.php'))) {
  52. return true;
  53. }
  54. if (strpos($_SERVER['HTTP_REFERER'], gethostname()) !== false && in_array($port, array('443',$_SERVER['SERVER_PORT']))) {
  55. return checkStrictness(2);
  56. } else {
  57. if (strpos($_SERVER['HTTP_REFERER'], $hostname) !== false && in_array($port, array('443',$_SERVER['SERVER_PORT']))) {
  58. return checkStrictness(1);
  59. } else {
  60. return checkStrictness(0);
  61. }
  62. }
  63. }
  64. }
  65. if ($check_csrf == true) {
  66. prevent_post_csrf();
  67. prevent_get_csrf();
  68. }