github-actions[bot] a15c1733db
Create rule S6547(C#): Environment variables should not be defined from untrusted input (#2875)
You can preview this rule
[here](https://sonarsource.github.io/rspec/#/rspec/S6547/csharp)
(updated a few minutes after each push).

## Review

A dedicated reviewer checked the rule description successfully for:

- [ ] logical errors and incorrect information
- [ ] information gaps and missing content
- [ ] text style and tone
- [ ] PR summary and labels follow [the
guidelines](https://github.com/SonarSource/rspec/#to-modify-an-existing-rule)

---------

Co-authored-by: daniel-teuchert-sonarsource <daniel-teuchert-sonarsource@users.noreply.github.com>
Co-authored-by: Daniel Teuchert <daniel.teuchert@sonarsource.com>
Co-authored-by: daniel-teuchert-sonarsource <141642369+daniel-teuchert-sonarsource@users.noreply.github.com>
Co-authored-by: Loris S. <91723853+loris-s-sonarsource@users.noreply.github.com>
2023-08-17 12:58:46 +02:00

48 lines
1.1 KiB
Plaintext

== How to fix it in .NET
=== Code examples
include::../../common/fix/code-rationale.adoc[]
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
----
using System.Diagnostics;
public class ExampleController : Controller
{
public void Example(string name, string value)
{
Process proc = new Process();
proc.StartInfo.FileName = "path/to/executable";
proc.StartInfo.EnvironmentVariables.Add(name, value); // Noncompliant
proc.Start();
}
}
----
==== Compliant solution
[source,csharp,diff-id=1,diff-type=compliant]
----
using System.Diagnostics;
using System.Text.RegularExpressions;
public class ExampleController : Controller
{
public void Example(string value)
{
Process proc = new Process();
proc.StartInfo.FileName = "path/to/executable";
string pattern = "^[a-zA-Z0-9]*$";
Match m = Regex.Match(value, pattern);
if (m.Success) {
proc.StartInfo.EnvironmentVariables.Add("ENV_VAR", value);
}
proc.Start();
}
}
----
include::../../common/fix/how-does-this-work.adoc[]