1

I have implemented user registration in Django Rest Framework but I don't know how to check for sql injection and etc.
For example a password like this: "< script>"

class UserRegisterSerializer(serializers.ModelSerializer):

def validate(self, data):
    password = data['password']
    password2 = data['password2']
    data.pop('password2')

    if password != password2:
        raise serializers.ValidationError({"password": "Passwords must match."})

    errors = dict()
    try:
        # validate the password and catch the exception
        validators.validate_password(password)

    # the exception raised here is different than serializers.ValidationError
    except exceptions.ValidationError as e:
        errors['password'] = list(e.messages)

    if errors:
        raise serializers.ValidationError(errors)

    return data

This code is in my settings:

AUTH_PASSWORD_VALIDATORS = [
{
    'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
    'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', #=> Default (8 characters)
    'OPTIONS': {
        'min_length': 4,
    }
},
{
    'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
    'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
1

1 Answer 1

0

From Django official security docs. Security in Django

Django’s querysets are protected from SQL injection since their queries are constructed using query parameterization. A query’s SQL code is defined separately from the query’s parameters. Since parameters may be user-provided and therefore unsafe, they are escaped by the underlying database driver.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.