prevent_csrf.php 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 followed a bookmark or an static link <a href='/'>please click here</a>";
  24. die();
  25. }
  26. }
  27. function prevent_post_csrf()
  28. {
  29. if (!empty($_SERVER['REQUEST_METHOD'])) {
  30. if ($_SERVER['REQUEST_METHOD']==='POST') {
  31. $hostname = explode(':', $_SERVER['HTTP_HOST']);
  32. $port=$hostname[1];
  33. $hostname=$hostname[0];
  34. if (isset($_SERVER['HTTP_ORIGIN'])) {
  35. $origin_host = parse_url($_SERVER['HTTP_ORIGIN'], PHP_URL_HOST);
  36. if (strcmp($origin_host, gethostname()) === 0 && in_array($port, array('443',$_SERVER['SERVER_PORT']))) {
  37. return checkStrictness(2);
  38. } else {
  39. if (strcmp($origin_host, $hostname) === 0 && in_array($port, array('443',$_SERVER['SERVER_PORT']))) {
  40. return checkStrictness(1);
  41. } else {
  42. return checkStrictness(0);
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }
  49. function prevent_get_csrf()
  50. {
  51. if (!empty($_SERVER['REQUEST_METHOD'])) {
  52. if ($_SERVER['REQUEST_METHOD']==='GET') {
  53. $hostname = explode(':', $_SERVER['HTTP_HOST']);
  54. $port=$hostname[1];
  55. $hostname=$hostname[0];
  56. //list of possible entries route and these should never be blocked
  57. 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'))) {
  58. return true;
  59. }
  60. if (isset($_SERVER['HTTP_REFERER'])) {
  61. $referrer_host = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
  62. if (strcmp($referrer_host, gethostname()) === 0 && in_array($port, array('443',$_SERVER['SERVER_PORT']))) {
  63. return checkStrictness(2);
  64. } else {
  65. if (strcmp($referrer_host, $hostname) === 0 && in_array($port, array('443',$_SERVER['SERVER_PORT']))) {
  66. return checkStrictness(1);
  67. } else {
  68. return checkStrictness(0);
  69. }
  70. }
  71. } else {
  72. return checkStrictness(0);
  73. }
  74. }
  75. }
  76. }
  77. if ($check_csrf == true) {
  78. prevent_post_csrf();
  79. prevent_get_csrf();
  80. }