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

Fixing that renaming Media Library files to a reserved Windows name should cause an error, fixes #6802 #6919

Merged
merged 2 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Orchard.MediaLibrary.Services;
using Orchard.Security;
using Orchard.UI.Notify;
using Orchard.Validation;

namespace Orchard.MediaLibrary.MediaFileName
{
Expand Down Expand Up @@ -61,9 +62,12 @@ protected override DriverResult Editor(MediaPart part, IUpdateModel updater, dyn
try {
_mediaLibraryService.RenameFile(part.FolderPath, priorFileName, model.FileName);
part.FileName = model.FileName;

_notifier.Add(NotifyType.Information, T("File '{0}' was renamed to '{1}'", priorFileName, model.FileName));
}
catch (InvalidWindowsPathException) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use OrchardException here.

updater.AddModelError("MediaFileNameEditorSettings.FileName", T("Unable to rename file. Invalid Windows file path."));
}
catch (Exception) {
updater.AddModelError("MediaFileNameEditorSettings.FileName", T("Unable to rename file"));
}
Expand Down
19 changes: 18 additions & 1 deletion src/Orchard/Validation/PathValidation.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using System;
using System.IO;
using System.Runtime.Serialization;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unnecessary using

using System.Text.RegularExpressions;
using NHibernate.Hql;
using Orchard.Environment;

namespace Orchard.Validation {
/// <summary>
/// Provides methods to validate paths.
/// </summary>
public static class PathValidation {

/// <summary>
/// Determines if a path lies within the base path boundaries.
/// If not, an exception is thrown.
Expand All @@ -26,10 +31,22 @@ public static string ValidatePath(string basePath, string mappedPath) {
}

if (!valid) {
throw new ArgumentException("Invalid path");
if (System.Environment.OSVersion.Platform == PlatformID.Win32NT)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to check the environment

throw new InvalidWindowsPathException("Invalid path");
else
throw new ArgumentException("Invalid path");
}

return mappedPath;
}
}

/// <summary>
/// Thrown when an invalid path is encountered on a Windows platform.
/// </summary>
[Serializable]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary as we can use OrchardException

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing OrchardException breaks unit testOrchard.Framework.Tests.Orchard.Tests.Storage.FileStystemStorageProviderTests.CreateFileFailsInInvalidPath.

public class InvalidWindowsPathException : ApplicationException {
public InvalidWindowsPathException(string message) : base(message) {
}
}
}