CVE-2021-39208: SharpCompress Path Traversal Vulnerability

MODERATE
Advisory Identifier: GHSA-jp7f-grcv-6mjf
CVSS 4.3
CWE-22
Published
Advisory Summary

CVE-2021-39208 is a MODERATE (CVSS 4.3) path traversal vulnerability (CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')) affecting SharpCompress libraries in the NuGet ecosystem. The issue involves partial path traversal in sharpcompress. Remediation is available by upgrading affected packages to version 0.29.

CVE-2021-39208 at a Glance

CVE Identifier CVE-2021-39208
GHSA Identifier GHSA-jp7f-grcv-6mjf
Severity MODERATE
CVSS v3.1 Base Score 4.3
CWE Classification CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
Affected Product SharpCompress
Package Ecosystem NuGet
Published Date
Last Updated

Security Impact

CVSS: 4.3

The security impact metrics below are parsed directly from the CVSS v3.1 vector string (CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:N/I:L/A:N).

Attack Vector Network (AV:N)
Attack Complexity Low (AC:L)
Privileges Required Low (PR:L)
User Interaction None (UI:N)
Scope Unchanged (S:U)
Confidentiality None (C:N)
Integrity Low (I:L)
Availability None (A:N)

How to Fix CVE-2021-39208

To remediate this vulnerability, upgrade all affected SharpCompress dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.

SharpCompress fixed: 0.29
Upgrade SharpCompress to 0.29
Verify Direct and Transitive Dependencies

Run the following command in your project directory to verify that all resolved dependencies use the patched release:

inspect dependencies for SharpCompress

How to Check if You Are Affected

To determine whether your application is affected by CVE-2021-39208, inspect your project dependencies for SharpCompress:

inspect dependencies for SharpCompress

Analyze the terminal output to locate any version of SharpCompress in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 0.29), your application is affected.

Note: Verify declared build manifests and deployed runtime container environments.

Technical Details

Vulnerability Mechanism & Technical Analysis

This vulnerability is classified under CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal'), where the product uses external input to construct a pathname that is intended to identify a file or directory that is located underneath a restricted parent directory, but the product does not properly neutralize special elements within the pathname that can cause the pathname to resolve to a location that is outside of the restricted directory. In SharpCompress, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.

Source Advisory Statement

SharpCompress recreates a hierarchy of directories under destinationDirectory if ExtractFullPath is set to true in options. In order to prevent extraction outside the destination directory the destinationFileName path is verified to begin with fullDestinationDirectoryPath. However it is not enforced that fullDestinationDirectoryPath ends with slash: ```csharp public static void WriteEntryToDirectory(IEntry entry, string destinationDirectory, ExtractionOptions? options, Action<string, ExtractionOptions?> write) { string destinationFileName; string file = Path.GetFileName(entry.Key); string fullDestinationDirectoryPath = Path.GetFullPath(destinationDirectory); ... throw new ExtractionException("Entry is trying to write a file outside of the destination directory."); } ``` If the destinationDirectory is not slash terminated like /home/user/dir it is possible to create a file with a name thats begins as the destination directory one level up from the directory, i.e. /home/user/dir.sh. #### Impact Because of the file name and destination directory constraints the arbitrary file creation impact is limited and depends on the use case.

CVE-2021-39208 Affected SharpCompress Versions

1 component(s)
Fixed Versions:
0.29
Product / Package Ecosystem Affected Versions Fixed Version
SharpCompress
NuGet
< 0.29
0.29

References

Frequently Asked Questions

