日記
trac拡張のセキュリティ 2Edit

trac拡張のセキュリティで疑問に思った件については、TracDev/DatabaseApiにその辺の話が書いてあった。

At any cost, try avoiding the use of string formatting to get values into the SQL statement. The database automatically escapes values you pass using execute() arguments, the same is not true if you use string formatting. If you absolutely cannot avoid it, be sure to apply the sql_escape function in trac.util to all parameters you're passing in, to avoid possible SQL injection attacks:

具体的には、

cursor.execute("SELECT author,ipnr,comment FROM wiki WHERE name=%s", [thename])

だとDBライブラリが正しくエスケープしてくれるけど、自前で%演算子とか使って結合するんだったら、明示的にsql_escape関数を使ってエスケープしなきゃダメよ、という話。

だから、前に例に出したBlogマクロだったら、

*** Blog.py.org 2006-02-16 09:15:06.000000000 +0900
--- Blog.py     2006-02-16 09:16:54.053072208 +0900
***************
*** 49,57 ****
for blogPattern in args.split(','):
blogPattern = blogPattern.strip()
!         sql = "SELECT DISTINCT name from wiki where name like '%s' order by time desc" % blogPattern
csName = db.cursor()   # the matched wiki name
!         csName.execute(sql)
while 1:
rowName = csName.fetchone()
if rowName == None:
--- 49,57 ----
for blogPattern in args.split(','):
blogPattern = blogPattern.strip()
!         sql = "SELECT DISTINCT name from wiki where name like %s order by time desc"
csName = db.cursor()   # the matched wiki name
!         csName.execute(sql, [blogPattern])
while 1:
rowName = csName.fetchone()
if rowName == None:

みたいにした方がいいってことだね。まあコード全部見てないんで、この部分以外がどうなのかは知らないけど。

Published At2006-02-16 00:00Updated At2006-02-16 00:00