1
0
mirror of https://github.com/ansible/awx.git synced 2024-10-31 23:51:09 +03:00

Disallow posting dictionary as credentials to JT launch API endpoint.

This commit is contained in:
Yunfan Zhang 2018-06-20 13:05:47 -04:00
parent 1b5ea07745
commit 9fec43c643
2 changed files with 27 additions and 0 deletions

View File

@ -29,6 +29,7 @@ from django.utils.functional import cached_property
# Django REST Framework
from rest_framework.exceptions import ValidationError, PermissionDenied
from rest_framework.relations import ManyRelatedField
from rest_framework import fields
from rest_framework import serializers
from rest_framework import validators
@ -277,6 +278,16 @@ class BaseSerializer(serializers.ModelSerializer):
created = serializers.SerializerMethodField()
modified = serializers.SerializerMethodField()
def __init__(self, *args, **kwargs):
super(BaseSerializer, self).__init__(*args, **kwargs)
# The following lines fix the problem of being able to pass JSON dict into PrimaryKeyRelatedField.
data = kwargs.get('data', False)
if data:
for field_name, field_instance in six.iteritems(self.fields):
if isinstance(field_instance, ManyRelatedField) and not field_instance.read_only:
if isinstance(data.get(field_name, False), dict):
raise serializers.ValidationError(_('Cannot use dictionary for %s' % field_name))
@property
def version(self):
"""

View File

@ -0,0 +1,16 @@
# Python
import pytest
# Django Rest Framework
from rest_framework.exceptions import ValidationError
# AWX
from awx.api.serializers import JobLaunchSerializer
def test_primary_key_related_field():
# We are testing if the PrimaryKeyRelatedField in this serializer can take dictionary.
# PrimaryKeyRelatedField should not be able to take dictionary as input, and should raise a ValidationError.
data = {'credentials' : {'1': '2', '3':'4'}}
with pytest.raises(ValidationError):
JobLaunchSerializer(data=data)