Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'security/escaper-usage'
Browse files Browse the repository at this point in the history
Fixes a number of components that were not using Zend\Escaper to escape HTML,
HTML attributes, and/or URLs.
  • Loading branch information
Showing 1 changed file with 40 additions and 6 deletions.
46 changes: 40 additions & 6 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Zend\Uri;

use Zend\Escaper\Escaper;
use Zend\Validator;

/**
Expand Down Expand Up @@ -125,6 +126,11 @@ class Uri implements UriInterface
*/
protected static $defaultPorts = array();

/**
* @var Escaper
*/
protected static $escaper;

/**
* Create a new URI object
*
Expand Down Expand Up @@ -152,6 +158,31 @@ public function __construct($uri = null)
}
}

/**
* Set Escaper instance
*
* @param Escaper $escaper
*/
public static function setEscaper(Escaper $escaper)
{
static::$escaper = $escaper;
}

/**
* Retrieve Escaper instance
*
* Lazy-loads one if none provided
*
* @return Escaper
*/
public static function getEscaper()
{
if (null === static::$escaper) {
static::setEscaper(new Escaper());
}
return static::$escaper;
}

/**
* Check if the URI is valid
*
Expand Down Expand Up @@ -935,8 +966,9 @@ public static function encodeUserInfo($userInfo)
}

$regex = '/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:]|%(?![A-Fa-f0-9]{2}))/';
$replace = function($match) {
return rawurlencode($match[0]);
$escaper = static::getEscaper();
$replace = function ($match) use ($escaper) {
return $escaper->escapeUrl($match[0]);
};

return preg_replace_callback($regex, $replace, $userInfo);
Expand All @@ -962,8 +994,9 @@ public static function encodePath($path)
}

$regex = '/(?:[^' . self::CHAR_UNRESERVED . ':@&=\+\$,\/;%]+|%(?![A-Fa-f0-9]{2}))/';
$replace = function($match) {
return rawurlencode($match[0]);
$escaper = static::getEscaper();
$replace = function ($match) use ($escaper) {
return $escaper->escapeUrl($match[0]);
};

return preg_replace_callback($regex, $replace, $path);
Expand All @@ -990,8 +1023,9 @@ public static function encodeQueryFragment($input)
}

$regex = '/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\/\?]+|%(?![A-Fa-f0-9]{2}))/';
$replace = function($match) {
return rawurlencode($match[0]);
$escaper = static::getEscaper();
$replace = function ($match) use ($escaper) {
return $escaper->escapeUrl($match[0]);
};

return preg_replace_callback($regex, $replace, $input);
Expand Down

0 comments on commit 94cf692

Please sign in to comment.