Repository navigation
Description
If Start-Transcript is ran without -Append and a user has write privileges on the file without write privileges on the parent folder, the file will be silently deleted without any kind of error message.
Steps to reproduce
As administrator create a folder and file with the privileges specified above. I'm assuming that a local user, "myuser" exist:
mkdir "c:\test\"
icacls.exe "c:\test" "/grant" "builtin\administrators:(CI)(OI)(F)" "/inheritance:r"
icacls.exe "c:\test" "/grant" "myuser:(CI)(OI)(RX)"
"" | Out-File -Encoding "ascii" -LiteralPath "c:\test\my-transcript.txt"
icacls.exe "c:\test\my-transcript.txt" "/grant:r" "myuser:(F)"Run Start-Transcript as "myuser":
Start-Transcript -LiteralPath "c:\test\my-transcript.txt"
Stop-TranscriptExpected behavior
The transcript should be written to c:\test\my-transcript.txt
Actual behavior
The output states success, but the file is actually deleted.
PS C:> Start-Transcript -LiteralPath "c:\test\my-transcript.txt"
Transcript started, output file is c:\test\my-transcript.txtPS C:> Stop-Transcript
Transcript stopped, output file is C:\test\my-transcript.txtPS C:> Test-Path "c:\test\my-transcript.txt"
False
Environment data
Not the newest PowerShell ...
PS C:> $PSVersionTable
Name Value
PSVersion 5.1.14393.1358
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14393.1358
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
... but you can clearly see the error in line 230 of StartTranscriptCmdlet.cs :
PowerShell/src/Microsoft.PowerShell.ConsoleHost/host/msh/StartTranscriptCmdlet.cs
Line 230 in 02b5f35
| System.IO.File.Delete(effectiveFilePath); |
// If they didn't specify -Append, delete the file
if (!_shouldAppend)
{
System.IO.File.Delete(effectiveFilePath);
}This part of the code should rather write some sort of zero length content to the file.
Activity
charlieschmidt commentedon Oct 22, 2017
Paal Braathen (@paalbra) are you able to reproduce this on a verison of powershell-core? That line is correct - if the file exists, and the user hasn't said -Append (after some other conditions) - then delete the file before starting any of the real logic. That wipes it out as expected.
And fwiw, works for me on:
SteveL-MSFT commentedon Oct 22, 2017
With beta.8, following the repro steps, it does delete the file if
-Appendis not used, as expected. However, the transcript file does exist including a bunch of header information as well as thestop-transcriptcommand.paalbra commentedon Oct 22, 2017
Steve Lee (@SteveL-MSFT) I just ran through the repro steps and the issue is still present in beta.8 (with some changes).
Did you follow my steps all the way? What account did you use? The account I'm referring to as "myuser" should be an unprivileged account. The account doing the file access setup should be privileged (e.g. an administrator).
Charlie Schmidt (@charlieschmidt) You should not be able to do that. I believe you might have missed some of the file ACL setup as well?
The ACLs should become the following after setup:
When you start a transcript as "myuser" you now get the following in beta.8:
There was no error before. So the silent part of my first reported issue is no longer the case.
While it properly emits an error now there is still an issue that should be addressed. Start-Transcript should not delete the file. It should rather wipe the content of the file.
Version
paalbra commentedon Oct 26, 2018
You could also reproduce this as an issue on Ubuntu. Since this is a different OS/filesystem the issue isn't completely the same. What happens on Ubuntu is that you won't be able to delete the file since this requires write permissions on the parent directory. This also wouldn't be an issue if Start-Transcript didn't try to delete the file.