Skip to content

Commit 1a90f7d

Browse files
committed
C#: ZipSlip - Address review comments.
- Add backticks - Add extra test.
1 parent f9227ee commit 1a90f7d

File tree

2 files changed

+11
-2
lines changed
  • csharp/ql
    • src/semmle/code/csharp/security/dataflow
    • test/query-tests/Security Features/CWE-022/ZipSlip

2 files changed

+11
-2
lines changed

csharp/ql/src/semmle/code/csharp/security/dataflow/ZipSlip.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ module ZipSlip {
112112
}
113113

114114
/**
115-
* A call to Substring.
115+
* A call to `Substring`.
116116
*
117117
* This is considered a sanitizer because `Substring` may be used to extract a single component
118118
* of a path to avoid ZipSlip.

csharp/ql/test/query-tests/Security Features/CWE-022/ZipSlip/ZipSlip.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private static int UnzipToStream(Stream zipStream, string installDir)
5959
foreach (ZipArchiveEntry entry in archive.Entries)
6060
{
6161
// figure out where we are putting the file
62-
string destFilePath = Path.Combine(InstallDir, entry.FullName);
62+
String destFilePath = Path.Combine(InstallDir, entry.FullName);
6363

6464
Directory.CreateDirectory(Path.GetDirectoryName(destFilePath));
6565

@@ -94,6 +94,15 @@ private static int UnzipToStream(Stream zipStream, string installDir)
9494
Console.WriteLine(@"Writing ""{0}""", destFilePath);
9595
archiveFileStream.CopyTo(fs);
9696
}
97+
98+
// GOOD: Use substring to pick out single component
99+
string fileName = destFilePath.Substring(destFilePath.LastIndexOf("\\"));
100+
var fileInfo2 = new FileInfo(fileName);
101+
using (FileStream fs = fileInfo2.Open(FileMode.Create))
102+
{
103+
Console.WriteLine(@"Writing ""{0}""", destFilePath);
104+
archiveFileStream.CopyTo(fs);
105+
}
97106
}
98107
}
99108
}

0 commit comments

Comments
 (0)