-
Notifications
You must be signed in to change notification settings - Fork 7.7k
The -Stream parameter now works with directories #13941
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
Conversation
Note: the Powershell-CI-static-analysis tests are failing because powershellgallery.com is down and the CI system cannot install Pester. (See PowerShell/PowerShellGallery#135) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a few notes for the reviewers.
@@ -1311,35 +1311,34 @@ protected override void GetItem(string path) | |||
// If we want to retrieve the file streams, retrieve them. | |||
if (retrieveStreams) | |||
{ | |||
if (!isContainer) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed isContainer check, because streams can be retrieved from containers as well.
@@ -6666,7 +6665,14 @@ public IContentReader GetContentReader(string path) | |||
|
|||
try | |||
{ | |||
if (Directory.Exists(path)) | |||
// Get-Content will write a non-terminating error if the target is a directory. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a few comments here, per Ilya in the V1 PR. Just needed to make sure to explain why the streamName check was here in a !UNIX block.
|
||
// An unexpected error was returned, that we don't know how to interpret. The most helpful | ||
// thing we can do at this point is simply throw the raw Win32 exception. | ||
throw new Win32Exception(error); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this isn't a a friendly error message, but I'd rather get functionality in and then gussy it up than fight with it in this PR. (again, an Ilya suggestion.)
@@ -8757,6 +8797,7 @@ internal static void SetZoneOfOrigin(string path, SecurityZone securityZone) | |||
internal static class NativeMethods | |||
{ | |||
internal const int ERROR_HANDLE_EOF = 38; | |||
internal const int ERROR_INVALID_PARAMETER = 87; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is returned when a filesystem doesn't support alternate data streams.
$result = Get-Item $altStreamDirectory -Stream $streamName | ||
$result.Length | Should -Be ($stringData.Length + [Environment]::NewLine.Length ) | ||
$result.Stream | Should -Be $streamName | ||
$result.PSIsContainer | Should -BeExactly $false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This particular line is to ensure that alternate data streams on containers are not themselves reported as being containers.
Closing and reopening to rerun CI. |
Reopening to rerun CI. |
@n3rdopolis Tagging you to test the compatibility, particularly that alternate data streams on directories should not report as containers, if you would like to do so. |
sweet, that attribute looks good now! |
Also, I request that this be merged, even with the CodeFactor analysis failing. I touched none of the functions that CodeFactor is complaining about, and for me to fix them in this PR would muddy the intent and scope of the actual changes. |
src/System.Management.Automation/namespaces/FileSystemProvider.cs
Outdated
Show resolved
Hide resolved
src/System.Management.Automation/namespaces/FileSystemProvider.cs
Outdated
Show resolved
Hide resolved
src/System.Management.Automation/namespaces/FileSystemProvider.cs
Outdated
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Management/Add-Content.Tests.ps1
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Management/Add-Content.Tests.ps1
Show resolved
Hide resolved
Current CI failures appear to be completely unrelated to this PR. |
Are they going to merge this? it worked for me |
We need reviews to merge a PR. Are you saying you've run this branch and it works? Would you be able to contribute a review of the PR as well? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This LGTM, with the suggestion from earlier
There. @rjmholt I think I got all the suggestions addressed (including properly hanging the if block, naming the parameter, and all the newlines). |
PowerShell-CI-static-analysis is failing because someone on the docs team removed PowerShell-CI-static-analysis (Markdown and Common Tests) is failing for a reason I can't discern, but I assume it's related to the prior issue. |
@rjmholt This is still saying merging is blocked, even after your approval. What else needs to be done? |
@kyanha Thanks for your contribution! |
🎉 Handy links: |
Take 3, final version.
PR Summary
This PR allows
Get-Item -Stream
,Get-Content -Stream
,Clear-Content -Stream
,Set-Content -Stream
,Add-Content -Stream
, andRemove-Item -Stream
to see and address alternate data streams on directories, not merely on files.Fixes #10570. Fixes #13656.
Supersedes #13650 (squashes all intermediary commits).
Supersedes #13795 (apparently some pull I did wanted to update the workflow, and my OAUTH token doesn't have workflow scope, and I can't figure out how to fix it)
Addresses addtional comments in #13795 regarding $PSIsContainer should be false on alternate data streams on directories because ADSes cannot contain other items.
PR Context
Issue #10570 has been open for a year. NTFS supports what are called "Alternate Data Streams" on both files and directories (multiple named discrete blobs of data which are associated with a single directory entry). PowerShell currently supports enumeration of these Alternate Data Streams on files, using the '-Stream' parameter to 'Get-Item'. It also supports manipulation of these alternate data streams on files, using the '-Stream' parameter to Set-Content, Add-Content, Clear-Content, and Remove-Item.
Unfortunately, the initial implementation of PowerShell only supported alternate data streams on files, not on directories. This makes an entire facility of the OS's file system invisible, and if an administration team is relying on PowerShell it makes an attractive place for a red team to store data to exfiltrate. (This is not an invitation to destroy the capability to store alternate data streams on directories, as they are useful for many purposes. It is merely a rationale for making their existence visible through PowerShell.)
To create and see an alternate data stream on a directory, use cmd.exe to run the following commands:
The output is something like:
To see the failure of PowerShell being able to see the stream on the file, but not the directory:
Writing the tests revealed that Set-Content internally calls Clear-Content, which is hardcoded to not check for streams on directories. This was raised as an issue in Issue #13656, but I decided to put that into this PR as well, because it made it easier to write the tests.
PR Checklist
.h
,.cpp
,.cs
,.ps1
and.psm1
files have the correct copyright headerWIP:
or[ WIP ]
to the beginning of the title (theWIP
bot will keep its status check atPending
while the prefix is present) and remove the prefix when the PR is ready.