今天突然想起這個問題,而我也在為自己的手機 app 寫一個 backend 及想有一個自己 customize 的 query 結果。翻查 sequelize 的 doc 這件事沒有比寫 raw query 簡單,所以就開始自己寫 raw sequel。嘗試 sql inject 自己一下。發現如果沒有做任何預防操施真的很危險,甚至把我整個 database 毀掉:
所以開始學習寫 prepare statement:
PREPARE get_notes (int) AS SELECT v."id", v."word", v."pronounciation", v."explanation", p."dateTime", p."croppedScreenshot" FROM vocabs v INNER JOIN pages p ON v."sqlitePageId"=p."sqliteId" WHERE p."sqliteNoteId"=$1; EXECUTE get_notes(${sqliteNoteId});在 postman get request 了一次,一切都很美好,再 get request 多一次,誒?
error: prepared statement "get_notes" already exists搜尋了一下解決方法,最後只要每一次完成 EXECUTE 後把儲存好的 prepared statement 移除就好,整句變成:
PREPARE get_notes (int) AS SELECT v."id", v."word", v."pronounciation", v."explanation", p."dateTime", p."croppedScreenshot" FROM vocabs v INNER JOIN pages p ON v."sqlitePageId"=p."sqliteId" WHERE p."sqliteNoteId"=$1; EXECUTE get_notes(${sqliteNoteId}); DEALLOCATE get_notes;
No comments:
Post a Comment