waflessnet

bug Security Arbitrary URL Redirection detected (FIX)

Sep 3rd, 2013
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.90 KB | None | 0 0
  1. <?php
  2.  
  3. class Tools extends ToolsCore
  4. {
  5.  
  6.     public static function redirect($url, $base_uri = __PS_BASE_URI__, Link $link = null, $headers = null)
  7.     {
  8.         if (!$link)
  9.             $link = Context::getContext()->link;
  10.  
  11.         //exit();
  12.         if(self::isValidUrl($url))
  13.             if(self::isExternalUrl($url))
  14.                 $url="/index.php";
  15.         if (strpos($url, 'http://') === false && strpos($url, 'https://') === false && $link)
  16.         {
  17.             if (strpos($url, $base_uri) === 0)
  18.                 $url = substr($url, strlen($base_uri));
  19.             if (strpos($url, 'index.php?controller=') !== false && strpos($url, 'index.php/') == 0)
  20.             {
  21.                 $url = substr($url, strlen('index.php?controller='));
  22.                 if (Configuration::get('PS_REWRITING_SETTINGS'))
  23.                     $url = Tools::strReplaceFirst('&', '?', $url);
  24.             }
  25.  
  26.             $explode = explode('?', $url);
  27.             // don't use ssl if url is home page
  28.             // used when logout for example
  29.             $use_ssl = !empty($url);
  30.             $url = $link->getPageLink($explode[0], $use_ssl);
  31.             if (isset($explode[1]))
  32.                 $url .= '?'.$explode[1];
  33.         }
  34.  
  35.         // Send additional headers
  36.         if ($headers)
  37.         {
  38.             if (!is_array($headers))
  39.                 $headers = array($headers);
  40.  
  41.             foreach ($headers as $header)
  42.                 header($header);
  43.         }
  44.  
  45.         header('Location: '.$url);
  46.         exit;
  47.     }
  48.     public static function redirectAdmin($url)
  49.     {
  50.       if(self::isValidUrl($url))
  51.         if(self::isExternalUrl($url))
  52.             $url="/index.php"; 
  53.         header('Location: '.$url);
  54.         exit;
  55.     }
  56.        
  57.     public static function isExternalUrl($url=null)
  58.     {
  59.         $url=parse_url($url);
  60.         $url=$url['host'];
  61.        if(!is_null($url))
  62.             if(strtolower($_SERVER['SERVER_NAME'])!=strtolower($url))
  63.                 return TRUE;
  64.         return FALSE;
  65.        
  66.        
  67.     }
  68.     public static function  isValidUrl($url){
  69.         if(!$url || !is_string($url))
  70.             return false;
  71.          if(!preg_match('%^((https?://)|(www\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i', $url))
  72.                         return false;
  73.         return true;
  74.     }
  75.    
  76.    
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment