Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add hashed password support. #1011

Merged
merged 11 commits into from

5 participants

Stefan van der Walt Fernando Perez Satrajit Ghosh Min RK Thomas Kluyver
Stefan van der Walt

Add hashing of passwords to notebook configuration [written with Mateusz Paprocki].

IPython/lib/security.py
((15 lines not shown))
+ ----------
+ passphrase : str
+ Password to hash.
+
+ Returns
+ -------
+ hashed_passphrase : str
+ Hashed password, in the format 'hash_algorithm:salt:passphrase_hash'.
+
+ Examples
+ --------
+ In [1]: passwd('mypassword')
+ Out[1]: 'sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12'
+
+ """
+ algorithm = 'sha1'
Min RK Owner
minrk added a note

if algorithm is an option, why isn't it an arg to passwd?

Stefan van der Walt
stefanv added a note

Because, for now, only sha1 is supported. We could add it though; would that be preferred?

Min RK Owner
minrk added a note

If the design is that multiple algorithms are to be supported, it should probably be an arg, even if the supported list is only length one at this point.

In fact, if you simply removed the length-check dict, and just let the hash go ahead, you would already have support for more than just sha1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/lib/security.py
((61 lines not shown))
+ In [3]: passwd_check('sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12',
+ ...: 'anotherpassword')
+ Out[3]: False
+
+ """
+ # Algorithm and hash length
+ supported_algorithms = {'sha1': 40}
+
+ try:
+ algorithm, salt, pw_digest = hashed_passphrase.split(':', 2)
+ except (ValueError, TypeError):
+ return False
+
+ if not (algorithm in supported_algorithms and \
+ len(pw_digest) == supported_algorithms[algorithm] and \
+ len(salt) == 4):
Min RK Owner
minrk added a note

The way you generate the salt above, it is not guaranteed to be 4 bytes because the random values can be < 16^3, which will be 3 (or fewer) characters, so this will actually fail once in every ~16 runs.

It also seems unnecessary to check the length of the salt, since it shouldn't actually matter.

Min RK Owner
minrk added a note

Why not just skip this step, and wrap the next hashlib.new();h.update() bit in try/except: return False?

That's obviously more expensive in the rare cases where input is invalid, but it means we would support pretty much everything in hashlib.algorithms without having to maintain a supported_algorithms dict. I cannot think of a situation where this extra cost would be significant, though.

In this way, less code actually supports more use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Stefan van der Walt stefanv referenced this pull request
Merged

Add logout button. #1012

IPython/lib/security.py
((4 lines not shown))
+
+import hashlib
+import random
+
+def passwd(passphrase, algorithm='sha1'):
+ """Generate hashed password and salt for use in notebook configuration.
+
+ In the notebook configuration, set `c.NotebookApp.password` to
+ the generated string.
+
+ Parameters
+ ----------
+ passphrase : str
+ Password to hash.
+ algorithm : str
+ Hashing algorithm to use.
Fernando Perez Owner
fperez added a note

Should indicate here that this string should be any that's a valid input to hashlib.new, so users know what to look for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/lib/security.py
((18 lines not shown))
+ algorithm : str
+ Hashing algorithm to use.
+
+ Returns
+ -------
+ hashed_passphrase : str
+ Hashed password, in the format 'hash_algorithm:salt:passphrase_hash'.
+
+ Examples
+ --------
+ In [1]: passwd('mypassword')
+ Out[1]: 'sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12'
+
+ """
+ h = hashlib.new(algorithm)
+ salt = '%04x' % random.getrandbits(16)
Fernando Perez Owner
fperez added a note

I'm not an expert on salting, but from a quick read on wikipedia it seems that the recommended salt length is 48 to 128 bits. Why not make it at least 64 bits?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/lib/security.py
@@ -0,0 +1,81 @@
+"""
+Password generation for the IPython notebook.
+"""
+
+import hashlib
+import random
+
+def passwd(passphrase, algorithm='sha1'):
Fernando Perez Owner
fperez added a note

This function should be callable without a passphrase (set to None). In that mode, it should use the getpass module to ask the user for the password interactively, doing it twice to verify correctness, and then return the encoded value. This would let users create passwords without echoing them to the screen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Fernando Perez
Owner

Thanks a ton for this, it's awesome. The fixes should be pretty easy and we can merge soon.

Fernando Perez

Why not use None here? It's a clearer way to indicate that this parameter is in an invalid state...

Fernando Perez

