From f8d59053aaa1576773b94235d4c09c3e0339a181 Mon Sep 17 00:00:00 2001 From: Patrick Uiterwijk Date: Sep 30 2016 13:22:38 +0000 Subject: Add a validator to check ssh keys on edit Signed-off-by: Patrick Uiterwijk --- diff --git a/pagure/forms.py b/pagure/forms.py index 219e425..344d74c 100644 --- a/pagure/forms.py +++ b/pagure/forms.py @@ -20,6 +20,7 @@ import wtforms import tempfile import pagure +import pagure.lib STRICT_REGEX = '^[a-zA-Z0-9-_]+$' @@ -68,6 +69,11 @@ def file_virus_validator(form, field): raise wtforms.ValidationError('Error scanning uploaded file') +def ssh_key_validator(form, field): + if not pagure.lib.are_valid_ssh_keys(field.data): + raise wtforms.ValidationError('Invalid SSH keys') + + class ProjectFormSimplified(wtf.Form): ''' Form to edit the description of a project. ''' description = wtforms.TextField( @@ -336,7 +342,8 @@ class UserSettingsForm(wtf.Form): ''' Form to create or edit project. ''' ssh_key = wtforms.TextAreaField( 'Public SSH key *', - [wtforms.validators.Required()] + [wtforms.validators.Required(), + ssh_key_validator] ) diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index 0364bc5..57df7a1 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -194,6 +194,11 @@ def is_valid_ssh_key(key): return proc.returncode == 0 +def are_valid_ssh_keys(keys): + return all([is_valid_ssh_key(key) is not False + for key in keys.split('\n')]) + + def create_user_ssh_keys_on_disk(user, gitolite_keydir): ''' Create the ssh keys for the user on the specific folder.