prevent_csrf.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. $check_csrf = true;
  3. if (
  4. $_SERVER["SCRIPT_FILENAME"] == "/usr/local/hestia/web/inc/mail-wrapper.php" ||
  5. $_SERVER["SCRIPT_FILENAME"] == "/usr/local/hestia//web/inc/mail-wrapper.php"
  6. ) {
  7. $check_csrf = false;
  8. } // execute only from CLI
  9. if (
  10. $_SERVER["SCRIPT_FILENAME"] == "/usr/local/hestia/web/reset/mail/index.php" ||
  11. $_SERVER["SCRIPT_FILENAME"] == "/usr/local/hestia/web//reset/mail/index.php"
  12. ) {
  13. $check_csrf = false;
  14. } // Localhost only
  15. if (
  16. $_SERVER["SCRIPT_FILENAME"] == "/usr/local/hestia/web/api/index.php" ||
  17. $_SERVER["SCRIPT_FILENAME"] == "/usr/local/hestia/web//api/index.php"
  18. ) {
  19. $check_csrf = false;
  20. } // Own check
  21. if (substr($_SERVER["SCRIPT_FILENAME"], 0, 22) == "/usr/local/hestia/bin/") {
  22. $check_csrf = false;
  23. }
  24. function checkStrictness($level) {
  25. if ($level >= $_SESSION["POLICY_CSRF_STRICTNESS"]) {
  26. return true;
  27. } else {
  28. http_response_code(400);
  29. echo "<h1>Potential use CSRF detected</h1>\n" .
  30. "<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>" .
  31. "<p>If you followed a bookmark or an static link <a href='/'>please click here</a>";
  32. die();
  33. }
  34. }
  35. function prevent_post_csrf() {
  36. if (!empty($_SERVER["REQUEST_METHOD"])) {
  37. if ($_SERVER["REQUEST_METHOD"] === "POST") {
  38. if (!empty($_SERVER["HTTP_HOST"])) {
  39. [$hostname, $port] = explode(":", $_SERVER["HTTP_HOST"] . ":");
  40. if (empty($port)) {
  41. $port = 443;
  42. }
  43. } else {
  44. $hostname = gethostname();
  45. $port = 443;
  46. }
  47. if (isset($_SERVER["HTTP_ORIGIN"])) {
  48. $origin_host = parse_url($_SERVER["HTTP_ORIGIN"], PHP_URL_HOST);
  49. if (
  50. strcmp($origin_host, gethostname()) === 0 &&
  51. in_array($port, ["443", $_SERVER["SERVER_PORT"]])
  52. ) {
  53. return checkStrictness(2);
  54. } else {
  55. if (
  56. strcmp($origin_host, $hostname) === 0 &&
  57. in_array($port, ["443", $_SERVER["SERVER_PORT"]])
  58. ) {
  59. return checkStrictness(1);
  60. } else {
  61. return checkStrictness(0);
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. function prevent_get_csrf() {
  69. if (!empty($_SERVER["REQUEST_METHOD"])) {
  70. if ($_SERVER["REQUEST_METHOD"] === "GET") {
  71. if (!empty($_SERVER["HTTP_HOST"])) {
  72. [$hostname, $port] = explode(":", $_SERVER["HTTP_HOST"] . ":");
  73. if (empty($port)) {
  74. $port = 443;
  75. }
  76. } else {
  77. $hostname = gethostname();
  78. $port = 443;
  79. }
  80. //list of possible entries route and these should never be blocked
  81. if (
  82. in_array($_SERVER["DOCUMENT_URI"], [
  83. "/list/user/index.php",
  84. "/login/index.php",
  85. "/list/web/index.php",
  86. "/list/dns/index.php",
  87. "/list/mail/index.php",
  88. "/list/db/index.php",
  89. "/list/cron/index.php",
  90. "/list/backup/index.php",
  91. "/reset/index.php",
  92. ])
  93. ) {
  94. return true;
  95. }
  96. if (isset($_SERVER["HTTP_REFERER"])) {
  97. $referrer_host = parse_url($_SERVER["HTTP_REFERER"], PHP_URL_HOST);
  98. if (
  99. strcmp($referrer_host, gethostname()) === 0 &&
  100. in_array($port, ["443", $_SERVER["SERVER_PORT"]])
  101. ) {
  102. return checkStrictness(2);
  103. } else {
  104. if (
  105. strcmp($referrer_host, $hostname) === 0 &&
  106. in_array($port, ["443", $_SERVER["SERVER_PORT"]])
  107. ) {
  108. return checkStrictness(1);
  109. } else {
  110. return checkStrictness(0);
  111. }
  112. }
  113. } else {
  114. return checkStrictness(0);
  115. }
  116. }
  117. }
  118. }
  119. if ($check_csrf == true) {
  120. prevent_post_csrf();
  121. prevent_get_csrf();
  122. }