Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix code scanning alert no. 311: Uncontrolled data used in path expression #542

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Web/Grand.Web.Admin/Controllers/SettingController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,11 @@ private void SavePushNotificationsToFile(PushNotificationsSettingsModel model, I
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);
Expand Down
Loading