PowerShell / PowerShell/PSScriptAnalyzer
PSShouldProcess behavior allows for unsafe code to pass where it should fail
Ninguém assumiu esta issue ainda.
- Linguagem predominante
- C#
- Estrelas
- 2.2k
- Forks
- 414
- Merge médio
- 13h 1min
- PRs com merge (30d)
- 2
Descrição
Steps to reproduce
The following function will pass PSShouldProcess
function Test-Fail {
[CmdletBinding(SupportsShouldProcess)]
param ()
process {
Remove-Item -Path C:\temp\a_testy.txt -Force -Confirm:$false -ErrorAction SilentlyContinue
}
}
@seeminglyscience pointesd me to this
https://github.com/PowerShell/PSScriptAnalyzer/blob/a7d23f7f4dbf4ee92077f5471e28ca334caef429/Rules/UseShouldProcessCorrectly.cs#L245-L267
Where it is checking upstream functions for supports should process. While on the surface it may seem like a good idea to allow the upstream functions to handle the confirmation, this allows for some unsafe code to pass where it should fail.
IMO, the best course would be to always warn when $PSCmdlet.ShouldProcess() is not present in the function itself and require suppression when allowing the upstream functions to handle the confirmation.
Expected behavior
Either a warning or the requirement to suppress PSShouldProcess
Actual behavior
Test passes
Environment data
> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.16299.251
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.16299.251
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.16.0
1.15.0
1.13.0
1.12.0
1.8.1
1.16.1
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Direção de pesquisa
Comece por Rules/UseShouldProcessCorrectly.cs, especialmente pela verificação da função upstream por volta das linhas 245-267, e reproduza o problema com a função PowerShell mostrada. Confirme que o analisador relata o comportamento ausente de $PSCmdlet.ShouldProcess() ou exige uma supressão e, em seguida, adicione cobertura para o aviso esperado ou para o requisito de supressão.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- csharp, powershell
- Domínio
- devtools, security
- Tipo de issue
- Bug
- Dificuldade
- 3/5
- Tempo estimado
- 1-2 dias
- Status de atividade
- Estagnada
- Clareza
- Razoavelmente clara
- Facilidade para iniciantes
- 35/100