PowerShell / PowerShell/PSScriptAnalyzer
PSShouldProcess behavior allows for unsafe code to pass where it should fail
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- C#
- Star
- 2.2k
- Fork
- 414
- Merge trung bình
- 13 giờ 1 phút
- Pull request đã merge (30 ngày)
- 2
Mô tả
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
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với Rules/UseShouldProcessCorrectly.cs, đặc biệt là phần kiểm tra hàm upstream quanh các dòng 245-267, và tái hiện vấn đề bằng hàm PowerShell được cung cấp. Xác nhận rằng analyzer báo cáo hành vi còn thiếu của $PSCmdlet.ShouldProcess() hoặc yêu cầu suppression, sau đó bổ sung coverage cho cảnh báo dự kiến hoặc yêu cầu suppression.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- csharp, powershell
- Lĩnh vực
- devtools, security
- Loại issue
- Lỗi
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100