CVE-2023-43662: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal') (CWE-22)
value: ShokoServer is a media server which specializes in organizing anime. In affected versions the `/api/Image/WithPath` endpoint is accessible without authentication and is supposed to return default server images. The endpoint accepts the parameter `serverImagePath`, which is not sanitized in any way before being passed to `System.IO.File.OpenRead`, which results in an arbitrary file read. This issue may lead to an arbitrary file read which is exacerbated in the windows installer which installs the ShokoServer as administrator. Any unauthenticated attacker may be able to access sensitive information and read files stored on the server. The `/api/Image/WithPath` endpoint has been removed in commit `6c57ba0f0` which will be included in subsequent releases. Users should limit access to the `/api/Image/WithPath` endpoint or manually patch their installations until a patched release is made. This issue was discovered by the GitHub Security lab and is also indexed as GHSL-2023-191.
CWE description: 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.
CVSS v3 base score: 8.6
Published: 2023-09-28T22:15Z
Repository: https://github.com/ShokoAnime/ShokoServer
Language: C#
Files changed: Shoko.Server/API/v1/Implementations/ShokoServiceImplementationImage.cs
Commit date: 2023-09-27 20:50:01 -0400
Commit message: Merge pull request from GHSA-mwcv-ghjq-8f2g

* Fix arbitrary file read WithPath endpoint

* Remove WithPath Endpoint from APIv1

It's unnecessary and a security issue

---------

Co-authored-by: da3dsoul <da3dsoul@gmail.com>
Diff: diff --git a/Shoko.Server/API/v1/Implementations/ShokoServiceImplementationImage.cs b/Shoko.Server/API/v1/Implementations/ShokoServiceImplementationImage.cs
index d4945772..405e3c54 100644
--- a/Shoko.Server/API/v1/Implementations/ShokoServiceImplementationImage.cs
+++ b/Shoko.Server/API/v1/Implementations/ShokoServiceImplementationImage.cs
@@ -37,23 +37,10 @@ public class ShokoServiceImplementationImage : Controller, IShokoServerImage, IH
         var mime = Mime.GetMimeMapping(path);
         Response.ContentType = mime;
         return System.IO.File.OpenRead(path);
     }
 
-    [HttpGet("WithPath/{serverImagePath}")]
-    public object GetImageUsingPath(string serverImagePath)
-    {
-        if (!System.IO.File.Exists(serverImagePath))
-        {
-            logger.Trace("Could not find AniDB_Cover image: {0}", serverImagePath);
-            return NotFound();
-        }
-
-        Response.ContentType = Mime.GetMimeMapping(serverImagePath);
-        return System.IO.File.OpenRead(serverImagePath);
-    }
-
     [HttpGet("Blank")]
     public object BlankImage()
     {
         var dta = Resources.blank;
         var ms = new MemoryStream(dta);

Vulnerable code:     [HttpGet("WithPath/{serverImagePath}")]
    public object GetImageUsingPath(string serverImagePath)
    {
        if (!System.IO.File.Exists(serverImagePath))
        {
            logger.Trace("Could not find AniDB_Cover image: {0}", serverImagePath);
            return NotFound();
        }

        Response.ContentType = Mime.GetMimeMapping(serverImagePath);
        return System.IO.File.OpenRead(serverImagePath);
    }