Skip to content

Commit 4692bda

Browse files
author
Matthew Fisher
committed
fix(controller): validate key material on upload
1 parent 2359596 commit 4692bda

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

controller/api/models.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66

77
from __future__ import unicode_literals
8+
import base64
89
import etcd
910
import importlib
1011
import logging
@@ -44,6 +45,14 @@ def log_event(app, msg, level=logging.INFO):
4445
app.log(msg) # local filesystem
4546

4647

48+
def validate_base64(value):
49+
"""Check that value contains only valid base64 characters."""
50+
try:
51+
base64.b64decode(value.split()[1])
52+
except Exception as e:
53+
raise ValidationError(e)
54+
55+
4756
def validate_id_is_docker_compatible(value):
4857
"""
4958
Check that the ID follows docker's image name constraints
@@ -825,7 +834,7 @@ class Key(UuidAuditedModel):
825834

826835
owner = models.ForeignKey(settings.AUTH_USER_MODEL)
827836
id = models.CharField(max_length=128)
828-
public = models.TextField(unique=True)
837+
public = models.TextField(unique=True, validators=[validate_base64])
829838

830839
class Meta:
831840
verbose_name = 'SSH Key'

controller/api/tests/test_key.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# -*- coding: utf-8 -*-
12
"""
23
Unit tests for the Deis api app.
34
@@ -31,6 +32,10 @@
3132
"+MQnrrza0jaQ3QUAQ= autotest@autotesting comment"
3233
)
3334

35+
BAD_KEY = (
36+
"ssh-rsa foo_bar"
37+
)
38+
3439

3540
class KeyTest(TestCase):
3641

@@ -63,12 +68,27 @@ def _check_key(self, pubkey):
6368
response = self.client.delete(url, HTTP_AUTHORIZATION='token {}'.format(self.token))
6469
self.assertEqual(response.status_code, 204)
6570

71+
def _check_bad_key(self, pubkey):
72+
"""
73+
Test that a user cannot add invalid SSH public keys
74+
"""
75+
url = '/v1/keys'
76+
body = {'id': 'mykey@box.local', 'public': pubkey}
77+
response = self.client.post(url, json.dumps(body), content_type='application/json',
78+
HTTP_AUTHORIZATION='token {}'.format(self.token))
79+
self.assertEqual(response.status_code, 400)
80+
return response
81+
6682
def test_rsa_key(self):
6783
self._check_key(RSA_PUBKEY)
6884

6985
def test_ecdsa_key(self):
7086
self._check_key(ECDSA_PUBKEY)
7187

88+
def test_bad_key(self):
89+
response = self._check_bad_key(BAD_KEY)
90+
self.assertEqual(response.data, {'public': ['Incorrect padding']})
91+
7292
def _check_duplicate_key(self, pubkey):
7393
"""
7494
Test that a user cannot add a duplicate key

0 commit comments

Comments
 (0)