Since this is inherently interactive code, instead of a simple check with a ValueError at the end, it should instead do a 3-loop attempt at getting it right, and if on the third time it still doesn't work, then simply print to stderr a short error message and return.

Fernando Perez

No, not an exception, just an error message to stderr: exceptions are useful for programmatic use, but they are not very user friendly. Alternatively, you can import from IPython.core.error import UsageError. That error is a real exception (in case this code is used by something else) but we special-case it and don't show a traceback, for situations like this.

Fernando Perez fperez merged commit 1f72dda into from
Min RK minrk commented on the diff
IPython/lib/security.py
((82 lines not shown))
+ In [3]: passwd_check('sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12',
+ ...: 'anotherpassword')
+ Out[3]: False
+
+ """
+ try:
+ algorithm, salt, pw_digest = hashed_passphrase.split(':', 2)
+ except (ValueError, TypeError):
+ return False
+
+ try:
+ h = hashlib.new(algorithm)
+ except ValueError:
+ return False
+
+ if len(pw_digest) == 0 or len(salt) != salt_len:
Min RK Owner
minrk added a note

Checking the salt length seems entirely unnecessary here. It has no valuable effect, because shorter or longer salts would still be perfectly valid.

Fernando Perez Owner
fperez added a note

Good point. I won't revert it quite yet, let's see if @stefanv had a specific reason to check for that. But I can't think of one and unless Stefan has a good reason for wanting it there, we can remove the length check.

Stefan van der Walt
stefanv added a note
Fernando Perez Owner
fperez added a note

Fixed in 4cd1067

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Fernando Perez fperez commented on the diff
IPython/lib/security.py
((44 lines not shown))
+ p1 = getpass.getpass('Verify password: ')
+ if p0 == p1:
+ passphrase = p0
+ break
+ else:
+ print('Passwords do not match.')
+ else:
+ raise UsageError('No matching passwords found. Giving up.')
+
+ h = hashlib.new(algorithm)
+ salt = ('%0' + str(salt_len) + 'x') % random.getrandbits(4 * salt_len)
+ h.update(passphrase + salt)
+
+ return ':'.join((algorithm, salt, h.hexdigest()))
+
+def passwd_check(hashed_passphrase, passphrase):
Fernando Perez Owner
fperez added a note

BTW, it just occurred to me that we should probably add to this function a delay option, defaulting to 0.25 seconds at least... That will prevent DOS attacks and make brute-force rapid-fire guessing impossible. What do you guys think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Satrajit Ghosh

is there anyway to do this, so it can be done on the command line when starting the notebook? i tried to add a function to the config.py file that asks for the password, but it seems the config file is parsed too many times during a session and hence i kept getting asked for a password.

Thomas Kluyver takluyver commented on the diff
IPython/lib/security.py
((40 lines not shown))
+ """
+ if passphrase is None:
+ for i in range(3):
+ p0 = getpass.getpass('Enter password: ')
+ p1 = getpass.getpass('Verify password: ')
+ if p0 == p1:
+ passphrase = p0
+ break
+ else:
+ print('Passwords do not match.')
+ else:
+ raise UsageError('No matching passwords found. Giving up.')
+
+ h = hashlib.new(algorithm)
+ salt = ('%0' + str(salt_len) + 'x') % random.getrandbits(4 * salt_len)
+ h.update(passphrase + salt)
Thomas Kluyver Owner

It's just occurred to me that this is not going to be portable to Python 3, because you can't directly hash unicode. For that matter, it will fail on non-ascii unicode strings in Python 2 as well.

I think the best thing is to do is: py3compat.cast_bytes((passphrase + salt), 'utf-8').

I realise this has been merged - I'll try to get round to doing another PR, unless someone else beats me to it.

Fernando Perez Owner
fperez added a note

Sorry about that! We haven't gotten into a good swing of keeping py3 in mind, my fault...

BTW, if you see anything similarly problematic on #1012, let me know. It doesn't have similar low-level pieces so it shouldn't be much of a problem, but still, pitch in if you see anything amiss regarding py3.

Thomas Kluyver Owner

I'll have a check.

Stefan van der Walt
stefanv added a note

Mateusz just reminded me that allowing unicode for a password is probably a bad idea, so I think we'll try to detect that situation.

Thomas Kluyver Owner

My feeling is that if people want to use non-ascii characters in their password, that's up to them.

Stefan van der Walt
stefanv added a note

So how about just doing .encode() on the string? That should work in both 2.7 and 3.

Thomas Kluyver Owner

