Skip to content

Commit

Permalink
win, compiler: fix and validate empty comment code
Browse files Browse the repository at this point in the history
This commit fixes empty code scripts that create revert code with only
comment lines.

It also improves compiler validation so if a generated code consists
solely of comments, it displays error message.

TODO: Missing unit tests for compiler
  • Loading branch information
undergroundwires committed Dec 18, 2024
1 parent 92ec138 commit b44c074
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 17 deletions.
Empty file.
Empty file.
112 changes: 95 additions & 17 deletions src/application/collections/windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40136,7 +40136,9 @@ functions:
Remove the registry key "{{ $keyPath }}"
{{ with $codeComment }}({{ . }}){{ end }}
revertCodeComment: >-
Recreate the registry key "{{ $keyPath }}"
{{ with $recreateOnRevert }}
Recreate the registry key "{{ $keyPath }}"
{{ end }}
-
# Marked: refactor-with-variables
# - Replacing SID is same as `CreateRegistryKey`
Expand Down Expand Up @@ -40710,32 +40712,108 @@ functions:
setupCodeUnelevated: '{{ with $setupCode }}{{ . }}{{ end }}'
minimumWindowsVersion: '{{ with $minimumWindowsVersion }}{{ . }}{{ end }}'
elevateToTrustedInstaller: '{{ with $elevateToTrustedInstaller }}true{{ end }}'
# Marked: refactor-with-variables
# - Setting registry value is same in `code` and `revert code`
code: |-
$registryPath = '{{ $keyPath }}'
$name = '{{ $valueName }}'
$rawPath = '{{ $keyPath }}'
$rawType = '{{ $dataType }}'
$data = '{{ $data }}'
{{ with $evaluateDataAsPowerShell }}
$data = $({{ $data }})
{{ end }}
reg add '{{ $keyPath }}' `
/v '{{ $valueName }}' `
/t '{{ $dataType }}' `
/d "$data" `
/f
Write-Host "Setting '$name' registry value in '$rawPath'."
$hive = $rawPath.Split('\')[0]
$path = $hive + ':' + $rawPath.Substring($hive.Length)
if (!(Test-Path $path)) {
New-Item -Path $path -Force -ErrorAction Stop | Out-Null
Write-Host 'Successfully created the parent registry key.'
}
$type = switch ($rawType) {
'REG_SZ' { 'String' }
'REG_DWORD' { 'DWord' }
'REG_QWORD' { 'QWord' }
'REG_EXPAND_SZ' { 'ExpandString' }
'REG_MULTI_SZ' { 'MultiString' }
default {
throw "Internal privacy$([char]0x002E)sexy error: Failed to find data type for: '$rawType'."
}
}
$key = Get-Item -Path $path -ErrorAction Stop
$currentData = $key.GetValue($name, $null, [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames)
if ($currentData -ne $null) {
$currentType = $key.GetValueKind($name)
if (($type -eq $currentType) -and ($currentData -eq $data)) {
Write-Output 'Skipping, no action needed.'
Exit 0
}
}
Set-ItemProperty `
-Path $path `
-Name $name `
-Value $data `
-Type $type `
-Force `
-ErrorAction Stop
Write-Host 'Successfully configured.'
revertCode: |-
{{ with $deleteOnRevert }}
reg delete '{{ $keyPath }}' `
/v '{{ $valueName }}' `
/f 2>$null
$name = '{{ $valueName }}'
$rawPath = '{{ $keyPath }}'
Write-Host "Restoring '$name' registry value in '$rawPath' by deleting."
$hive = $rawPath.Split('\')[0]
$path = $hive + ':' + $rawPath.Substring($hive.Length)
if (!(Test-Path $path)) {
Write-Host 'Skipping: Parent key does not exist; no action needed.'
Exit 0
}
Remove-ItemProperty `
-Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer' `
-Name 'DisallowRun' `
-Force `
-ErrorAction Stop
{{ end }}{{ with $dataOnRevert }}
$revertData = '{{ . }}'
$name = '{{ $valueName }}'
$rawPath = '{{ $keyPath }}'
$data = '{{ $data }}'
$rawType = '{{ $dataType }}'
{{ with $evaluateDataAsPowerShell }}
$revertData = $({{ . }})
$data = $({{ $data }})
{{ end }}
reg add '{{ $keyPath }}' `
/v '{{ $valueName }}' `
/t '{{ $dataType }}' `
/d "$revertData" `
/f
Write-Host "Restoring '$name' registry value in '$rawPath' by updating."
$hive = $rawPath.Split('\')[0]
$path = $hive + ':' + $rawPath.Substring($hive.Length)
if (!(Test-Path $path)) {
New-Item -Path $path -Force -ErrorAction Stop | Out-Null
Write-Host 'Successfully created the parent registry key.'
}
$type = switch ($rawType) {
'REG_SZ' { 'String' }
'REG_DWORD' { 'DWord' }
'REG_QWORD' { 'QWord' }
'REG_EXPAND_SZ' { 'ExpandString' }
'REG_MULTI_SZ' { 'MultiString' }
default {
throw "Internal privacy$([char]0x002E)sexy error: Failed to find data type for: '$rawType'."
}
}
$key = Get-Item -Path $path -ErrorAction Stop
$currentData = $key.GetValue($name, $null, [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames)
if ($currentData -ne $null) {
$currentType = $key.GetValueKind($name)
if (($type -eq $currentType) -and ($currentData -eq $data)) {
Write-Output 'Skipping, no action needed.'
Exit 0
}
}
Set-ItemProperty `
-Path $path `
-Name $name `
-Value $data `
-Type $type `
-Force `
-ErrorAction Stop
Write-Host 'Successfully restored.'
{{ end }}
-
name: EnableTLSProtocol
Expand Down

0 comments on commit b44c074

Please sign in to comment.