Skip to content

Commit 86a7df0

Browse files
committed
C#: ZipSlip - Address doc team comments.
1 parent 4f57456 commit 86a7df0

File tree

7 files changed

+21
-22
lines changed

7 files changed

+21
-22
lines changed

change-notes/1.18/analysis-csharp.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
| **Query** | **Tags** | **Purpose** |
1717
|-----------------------------|-----------|--------------------------------------------------------------------|
18-
| Arbitrary file write during zip extraction ("ZipSlip") | security external/cwe/cwe-022 | Identifies zip extraction routines which allow tainted path and arbitrary file overwrite vulnerabilities.
18+
| Arbitrary file write during zip extraction ("Zip Slip") (cs/zipslip) | security external/cwe/cwe-022 | Identifies zip extraction routines which allow arbitrary file overwrite vulnerabilities.
1919
| Constant condition (cs/constant-condition) | More results | The query has been generalized to cover both `Null-coalescing left operand is constant (cs/constant-null-coalescing)` and `Switch selector is constant (cs/constant-switch-selector)`. |
2020
| Exposing internal representation (cs/expose-implementation) | Different results | The query has been rewritten, based on the equivalent Java query. |
2121
| Local scope variable shadows member (cs/local-shadows-member) | maintainability, readability | Replaces the existing queries [Local variable shadows class member (cs/local-shadows-class-member)](https://help.semmle.com/wiki/display/CSHARP/Local+variable+shadows+class+member), [Local variable shadows struct member (cs/local-shadows-struct-member)](https://help.semmle.com/wiki/display/CSHARP/Local+variable+shadows+struct+member), [Parameter shadows class member (cs/parameter-shadows-class-member)](https://help.semmle.com/wiki/display/CSHARP/Parameter+shadows+class+member), and [Parameter shadows struct member (cs/parameter-shadows-struct-member)](https://help.semmle.com/wiki/display/CSHARP/Parameter+shadows+struct+member). |

csharp/ql/src/Security Features/CWE-022/ZipSlip.qhelp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,18 @@ directory, aborting if this is not true.</li>
4646
<example>
4747

4848
<p>In this example, a file path taken from a zip archive item entry is combined with a
49-
destination directory, and the result is used as the destination file path without verifying that
49+
destination directory. The result is used as the destination file path without verifying that
5050
the result is within the destination directory. If provided with a zip file containing an archive
51-
path like <code>..\sneaky-file</code>, then this file would be written outside of the destination
51+
path like <code>..\sneaky-file</code>, then this file would be written outside the destination
5252
directory.</p>
5353

5454
<sample src="ZipSlipBad.cs" />
5555

56-
<p>In order to fix this vulnerability, we need to make three changes. Firstly, we need to resolve any
56+
<p>To fix this vulnerability, we need to make three changes. Firstly, we need to resolve any
5757
directory traversal or other special characters in the path by using <code>Path.GetFullPath</code>.
5858
Secondly, we need to identify the destination output directory, again using
5959
<code>Path.GetFullPath</code>, this time on the output directory. Finally, we need to ensure that
60-
the resolved output starts with the resolved destination directory, and throw and exception if this
60+
the resolved output starts with the resolved destination directory, and throw an exception if this
6161
is not the case.</p>
6262

6363
<sample src="ZipSlipGood.cs" />

csharp/ql/src/Security Features/CWE-022/ZipSlip.ql

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/**
2-
* @name Arbitrary file write during zip extraction ("ZipSlip")
2+
* @name Arbitrary file write during zip extraction ("Zip Slip")
33
* @description Extracting files from a malicious zip archive without validating that the
44
* destination file path is within the destination directory can cause files outside
5-
* the destination directory to be overwritten, due to the possible presence of
6-
* directory traversal elements ("..") in archive paths.
5+
* the destination directory to be overwritten.
76
* @kind problem
87
* @id cs/zipslip
98
* @problem.severity error
@@ -17,4 +16,4 @@ import semmle.code.csharp.security.dataflow.ZipSlip::ZipSlip
1716

1817
from TaintTrackingConfiguration zipTaintTracking, DataFlow::Node source, DataFlow::Node sink
1918
where zipTaintTracking.hasFlow(source, sink)
20-
select sink, "Unsanitized zip archive $@ which may contain '..' used in a file system operation.", source, "item path"
19+
select sink, "Unsanitized zip archive $@, which may contain '..', is used in a file system operation.", source, "item path"

csharp/ql/src/Security Features/CWE-022/ZipSlipGood.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static void WriteToDirectory(ZipArchiveEntry entry,
99
string destFileName = Path.GetFullPath(Path.Combine(destDirectory, entry.FullName));
1010
string fullDestDirPath = Path.GetFullPath(destDirectory + Path.DirectorySeparatorChar);
1111
if (!destFileName.StartsWith(fullDestDirPath)) {
12-
throw new System.InvalidOperationException("Entry is outside of the target dir: " +
12+
throw new System.InvalidOperationException("Entry is outside the target dir: " +
1313
destFileName);
1414
}
1515
entry.ExtractToFile(destFileName);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Provides a taint-tracking configuration for reasoning about unsafe zip extraction.
2+
* Provides a taint tracking configuration for reasoning about unsafe zip extraction.
33
*/
44
import csharp
55

@@ -15,11 +15,11 @@ module ZipSlip {
1515
abstract class Sink extends DataFlow::ExprNode { }
1616

1717
/**
18-
* A sanitizer for unsafe zipe extraction.
18+
* A sanitizer for unsafe zip extraction.
1919
*/
2020
abstract class Sanitizer extends DataFlow::ExprNode { }
2121

22-
/** A taint tracking configuration for ZipSlip */
22+
/** A taint tracking configuration for Zip Slip */
2323
class TaintTrackingConfiguration extends TaintTracking::Configuration {
2424
TaintTrackingConfiguration() {
2525
this = "ZipSlipTaintTracking"
@@ -59,7 +59,7 @@ module ZipSlip {
5959
}
6060
}
6161

62-
/** A path argument to a `File.Open`, `File.OpenWrite` or `File.Create` method call. */
62+
/** A path argument to a `File.Open`, `File.OpenWrite`, or `File.Create` method call. */
6363
class FileOpenArgSink extends Sink {
6464
FileOpenArgSink() {
6565
exists(MethodCall mc |
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
| ZipSlip.cs:24:41:24:52 | access to local variable destFileName | Unsanitized zip archive $@ which may contain '..' used in a file system operation. | ZipSlip.cs:19:31:19:44 | access to property FullName | item path |
2-
| ZipSlip.cs:32:41:32:52 | access to local variable destFilePath | Unsanitized zip archive $@ which may contain '..' used in a file system operation. | ZipSlip.cs:16:52:16:65 | access to property FullName | item path |
3-
| ZipSlip.cs:61:74:61:85 | access to local variable destFilePath | Unsanitized zip archive $@ which may contain '..' used in a file system operation. | ZipSlip.cs:54:72:54:85 | access to property FullName | item path |
4-
| ZipSlip.cs:68:71:68:82 | access to local variable destFilePath | Unsanitized zip archive $@ which may contain '..' used in a file system operation. | ZipSlip.cs:54:72:54:85 | access to property FullName | item path |
5-
| ZipSlip.cs:75:57:75:68 | access to local variable destFilePath | Unsanitized zip archive $@ which may contain '..' used in a file system operation. | ZipSlip.cs:54:72:54:85 | access to property FullName | item path |
6-
| ZipSlip.cs:83:58:83:69 | access to local variable destFilePath | Unsanitized zip archive $@ which may contain '..' used in a file system operation. | ZipSlip.cs:54:72:54:85 | access to property FullName | item path |
7-
| ZipSlipBad.cs:10:29:10:40 | access to local variable destFileName | Unsanitized zip archive $@ which may contain '..' used in a file system operation. | ZipSlipBad.cs:9:59:9:72 | access to property FullName | item path |
1+
| ZipSlip.cs:24:41:24:52 | access to local variable destFileName | Unsanitized zip archive $@, which may contain '..', is used in a file system operation. | ZipSlip.cs:19:31:19:44 | access to property FullName | item path |
2+
| ZipSlip.cs:32:41:32:52 | access to local variable destFilePath | Unsanitized zip archive $@, which may contain '..', is used in a file system operation. | ZipSlip.cs:16:52:16:65 | access to property FullName | item path |
3+
| ZipSlip.cs:61:74:61:85 | access to local variable destFilePath | Unsanitized zip archive $@, which may contain '..', is used in a file system operation. | ZipSlip.cs:54:72:54:85 | access to property FullName | item path |
4+
| ZipSlip.cs:68:71:68:82 | access to local variable destFilePath | Unsanitized zip archive $@, which may contain '..', is used in a file system operation. | ZipSlip.cs:54:72:54:85 | access to property FullName | item path |
5+
| ZipSlip.cs:75:57:75:68 | access to local variable destFilePath | Unsanitized zip archive $@, which may contain '..', is used in a file system operation. | ZipSlip.cs:54:72:54:85 | access to property FullName | item path |
6+
| ZipSlip.cs:83:58:83:69 | access to local variable destFilePath | Unsanitized zip archive $@, which may contain '..', is used in a file system operation. | ZipSlip.cs:54:72:54:85 | access to property FullName | item path |
7+
| ZipSlipBad.cs:10:29:10:40 | access to local variable destFileName | Unsanitized zip archive $@, which may contain '..', is used in a file system operation. | ZipSlipBad.cs:9:59:9:72 | access to property FullName | item path |

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static void WriteToDirectory(ZipArchiveEntry entry,
99
string destFileName = Path.GetFullPath(Path.Combine(destDirectory, entry.FullName));
1010
string fullDestDirPath = Path.GetFullPath(destDirectory + Path.DirectorySeparatorChar);
1111
if (!destFileName.StartsWith(fullDestDirPath)) {
12-
throw new System.InvalidOperationException("Entry is outside of the target dir: " +
12+
throw new System.InvalidOperationException("Entry is outside the target dir: " +
1313
destFileName);
1414
}
1515
entry.ExtractToFile(destFileName);

0 commit comments

Comments
 (0)