I've done essentially that in PR #1016.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Fernando Perez
Owner

@satra, that's a good point. There's a quick and easy hack (until we have a chance to disentangle why the init file is being parsed more than once): write a small wrapper function in your init file (or a locally loaded utility) that simply tracks its own state and uses passwd:

def password():
  from IPython.lib import passwd
  if password.pwd is None:
    pwd = passwd()
    password.pwd = pwd
    return pwd
  else:
    return password.pwd

password.pwd = None
Fernando Perez fperez referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
16 IPython/frontend/html/notebook/handlers.py
View
@@ -28,6 +28,7 @@
from IPython.external.decorator import decorator
from IPython.zmq.session import Session
+from IPython.lib.security import passwd_check
try:
from docutils.core import publish_string
@@ -166,16 +167,25 @@ def get(self):
class LoginHandler(AuthenticatedHandler):
- def get(self):
+ def _render(self, message=''):
self.render('login.html',
next=self.get_argument('next', default='/'),
read_only=self.read_only,
+ message=message
)
+ def get(self):
+ self._render()
+
def post(self):
pwd = self.get_argument('password', default=u'')
- if self.application.password and pwd == self.application.password:
- self.set_secure_cookie('username', str(uuid.uuid4()))
+ if self.application.password:
+ if passwd_check(self.application.password, pwd):
+ self.set_secure_cookie('username', str(uuid.uuid4()))
+ else:
+ self._render(message='Invalid password')
+ return
+
self.redirect(self.get_argument('next', default='/'))
11 IPython/frontend/html/notebook/notebookapp.py
View
@@ -208,7 +208,16 @@ def _ip_changed(self, name, old, new):
)
password = Unicode(u'', config=True,
- help="""Password to use for web authentication"""
+ help="""Hashed password to use for web authentication.
+
+ To generate, do:
+
+ from IPython.lib import passwd
+
+ passwd('mypassphrase')
+
+ The string should be of the form type:salt:hashed-password.
+ """
)
open_browser = Bool(True, config=True,
12 IPython/frontend/html/notebook/static/css/layout.css
View
@@ -101,3 +101,15 @@
-moz-box-pack: center;
box-pack: center;
}
+
+#message {
+ border: 1px solid red;
+ background-color: #FFD3D1;
+ text-align: center;
+ padding: 0.5em;
+ margin: 0.5em;
+}
+
+#content_panel {
+ margin: 0.5em;
+}
2  IPython/frontend/html/notebook/static/css/projectdashboard.css
View
@@ -31,7 +31,7 @@ body {
}
#content_toolbar {
- padding: 10px 5px 5px 5px;
+ padding: 5px;
height: 25px;
line-height: 25px;
}
6 IPython/frontend/html/notebook/templates/login.html
View
@@ -31,6 +31,12 @@
</div>
<div id="content_panel">
+ {% if message %}
+ <div id="message">
+ {{message}}
+ </div>
+ {% end %}
+
<form action="/login?next={{url_escape(next)}}" method="post">
Password: <input type="password" name="password">
<input type="submit" value="Sign in" id="signin">
2  IPython/lib/__init__.py
View
@@ -25,6 +25,8 @@
current_gui
)
+from IPython.lib.security import passwd
+
#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------
102 IPython/lib/security.py
View
@@ -0,0 +1,102 @@
+"""
+Password generation for the IPython notebook.
+"""
+
+import hashlib
+import random
+import getpass
+
+from IPython.core.error import UsageError
+
+# Length of the salt in nr of hex chars, which implies salt_len * 4
+# bits of randomness.
+salt_len = 12
+
+def passwd(passphrase=None, algorithm='sha1'):
+ """Generate hashed password and salt for use in notebook configuration.
+
+ In the notebook configuration, set `c.NotebookApp.password` to
+ the generated string.
+
+ Parameters
+ ----------
+ passphrase : str
+ Password to hash. If unspecified, the user is asked to input
+ and verify a password.
+ algorithm : str
+ Hashing algorithm to use (e.g, 'sha1' or any argument supported
+ by :func:`hashlib.new`).
+
+ Returns
+ -------
+ hashed_passphrase : str
+ Hashed password, in the format 'hash_algorithm:salt:passphrase_hash'.
+
+ Examples
+ --------
+ In [1]: passwd('mypassword')
+ Out[1]: 'sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12'
+
+ """
+ if passphrase is None:
+ for i in range(3):
+ p0 = getpass.getpass('Enter password: ')
+ p1 = getpass.getpass('Verify password: ')
+ if p0 == p1:
+ passphrase = p0
+ break
+ else:
+ print('Passwords do not match.')
+ else:
+ raise UsageError('No matching passwords found. Giving up.')
+
+ h = hashlib.new(algorithm)
+ salt = ('%0' + str(salt_len) + 'x') % random.getrandbits(4 * salt_len)
+ h.update(passphrase + salt)
Thomas Kluyver Owner

