prevent_csrf.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. $check_csrf = true;
  3. if ( $_SERVER['SCRIPT_FILENAME'] == '/usr/local/hestia/web/inc/mail-wrapper.php '){ $check_csrf=false; } // execute only from CLI
  4. if ( $_SERVER['SCRIPT_FILENAME'] == '/usr/local/hestia/web/reset/mail/index.php '){ $check_csrf=false; } // Localhost only
  5. if ( $_SERVER['SCRIPT_FILENAME'] == '/usr/local/hestia/web/api/index.php' ){ $check_csrf=false; } // Own check
  6. if (substr($_SERVER['SCRIPT_FILENAME'], 0, 22)=='/usr/local/hestia/bin/' ){ $check_csrf=false; }
  7. function checkStrictness($level){
  8. if ($level >= $_SESSION['POLICY_CSRF_STRICTNESS']) {
  9. return true;
  10. }else{
  11. echo "<h1>Potential use CSRF detected</h1>\n".
  12. "<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>".
  13. "<p>If you folowed a bookmark or an static link <a href='/'>please click here</a>";
  14. die();
  15. }
  16. }
  17. function prevent_post_csrf(){
  18. if ($_SERVER['REQUEST_METHOD']=='POST') {
  19. $hostname = explode( ':', $_SERVER['HTTP_HOST']);
  20. $port=$hostname[1];
  21. $hostname=$hostname[0];
  22. if (strpos($_SERVER['HTTP_ORIGIN'],gethostname()) !== false && in_array($port, array('443',$_SERVER['SERVER_PORT'])) ) {
  23. return checkStrictness(2);
  24. }else{
  25. if (strpos($_SERVER['HTTP_ORIGIN'],$hostname) !== false && in_array($port, array('443',$_SERVER['SERVER_PORT'])) ){
  26. return checkStrictness(1);
  27. } else {
  28. return checkStrictness(0);
  29. }
  30. }
  31. }
  32. }
  33. function prevent_get_csrf(){
  34. if ($_SERVER['REQUEST_METHOD']=='GET') {
  35. $hostname = explode( ':', $_SERVER['HTTP_HOST']);
  36. $port=$hostname[1];
  37. $hostname=$hostname[0];
  38. //list of possible entries route and these should never be blocked
  39. 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'))){
  40. return true;
  41. }
  42. if (strpos($_SERVER['HTTP_REFERER'],gethostname()) !== false && in_array($port, array('443',$_SERVER['SERVER_PORT'])) ) {
  43. return checkStrictness(2);
  44. }else{
  45. if (strpos($_SERVER['HTTP_REFERER'],$hostname) !== false && in_array($port, array('443',$_SERVER['SERVER_PORT'])) ){
  46. return checkStrictness(1);
  47. } else {
  48. return checkStrictness(0);
  49. }
  50. }
  51. }
  52. }
  53. if ( $check_csrf == true){
  54. prevent_post_csrf();
  55. prevent_get_csrf();
  56. }