Fred Tingaud 51369b610e
Make sure that includes are always surrounded by empty lines (#2270)
When an include is not surrounded by empty lines, its content is inlined
on the same line as the adjacent content. That can lead to broken tags
and other display issues.
This PR fixes all such includes and introduces a validation step that
forbids introducing the same problem again.
2023-06-22 10:38:01 +02:00

62 lines
1.8 KiB
Plaintext

== How to fix it in Symfony
=== Code examples
include::../../common/fix/code-rationale.adoc[]
==== Noncompliant code example
[source,php,diff-id=1,diff-type=noncompliant]
----
public function unsafe(Request $request): JsonResponse
{
$process = Process::fromShellCommandline($request->query->get("cmd")); // Noncompliant
$process->run();
$code = $process->wait();
return $this->json($code == 0);
}
----
==== Compliant solution
[source,php,diff-id=1,diff-type=compliant]
----
public function safe(Request $request): JsonResponse
{
$allowedCommands = [["/bin/ping","-c","1","--"],["/usr/bin/host","--"]];
$cmd = $allowedCommands[$request->query->get("cmdId")];
$cmd[] = $request->query->get("host");
$process = new Process($cmd);
$process->run();
$code = $process->wait();
return $this->json($code == 0);
}
----
=== How does this work?
include::../../common/fix/introduction.adoc[]
include::../../common/fix/pre-approved-list.adoc[]
In the example compliant code, a static list of allowed commands is used.
Users are only allowed to provide a command index that will be used to access
this list. The command resulting from the list access can be considered
trusted.
:sanitizationLib: Symfony\Component\Process\Process
include::../../common/fix/sanitize-meta-characters.adoc[]
In the example compliant code, the `Process` constructor is used in place of
the less safe `fromShellCommandline` function. It accepts a list of command
arguments that will be properly escaped and concatenated to form the command
line to execute.
include::../../common/fix/shell_integration.adoc[]
In the example compliant code, using the `Process` constructor is preferred
over the less safe `fromShellCommandline` `Process` factory. This way of
creating `Process` instances disables shell integration by default.