What is CVE-2021-39208?
CVE-2021-39208 is a security vulnerability in SharpCompress involving Partial path traversal in sharpcompress under CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal').
Which SharpCompress versions are affected?
Affected versions include releases prior to 0.29 across affected packages: SharpCompress.
What version fixes CVE-2021-39208?
Upgrading to 0.29 (or the latest patched release for your respective branch) resolves the vulnerability.
Is SharpCompress affected?
Yes, SharpCompress is affected in releases prior to 0.29. Upgrade your dependency to 0.29 or later.
What is the CVSS score of CVE-2021-39208?
CVE-2021-39208 has a CVSS v3.1 base score of 4.3 (MODERATE) with vector string CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:N/I:L/A:N.
What CWE is associated with CVE-2021-39208?
CVE-2021-39208 is classified under CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal') (The product uses external input to construct a pathname that is intended to identify a file or directory that is located underneath a restricted parent directory, but the product does not properly neutralize special elements within the pathname that can cause the pathname to resolve to a location that is outside of the restricted directory.).
How can I check NuGet dependencies for CVE-2021-39208?
Run the dependency tree command: 'inspect dependencies for SharpCompress'. This command lists direct and transitive dependencies in your build configuration so you can verify if a vulnerable version is resolved.
Machine-readable OSV JSON OSV v1.5 Schema
Normalized schema output
{
  "schema_version" : "1.4.0",
  "id" : "GHSA-jp7f-grcv-6mjf",
  "modified" : "2024-07-08T14:33:23Z",
  "published" : "2021-09-20T19:53:42Z",
  "aliases" : [ "CVE-2021-39208" ],
  "summary" : "Partial path traversal in sharpcompress",
  "details" : "SharpCompress recreates a hierarchy of directories under destinationDirectory if ExtractFullPath is set to true in options. In order to prevent extraction outside the destination directory the destinationFileName path is verified to begin with fullDestinationDirectoryPath. However it is not enforced that fullDestinationDirectoryPath ends with slash:\n\n```csharp\npublic static void WriteEntryToDirectory(IEntry entry,\n                                         string destinationDirectory,\n                                         ExtractionOptions? options,\n                                         Action<string, ExtractionOptions?> write)\n{\n    string destinationFileName;\n    string file = Path.GetFileName(entry.Key);\n    string fullDestinationDirectoryPath = Path.GetFullPath(destinationDirectory);\n...\n        throw new ExtractionException(\"Entry is trying to write a file outside of the destination directory.\");\n}\n```\n\nIf the destinationDirectory is not slash terminated like /home/user/dir it is possible to create a file with a name thats begins as the destination directory one level up from the directory, i.e. /home/user/dir.sh.\n\n#### Impact\nBecause of the file name and destination directory constraints the arbitrary file creation impact is limited and depends on the use case.",
  "severity" : [ {
    "type" : "CVSS_V3",
    "score" : "CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:N/I:L/A:N"
  }, {
    "type" : "CVSS_V4",
    "score" : "CVSS:4.0/AV:N/AC:L/AT:N/PR:L/UI:N/VC:N/VI:L/VA:N/SC:N/SI:N/SA:N"
  } ],
  "affected" : [ {
    "package" : {
      "ecosystem" : "NuGet",
      "name" : "SharpCompress"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "0"
      }, {
        "fixed" : "0.29"
      } ]
    } ]
  } ],
  "references" : [ {
    "type" : "WEB",
    "url" : "https://github.com/adamhathcock/sharpcompress/security/advisories/GHSA-jp7f-grcv-6mjf"
  }, {
    "type" : "ADVISORY",
    "url" : "https://nvd.nist.gov/vuln/detail/CVE-2021-39208"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/adamhathcock/sharpcompress/pull/614"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/adamhathcock/sharpcompress/commit/ea5c8dc06314c7a5068e7901c51a625224d2b288"
  }, {
    "type" : "PACKAGE",
    "url" : "https://github.com/adamhathcock/sharpcompress"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/adamhathcock/sharpcompress/releases/tag/0.29.0"
  } ],
  "database_specific" : {
    "cwe_ids" : [ "CWE-22" ],
    "severity" : "MODERATE",
    "github_reviewed" : true,
    "github_reviewed_at" : "2021-09-17T18:37:10Z",
    "nvd_published_at" : "2021-09-16T15:15:00Z"
  }
}