prevent_csrf.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. if(!empty($_SERVER["HTTP_HOST"])){
  32. list($hostname, $port) = explode(':', $_SERVER["HTTP_HOST"].":");
  33. if(empty($port)){
  34. $port = 443;
  35. }
  36. }else{
  37. $hostname = gethostname();
  38. $port = 443;
  39. }
  40. if (isset($_SERVER['HTTP_ORIGIN'])) {
  41. $origin_host = parse_url($_SERVER['HTTP_ORIGIN'], PHP_URL_HOST);
  42. if (strcmp($origin_host, gethostname()) === 0 && in_array($port, array('443',$_SERVER['SERVER_PORT']))) {
  43. return checkStrictness(2);
  44. } else {
  45. if (strcmp($origin_host, $hostname) === 0 && in_array($port, array('443',$_SERVER['SERVER_PORT']))) {
  46. return checkStrictness(1);
  47. } else {
  48. return checkStrictness(0);
  49. }
  50. }
  51. }
  52. }
  53. }
  54. }
  55. function prevent_get_csrf()
  56. {
  57. if (!empty($_SERVER['REQUEST_METHOD'])) {
  58. if ($_SERVER['REQUEST_METHOD']==='GET') {
  59. if(!empty($_SERVER["HTTP_HOST"])){
  60. list($hostname, $port) = explode(':', $_SERVER["HTTP_HOST"].":");
  61. if(empty($port)){
  62. $port = 443;
  63. }
  64. }else{
  65. $hostname = gethostname();
  66. $port = 443;
  67. }
  68. //list of possible entries route and these should never be blocked
  69. 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'))) {
  70. return true;
  71. }
  72. if (isset($_SERVER['HTTP_REFERER'])) {
  73. $referrer_host = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
  74. if (strcmp($referrer_host, gethostname()) === 0 && in_array($port, array('443',$_SERVER['SERVER_PORT']))) {
  75. return checkStrictness(2);
  76. } else {
  77. if (strcmp($referrer_host, $hostname) === 0 && in_array($port, array('443',$_SERVER['SERVER_PORT']))) {
  78. return checkStrictness(1);
  79. } else {
  80. return checkStrictness(0);
  81. }
  82. }
  83. } else {
  84. return checkStrictness(0);
  85. }
  86. }
  87. }
  88. }
  89. if ($check_csrf == true) {
  90. prevent_post_csrf();
  91. prevent_get_csrf();
  92. }