PowerShell / PowerShell/PSScriptAnalyzer
PSShouldProcess behavior allows for unsafe code to pass where it should fail
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- C#
- Estrellas
- 2.2k
- Forks
- 414
- Merge medio
- 13 h 1 min
- PR fusionados (30 d)
- 2
Descripción
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
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Empieza por Rules/UseShouldProcessCorrectly.cs, especialmente por la comprobación de la función upstream alrededor de las líneas 245-267, y reproduce el problema con la función de PowerShell mostrada. Confirma que el analizador informa del comportamiento ausente de $PSCmdlet.ShouldProcess() o requiere una supresión, y después añade cobertura para la advertencia esperada o el requisito de supresión.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- csharp, powershell
- Área
- devtools, security
- Tipo de issue
- Error
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100