Do not use text() as it leads to SQL injection

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

Metadata

ID: python-flask/disable-sqlalchemy-text

Language: Python

Severity: Warning

Category: Security

CWE: 89

Description

The text function from SQLAlchemy lets you build custom SQL statements. It is recommended to use the ORM functions to build queries and avoid building custom queries, which are vulnerable to SQL injections.

Learn More

Non-Compliant Code Examples

from sqlalchemy.sql import text


con = engine.connect()

data = ( { "id": 1, "title": "The Hobbit", "primary_author": "Tolkien" },
            { "id": 2, "title": "The Silmarillion", "primary_author": "Tolkien" },
)

statement = text("""INSERT INTO book(id, title, primary_author) VALUES(:id, :title, :primary_author)""")

for line in data:
    con.execute(statement, **line)

Compliant Code Examples

con = engine.connect()

data = ( { "id": 1, "title": "The Hobbit", "primary_author": "Tolkien" },
            { "id": 2, "title": "The Silmarillion", "primary_author": "Tolkien" },
)

statement = text("""INSERT INTO book(id, title, primary_author) VALUES(:id, :title, :primary_author)""")

for line in data:
    con.execute(statement, **line)
from sqlalchemy import text

BOOKS = meta.tables['books']
query = sqlalchemy.select(BOOKS).where(BOOKS.c.genre == 'fiction')
result = engine.execute(query).fetchall()
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis

PREVIEWING: may/unit-testing