简体   繁体   English

Laravel 工艺参数转义

[英]Laravel process parameter escape

I'm running a process using Laravel which relies on Symfony Process component as follow.我正在使用 Laravel 运行一个进程,它依赖于 Symfony 进程组件,如下所示。

$process= new Process(['binary', $param1, $param2]);
$process->setTimeout(3600);
$process->run();

It works fine excepted when a parameter contains special characters which are interpreted by the shell.它可以正常工作,除非参数包含由 shell 解释的特殊字符。

When I run my process directly in shell I have the exact same issue.当我直接在 shell 中运行我的进程时,我遇到了完全相同的问题。

If I escape parameters by surrounding them with simple quotes it works well.如果我通过用简单的引号将参数括起来来转义参数,则效果很好。

So it seems that the issue comes from how the Process component escapes parameters.因此,问题似乎来自 Process 组件如何转义参数。

Since Symfony 5, the Process component doesn't accept strings as constructor parameter anymore自 Symfony 5 起,Process 组件不再接受字符串作为构造函数参数

So I can't escape parameters as follow所以我不能转义参数如下

new Process("binary '".$param1."' '".$param2."'");

From my opinion, the Process component should escape parameters correctly but it's obviously not the case.在我看来, Process 组件应该正确地转义参数,但显然不是这样。

Does anybody knows why special characters are not correctly escaped?有谁知道为什么特殊字符没有正确转义? How could I surround both username and password with simple quotes?如何用简单的引号括住用户名和密码?

I achieved my goal by using Process::fromShellCommandline我通过使用Process::fromShellCommandline实现了我的目标

I directly compose the command into a string and then pass the command with escaped parameters.我直接将命令组合成一个字符串,然后使用转义参数传递命令。

$command = "binary '".$param1."' '".$param2."'";
$process = Process::fromShellCommandline($command);
$process->setTimeout(3600);
$process->run();

Still don't know why special characters are not correctly escaped using a string array with new Process仍然不知道为什么使用带有new Process的字符串数组不能正确转义特殊字符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM