Skip to content

Commit db79553

Browse files
authored
Prevent creating a folder or file with restricted names or characters (#933)
1 parent 088b853 commit db79553

21 files changed

+355
-38
lines changed

Files/Dialogs/RenameDialog.xaml

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,22 @@
1919
RequestedTheme="{x:Bind local2:ThemeHelper.RootTheme}">
2020

2121
<Grid MinWidth="300">
22-
<TextBox x:Name="RenameInput"
23-
x:Uid="RenameDialogInputText"
24-
PlaceholderText="Enter an item name without an extension"
25-
Height="35" />
22+
<StackPanel
23+
Orientation="Vertical"
24+
Spacing="4">
25+
<TextBox
26+
x:Name="RenameInput"
27+
x:Uid="RenameDialogInputText"
28+
PlaceholderText="Enter an item name without an extension"
29+
Height="35"
30+
TextChanged="RenameInput_TextChanged" />
31+
<TextBlock
32+
x:Name="RenameDialogSymbolsTip"
33+
x:Uid="RenameDialogSymbolsTip"
34+
Margin="0,0,4,0"
35+
TextWrapping="Wrap"
36+
Opacity="0"/>
37+
</StackPanel>
38+
2639
</Grid>
2740
</ContentDialog>

Files/Dialogs/RenameDialog.xaml.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,31 @@ private void NameDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogBu
1919
{
2020
storedRenameInput = inputBox.Text;
2121
}
22+
23+
private void RenameInput_TextChanged(object sender, TextChangedEventArgs e)
24+
{
25+
var textBox = sender as TextBox;
26+
27+
if (App.CurrentInstance.InteractionOperations.ContainsRestrictedCharacters(textBox.Text))
28+
{
29+
RenameDialogSymbolsTip.Opacity = 1;
30+
IsPrimaryButtonEnabled = false;
31+
return;
32+
}
33+
else
34+
{
35+
RenameDialogSymbolsTip.Opacity = 0;
36+
IsPrimaryButtonEnabled = true;
37+
}
38+
39+
if (App.CurrentInstance.InteractionOperations.ContainsRestrictedFileName(textBox.Text))
40+
{
41+
IsPrimaryButtonEnabled = false;
42+
}
43+
else
44+
{
45+
IsPrimaryButtonEnabled = true;
46+
}
47+
}
2248
}
2349
}

Files/Interacts/Interaction.cs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.IO.Compression;
1111
using System.Linq;
1212
using System.Reflection;
13+
using System.Text.RegularExpressions;
1314
using System.Threading.Tasks;
1415
using Windows.ApplicationModel.Core;
1516
using Windows.ApplicationModel.DataTransfer;
@@ -608,12 +609,54 @@ public void RenameItem_Click(object sender, RoutedEventArgs e)
608609
}
609610
}
610611

