Skip to content

SQL statements use Python-formatted strings

High
DogukanUrker published GHSA-66hm-3w7j-jmch Jun 17, 2023

Package

flaskBlog (Python)

Affected versions

Master branch

Patched versions

None

Description

Summary

SQL statements throughout the application use Python-native format strings rather than sqlite3 formatting strings. Note how parameters on the documents page make use of ? within SQL queries rather than formatted strings: https://docs.python.org/3/library/sqlite3.html

Details

Unsafe example from createPost.py:

cursor.execute(
    f"""
    insert into posts(title,tags,content,author,views,date,time,lastEditDate,lastEditTime) 
    values("{postTitle}","{postTags}","{postContent}",
    "{session["userName"]}",0,
    "{currentDate()}",
    "{currentTime()}",
    "{currentDate()}",
    "{currentTime()}")
    """
)

Remediated example:

cursor.execute(
"insert into posts(title,tags,content,author,views,date,time,lastEditDate,lastEditTime) \
    values(?, ?, ?, ?, ?, ?, ?, ?, ?)", 
    (postTitle, postTags, postContent, 
        session["userName"], 0, 
        currentDate(), 
        currentTime(), 
        currentDate(),
        currentTime()))

PoC

For the post creation example above, create a post containing the content " + (SELECT( 1 || 2 || 3 || 4 || 5)) + ", as shown below.

Screenshot 2023-06-16 at 5 06 24 PM

When the post is created, notice how 1 || 2 || 3 || 4 || 5 has been concatenated to 12345.

Screenshot 2023-06-16 at 5 07 06 PM

Impact

A low privilege user can execute commands as the underlying sqlite3 server.

Severity

High

CVE ID

No known CVE

Weaknesses

No CWEs

Credits