Skip to content

Commit 6d26d71

Browse files
Add initial text
1 parent 62f0b9d commit 6d26d71

File tree

3 files changed

+50
-31
lines changed

3 files changed

+50
-31
lines changed

rules/S8347/csharp/metadata.json

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
{
2-
"title": "FIXME",
3-
"type": "CODE_SMELL",
2+
"title": "ASP.NET applications should prefer file providers over direct file access",
3+
"type": "VULNERABILITY",
44
"status": "ready",
55
"remediation": {
66
"func": "Constant\/Issue",
77
"constantCost": "5min"
88
},
9-
"tags": [
10-
],
11-
"defaultSeverity": "Major",
9+
"tags": [],
10+
"defaultSeverity": "Minor",
1211
"ruleSpecification": "RSPEC-8347",
1312
"sqKey": "S8347",
1413
"scope": "All",
15-
"defaultQualityProfiles": ["Sonar way"],
14+
"defaultQualityProfiles": [
15+
"Sonar way"
16+
],
1617
"quickfix": "unknown",
1718
"code": {
1819
"impacts": {
19-
"MAINTAINABILITY": "HIGH",
20-
"RELIABILITY": "MEDIUM",
2120
"SECURITY": "LOW"
2221
},
23-
"attribute": "CONVENTIONAL"
22+
"attribute": "COMPLETE"
2423
}
25-
}
24+
}

rules/S8347/csharp/rule.adoc

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,65 @@
1-
FIXME: add a description
2-
3-
// If you want to factorize the description uncomment the following line and create the file.
4-
//include::../description.adoc[]
1+
Using `PhysicalFileResult` with dynamic file paths can expose ASP.NET applications to path traversal attacks. An attacker could inject specially crafted values, such as `../`, to access files outside the intended directory.
52

63
== Why is this an issue?
74

8-
FIXME: remove the unused optional headers (that are commented out)
5+
When `PhysicalFileResult` is instantiated or its `FileName` property is set with a value that is not a static string, the application may become vulnerable to path injection. If this path ever becomes user-controlled, a malicious user could manipulate the path to read arbitrary files from the file system, potentially exposing sensitive data like configuration files or credentials.
6+
7+
=== What is the potential impact?
8+
9+
An attacker exploiting this vulnerability could read any file accessible to the application process. This may include:
910

10-
//=== What is the potential impact?
11+
* Application configuration files containing secrets
12+
* Source code or binaries
13+
* General system files which may help an attacker gain further access to the system
1114

1215
== How to fix it
13-
//== How to fix it in FRAMEWORK NAME
16+
17+
In general, it is recommended to use File Providers to access files. This approach ensures that file access is restricted to a specific root directory and prevents path traversal attacks. It is generally good practice to use this rather than using the `PhysicalFileResult` class (even together with own sanitization logic).
1418

1519
=== Code examples
1620

1721
==== Noncompliant code example
1822

1923
[source,csharp,diff-id=1,diff-type=noncompliant]
2024
----
21-
FIXME
25+
public class FileController : Controller
26+
{
27+
public IActionResult Download(string filename)
28+
{
29+
string path = Path.Combine("/var/www/files", filename);
30+
return new PhysicalFileResult(path, "application/octet-stream"); // Noncompliant
31+
}
32+
}
2233
----
2334

2435
==== Compliant solution
2536

2637
[source,csharp,diff-id=1,diff-type=compliant]
2738
----
28-
FIXME
39+
public class FileController : Controller
40+
{
41+
private readonly IFileProvider _fileProvider =
42+
new PhysicalFileProvider("/var/www/files");
43+
44+
public IActionResult Download(string filename)
45+
{
46+
IFileInfo fileInfo = _fileProvider.GetFileInfo(filename);
47+
if (!fileInfo.Exists)
48+
{
49+
return NotFound();
50+
}
51+
return File(fileInfo.CreateReadStream(), "application/octet-stream");
52+
}
53+
}
2954
----
3055

31-
//=== How does this work?
56+
=== How does this work?
3257

33-
//=== Pitfalls
58+
`PhysicalFileProvider` restricts file access to a designated root directory. When `GetFileInfo` is called, the provider automatically prevents path traversal by ensuring the resolved path remains within the root directory. This built-in protection eliminates the risk of directory escape attacks.
3459

35-
//=== Going the extra mile
60+
== Resources
3661

62+
=== Documentation
3763

38-
//== Resources
39-
//=== Documentation
40-
//=== Articles & blog posts
41-
//=== Conference presentations
42-
//=== Standards
43-
//=== External coding guidelines
44-
//=== Benchmarks
64+
* Microsoft Learn - https://learn.microsoft.com/en-us/aspnet/core/fundamentals/file-providers[File Providers in ASP.NET Core]
65+
* OWASP - https://owasp.org/www-community/attacks/Path_Traversal[Path Traversal]

rules/S8347/metadata.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
{
2-
}
1+
{}

0 commit comments

Comments
 (0)