Skip to content

Commit

Permalink
Merge pull request laravel#9150 from franzliedke/unencrypted-cookies
Browse files Browse the repository at this point in the history
[5.1] Disable encryption for certain cookies
  • Loading branch information
taylorotwell committed Jun 9, 2015
2 parents 614eff5 + 7a114d0 commit ad8a241
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/Illuminate/Cookie/Middleware/EncryptCookies.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class EncryptCookies
*/
protected $encrypter;

/**
* The names of all cookies for which encryption is disabled.
*
* @var array
*/
protected static $disabled = [];

/**
* Create a new CookieGuard instance.
*
Expand All @@ -29,6 +36,17 @@ public function __construct(EncrypterContract $encrypter)
$this->encrypter = $encrypter;
}

/**
* Disable encryption for the given cookie name(s).
*
* @param string|array $cookieName
* @return void
*/
public static function disableFor($cookieName)
{
static::$disabled[] = array_merge(static::$disabled, (array) $cookieName);
}

/**
* Handle an incoming request.
*
Expand All @@ -50,6 +68,10 @@ public function handle($request, Closure $next)
protected function decrypt(Request $request)
{
foreach ($request->cookies as $key => $c) {
if ($this->isDisabled($key)) {
continue;
}

try {
$request->cookies->set($key, $this->decryptCookie($c));
} catch (DecryptException $e) {
Expand Down Expand Up @@ -99,6 +121,10 @@ protected function decryptArray(array $cookie)
protected function encrypt(Response $response)
{
foreach ($response->headers->getCookies() as $key => $cookie) {
if ($this->isDisabled($key)) {
continue;
}

$response->headers->setCookie($this->duplicate(
$cookie, $this->encrypter->encrypt($cookie->getValue())
));
Expand All @@ -121,4 +147,15 @@ protected function duplicate(Cookie $c, $value)
$c->getDomain(), $c->isSecure(), $c->isHttpOnly()
);
}

/**
* Determine whether encryption has been disabled for the given cookie.
*
* @param string $name
* @return bool
*/
protected function isDisabled($name)
{
return in_array($name, static::$disabled);
}
}

0 comments on commit ad8a241

Please sign in to comment.