Skip to content

Commit 8a54137

Browse files
authored
Fix: Prevent creating folders with forbidden names (#10967)
1 parent 985d66e commit 8a54137

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/Files.App/Filesystem/FilesystemOperations/FilesystemOperations.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,4 +960,4 @@ public Task<IStorageHistory> CreateShortcutItemsAsync(IList<IStorageItemWithPath
960960
throw new NotImplementedException("Cannot create shortcuts in UWP.");
961961
}
962962
}
963-
}
963+
}

src/Files.App/Filesystem/FilesystemOperations/Helpers/FilesystemHelpers.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ public FilesystemHelpers(IShellPage associatedInstance, CancellationToken cancel
9696
var progress = new Progress<FileSystemProgress>();
9797
progress.ProgressChanged += (s, e) => returnStatus = returnStatus < ReturnResult.Failed ? e.Status!.Value.ToStatus() : returnStatus;
9898

99+
if (!IsValidForFilename(source.Name))
100+
{
101+
await DialogDisplayHelper.ShowDialogAsync(
102+
"ErrorDialogThisActionCannotBeDone".GetLocalizedResource(),
103+
"ErrorDialogNameNotAllowed".GetLocalizedResource());
104+
return (ReturnResult.Failed, null);
105+
}
106+
99107
var result = await filesystemOperations.CreateAsync(source, progress, cancellationToken);
100108

101109
if (registerHistory && !string.IsNullOrWhiteSpace(source.Path))
@@ -538,6 +546,14 @@ public async Task<ReturnResult> RenameAsync(IStorageItemWithPath source, string
538546
var progress = new Progress<FileSystemProgress>();
539547
progress.ProgressChanged += (s, e) => returnStatus = returnStatus < ReturnResult.Failed ? e.Status!.Value.ToStatus() : returnStatus;
540548

549+
if (!IsValidForFilename(newName))
550+
{
551+
await DialogDisplayHelper.ShowDialogAsync(
552+
"ErrorDialogThisActionCannotBeDone".GetLocalizedResource(),
553+
"ErrorDialogNameNotAllowed".GetLocalizedResource());
554+
return ReturnResult.Failed;
555+
}
556+
541557
IStorageHistory history = null;
542558

543559
switch (source.ItemType)
@@ -632,6 +648,9 @@ public async Task<ReturnResult> RecycleItemsFromClipboard(DataPackageView packag
632648

633649
#endregion IFilesystemHelpers
634650

651+
private static bool IsValidForFilename(string name)
652+
=> !string.IsNullOrWhiteSpace(name) && !ContainsRestrictedCharacters(name) && !ContainsRestrictedFileName(name);
653+
635654
private static async Task<(List<FileNameConflictResolveOptionType> collisions, bool cancelOperation, IEnumerable<IFileSystemDialogConflictItemViewModel>)> GetCollision(FilesystemOperationType operationType, IEnumerable<IStorageItemWithPath> source, IEnumerable<string> destination, bool forceDialog)
636655
{
637656
var incomingItems = new List<BaseFileSystemDialogItemViewModel>();

src/Files.App/Strings/en-US/Resources.resw

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2877,6 +2877,9 @@
28772877
<data name="ReleaseNotes" xml:space="preserve">
28782878
<value>Release Notes</value>
28792879
</data>
2880+
<data name="ErrorDialogNameNotAllowed" xml:space="preserve">
2881+
<value>The specified item name is invalid</value>
2882+
</data>
28802883
<data name="Settings" xml:space="preserve">
28812884
<value>Settings</value>
28822885
</data>

0 commit comments

Comments
 (0)