Skip to content

Commit

Permalink
fix sql injection vulnerability in get_song_relations 😅
Browse files Browse the repository at this point in the history
  • Loading branch information
ridhoq committed Mar 18, 2017
1 parent d45460e commit 90bb4fb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/api/songs.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ def new_song():

@api.route('/songs/<int:id>/related')
def get_song_relations(id):
top = request.args.get('top')
top_str = request.args.get('top')
if not top_str.isdigit() or not int(top_str) > 0:
message = 'top query param must be an int greater than 0'
return bad_request(message)
top = int(request.args.get('top'))
song = Song.query.filter_by(id=id).first()
if not song:
return route_not_found(song)
Expand Down
19 changes: 19 additions & 0 deletions test/api/test_song_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,22 @@ def test_song_relation_get_related_songs(self):
assert len(res.json) == 1
assert res.json[0]['id'] == self.songs['edm'][1]['id']

def test_get_song_relations_top_query_param(self):
song_id = self.songs['edm'][0]['id']
res = self.client.get(url_for('api.get_song_relations', id=song_id, top=0),
content_type='application/json')
assert res.status_code == 400
assert res.json['error'] == 'bad request'
assert res.json['message'] == 'top query param must be an int greater than 0'

res = self.client.get(url_for('api.get_song_relations', id=song_id, top=-1),
content_type='application/json')
assert res.status_code == 400
assert res.json['error'] == 'bad request'
assert res.json['message'] == 'top query param must be an int greater than 0'

res = self.client.get(url_for('api.get_song_relations', id=song_id, top='10; select * from users'),
content_type='application/json')
assert res.status_code == 400
assert res.json['error'] == 'bad request'
assert res.json['message'] == 'top query param must be an int greater than 0'

0 comments on commit 90bb4fb

Please sign in to comment.