Skip to content
This repository was archived by the owner on May 28, 2022. It is now read-only.

Commit 8a6ab91

Browse files
Adds validation to create profile.
1 parent 07649ef commit 8a6ab91

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/freeseer/frontend/controller/configuration.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import signal
2525

2626
from flask import Blueprint, request
27+
import re
2728
from freeseer import settings, logging
2829
from freeseer.framework.config.exceptions import InvalidOptionValueError
2930
from freeseer.framework.config.profile import ProfileDoesNotExist, ProfileAlreadyExists
@@ -90,19 +91,19 @@ def map_plugin_category(plugin_type):
9091
return category
9192

9293

93-
def update_config(config, changes):
94+
def update_config(config, data):
9495
"""
95-
Helper to update Config instances with request data.
96+
Updates given Config instance with request data.
9697
"""
97-
for key, value in changes.items():
98+
for key, value in data.items():
9899
try:
99100
opt_instance = config.options[key]
100101
value = opt_instance.decode(value)
101102
setattr(config, key, value)
102103
except KeyError:
103104
raise HTTPError('Invalid Option: {}'.format(key), 400)
104105
except InvalidOptionValueError:
105-
raise HTTPError('Invalid Value: {}'.format(value), 400)
106+
raise HTTPError('Invalid Value {} for option {}'.format(value, key), 400)
106107
config.save()
107108

108109

@@ -172,7 +173,11 @@ def create_profile():
172173
"""
173174
Create new profile under 'name' specified in request arg.
174175
"""
176+
pattern = '^\w+$'
175177
profile_name = request.form['name']
178+
if not re.match(pattern, profile_name):
179+
raise HTTPError('Invalid Profile Name: {}'.format(profile_name), 400)
180+
176181
try:
177182
settings.profile_manager.create(profile_name)
178183
except ProfileAlreadyExists:
@@ -237,8 +242,6 @@ def modify_general_configuration(profile):
237242
changes = request.form
238243
update_config(profile_configuration, changes)
239244
return ''
240-
241-
242245
#
243246
# General Configuration Endpoints
244247
#
@@ -278,8 +281,6 @@ def modify_recording_configuration(profile):
278281
changes = request.form
279282
update_config(profile_configuration, changes)
280283
return ''
281-
282-
283284
#
284285
# End recording configuration endpoints.
285286
#
@@ -300,15 +301,15 @@ def list_plugin_category(profile, category):
300301
return {
301302
'plugins': [plugin.name for plugin in plugin_infos]
302303
}
303-
304-
305304
#
306305
# End of plugin category endpoints.
307306
#
308307

309308
#
310309
# Plugin endpoints.
311310
#
311+
312+
312313
@configuration.route('/profiles/<string:profile>/recording/<string:category>/<string:plugin>', methods=['GET'])
313314
@http_response(200)
314315
def list_plugin_instances(profile, category, plugin):
@@ -360,7 +361,6 @@ def modify_plugin_instance(profile, category, plugin, id):
360361
changes = request.form
361362
update_config(plugin_object.config, changes)
362363
return ''
363-
364364
#
365365
# End of plugin endpoints.
366366
#

src/freeseer/tests/frontend/controller/test_configuration.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ def test_create_profile(self, test_client):
7979
assert new_profile
8080

8181
def test_create_profile_invalid_args(self, test_client):
82-
response = test_client.post('/profiles')
82+
response = test_client.post('/profiles', data={'name': '$%@DN@'})
83+
data = json.loads(response.data)
8384
assert response.status_code == 400
85+
assert data['error_message'] == 'Invalid Profile Name: $%@DN@'
8486

8587
def test_create_profile_already_exists(self, test_client):
8688
response = test_client.post('/profiles',

0 commit comments

Comments
 (0)