Skip to content
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

[IMP] Prevent SQL Injections #27

Merged
merged 1 commit into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ __pycache__/

# Other
*.out

# DBs
*.db
18 changes: 9 additions & 9 deletions rldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def _generate_reactionrole_id(self):
while True:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, this is not the proper way to proceed, that why we use Primary Keys

self.reactionrole_id = randint(0, 100000)
db.execute(
f"SELECT * FROM messages WHERE reactionrole_id = '{self.reactionrole_id}'"
"SELECT * FROM messages WHERE reactionrole_id = ?", (self.reactionrole_id,)
)
already_exists = db.fetchall()
if already_exists:
Expand All @@ -43,12 +43,12 @@ def _generate_reactionrole_id(self):

def commit(self):
db.execute(
f"INSERT INTO 'messages' ('message_id', 'channel', 'reactionrole_id') values('{self.message_id}', '{self.target_channel}', '{self.reactionrole_id}');"
"INSERT INTO 'messages' ('message_id', 'channel', 'reactionrole_id') values(?, ?, ?);", (self.message_id, self.target_channel, self.reactionrole_id)
)
for reaction in self.combos:
role_id = self.combos[reaction]
db.execute(
f"INSERT INTO 'reactionroles' ('reactionrole_id', 'reaction', 'role_id') values('{self.reactionrole_id}', '{reaction}', '{role_id}');"
"INSERT INTO 'reactionroles' ('reactionrole_id', 'reaction', 'role_id') values(?, ?, ?);", (self.reactionrole_id, reaction, role_id)
)
database.commit()

Expand Down Expand Up @@ -112,18 +112,18 @@ def end_creation(user, channel, message_id):


def exists(message_id):
db.execute(f"SELECT * FROM messages WHERE message_id = '{message_id}';")
db.execute("SELECT * FROM messages WHERE message_id = ?;", (message_id,))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the commas after, for example, message_id serve a purpose or can I delete them? I do not see examples in the sqlite3 library using trailing commas.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's for forcing tuples

result = db.fetchall()
return result


def get_reactions(message_id):
db.execute(
f"SELECT reactionrole_id FROM messages WHERE message_id = '{message_id}';"
"SELECT reactionrole_id FROM messages WHERE message_id = ?;", (message_id,)
)
reactionrole_id = db.fetchall()[0][0]
db.execute(
f"SELECT reaction, role_id FROM reactionroles WHERE reactionrole_id = '{reactionrole_id}';"
"SELECT reaction, role_id FROM reactionroles WHERE reactionrole_id = ?;", (reactionrole_id,)
)
combos = {}
for row in db:
Expand All @@ -134,7 +134,7 @@ def get_reactions(message_id):


def fetch_messages(channel):
db.execute(f"SELECT message_id FROM messages WHERE channel = '{channel}';")
db.execute("SELECT message_id FROM messages WHERE channel = ?;", (channel,))
all_messages = []
for row in db:
message_id = int(row[0])
Expand All @@ -143,12 +143,12 @@ def fetch_messages(channel):


def add_admin(role):
db.execute(f"INSERT INTO 'admins' ('role_id') values('{role}');")
db.execute(f"INSERT INTO 'admins' ('role_id') values(?);", (role,))
database.commit()


def remove_admin(role):
db.execute(f"DELETE FROM admins WHERE role_id = '{role}';")
db.execute(f"DELETE FROM admins WHERE role_id = ?;", (role,))
database.commit()


Expand Down