It's just occurred to me that this is not going to be portable to Python 3, because you can't directly hash unicode. For that matter, it will fail on non-ascii unicode strings in Python 2 as well.

I think the best thing is to do is: py3compat.cast_bytes((passphrase + salt), 'utf-8').

I realise this has been merged - I'll try to get round to doing another PR, unless someone else beats me to it.

Fernando Perez Owner
fperez added a note

Sorry about that! We haven't gotten into a good swing of keeping py3 in mind, my fault...

BTW, if you see anything similarly problematic on #1012, let me know. It doesn't have similar low-level pieces so it shouldn't be much of a problem, but still, pitch in if you see anything amiss regarding py3.

Thomas Kluyver Owner

I'll have a check.

Stefan van der Walt
stefanv added a note

Mateusz just reminded me that allowing unicode for a password is probably a bad idea, so I think we'll try to detect that situation.

Thomas Kluyver Owner

My feeling is that if people want to use non-ascii characters in their password, that's up to them.

Stefan van der Walt
stefanv added a note

So how about just doing .encode() on the string? That should work in both 2.7 and 3.

Thomas Kluyver Owner

I've done essentially that in PR #1016.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+ return ':'.join((algorithm, salt, h.hexdigest()))
+
+def passwd_check(hashed_passphrase, passphrase):
Fernando Perez Owner
fperez added a note

BTW, it just occurred to me that we should probably add to this function a delay option, defaulting to 0.25 seconds at least... That will prevent DOS attacks and make brute-force rapid-fire guessing impossible. What do you guys think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ """Verify that a given passphrase matches its hashed version.
+
+ Parameters
+ ----------
+ hashed_passphrase : str
+ Hashed password, in the format returned by `passwd`.
+ passphrase : str
+ Passphrase to validate.
+
+ Returns
+ -------
+ valid : bool
+ True if the passphrase matches the hash.
+
+ Examples
+ --------
+ In [1]: from IPython.lib.security import passwd_check
+
+ In [2]: passwd_check('sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12',
+ ...: 'mypassword')
+ Out[2]: True
+
+ In [3]: passwd_check('sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12',
+ ...: 'anotherpassword')
+ Out[3]: False
+
+ """
+ try:
+ algorithm, salt, pw_digest = hashed_passphrase.split(':', 2)
+ except (ValueError, TypeError):
+ return False
+
+ try:
+ h = hashlib.new(algorithm)
+ except ValueError:
+ return False
+
+ if len(pw_digest) == 0 or len(salt) != salt_len:
Min RK Owner
minrk added a note

Checking the salt length seems entirely unnecessary here. It has no valuable effect, because shorter or longer salts would still be perfectly valid.

Fernando Perez Owner
fperez added a note

Good point. I won't revert it quite yet, let's see if @stefanv had a specific reason to check for that. But I can't think of one and unless Stefan has a good reason for wanting it there, we can remove the length check.

Stefan van der Walt
stefanv added a note
Fernando Perez Owner
fperez added a note

Fixed in 4cd1067

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ return False
+
+ h.update(passphrase + salt)
+
+ return h.hexdigest() == pw_digest
21 IPython/lib/tests/test_security.py
View
@@ -0,0 +1,21 @@
+from IPython.lib import passwd
+from IPython.lib.security import passwd_check, salt_len
+import nose.tools as nt
+
+def test_passwd_structure():
+ p = passwd('passphrase')
+ algorithm, salt, hashed = p.split(':')
+ nt.assert_equals(algorithm, 'sha1')
+ nt.assert_equals(len(salt), salt_len)
+ nt.assert_equals(len(hashed), 40)
+
+def test_roundtrip():
+ p = passwd('passphrase')
+ nt.assert_equals(passwd_check(p, 'passphrase'), True)
+
+def test_bad():
+ p = passwd('passphrase')
+ nt.assert_equals(passwd_check(p, p), False)
+ nt.assert_equals(passwd_check(p, 'a:b:c:d'), False)
+ nt.assert_equals(passwd_check(p, 'a:b'), False)
+
Something went wrong with that request. Please try again.