Java Http-Only Cookie Test

This tests the ability for a Java applet to use http-only cookies. There are two cookies: normal and httpOnly, each with a value of 'xxx'. Try getting them from within the browser, from the server using AJAX, and from the server using Java.


Expected output:
  Has 'normal' cookie.
  Does not have 'httpOnly' cookie.

Expected output:
  Has 'normal' cookie.
  Has 'httpOnly' cookie.
No Java, cannot do test
Expected output:
  Has 'normal' cookie.
  Has 'httpOnly' cookie.

Why do we care?

For security. Http-only cookies are great for protecting sessions and they are less likely to be picked up using some cross-site JavaScript attack.
Since the applet cannot make URL connections outside of the domain it is from it can NEVER use third party cookies unless it is signed or approved to do this. The applet may need to maintain the session that the containing document is part of. Java also does not allow reading the cookie information from within the applet and are seamlessly added to the HTTP connection, thus meaning they cannot even be read in the Java applet.

Browser Results

BrowserOSVersionCookie SettingBrowserAJAXJava
FirefoxVista3.6.3Do not accept third-partyGoodGoodNo cookies at all
FirefoxVista3.6.3Accept third-partyGoodGoodNo httpOnly cookie
SafariVista3.2.2Only from sites you visitHas httpOnly CcookieGoodGoodSafari 3 doesn't support protecting
http-only cookies at all
SafariVista3.2.2AlwaysHas httpOnly CcookieGoodGood
SafariVista4.0.5Only from sites you visitGoodNo httpOnly cookieNo httpOnly cookieSafari 4 doesn't support
http-only cookies at all
SafariVista4.0.5AlwaysGoodNo httpOnly cookieNo httpOnly cookie
OperaVista9.63Only from the site I visitGoodGoodGood
OperaVista9.63AllGoodGoodGood
ChromeVista4.1.249Restrict third-party cookiesGoodGoodNo httpOnly cookie
ChromeVista4.1.249AllGoodGoodNo httpOnly cookie
IEVista8.0.6001HighNo cookies at allGoodNo cookies at allConsitent between JavaScript and Java
IEVista8.0.6001Medium-HighGoodGoodNo httpOnly cookie
IEVista8.0.6001MediumGoodGoodNo httpOnly cookie
IEVista8.0.6001AllGoodGoodNo httpOnly cookie
If you have more data to add, please email me at [email protected].

Relevent bug reports:

Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=441166
Safari: https://bugs.webkit.org/show_bug.cgi?id=10957


The Code

The PHP code to set the cookies is:
  setcookie('normal', 'xxx', 0, '/', null, false, false);
  setcookie('httpOnly', 'xxx', 0, '/', null, false, true);

The PHP code to check the cookies is (used by AJAX and Java):
  if (array_key_exists('normal', $_COOKIE) && $_COOKIE['normal'] == 'xxx') {
    echo "Has 'normal' cookie.\n";
  } else {
    echo "Does not have 'normal' cookie.\n";
  }

  if (array_key_exists('httpOnly', $_COOKIE) && $_COOKIE['httpOnly'] == 'xxx') {
    echo "Has 'httpOnly' cookie.";
  } else {
    echo "Does not have 'httpOnly' cookie.\n";
  }

The Java code can be viewed here: CookieTest.java

And feel free to look at the JavaScript code by using view source.