2021-02-02 04:09:31 +00:00
|
|
|
include::../description.adoc[]
|
|
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,csharp]
|
2021-02-02 04:09:31 +00:00
|
|
|
----
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.IO.Compression;
|
|
|
|
|
|
|
|
namespace ZipSlip
|
|
|
|
{
|
|
|
|
public class ZipSlipNoncompliant
|
|
|
|
{
|
|
|
|
public void ExtractEntry(IEnumerator<ZipArchiveEntry> entriesEnumerator, string destinationDirectory)
|
|
|
|
{
|
|
|
|
var entry = entriesEnumerator.Current;
|
|
|
|
var destinationPath = Path.Combine(destinationDirectory, entry.FullName);
|
|
|
|
entry.ExtractToFile(destinationPath); // Noncompliant
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,csharp]
|
2021-02-02 04:09:31 +00:00
|
|
|
----
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.IO.Compression;
|
|
|
|
|
|
|
|
namespace ZipSlip
|
|
|
|
{
|
|
|
|
public class ZipSlipCompliant
|
|
|
|
{
|
|
|
|
public void ExtractEntry(IEnumerator<ZipArchiveEntry> entriesEnumerator, string destinationDirectory)
|
|
|
|
{
|
|
|
|
var entry = entriesEnumerator.Current;
|
|
|
|
var destinationDirectoryFullPath = Path.GetFullPath(destinationDirectory);
|
|
|
|
var destinationPath = Path.Combine(destinationDirectoryFullPath, entry.FullName);
|
|
|
|
var destinationFullPath = Path.GetFullPath(destinationPath);
|
|
|
|
if (!destinationFullPath.StartsWith(destinationDirectoryFullPath))
|
|
|
|
{
|
|
|
|
throw new IOException("Attempting to extract archive entry outside destination directory");
|
|
|
|
}
|
|
|
|
entry.ExtractToFile(destinationFullPath); // OK
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
include::../see.adoc[]
|
2021-09-20 15:38:42 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
|
|
|
include::../message.adoc[]
|
|
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|