Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix report module to handle italics and multiline processing in policy description #730

Merged
merged 30 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
0a5ebff
italics replace, but rest of line is italicized
isab-m Dec 11, 2023
11d0bc2
fix for cases with whitespaces
isab-m Dec 11, 2023
ef45d64
clean unused line
isab-m Dec 11, 2023
cda9ab1
clean comment
isab-m Dec 11, 2023
4e50237
multiline list case processing
isab-m Dec 13, 2023
5764292
html italic function declaration
isab-m Dec 13, 2023
1020f6f
Added bold option support for markdown transformation
isab-m Jan 8, 2024
4110b6c
clean debug comments, add bold option support
isab-m Jan 8, 2024
76aa643
escape regex for bold translation
isab-m Jan 12, 2024
5f3000a
Baseline revert
isab-m Jan 12, 2024
2d2bf9f
initial resolve-htmlmarkdown unit tests
isab-m Jan 18, 2024
5c8b52d
unit test logic fix
isab-m Jan 22, 2024
bd15b1c
added invalid paramater throw error for resolve-htmlmarkdown
isab-m Jan 22, 2024
b6b3b36
italics replace, but rest of line is italicized
isab-m Dec 11, 2023
7dfdac7
fix for cases with whitespaces
isab-m Dec 11, 2023
789cf79
clean unused line
isab-m Dec 11, 2023
2dbbad3
clean comment
isab-m Dec 11, 2023
e7f5e28
multiline list case processing
isab-m Dec 13, 2023
3698043
html italic function declaration
isab-m Dec 13, 2023
dc14aef
Added bold option support for markdown transformation
isab-m Jan 8, 2024
d49a6bf
clean debug comments, add bold option support
isab-m Jan 8, 2024
58ab285
escape regex for bold translation
isab-m Jan 12, 2024
2f1d65b
Baseline revert
isab-m Jan 12, 2024
a5a1e3b
initial resolve-htmlmarkdown unit tests
isab-m Jan 18, 2024
a593151
unit test logic fix
isab-m Jan 22, 2024
a13f4d7
added invalid paramater throw error for resolve-htmlmarkdown
isab-m Jan 22, 2024
d2bb412
Move unit test to new package location
schrolla Jan 25, 2024
25c59d2
* Fix import path after test file move
schrolla Jan 25, 2024
edef100
Update italics regex match to address empty italics properly.
schrolla Jan 25, 2024
82f6b1c
Merge branch '604-html-report-markdown-cleanup' of https://github.com…
crutchfield Jan 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions PowerShell/ScubaGear/Modules/CreateReport/CreateReport.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ function Import-SecureBaseline{

# Iterate over matched policy ids found
foreach ($LineNumber in $LineNumbers) {

$Value = [System.Net.WebUtility]::HtmlEncode($Value)
$Id = [string]$MdLines[$LineNumber].Substring(5)

Expand All @@ -283,6 +282,7 @@ function Import-SecureBaseline{
$MaxLineSearch = 20;
$Value = ([string]$MdLines[$LineNumber+$LineAdvance]).Trim()
$IsMalformedDescription = $false
$IsList = $false

try {
if ([string]::IsNullOrWhiteSpace($Value)){
Expand All @@ -300,8 +300,20 @@ function Import-SecureBaseline{
# Reached Criticality comment so policy description is complete.
break
}

# Policy description contains a list assuming list is denoted by a colon character.
if ($Value[-1] -eq ":") {
$isList = $true
}

if (-not [string]::IsNullOrWhiteSpace([string]$MdLines[$LineNumber+$LineAdvance])) {
$Value += "`n" + ([string]$MdLines[$LineNumber+$LineAdvance]).Trim()
# List case, use newline character between value text
if ($isList) {
$Value += "`n" + ([string]$MdLines[$LineNumber+$LineAdvance]).Trim()
}
else { # Value ending with newline char, use whitespace character between value text
$Value += " " + ([string]$MdLines[$LineNumber+$LineAdvance]).Trim()
}
}

if ($LineAdvance -gt $MaxLineSearch){
Expand All @@ -310,6 +322,12 @@ function Import-SecureBaseline{
}
}

# Description italics substitution
$Value = Resolve-HTMLMarkdown -OriginalString $Value -HTMLReplace "italic"

# Description bold substitution
$Value = Resolve-HTMLMarkdown -OriginalString $Value -HTMLReplace "bold"

$Group.Controls += @{"Id"=$Id; "Value"=$Value; "Deleted"=$Deleted; MalformedDescription=$IsMalformedDescription}
}
catch {
Expand Down Expand Up @@ -352,6 +370,32 @@ function New-MarkdownAnchor{
}
}

function Resolve-HTMLMarkdown{
param (
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]
$OriginalString,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]
$HTMLReplace
)

# Replace markdown with italics substitution
if ($HTMLReplace.ToLower() -match "italic") {
$ResolvedString = $OriginalString -replace '(_)([^\v][^_]*[^\v])?(_)', '<i>${2}</i>'
return $ResolvedString
} elseif($HTMLReplace.ToLower() -match "bold") {
$ResolvedString = $OriginalString -replace '(\*\*)(.*?)(\*\*)', '<b>${2}</b>'
return $ResolvedString
} else {
$InvalidHTMLReplace = New-Object System.ArgumentException "$HTMLReplace is not valid"
throw $InvalidHTMLReplace
return $OriginalString
}
}

Export-ModuleMember -Function @(
'New-Report',
'Import-SecureBaseline'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
$CreateReportModulePath = Join-Path -Path $PSScriptRoot -ChildPath "../../../../Modules/CreateReport/CreateReport.psm1"
Import-Module $CreateReportModulePath -Force

InModuleScope CreateReport {
Describe -Tag "Resolve-HTMLMarkdown" -name "Parameter error handling" {
It "Empty original string" {
{Resolve-HTMLMarkdown -OriginalString "" -HTMLReplace "italic"} |
Should -Throw -Because "Invalid OriginalString parameter"
}
It "Empty html replacement string" {
{Resolve-HTMLMarkdown -OriginalString "A valid string" -HTMLReplace ""} |
Should -Throw -Because "Invalid HTMLReplace parameter"
}
It "Null original string" {
{Resolve-HTMLMarkdown -OriginalString $null -HTMLReplace "italic"} |
Should -Throw -Because "Invalid OriginalString parameter"
}
It "Null html replacement string" {
{Resolve-HTMLMarkdown -OriginalString "A valid string" -HTMLReplace $null} |
Should -Throw -Because "Invalid HTMLReplace parameter"
}
It "Bad html replacement string" {
{Resolve-HTMLMarkdown -OriginalString "A valid string" -HTMLReplace "underline"} |
Should -Throw -ExceptionType ArgumentException
}
}

Describe -tag "Resolve-HTMLMarkdown" -name 'Test resolve HTML Markdown in baseline descriptions' {
It "Test Valid html markdown resolution: <OriginalString> <HTMLReplace>" -ForEach @(
@{ OriginalString = "_A test string._"; HTMLReplace = "italic"; HTMLTranslation = "<i>A test string.</i>"},
@{ OriginalString = "**A test string.**"; HTMLReplace = "bold"; HTMLTranslation = "<b>A test string.</b>"}
){
$ResolvedString = Resolve-HTMLMarkdown -OriginalString $OriginalString -HTMLReplace $HTMLReplace
$ResolvedString -eq $HTMLTranslation | Should -BeTrue
}
}
AfterAll {
Remove-Module CreateReport -ErrorAction SilentlyContinue
}
}
4 changes: 2 additions & 2 deletions baselines/aad.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ Security logs SHALL be sent to the agency's security operations center for monit

#### MS.AAD.4.1v1 Instructions

Follow the configuration instructions unique to the products and integration patterns at your organization to send the security logs to the security operations center for monitoring.
Follow the configuration instructions unique to the products and integration patterns at your organization to send the security logs to the security operations center for monitoring.

## 5. Application Registration and Consent

Expand Down Expand Up @@ -524,7 +524,7 @@ Permanent active role assignments SHALL NOT be allowed for highly privileged rol
- _Rationale:_ Instead of giving users permanent assignments to privileged roles, provisioning access just in time lessens exposure if those accounts become compromised. In Azure AD PIM or an alternative PAM system, just in time access can be provisioned by assigning users to roles as eligible instead of perpetually active.
- _Last modified:_ June 2023
- _Note:_ Exceptions to this policy are:
- Emergency access accounts that need perpetual access to the tenant in the rare event of system degradation or other scenarios.
- Emergency access accounts that need perpetual access to the tenant in the rare event of system degradation or other scenarios.
- Some types of service accounts that require a user account with privileged roles; since these accounts are used by software programs, they cannot perform role activation.

#### MS.AAD.7.5v1
Expand Down
2 changes: 1 addition & 1 deletion baselines/defender.md
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ See [MS.DEFENDER.4.1v1 Instructions](#msdefender41v1-instructions) steps
1. Sign in to the **Microsoft Purview compliance portal**.

2. Under **Solutions**, select **Data loss prevention**.

3. Go to **Endpoint DLP Settings**.

4. Go to **Restricted apps and app groups**.
Expand Down