国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

在 python 中使用字典更新 mysql 表?xiàng)l目的安全方法(防止 SQL 注入)
P粉627427202
P粉627427202 2023-09-08 20:06:07
0
1
844

我正在為我的 web 應(yīng)用程序編寫一個(gè)輔助函數(shù),該函數(shù)根據(jù)從外部 API(不是用戶輸入的)獲取的一些信息來更新數(shù)據(jù)庫。我有以下代碼,但它被 Bandit python 包標(biāo)記為“不安全”。

理想情況下,我可以以硬編碼要更新的列的方式編寫一個(gè)函數(shù),但我認(rèn)為它也應(yīng)該可以動(dòng)態(tài)實(shí)現(xiàn)。

這是更新表的安全方法(不可能進(jìn)行 SQL 注入)嗎?

import mysql.connector as database

def update_message_by_uid(uid: str, update_dict: dict) -> None:

    # Fetches the previous entry from the database using the unique identifier
    message_info_dict = get_message_by_uid(uid)

    # check that all the keys of the update dict are also in the original dict
    assert set(update_dict.keys()) <= set(
        message_info_dict.keys()
    ), "Some of the keys in the dictionary passed are not valid database columns"

    # We update the entry for all entries in the dictionary containing the updates
    statement = 'UPDATE messages SET {}  WHERE uid = %s'.format(", ".join('{}=%s'.format(k) for k in update_dict))


    # Concatenates the values of the dict with the unique identifier to pass it to the execution method as one variable
    data = list(update_dict.values()) + [uid]

    cursor.execute(statement, data)

P粉627427202
P粉627427202

全部回復(fù)(1)
P粉926174288

您應(yīng)該將列名放在反引號(hào)中,以防列名是 SQL 保留關(guān)鍵字 或包含空格、標(biāo)點(diǎn)符號(hào)或國(guó)際字符。還要確保列名稱中的反引號(hào)字符替換為兩個(gè)反引號(hào)。

assignments = ", ".join(f"`{k.replace('`', '``')}`=%s" for k in update_dict)
statement = f"UPDATE messages SET {assignments}  WHERE uid = %s"

我更喜歡使用 f 字符串而不是 format()。

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板