我正在编写一个厨师配方,它调用mysql命令并执行查询。问题是,传递到该命令中的查询可以有回退,而我对此没有控制权。我要做这样的事。
execute "update_customer_segment_custom_condition_#{no_space_segment_name}" do action :run command "mysql --user=#{user} #{db_name} --execute="CALL someStoredProcedure(\"A String\", \"A QUERY CONTAINING BACKTICKS OVER WHICH I HAVE NO CONTROL THAT NEEDS TO BE PASSED VERBATIM\");" end
但是Bash总是抱怨语法,因为它试图解释回退和计算。我怎么能告诉它完全忽略那个角色呢?
FYI --这是在主厨execute块中作为command调用的,因此使用# for变量。
发布于 2015-01-06 00:59:47
您还可以在主厨中使用execute资源,而不是使用bash。这将不会使用bash,因此后退没有特殊的意义。
编辑:
要更具体地使用您发布的示例资源
execute "update_customer_segment_custom_condition_#{no_space_segment_name}" do
command ['mysql', "--user=#{user}", db_name, '--execute="CALL someStoredProcedure(\"A String\", \"A QUERY CONTAINING BACKTICKS OVER WHICH I HAVE NO CONTROL THAT NEEDS TO BE PASSED VERBATIM\");"']
end command的数组表单关闭了shell的任何处理。
发布于 2015-01-06 15:04:01
这可能是因为您用ruby构建了一个字符串(我不知道ruby),然后将它传递到shell中,执行操作运行。
当双引号以中间字符串结尾时,这已经有了问题:
命令"mysql --user=#{user} #{db_name} --execute=“调用someStoredProcedure(\"A String\",\”包含BACKTICKS的查询,我不需要逐字传递该查询“);
因此,我想知道ruby是否在这里没有给出一个语法错误的补充“在结尾。
您希望生成的字符串(可能)如下所示:
mysql --user=#{user} #{db_name} --execute="CALL someStoredProcedure(\"A String\", \"A QUERY\");"您需要这样生成它:
mysql --user=#{user} #{db_name} --execute='CALL someStoredProcedure("A String", "A QUERY");"使用"A QUERY“的部分,您需要将所有出现的'替换为'\'',否则仍然可以得到一个shell代码注入。
https://stackoverflow.com/questions/27790394
复制相似问题