Skip to content

Commit ba55bed

Browse files
committed
More general approach for PostgreSQL concatenation operator precedence problem (Issue sqlmapproject#219)
1 parent afd82b9 commit ba55bed

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

plugins/dbms/postgresql/syntax.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ def __init__(self):
1414

1515
@staticmethod
1616
def unescape(expression, quote=True):
17+
"""
18+
Note: PostgreSQL has a general problem with concenation operator (||) precedence (hence the parentheses enclosing)
19+
e.g. SELECT 1 WHERE 'a'!='a'||'b' will trigger error ("argument of WHERE must be type boolean, not type text")
20+
"""
21+
1722
if quote:
1823
while True:
1924
index = expression.find("'")
@@ -28,11 +33,11 @@ def unescape(expression, quote=True):
2833

2934
lastIndex = firstIndex + index
3035
old = "'%s'" % expression[firstIndex:lastIndex]
31-
unescaped = "||".join("CHR(%d)" % (ord(expression[i])) for i in xrange(firstIndex, lastIndex)) # Postgres CHR() function already accepts Unicode code point of character(s)
36+
unescaped = "(%s)" % "||".join("CHR(%d)" % (ord(expression[i])) for i in xrange(firstIndex, lastIndex)) # Postgres CHR() function already accepts Unicode code point of character(s)
3237

3338
expression = expression.replace(old, unescaped)
3439
else:
35-
expression = "||".join("CHR(%d)" % ord(c) for c in expression)
40+
expression = "(%s)" % "||".join("CHR(%d)" % ord(c) for c in expression)
3641

3742
return expression
3843

plugins/generic/databases.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def getTables(self, bruteForce=None):
256256
if condition:
257257
if conf.excludeSysDbs:
258258
query += " WHERE "
259-
query += " AND ".join("%s != ('%s')" % (condition, unsafeSQLIdentificatorNaming(db)) for db in self.excludeDbsList)
259+
query += " AND ".join("%s != '%s'" % (condition, unsafeSQLIdentificatorNaming(db)) for db in self.excludeDbsList)
260260
infoMsg = "skipping system database%s '%s'" % ("s" if len(self.excludeDbsList) > 1 else "", ", ".join(db for db in self.excludeDbsList))
261261
logger.info(infoMsg)
262262
elif not Backend.isDbms(DBMS.SQLITE):

0 commit comments

Comments
 (0)