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

Support relative paths that don't begin with '.' #67

Merged
merged 1 commit into from
Oct 13, 2017
Merged

Support relative paths that don't begin with '.' #67

merged 1 commit into from
Oct 13, 2017

Conversation

danbelcher-MSFT
Copy link
Contributor

@danbelcher-MSFT danbelcher-MSFT commented Oct 2, 2017

Support relative paths that don't begin with '.' to denote the current working directory.

For example, an -OutPath of "SomeFolder" should represent a sub-folder of the current working directory. While PowerShell can handle path fragments of this type, interacting with .NET methods, such as XmlDocument.Save() can cause the file to be saved to an unintended directory.

The fix is to use Resolve-UnverifiedPath in-order to resolve path fragments like this to a full path.

Additonally, adds a new function Ensure-Directory, to ensure a directory is created/exists.

#64

@@ -2907,7 +2909,6 @@ function New-SubmissionPackage
})]
[string[]] $AppxPath,

[ValidateScript({ if (Test-Path -PathType Container $_) { $true } else { throw "$_ cannot be found." } })]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removing the existence check for OutPath here and below. Non-existent paths are still valid and can be created during the output step.

@HowardWolosky
Copy link
Member

You should clarify in your commit message that the issue really only can be seen when using a .NET method (like [xml]::Save()) with a relative path. Relative paths work just fine when you stay completely within PowerShell commands...

$Path = $resolvedPath
}

if (-not (Test-Path -PathType Container -Path $Path ))
Copy link
Member

Choose a reason for hiding this comment

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

nit: extra space before the closing parens

try
{
$resolvedPath = Resolve-UnverifiedPath -Path $Path
if ($null -ne $resolvedPath)
Copy link
Member

Choose a reason for hiding this comment

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

What's the scenario when this will be $null? (besides of course the input value being $null)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This came about from testing with -ErrorAction Ignore, as explained above. I can remove the check.

Copy link
Member

Choose a reason for hiding this comment

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

We really shouldn't be in a situation where Resolve-UnverifiedPath is returning $null. If it did, I'm not sure that just silently falling back to using the original $Path is the best idea....it just means that the caller is not guaranteed that they'll be getting their file written to the expected location...especially since the .zip file is going to run into trouble since it also depends on a valid value coming back from Resolve-UnverifiedPath

@@ -539,6 +539,62 @@ function Show-ImageFileNames
}
}

function Resolve-UnverifiedPath
Copy link
Member

Choose a reason for hiding this comment

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

I know that we wanted these scripts to work independent of the module, but now I'm not 100% sure. Having to duplicate this logic in here now across three different files seems like a maintenance nightmare. What's the drawback of dot-sourcing Helpers.ps1 here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In fact, I forgot we are already dot-sourcing Helpers.ps1 at the top of the file. I'll remove the redefinition of the function.

@@ -829,7 +829,7 @@ function Resolve-UnverifiedPath

$resolvedPath = Resolve-Path -Path $Path -ErrorVariable resolvePathError -ErrorAction SilentlyContinue

if ($null -eq $resolvedPath)
if (($null -eq $resolvedPath) -and ($resolvePathError.Count -gt 0))
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain the scenario for this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This came about after testing with -ErrorAction Ignore. In that case, $resolvePathError was still $null and $resolvedPath was also $null. An exception was thrown trying to index into $resolvePathError. After changing back to -ErrorAction SilentlyContinue, I left the additional guard in just to be safe.

I'm fine removing if you don't think we need it.

Copy link
Member

Choose a reason for hiding this comment

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

It's not going to help anything in this case...you'll just force it to go in to the else, and then it'll try accessing a property off of $null and get back an empty string, which will end up causing issues later on when the caller tries to use that "resolved" path. Better to just let it crash here if it really doesn't have any errors that we can index into.

@@ -829,7 +829,7 @@ function Resolve-UnverifiedPath

$resolvedPath = Resolve-Path -Path $Path -ErrorVariable resolvePathError -ErrorAction SilentlyContinue

if ($null -eq $resolvedPath)
if (($null -eq $resolvedPath) -and ($resolvePathError.Count -gt 0))
Copy link
Member

Choose a reason for hiding this comment

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

It's not going to help anything in this case...you'll just force it to go in to the else, and then it'll try accessing a property off of $null and get back an empty string, which will end up causing issues later on when the caller tries to use that "resolved" path. Better to just let it crash here if it really doesn't have any errors that we can index into.

try
{
$resolvedPath = Resolve-UnverifiedPath -Path $Path
if ($null -ne $resolvedPath)
Copy link
Member

Choose a reason for hiding this comment

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

We really shouldn't be in a situation where Resolve-UnverifiedPath is returning $null. If it did, I'm not sure that just silently falling back to using the original $Path is the best idea....it just means that the caller is not guaranteed that they'll be getting their file written to the expected location...especially since the .zip file is going to run into trouble since it also depends on a valid value coming back from Resolve-UnverifiedPath

Copy link
Member

@HowardWolosky HowardWolosky left a comment

Choose a reason for hiding this comment

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

You'll need to bump up the version in the psd1 as well.

…t working directory.

For example, an -OutPath of "SomeFolder" should represent a sub-folder of the current working directory. While PowerShell can handle path fragments of this type, interacting with .NET methods, such as XmlDocument.Save() can cause the file to be saved to an unintended directory.

The fix is to use Resolve-UnverifiedPath in-order to resolve path fragments like this to a full path.

Additonally, adds a new function Ensure-Directory, to ensure a directory is created/exists.

Bump module version to 1.11.0
@danbelcher-MSFT danbelcher-MSFT merged commit 7378e93 into microsoft:master Oct 13, 2017
@danbelcher-MSFT danbelcher-MSFT deleted the forked/SupportRelativePaths branch October 13, 2017 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants