Securing http cookies without changing your base code

6. January 2009

I came across a situation where a ASP.Net 1.1 application had a security scan done and one of the issues that came back was the fact that the cookies were not marked secure.  Not a major issue.  However, if you have a product base that is customized many times over, how do you change everything in production without wasting a lot of time and resources.  The goal is to not modify the existing code for any of the product bases, create complete http cookie coverage, and simplify the testing and redeployment of a single assembly.

Interested in the fix?

Here is what I came up with....

An HttpModule.  My HttpModule reads every cookie coming in and out, marks them as secure, sets and expiration time and forces it to http only.  Basically, everything that needed to be done and more to the cookies in order to satisfy the security scans.  Below is all the code we needed to create the http module.

 
using System;
using System.Web;

namespace Jaws.Core.Web
{
    public class SecureCookies : IHttpModule
    {
        #region Implementation of IHttpModule

        /// <summary>
        /// Initializes a module and prepares it to handle requests.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpApplication" /> that provides access to the methods, properties, and events common to all application objects within an ASP.NET application </param>
        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute += SecureAllCookies;
            context.PreSendRequestContent += SecureAllCookies;
        }

        /// <summary>
        /// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule" />.
        /// </summary>
        public void Dispose()
        {
            //Nothing to Dispose of at this point.
        }

        private static void SecureAllCookies(object sender, EventArgs e)
        {
            var context = (sender as HttpApplication);

            if (context != null)
            {
                foreach (string cookie in context.Request.Cookies)
                {
                    context.Request.Cookies[cookie].Secure = true;
                    context.Request.Cookies[cookie].Expires = DateTime.Now.AddMinutes(10);
                    context.Request.Cookies[cookie].HttpOnly = true;
                }
            }
        }

        #endregion
    }
}

.Net, Tips and Tricks

Comments

7/11/2010 1:43:17 AM #
Safe answer ;)
7/12/2010 10:50:36 AM #
This is a great web site.  Good sparkling UI and very informative articles. I will be coming back soon, thanks for the great article.
7/14/2010 10:02:36 PM #
amazon coupon codes
7/15/2010 10:51:22 AM #
U wilt geld lenen zonder BKR toetsing? De opties hiervoor worden groter, kijk verder en ontdek hoe u wél geld kunt lenen, snel & eenvoudig.
7/25/2010 9:31:50 AM #
Migraine | Voedingscentrum, eerlijk over eten Een migraine-aanval ontstaat door een te heftige reactie op bepaalde prikkels. Welke prikkel een aanval kan uitlokken, verschilt van persoon tot persoon.
7/26/2010 1:23:52 AM #
Do you have problems with spammers?  I also use Blog Engine and I have some good anti-spam techniques; please Email me if you are interested in an exchange of practices.
7/26/2010 2:07:10 AM #
Pheromones!! Great write up, bookmarked, will be back soon. Because this, pheromone,  was a fantastic article, pheromones,, keep up the good work .
7/30/2010 8:35:10 PM #
thanks for share. wait for more article

Regards
Replica Watches
7/30/2010 9:59:52 PM #
There are definitely a whole bunch more details to take into consideration, but thanks for sharing this article.
7/31/2010 5:16:55 AM #
Ha, can't say you're wrong. Just need to get my head around this.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading