-
Notifications
You must be signed in to change notification settings - Fork 4
Fix db query injections #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -274,9 +274,9 @@ class Warn { | |
| */ | ||
| static last (target) { | ||
| const query = target | ||
| ? `SELECT * FROM warns WHERE id = (SELECT MAX(id) FROM warns WHERE target = ${target})` | ||
| ? `SELECT * FROM warns WHERE id = (SELECT MAX(id) FROM warns WHERE target = ?)` | ||
| : `SELECT * FROM warns WHERE id = (SELECT MAX(id) FROM warns)`; | ||
| const data = DB.query(query); | ||
| const data = DB.query(query, target ? [target] : undefined); | ||
|
Comment on lines
-277
to
+279
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это выглядит крайне сомнительно. По сути ты делаешь 2 проверки. const data = target
? DB.query('SELECT * FROM warns WHERE id = (SELECT MAX(id) FROM warns WHERE target = ?)', target)
: DB.query('SELECT * FROM warns WHERE id = (SELECT MAX(id) FROM warns)');И сам запрос выглядит как лютый ужас. По сути, твоя задача - получить последнюю добавленную строку. Хотя для твоей цели подойдёт такой запрос: SELECT * FROM warns WHERE target = ? ORDER BY id DESC LIMIT 1;
SELECT * FROM warns ORDER BY id DESC LIMIT 1;Тут ты сортируешь от максимального айдишника и лимитом забираешь лишь одну строку. Этот запрос будет в тысячи раз менее ресурсоёмким. |
||
| if (!data[0]) return undefined; | ||
|
|
||
| return new this(data[0]); | ||
|
|
@@ -290,9 +290,9 @@ class Warn { | |
| */ | ||
| static all (target) { | ||
| const query = target | ||
| ? `SELECT * FROM warns WHERE NOT flags & 4 AND target = ${target}` | ||
| ? `SELECT * FROM warns WHERE NOT flags & 4 AND target = ?` | ||
| : `SELECT * FROM warns WHERE NOT flags & 4`; | ||
| const data = DB.query(query); | ||
| const data = DB.query(query, target ? [target] : undefined); | ||
|
Comment on lines
-293
to
+295
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Та же история с двумя проверками и лишней константой |
||
|
|
||
| let warns = []; | ||
| for (let i = data.length; i >= 0; i--) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Гравис лучше заменять на обычные одинарные кавычки.
И зачем мы в конце имеем
[0]?