【发布时间】:2014-04-19 14:27:55
【问题描述】:
使用postgresql的sql,从其他文档中得知,为了防止sql注入攻击,需要过滤几个特殊的关键字,比如', ;, &, 。
quote(Value) when is_integer(Value)->
Value;
quote(Value) ->
%% seperate_by(["'",Value,"'"],"").
Value_a = lists:dropwhile(fun($')->true;
($;)->true;
($<)->true;
($>)->true;
($&)->true;
(_)->false
end,Value),
seperate_by(["'",Value_a,"'"],"").
(emacs@yus-iMac.local)62> john_worker:quote("<>&asdf'").
"'asdf''"
(emacs@yus-iMac.local)63> john_worker:quote("'asdf").
"'asdf''"
(emacs@yus-iMac.local)64> john_worker:quote("'asdf").
"'asdf'"
(emacs@yus-iMac.local)65> john_worker:quote("'asdf").
"'asdf'"
(emacs@yus-iMac.local)66> john_worker:quote("a'sdf").
"'a'sdf'"
(emacs@yus-iMac.local)67> john_worker:quote("a>sdf").
"'a>sdf'"
lists:filter 适用于以这些特殊字符为前缀的单词,但不适用于其他条件。为什么?
【问题讨论】:
-
lists:dropwhile将丢弃头部,直到谓词返回 false - 这与您的结果一致。我认为lists:filter是您想要的,但是您必须反转当前谓词的布尔返回值。
标签: erlang