Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Uncontrolled data used in path expression (#542)

Krzysztof Pajaka45a54b3f5d8
diff --git a/src/Web/Grand.Web.Admin/Controllers/SettingController.cs b/src/Web/Grand.Web.Admin/Controllers/SettingController.cs
index a14ac9946..9543d8443 100644
--- a/src/Web/Grand.Web.Admin/Controllers/SettingController.cs
+++ b/src/Web/Grand.Web.Admin/Controllers/SettingController.cs
@@ -780,6 +780,11 @@ public class SettingController(
private string GetSafeFilePath(IConfiguration configuration, IWebHostEnvironment webHostEnvironment, string filename)
{
var directoryParam = configuration[CommonPath.DirectoryParam] ?? "";
+
+ // Validate directoryParam to ensure it does not contain ".." or path separators
+ if (directoryParam.Contains("..") || directoryParam.Contains("/") || directoryParam.Contains("\\"))
+ throw new ArgumentException("Invalid directory parameter - contains illegal characters.");
+
var safeDirectoryName = Path.GetFileName(directoryParam);
var combinedPath = Path.Combine(webHostEnvironment.WebRootPath, safeDirectoryName, filename);
var fullPath = Path.GetFullPath(combinedPath, webHostEnvironment.WebRootPath);