Unsanitized user input in Access-Control-Allow-Origin

Description

Using unverified user-defined input to set the Access-Control-Allow-Origin header can result in unauthorized access to sensitive data. This vulnerability exposes your application to potential security risks by allowing attackers to specify origins that can access resources.

Remediations

  • Do not use user input to define the Access-Control-Allow-Origin header without validation. This practice can inadvertently grant access to sensitive information.
    response.headers['Access-Control-Allow-Origin'] = request.GET["my_origin"] # unsafe
  • Do validate user input if it must be used to set the Access-Control-Allow-Origin header. Ensure that only trusted origins are allowed by implementing a safelist of approved origins.
      allowed_origins = ['http://www.example.com', 'https://www.secure.example.com']

    user_origin = request.GET["my_origin"]

    if user_origin in allowed_origins:
    response.headers['Access-Control-Allow-Origin'] = user_origin

References

Associated CWE

OWASP Top 10

Configuration

To skip this rule during a scan, use the following flag

bearer scan /path/to/your-project/ --skip-rule=python_django_insecure_allow_origin

To run only this rule during a scan, use the following flag

bearer scan /path/to/your-project/ --only-rule=python_django_insecure_allow_origin