Skip to content

Commit

Permalink
fix (Transformation): use _escapeString sparingly in favour of parame…
Browse files Browse the repository at this point in the history
…terised query
  • Loading branch information
ryuwd committed Nov 18, 2024
1 parent 54e31f6 commit 895af75
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 25 deletions.
6 changes: 4 additions & 2 deletions src/DIRAC/Core/Utilities/MySQL.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,9 +752,11 @@ def _query(self, cmd, *, conn=None, debug=True):
return retDict

@captureOptimizerTraces
def _update(self, cmd, *, conn=None, debug=True):
def _update(self, cmd, *, args=None, conn=None, debug=True):
"""execute MySQL update command
:param args: parameters passed to cursor.execute(..., args=args) method.
:param conn: connection object.
:param debug: print or not the errors
return S_OK with number of updated registers upon success
Expand All @@ -772,7 +774,7 @@ def _update(self, cmd, *, conn=None, debug=True):

try:
cursor = connection.cursor()
res = cursor.execute(cmd)
res = cursor.execute(cmd, args=args)
retDict = S_OK(res)
if cursor.lastrowid:
retDict["lastRowId"] = cursor.lastrowid
Expand Down
29 changes: 6 additions & 23 deletions src/DIRAC/TransformationSystem/DB/TransformationDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,6 @@ def addTransformation(
return S_ERROR("Failed to parse the transformation body")
body = res["Value"]

escapable_columns = [
"TransformationName",
"Description",
"LongDescription",
"AuthorDN",
"AuthorGroup",
"Type",
"Plugin",
"AgentType",
"FileMask",
"Status",
"TransformationGroup",
]

params = {
"TransformationName": transName,
"Description": description,
Expand All @@ -196,16 +182,13 @@ def addTransformation(
"EventsPerTask": eventsPerTask,
}

for key in params:
if key in escapable_columns:
res = self._escapeString(params[key])
if not res["OK"]:
return S_ERROR(f"Could not escape parameter {key}")
params[key] = res["Value"]
# In an ideal world we would not do this at all, in favour of prepared statements or statement parameters.
req = f"INSERT INTO Transformations ({', '.join(params)}) VALUES ({', '.join(params.values())});"
# I'm erring on the side of caution by using the _escapeString(body) version of the Body parameter,
# but for everything else it seems reasonably safe to use the parameterised query feature
subst = ", ".join(f"%({name})s" if name != "Body" else body for name in params)

res = self._update(req, conn=connection)
req = f"INSERT INTO Transformations ({', '.join(params)}) VALUES ({subst});"

res = self._update(req, args=params, conn=connection)
if not res["OK"]:
self.lock.release()
return res
Expand Down

0 comments on commit 895af75

Please sign in to comment.