microsoft/hve-core

Public

mirrored fromhttps://github.com/microsoft/hve-coreAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
3e2dac35fe558d8815fb3594a2e87238732f8ecd

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

scripts/security/Modules/SecurityClasses.psm1

147lines · modecode

1# Copyright (c) Microsoft Corporation.
2# SPDX-License-Identifier: MIT
3
4# SecurityClasses.psm1
5#
6# Purpose: Shared class definitions for security scanning scripts.
7# Author: HVE Core Team
8
9<#
10.SYNOPSIS
11 Shared class definitions for dependency pinning and compliance reporting.
12
13.DESCRIPTION
14 This module contains class definitions used by security scanning scripts:
15 - DependencyViolation: Represents a single dependency pinning violation
16 - ComplianceReport: Aggregates violations and generates compliance reports
17
18.NOTES
19 Classes must be imported using 'using module' syntax at the top of scripts:
20 using module ./Modules/SecurityClasses.psm1
21#>
22
23class DependencyViolation {
24 <#
25 .SYNOPSIS
26 Represents a single dependency pinning violation.
27
28 .DESCRIPTION
29 Contains information about a dependency that is not properly SHA-pinned,
30 including file location, dependency details, and remediation guidance.
31
32 ViolationType values:
33 - Unpinned: Dependency uses tag or branch instead of SHA
34 - Stale: SHA is pinned but newer version available
35 - VersionMismatch: Version comment does not match resolved SHA
36 - MissingVersionComment: SHA pinned but no version comment present
37 - Empty string: Default or unclassified violation
38 #>
39
40 [string]$File
41 [int]$Line
42 [string]$Type
43 [string]$Name
44 [string]$Version
45 [string]$CurrentRef
46 [string]$Severity
47 [ValidateSet('Unpinned', 'Stale', 'VersionMismatch', 'MissingVersionComment', 'MissingPermissions', '')]
48 [string]$ViolationType
49 [string]$Description
50 [string]$Remediation
51 [hashtable]$Metadata
52
53 DependencyViolation() {
54 $this.Metadata = @{}
55 }
56
57 DependencyViolation(
58 [string]$File,
59 [int]$Line,
60 [string]$Type,
61 [string]$Name,
62 [string]$Severity,
63 [string]$Description
64 ) {
65 $this.File = $File
66 $this.Line = $Line
67 $this.Type = $Type
68 $this.Name = $Name
69 $this.Severity = $Severity
70 $this.Description = $Description
71 $this.Metadata = @{}
72 }
73}
74
75class ComplianceReport {
76 <#
77 .SYNOPSIS
78 Aggregates dependency violations and generates compliance reports.
79
80 .DESCRIPTION
81 Collects violations from dependency scans and provides metrics like
82 compliance score, total dependencies, and summary by type.
83 #>
84
85 [string]$ScanPath
86 [datetime]$Timestamp
87 [int]$TotalFiles
88 [int]$ScannedFiles
89 [int]$TotalDependencies
90 [int]$PinnedDependencies
91 [int]$UnpinnedDependencies
92 [decimal]$ComplianceScore
93 [DependencyViolation[]]$Violations
94 [hashtable]$Summary
95 [hashtable]$Metadata
96
97 ComplianceReport() {
98 $this.Timestamp = Get-Date
99 $this.Violations = @()
100 $this.Summary = @{}
101 $this.Metadata = @{}
102 }
103
104 ComplianceReport([string]$ScanPath) {
105 $this.ScanPath = $ScanPath
106 $this.Timestamp = Get-Date
107 $this.Violations = @()
108 $this.Summary = @{}
109 $this.Metadata = @{}
110 }
111
112 [void] AddViolation([DependencyViolation]$Violation) {
113 $this.Violations += $Violation
114 $this.UnpinnedDependencies = $this.Violations.Count
115 }
116
117 [void] CalculateScore() {
118 if ($this.TotalDependencies -gt 0) {
119 $this.ComplianceScore = [math]::Round(
120 ($this.PinnedDependencies / $this.TotalDependencies) * 100, 2
121 )
122 }
123 else {
124 $this.ComplianceScore = 100.0
125 }
126 }
127
128 [hashtable] ToHashtable() {
129 return @{
130 ScanPath = $this.ScanPath
131 Timestamp = $this.Timestamp.ToString('yyyy-MM-ddTHH:mm:ss.fffZ')
132 TotalFiles = $this.TotalFiles
133 ScannedFiles = $this.ScannedFiles
134 TotalDependencies = $this.TotalDependencies
135 PinnedDependencies = $this.PinnedDependencies
136 UnpinnedDependencies = $this.UnpinnedDependencies
137 ComplianceScore = $this.ComplianceScore
138 Violations = $this.Violations
139 Summary = $this.Summary
140 Metadata = $this.Metadata
141 }
142 }
143}
144
145# Classes are exported automatically when imported via 'using module' syntax.
146# No functions to export.
147Export-ModuleMember -Function @()
148