Built-in security mechanisms of Python frameworks

When conducting software development process audits, we often hear that functionality is implemented in the framework, and this may raise questions from security personnel.

Introduction

When auditing software development processes, we often hear that functionality is implemented in a framework, and this can raise questions from information security specialists.

Python, being one of the popular programming languages, offers many frameworks, each of which must be secure and have built-in security mechanisms or the ability to embed these mechanisms. In this article, we will try to understand what capabilities the frameworks actually provide, examine the security mechanisms, and ways to configure them using the example of common frameworks: Django, FastAPI, and Flask.

All these frameworks provide developers with powerful tools for creating secure applications, but their approaches and implementations differ.

1. Django Security

Django is a powerful and reliable web framework in Python that includes a wide range of security mechanisms that contribute to the creation of secure web applications. One of the key features of Django compared to other frameworks is its built-in security mechanisms, which provide a range of tools to protect against the most common attacks, such as CSRF, XSS, SQL injection, and others.

Built-in Security Mechanisms

CSRF (Cross-Site Request Forgery)

Django prevents CSRF attacks by using tokens that are checked with each data modification request. The CSRF token ensures that each request to the server comes from a trusted user and a trusted source. Django automatically adds the CSRF token to each form and checks this token when receiving a POST request.

XSS (Cross-Site Scripting)

Django protects against XSS by automatically escaping all variables output in templates. This prevents malicious scripts that could be inserted through input forms from being executed in the user's browser.

SQL Injection

Django uses ORM to generate all SQL queries to the database. This isolates developers from directly writing SQL code and protects against SQL injection, as all queries are built using parameterization and checked before execution.

Password Handling and Storage

By default, Django uses hashing and salting for passwords, making it extremely difficult to recover the original password from the hash. It is recommended to use Django's built-in classes, such as User and make_password, for working with passwords.

Authentication and Authorization

Django offers a powerful authentication and authorization system that makes it easy to manage users, groups, access rights, and fine-tune access to resources. Django provides built-in tools for user authentication, such as the User class and Authentication Middleware.

Clickjacking

Protection against clickjacking in Django is implemented through the X-Frame-Options mechanism, which allows sites to specify the circumstances under which content can be loaded in a frame. This prevents attacks where an attacker could mislead a user into clicking on a hidden frame.

2. Configuring Django Security Settings

Security settings in Django are configured through the settings.py file, which is the central place for application configuration. In this file, you can configure various parameters that will affect the security of your application.

Main security parameters

SECRET_KEY

  • Default value: Not set by default, must be unique.

  • Purpose: Used for cryptographic signing, important for the security of sessions and data associated with cookies.

  • Note: Never place SECRET_KEY in publicly accessible sources.

DEBUG

  • Default value: True

  • Purpose: When enabled, it displays detailed error information, which can be dangerous on a production server.

  • Note: Always set DEBUG = False on production servers.

ALLOWED_HOSTS

  • Default value: []

  • Purpose: Defines a list of strings representing the host/domain names that this site can serve.

  • Note: Configure this value according to the domains on which your application should run.

SECURE_BROWSER_XSS_FILTER

  • Default value: False

  • Purpose: Enables the XSS filter in the user's browser.

  • Note: It is recommended to set True to protect against XSS attacks.

X_FRAME_OPTIONS

  • Default value: DENY

  • Purpose: Controls the loading of the site inside the ,