Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public sealed class ImportModuleCommand : ModuleCmdletBase, IDisposable
private const string ParameterSet_FQName_ViaPsrpSession = "FullyQualifiedNameAndPSSession";
private const string ParameterSet_ViaWinCompat = "WinCompat";
private const string ParameterSet_FQName_ViaWinCompat = "FullyQualifiedNameAndWinCompat";
private static readonly char[] s_pathTraversalChars =
#if UNIX
['\\', '/'];
#else
['\\', '/', ':'];
#endif

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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


/// <summary>
/// This parameter specifies whether to import to the current session state
Expand Down Expand Up @@ -1078,6 +1084,22 @@ private PSModuleInfo ImportModule_RemotelyViaPsrpSession_SinglePreimportedModule
Version remoteModuleVersion,
PSSession psSession)
{
if (string.IsNullOrEmpty(remoteModuleName) || remoteModuleName.IndexOfAny(s_pathTraversalChars) >= 0 || remoteModuleName is "." or "..")
{
string errorMessage = string.Format(
CultureInfo.InvariantCulture,
Modules.SkipModuleWithInvalidModuleName,
remoteModuleName);
ErrorRecord errorRecord = new ErrorRecord(
new ArgumentException(errorMessage),
nameof(Modules.SkipModuleWithInvalidModuleName),
ErrorCategory.InvalidData,
remoteModuleName);
this.WriteError(errorRecord);

return null;
}

string temporaryModulePath = RemoteDiscoveryHelper.GetModulePath(
remoteModuleName,
remoteModuleVersion,
Expand Down Expand Up @@ -1125,6 +1147,7 @@ private PSModuleInfo ImportModule_RemotelyViaPsrpSession_SinglePreimportedModule

//
// rename the psd1 file
// remoteModuleName is sanitized at this point
//
string localPsd1File = Path.Combine(temporaryModulePath, remoteModuleName + ".psd1");
if (File.Exists(localPsd1File))
Expand Down
3 changes: 3 additions & 0 deletions src/System.Management.Automation/resources/Modules.resx
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@
<data name="CantUseAsCustomObjectWithBinaryModule" xml:space="preserve">
<value>No custom object was returned for module '{0}' because the -AsCustomObject parameter can only be used with script modules.</value>
</data>
<data name="SkipModuleWithInvalidModuleName" xml:space="preserve">
<value>Module with name '{0}' was skipped as the name contains invalid character(s).</value>
</data>
<data name="InvalidModuleManifest" xml:space="preserve">
<value>The module manifest '{0}' could not be processed because it is not a valid PowerShell module manifest file. Remove the elements that are not permitted: {1}</value>
</data>
Expand Down
Loading