"Curiosity is the very basis of education and if you tell me that curiosity killed the cat, I say only the cat died nobly." - Arnold Edinborough

For security reasons it’s fairly good practice to invalidate all log-in sessions when a users password is changed. This is especially useful when a users account has been compromised and they go to change or reset their password. Without log-in session invalidation the attacker will still be logged in and able to cause chaos.

Unfortunately Laravel does not provide this functionality out of the box. We actually have to go through quite a bit of trouble to make Laravel play ball here but it’s definitely worth it, so lets get to it!

Implementing this feature is a two step process.

  1. Track the session IDs of all logged in users.
  2. Invalidate the session data attached to those user sessions.

Part one is fairly simple and can be done in your own application code. The session ID is exposed through Session::getId() so simply add this to an array of session ids stored in a persistent cache. When When you want to invalidate the log-in sessions simply fetch this cached array. I will leave this part as an exercise to the reader.

Part two is where it gets tricky. The Laravel session providers themselves implement a very suitable destroy method that takes a session ID. However, unfortunately the Laravel session store does not expose this method but instead implements a migrate() method. This method does not take a session ID, but instead offers to destroy the current session. Invalidating the session of the user changing the password is all perfectly fine, but that still leaves our attacker logged in. In order to fix this we need to implement a custom session store that properly exposes the destroy() method of the individual session providers.

The following code is done in Laravel 4.2. Version 5 has quite a few changes so this might be significantly different. If anyone uses Laravel 5 please let me know if this applies there as well.

In order to implement this we need to backtrack to where Laravel actually loads session classes. This is defined in your app.php providers array as ‘Illuminate\Session\SessionServiceProvider’. The SessionServiceProvider registers a SessionManager which then creates the Store class we’re looking to extend. This means we need to provide our own version of these 3 classes and then modify app.php to load our own SessionServiceProvider class.

Read More

As of Laravel 5.0 it’s still not possible to set the remember me cookie with a secure flag. This is slightly weird as there is a configuration option for secure session cookies. Fortunately modifying Laravel to set a secure log-in cookie is not too difficult – all we need to do is provide a custom Guard class for the Auth driver which overrides the setRecaller() method.

This code is done against Laravel 4.2, I’m not sure how simple it is to adapt to 5.0 as I have not had a chance to work with that yet. Feel free to let me know in a comment.

<?php 
/*  
 * Custom guard class that sets a secure log-in cookie.
 */ 
class SecureGuard extends \Illuminate\Auth\Guard
{
	/**
	 * Create a secure remember me cookie for a given ID.
	 *
	 * @param  string  $value
	 * @return \Symfony\Component\HttpFoundation\Cookie
	 */
	protected function createRecaller($value)
	{
		return $this->getCookieJar()->forever($this->getRecallerName(), $value, null, null, true);
	}
}

Now that we have our custom guard class we need to tell Laravel to use this new class. While not completely intuitive the best way to do that is to configure a custom auth driver where we wrap the default EloquentUserProvider class in our new SecureGuard class. Add the following to your global.php file.

<?php
/*
|--------------------------------------------------------------------------
| Auth Driver
|--------------------------------------------------------------------------
|
| Extend the auth driver to support secure cookies.
|
*/

Auth::extend('SecureAuth', function($app)
{
	$model    = $app['config']['auth.model'];
	$provider = new Illuminate\Auth\EloquentUserProvider($app['hash'], $model);

	return new SecureGuard($provider, $app['session.store']);
});

Finally update your auth.php config file to set the new auth driver.

'driver' => 'SecureAuth',

I’m super excited to announce that I will be speaking at the nginx conference starting on the 20th. I haven’t really put much content up on this blog for a long while, but for the past year I have been working at a smartphone company called OnePlus. We have gotten a ton of traction in the past few months with press from all over the world and great marketing by our team. This naturally means a lot of traffic to handle and because of the way we do our marketing that traffic comes in large bursts rather than sustained over time. During one of our campaigns we had 22,000 connections inside just 10 seconds of our campaign page being live.

At nginx conf I will speak about just how we handle this type of traffic and how it’s different from more normal traffic patterns. If you’re attending stop by my talk on the 22nd at 11:45 am! If you don’t have a ticket yet it’s not too late! Until the 19th you can still register for the conference. (use the discount code SPEAKER25 for 25% off!)

Looking  forward to hopefully seeing a bunch of you guys there!

I get contacted by advertising agencies fairly regularly. Most of them are similar in their offering, and most of them perform equally badly. I suppose if they performed well they probably wouldn’t be contacting me as they’d already have publishers queueing up to display their ads.

The latest one to join the batch is IronSource which differs from the crowd by offering installer advertising based on InstallerCore. As customary for these types I declined since I’ve never seen an installer based advertising solution that wasn’t basically a scam and based on tricking users into installing the offers. IronSource was persistent, though, and told me how much money a competitor of mine was making, (thanks!) which was indeed a large amount of money.

Furthermore they assured me they were “one of Google’s biggest partners working directly with their compliance team which means we have to follow their strict guidelines.”

Figuring it couldn’t possibly hurt to check it out (this is called foreshadowing) I went to my competitor and downloaded the installer. What I experienced is best described in images, anonymized to protect the innocents who just want to make a lot of money.

Read More

Dealing with errors in nginx can be a frustrating experience if nginx isn’t configured correctly. Sadly, the default value for error log is less than optimal and some of the tricks to getting information from nginx are not obvious. This post is intended to be a reference for the tools nginx provide and how to configure them; as well as a general guide on what’s important when facing issues in nginx.

I will probably be vilified for putting this as the first thing, however. Knowing the basics is the most important part to understanding the more difficult issues not just related to syntax. Don’t skip basic nginx syntax or how an nginx request flows. Once you understand these concepts the non-error issues become far more tangible to work with.

The Error Log

For issues where there’s an error involved, having nginx configured correctly is absolutely essential. The error_log directive should be configured with an error log level of warn, either at server or http level depending on whether you want per-vhost logs or server wide logs.

Read More

During the last few months I have been working on an nginx book for Packt Publishing. The book is called Instant Nginx Starter and is now published!

My goal with this book was to provide a concise introduction to the nginx configuration in a way that allowed people to build on top of the knowledge I provided. This meshed well with the Instant line of books from Packt as they are intended to be short and get the reader started quickly.

The knowledge contained in this book is basically a structured, revised and expanded upon version of the information on this blog. It provides a red-thread to help you process the configuration format and syntax in a logical way. I’m really satisfied with how this book turned out and I hope it will help some of you.

Check it out on Packt Publishing

Read More