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

chore: dbconn - add requestId info as a comment in the database logs #3110

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Changes from 2 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
34 changes: 32 additions & 2 deletions waku/common/databases/db_postgres/dbconn.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import
std/[times, strutils, asyncnet, os, sequtils, sets],
std/[times, strutils, asyncnet, os, sequtils, sets, strformat],
results,
chronos,
chronos/threadsync,
Expand Down Expand Up @@ -207,13 +207,42 @@ proc waitQueryToFinish(

pqclear(pqResult)

proc containsRiskyPatterns(input: string): bool =
Copy link
Contributor

Choose a reason for hiding this comment

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

Really awesome! I think next time we need to generalize this to apply wherever we think needed. WDYT?
Just an idea, usually what SQL inject attacker use is to trick the comments inside an SQL string, so that must be banned first (sometimes it is just enough to filter out threats).

let riskyPatterns =
@[
" OR ", " AND ", " UNION ", " SELECT ", "INSERT ", "DELETE ", "UPDATE ", "DROP ",
"EXEC ", "--", "/*", "*/",
]

for pattern in riskyPatterns:
if pattern.toLowerAscii() in input.toLowerAscii():
return true

return false

proc isSecureString(input: string): bool =
## Returns `false` if the string contains risky characters or patterns, `true` otherwise.
let riskyChars = {'\'', '\"', ';', '-', '#', '\\', '%', '_', '/', '*', '\0'}

for ch in input:
if ch in riskyChars:
return false

if containsRiskyPatterns(input):
return false

return true

proc dbConnQuery*(
dbConnWrapper: DbConnWrapper,
query: SqlQuery,
args: seq[string],
rowCallback: DataProc,
requestId: string,
): Future[Result[void, string]] {.async, gcsafe.} =
if not requestId.isSecureString():
return err("the passed request id is not secure: " & requestId)

dbConnWrapper.futBecomeFree = newFuture[void]("dbConnQuery")

let cleanedQuery = ($query).replace(" ", "").replace("\n", "")
Expand All @@ -224,7 +253,8 @@ proc dbConnQuery*(

var queryStartTime = getTime().toUnixFloat()

(await dbConnWrapper.sendQuery(query, args)).isOkOr:
let reqIdAndQuery = "/* requestId=" & requestId & " */ " & $query
(await dbConnWrapper.sendQuery(SqlQuery(reqIdAndQuery), args)).isOkOr:
error "error in dbConnQuery", error = $error
dbConnWrapper.futBecomeFree.fail(newException(ValueError, $error))
return err("error in dbConnQuery calling sendQuery: " & $error)
Expand Down
Loading