Repository: ipython/ipython

Issue:
ipv6 address triggers cookie exception
# Error Summary

When accessing IPython Notebook via an ipv6 ip address a http.cookies.CookieError is raised:

```
http.cookies.CookieError: Illegal key value: username-[2001-4dd0-2030-0-ba27-ebff-fe7c-a4b4]-8888
```

The URL was http://[2001:4dd0:2030:0:ba27:ebff:fe7c:a4b4]:8888/
## System Information

```
{'codename': 'An Afternoon Hack',
 'commit_hash': '7c2ea3a',
 'commit_source': 'installation',
 'default_encoding': 'UTF-8',
 'ipython_path': '/usr/lib/python3.3/site-packages/IPython',
 'ipython_version': '1.1.0',
 'os_name': 'posix',
 'platform': 'Linux-3.10.25-1-ARCH-armv6l-with-arch',
 'sys_executable': '/usr/bin/python',
 'sys_platform': 'linux',
 'sys_version': '3.3.3 (default, Nov 29 2013, 11:19:14) \n[GCC 4.7.2]'}
```
## Traceback

```
HTTPRequest(protocol='http', host='[2001:4dd0:2030:0:ba27:ebff:fe7c:a4b4]:8888', method='GET', uri='/', version='HTTP/1.1', remote_ip='2001:4dd0:2030:0:6aa8:6dff:fe46:d8f6', headers={'Accept-Encoding': 'gzip, def
late', 'Dnt': '1', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:26.0) Gecko/20100101 Firefox/26.0', 'Connection': 'keep-alive', 'Accept-Language': 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3', 'Accept'
: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Host': '[2001:4dd0:2030:0:ba27:ebff:fe7c:a4b4]:8888'})
Traceback (most recent call last):
  File "/usr/lib/python3.3/site-packages/tornado/web.py", line 1141, in _when_complete
    callback()
  File "/usr/lib/python3.3/site-packages/tornado/web.py", line 1162, in _execute_method
    self._when_complete(method(*self.path_args, **self.path_kwargs),
  File "/usr/lib/python3.3/site-packages/tornado/web.py", line 2284, in wrapper
    if not self.current_user:
  File "/usr/lib/python3.3/site-packages/tornado/web.py", line 906, in current_user
    self._current_user = self.get_current_user()
  File "/usr/lib/python3.3/site-packages/IPython/html/base/handlers.py", line 116, in get_current_user
    self.clear_login_cookie()
  File "/usr/lib/python3.3/site-packages/IPython/html/base/handlers.py", line 107, in clear_login_cookie
    self.clear_cookie(self.cookie_name)
  File "/usr/lib/python3.3/site-packages/tornado/web.py", line 443, in clear_cookie
    domain=domain)
  File "/usr/lib/python3.3/site-packages/tornado/web.py", line 423, in set_cookie
    self._new_cookie[name] = value
  File "/usr/lib/python3.3/http/cookies.py", line 492, in __setitem__
    self.__set(key, rval, cval)
  File "/usr/lib/python3.3/http/cookies.py", line 486, in __set
    M.set(key, real_value, coded_value)
  File "/usr/lib/python3.3/http/cookies.py", line 366, in set
    raise CookieError("Illegal key value: %s" % key)
http.cookies.CookieError: Illegal key value: username-[2001-4dd0-2030-0-ba27-ebff-fe7c-a4b4]-8888
ERROR:tornado.application:Uncaught exception GET / (2001:4dd0:2030:0:6aa8:6dff:fe46:d8f6)
HTTPRequest(protocol='http', host='[2001:4dd0:2030:0:ba27:ebff:fe7c:a4b4]:8888', method='GET', uri='/', version='HTTP/1.1', remote_ip='2001:4dd0:2030:0:6aa8:6dff:fe46:d8f6', headers={'Accept-Encoding': 'gzip, def
late', 'Dnt': '1', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:26.0) Gecko/20100101 Firefox/26.0', 'Connection': 'keep-alive', 'Accept-Language': 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3', 'Accept'
: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Host': '[2001:4dd0:2030:0:ba27:ebff:fe7c:a4b4]:8888'})
Traceback (most recent call last):
  File "/usr/lib/python3.3/site-packages/tornado/web.py", line 1141, in _when_complete
    callback()
  File "/usr/lib/python3.3/site-packages/tornado/web.py", line 1162, in _execute_method
    self._when_complete(method(*self.path_args, **self.path_kwargs),
  File "/usr/lib/python3.3/site-packages/tornado/web.py", line 2284, in wrapper
    if not self.current_user:
  File "/usr/lib/python3.3/site-packages/tornado/web.py", line 906, in current_user
    self._current_user = self.get_current_user()
  File "/usr/lib/python3.3/site-packages/IPython/html/base/handlers.py", line 116, in get_current_user
    self.clear_login_cookie()
  File "/usr/lib/python3.3/site-packages/IPython/html/base/handlers.py", line 107, in clear_login_cookie
    self.clear_cookie(self.cookie_name)
  File "/usr/lib/python3.3/site-packages/tornado/web.py", line 443, in clear_cookie
    domain=domain)
  File "/usr/lib/python3.3/site-packages/tornado/web.py", line 423, in set_cookie
    self._new_cookie[name] = value
  File "/usr/lib/python3.3/http/cookies.py", line 492, in __setitem__
    self.__set(key, rval, cval)
  File "/usr/lib/python3.3/http/cookies.py", line 486, in __set
    M.set(key, real_value, coded_value)
  File "/usr/lib/python3.3/http/cookies.py", line 366, in set
    raise CookieError("Illegal key value: %s" % key)
http.cookies.CookieError: Illegal key value: username-[2001-4dd0-2030-0-ba27-ebff-fe7c-a4b4]-8888
2014-01-06 22:28:10.570 [tornado.access] ERROR | 500 GET / (2001:4dd0:2030:0:6aa8:6dff:fe46:d8f6) 109.15ms
ERROR:tornado.access:500 GET / (2001:4dd0:2030:0:6aa8:6dff:fe46:d8f6) 109.15ms
2014-01-06 22:28:10.755 [tornado.access] WARNING | 404 GET /favicon.ico (2001:4dd0:2030:0:6aa8:6dff:fe46:d8f6) 21.05ms
WARNING:tornado.access:404 GET /favicon.ico (2001:4dd0:2030:0:6aa8:6dff:fe46:d8f6) 21.05ms
2014-01-06 22:28:10.794 [tornado.access] WARNING | 404 GET /favicon.ico (2001:4dd0:2030:0:6aa8:6dff:fe46:d8f6) 19.79ms
WARNING:tornado.access:404 GET /favicon.ico (2001:4dd0:2030:0:6aa8:6dff:fe46:d8f6) 19.79ms
```



Discussion:

Resolving patch:
diff --git a/IPython/html/base/handlers.py b/IPython/html/base/handlers.py
--- a/IPython/html/base/handlers.py
+++ b/IPython/html/base/handlers.py
@@ -21,6 +21,7 @@
 import json
 import logging
 import os
+import re
 import stat
 import sys
 import traceback
@@ -43,6 +44,7 @@
 #-----------------------------------------------------------------------------
 # Top-level handlers
 #-----------------------------------------------------------------------------
+non_alphanum = re.compile(r'[^A-Za-z0-9]')
 
 class RequestHandler(web.RequestHandler):
     """RequestHandler with default variable setting."""
@@ -71,9 +73,9 @@ def get_current_user(self):
 
     @property
     def cookie_name(self):
-        default_cookie_name = 'username-{host}'.format(
-            host=self.request.host,
-        ).replace(':', '-')
+        default_cookie_name = non_alphanum.sub('-', 'username-{}'.format(
+            self.request.host
+        ))
         return self.settings.get('cookie_name', default_cookie_name)
     
     @property


Test patch: