Modify rule S2083[C#]: Educational content (APPSEC-49) (#1121)
This commit is contained in:
parent
9d944403b4
commit
dbca6ec12c
@ -1,11 +1,13 @@
|
|||||||
// C#
|
// C#
|
||||||
* ASP.NET
|
* ASP.NET
|
||||||
* Razor
|
* Razor
|
||||||
|
* .NET
|
||||||
// Java
|
// Java
|
||||||
* JSP
|
* JSP
|
||||||
* Servlet
|
* Servlet
|
||||||
* Spring
|
* Spring
|
||||||
* Thymeleaf
|
* Thymeleaf
|
||||||
|
* Java SE
|
||||||
// JS
|
// JS
|
||||||
* Express.js
|
* Express.js
|
||||||
// PHP
|
// PHP
|
||||||
|
1
rules/S2083/common/fix/code-rationale.adoc
Normal file
1
rules/S2083/common/fix/code-rationale.adoc
Normal file
@ -0,0 +1 @@
|
|||||||
|
The following code is vulnerable to path injection as it is constructing a path using untrusted data. This path is then used to delete a file without being validated first. Therefore, it can be leveraged by an attacker to delete arbitrary files.
|
6
rules/S2083/common/fix/how-does-this-work.adoc
Normal file
6
rules/S2083/common/fix/how-does-this-work.adoc
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
The universal way to prevent path injection is to validate paths constructed from untrusted data.
|
||||||
|
|
||||||
|
The validation should be done as follow:
|
||||||
|
|
||||||
|
1. Resolve the canonical path of the file by using methods like `{canonicalization_function}`. This will resolve relative path or path components like `../` and removes any ambiguity regarding the file's location.
|
||||||
|
2. Check that the canonical path is within the directory where the file should be located.
|
47
rules/S2083/csharp/how-to-fix-it/dotnet.adoc
Normal file
47
rules/S2083/csharp/how-to-fix-it/dotnet.adoc
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
=== How to fix it in .NET
|
||||||
|
|
||||||
|
:canonicalization_function: System.IO.Path.GetFullPath
|
||||||
|
include::../../common/fix/code-rationale.adoc[]
|
||||||
|
|
||||||
|
[cols="a"]
|
||||||
|
|===
|
||||||
|
h| Non-compliant code example
|
||||||
|
|
|
||||||
|
[source,csharp]
|
||||||
|
----
|
||||||
|
public class ExampleController : Controller
|
||||||
|
{
|
||||||
|
private static string TargetDirectory;
|
||||||
|
|
||||||
|
public void Example(string filename)
|
||||||
|
{
|
||||||
|
string path = Path.Combine(TargetDirectory, filename);
|
||||||
|
System.IO.File.Delete(path); // Noncompliant
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----
|
||||||
|
h| Compliant solution
|
||||||
|
|
|
||||||
|
[source,csharp]
|
||||||
|
----
|
||||||
|
public class ExampleController : Controller
|
||||||
|
{
|
||||||
|
private static string TargetDirectory;
|
||||||
|
|
||||||
|
public void Example(string filename)
|
||||||
|
{
|
||||||
|
string path = Path.Combine(TargetDirectory, filename);
|
||||||
|
string canonicalDestinationPath = Path.GetFullPath(path);
|
||||||
|
|
||||||
|
if (canonicalDestinationPath.StartsWith(TargetDirectory, StringComparison.Ordinal))
|
||||||
|
{
|
||||||
|
System.IO.File.Delete(canonicalDestinationPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|===
|
||||||
|
|
||||||
|
=== How does this work?
|
||||||
|
|
||||||
|
include::../../common/fix/how-does-this-work.adoc[]
|
@ -6,6 +6,8 @@ include::../impact.adoc[]
|
|||||||
|
|
||||||
== How to fix it?
|
== How to fix it?
|
||||||
|
|
||||||
|
include::how-to-fix-it/dotnet.adoc[]
|
||||||
|
|
||||||
== Resources
|
== Resources
|
||||||
|
|
||||||
include::../common/resources/standards.adoc[]
|
include::../common/resources/standards.adoc[]
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
=== How to fix it in Java SE
|
=== How to fix it in Java SE
|
||||||
|
|
||||||
The following code is vulnerable to path injection as it is constructing a path using untrusted data. This path is then used to delete a file without being validated first. Therefore, it can be leveraged by an attacker to delete arbitrary files.
|
:canonicalization_function: java.io.File.getCanonicalPath
|
||||||
|
include::../../common/fix/code-rationale.adoc[]
|
||||||
|
|
||||||
[cols="a"]
|
[cols="a"]
|
||||||
|===
|
|===
|
||||||
@ -48,9 +49,4 @@ public class ApiController
|
|||||||
|
|
||||||
=== How does this work?
|
=== How does this work?
|
||||||
|
|
||||||
The universal way to prevent path injection is to validate paths constructed from untrusted data.
|
include::../../common/fix/how-does-this-work.adoc[]
|
||||||
|
|
||||||
The validation should be done as follow:
|
|
||||||
|
|
||||||
1. Resolve the canonical path of the file by using methods like java.io.File/getCanonicalPath. This will resolve relative path or path components like `../` and removes any ambiguity regarding the file's location.
|
|
||||||
2. Check that the canonical path is within the directory where the file should be located.
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user