Skip to content
This repository has been archived by the owner on Jul 4, 2022. It is now read-only.

Commit

Permalink
Validate device handle format.
Browse files Browse the repository at this point in the history
  • Loading branch information
dainnilsson committed Apr 7, 2017
1 parent f6c5f12 commit ee9f873
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
17 changes: 17 additions & 0 deletions test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,23 @@ def test_get_device_descriptor_and_cert(self):
).data, default_backend())
self.assertEqual(CERT, cert.public_bytes(Encoding.DER))

def test_get_invalid_device(self):
resp = self.app.get('/foouser/' + ('ab' * 16),
environ_base={'REMOTE_USER': 'fooclient'}
)
self.assertEqual(resp.status_code, 404)

self.do_register(SoftU2FDevice())
resp = self.app.get('/foouser/' + ('ab' * 16),
environ_base={'REMOTE_USER': 'fooclient'}
)
self.assertEqual(resp.status_code, 404)

resp = self.app.get('/foouser/InvalidHandle',
environ_base={'REMOTE_USER': 'fooclient'}
)
self.assertEqual(resp.status_code, 400)

def test_delete_user(self):
self.do_register(SoftU2FDevice())
self.do_register(SoftU2FDevice())
Expand Down
13 changes: 9 additions & 4 deletions u2fval/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from six.moves.urllib.parse import unquote
import json
import os
import re


if app.config['USE_MEMCACHED']:
Expand Down Expand Up @@ -327,15 +328,19 @@ def sign(user_id):
return jsonify(_sign_request(user_id, challenge, handles, properties))


_HANDLE_PATTERN = re.compile(r'^[a-f0-9]{32}$')


@app.route('/<user_id>/<handle>', methods=['GET', 'POST', 'DELETE'])
def device(user_id, handle):
if _HANDLE_PATTERN.match(handle) is None:
raise exc.BadInputException('Invalid device handle: ' + handle)

user = get_user(user_id)
if user is None:
raise exc.NotFoundException('Device not found')
try:
dev = user.devices[handle]
except KeyError:
raise exc.BadInputException('Invalid device handle: ' + handle)
except (AttributeError, KeyError):
raise exc.NotFoundException('Device not found')

if request.method == 'DELETE':
if dev is not None:
Expand Down

0 comments on commit ee9f873

Please sign in to comment.