612+
public bool ContainsRestrictedCharacters(string input)
613+
{
614+
Regex regex = new Regex("\\\\|\\/|\\:|\\*|\\?|\\\"|\\<|\\>|\\|"); //restricted symbols for file names
615+
MatchCollection matches = regex.Matches(input);
616+
if (matches.Count > 0)
617+
{
618+
return true;
619+
}
620+
return false;
621+
}
622+
623+
private static readonly List<string> RestrictedFileNames = new List<string>()
624+
{
625+
"CON", "PRN", "AUX",
626+
"NUL", "COM1", "COM2",
627+
"COM3", "COM4", "COM5",
628+
"COM6", "COM7", "COM8",
629+
"COM9", "LPT1", "LPT2",
630+
"LPT3", "LPT4", "LPT5",
631+
"LPT6", "LPT7", "LPT8", "LPT9"
632+
};
633+
634+
public bool ContainsRestrictedFileName(string input)
635+
{
636+
637+
foreach (var name in RestrictedFileNames)
638+
{
639+
Regex regex = new Regex($"^{name}($|\\.)(.+)?");
640+
MatchCollection matches = regex.Matches(input.ToUpper());
641+
if (matches.Count > 0)
642+
{
643+
return true;
644+
}
645+
}
646+
647+
return false;
648+
}
649+
611650
public async Task<bool> RenameFileItem(ListedItem item, string oldName, string newName)
612651
{
613652
if (oldName == newName)
653+
{
614654
return true;
655+
}
615656

616-
if (!string.IsNullOrWhiteSpace(newName))
657+
if (!string.IsNullOrWhiteSpace(newName)
658+
&& !ContainsRestrictedCharacters(newName)
659+
&& !ContainsRestrictedFileName(newName))
617660
{
618661
try
619662
{
@@ -629,7 +672,6 @@ public async Task<bool> RenameFileItem(ListedItem item, string oldName, string n
629672
}
630673
}
631674
catch (Exception)
632-
633675
{
634676
var ItemAlreadyExistsDialog = new ContentDialog()
635677
{
@@ -673,6 +715,10 @@ public async Task<bool> RenameFileItem(ListedItem item, string oldName, string n
673715
}
674716
}
675717
}
718+
else
719+
{
720+
return false;
721+
}
676722

677723
CurrentInstance.NavigationToolbar.CanGoForward = false;
678724
return true;

Files/MultilingualResources/Files.de-DE.xlf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,18 @@
776776
<source>Open With</source>
777777
<target state="new">Open With</target>
778778
</trans-unit>
779+
<trans-unit id="FileNameTeachingTip.Subtitle" translate="yes" xml:space="preserve">
780+
<source>The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</source>
781+
<target state="new">The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</target>
782+
</trans-unit>
783+
<trans-unit id="FileNameTeachingTip.CloseButtonContent" translate="yes" xml:space="preserve">
784+
<source>OK</source>
785+
<target state="new">OK</target>
786+
</trans-unit>
787+
<trans-unit id="RenameDialogSymbolsTip.Text" translate="yes" xml:space="preserve">
788+
<source>The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</source>
789+
<target state="new">The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</target>
790+
</trans-unit>
779791
</group>
780792
</body>
781793
</file>

Files/MultilingualResources/Files.es-ES.xlf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,18 @@
774774
<source>Open With</source>
775775
<target state="translated">Abrir con</target>
776776
</trans-unit>
777+
<trans-unit id="FileNameTeachingTip.Subtitle" translate="yes" xml:space="preserve">
778+
<source>The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</source>
779+
<target state="new">The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</target>
780+
</trans-unit>
781+
<trans-unit id="FileNameTeachingTip.CloseButtonContent" translate="yes" xml:space="preserve">
782+
<source>OK</source>
783+
<target state="new">OK</target>
784+
</trans-unit>
785+
<trans-unit id="RenameDialogSymbolsTip.Text" translate="yes" xml:space="preserve">
786+
<source>The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</source>
787+
<target state="new">The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</target>
788+
</trans-unit>
777789
</group>
778790
</body>
779791
</file>

Files/MultilingualResources/Files.fr-FR.xlf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,18 @@
777777
<source>Open With</source>
778778
<target state="new">Open With</target>
779779
</trans-unit>
780+
<trans-unit id="FileNameTeachingTip.Subtitle" translate="yes" xml:space="preserve">
781+
<source>The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</source>
782+
<target state="new">The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</target>
783+
</trans-unit>
784+
<trans-unit id="FileNameTeachingTip.CloseButtonContent" translate="yes" xml:space="preserve">
785+
<source>OK</source>
786+
<target state="new">OK</target>
787+
</trans-unit>
788+
<trans-unit id="RenameDialogSymbolsTip.Text" translate="yes" xml:space="preserve">
789+
<source>The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</source>
790+
<target state="new">The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</target>
791+
</trans-unit>
780792
</group>
781793
</body>
782794
</file>

Files/MultilingualResources/Files.it-IT.xlf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,18 @@
776776
<source>Open With</source>
777777
<target state="new">Open With</target>
778778
</trans-unit>
779+
<trans-unit id="FileNameTeachingTip.Subtitle" translate="yes" xml:space="preserve">
780+
<source>The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</source>
781+
<target state="new">The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</target>
782+
</trans-unit>
783+
<trans-unit id="FileNameTeachingTip.CloseButtonContent" translate="yes" xml:space="preserve">
784+
<source>OK</source>
785+
<target state="new">OK</target>
786+
</trans-unit>
787+
<trans-unit id="RenameDialogSymbolsTip.Text" translate="yes" xml:space="preserve">
788+
<source>The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</source>
789+
<target state="new">The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</target>
790+
</trans-unit>
779791
</group>
780792
</body>
781793
</file>

Files/MultilingualResources/Files.nl-NL.xlf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,18 @@
777777
<source>Open With</source>
778778
<target state="new">Open With</target>
779779
</trans-unit>
780+
<trans-unit id="FileNameTeachingTip.Subtitle" translate="yes" xml:space="preserve">
781+
<source>The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</source>
782+
<target state="new">The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</target>
783+
</trans-unit>
784+
<trans-unit id="FileNameTeachingTip.CloseButtonContent" translate="yes" xml:space="preserve">
785+
<source>OK</source>
786+
<target state="new">OK</target>
787+
</trans-unit>
788+
<trans-unit id="RenameDialogSymbolsTip.Text" translate="yes" xml:space="preserve">
789+
<source>The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</source>
790+
<target state="new">The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</target>
791+
</trans-unit>
780792
</group>
781793
</body>
782794
</file>

Files/MultilingualResources/Files.pl-PL.xlf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,18 @@
777777
<source>Open With</source>
778778
<target state="new">Open With</target>
779779
</trans-unit>
780+
<trans-unit id="FileNameTeachingTip.Subtitle" translate="yes" xml:space="preserve">
781+
<source>The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</source>
782+
<target state="new">The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</target>
783+
</trans-unit>
784+
<trans-unit id="FileNameTeachingTip.CloseButtonContent" translate="yes" xml:space="preserve">
785+
<source>OK</source>
786+
<target state="new">OK</target>
787+
</trans-unit>
788+
<trans-unit id="RenameDialogSymbolsTip.Text" translate="yes" xml:space="preserve">
789+
<source>The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</source>
790+
<target state="new">The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</target>
791+
</trans-unit>
780792
</group>
781793
</body>
782794
</file>

Files/MultilingualResources/Files.ru-RU.xlf

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,7 @@
152152
</trans-unit>
153153
<trans-unit id="SettingsOnStartupOpenASpecificPage.Content" translate="yes" xml:space="preserve">
154154
<source>Open a specific page</source>
155-
<target state="needs-review-translation">Открыть определенную страницу(ы)</target>
156-
<note from="MultilingualUpdate" annotates="source" priority="2">Please verify the translation’s accuracy as the source string was updated after it was translated.</note>
155+
<target state="translated">Открыть определенную страницу(ы)</target>
157156
</trans-unit>
158157
<trans-unit id="SettingsOnStartupTitle.Text" translate="yes" xml:space="preserve">
159158
<source>On Startup</source>
@@ -765,15 +764,27 @@
765764
</trans-unit>
766765
<trans-unit id="SettingsOnStartupOpenASpecificPagePath.PlaceholderText" translate="yes" xml:space="preserve">
767766
<source>New Tab</source>
768-
<target state="new">New Tab</target>
767+
<target state="translated">Новая вкладка</target>
769768
</trans-unit>
770769
<trans-unit id="ErrorDialogUnsupportedOperation" translate="yes" xml:space="preserve">
771770
<source>The requested operation is not supported</source>
772-
<target state="new">The requested operation is not supported</target>
771+
<target state="translated">Запрошенная операция не поддерживается</target>
773772
</trans-unit>
774773
<trans-unit id="BaseLayoutItemContextFlyoutOpenItemWith.Text" translate="yes" xml:space="preserve">
775774
<source>Open With</source>
776-
<target state="new">Open With</target>
775+
<target state="translated">Открыть с помощью</target>
776+
</trans-unit>
777+
<trans-unit id="FileNameTeachingTip.Subtitle" translate="yes" xml:space="preserve">
778+
<source>The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</source>
779+
<target state="translated">Имя элемента не должно содержать следующих знаков: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</target>
780+
</trans-unit>
781+
<trans-unit id="FileNameTeachingTip.CloseButtonContent" translate="yes" xml:space="preserve">
782+
<source>OK</source>
783+
<target state="translated">OK</target>
784+
</trans-unit>
785+
<trans-unit id="RenameDialogSymbolsTip.Text" translate="yes" xml:space="preserve">
786+
<source>The item name must not contain the following characters: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</source>
787+
<target state="translated">Имя элемента не должно содержать следующих знаков: \ / : * ? " <it id="1" pos="open">&lt; &gt;</it> |</target>
777788
</trans-unit>
778789
</group>
779790
</body>

0 commit comments

Comments
 (0)