Skip to content

Commit 64d0907

Browse files
author
James Brundage
committed
ValidateScriptBlock: Improving Detection and Fixing Bugs (Fixes #227 Fixes #225)
1 parent 0784d75 commit 64d0907

File tree

2 files changed

+44
-32
lines changed

2 files changed

+44
-32
lines changed

Transpilers/Parameters/ValidateScriptBlock.psx.ps1

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -301,42 +301,48 @@ return $true}
301301
param(`$ast)
302302
`$included = $(
303303
if (-not $IncludeType) { '$null' }
304-
@($(foreach ($incT in $IncludeType) {
305-
if ($incT -is [string]) {
306-
"'$($incT -replace "'","''")'"
304+
@($(foreach ($inc in $IncludeType) {
305+
if ($inc -is [string]) {
306+
"'$($inc -replace "'","''")'"
307307
}
308-
elseif ($incT -is [type]) {
309-
"[$($incT.FullName -replace '^System\.')]"
308+
elseif ($inc -is [type]) {
309+
"[$($inc.FullName -replace '^System\.')]"
310310
}
311-
elseif ($incT -is [regex]) {
312-
"[Regex]::new('$($incT.ToString().Replace("'","''"))','$($incT.Options)','$($incT.MatchTimeout)')"
311+
elseif ($inc -is [regex]) {
312+
"[Regex]::new('$($inc.ToString().Replace("'","''"))','$($inc.Options)','$($inc.MatchTimeout)')"
313313
}
314314
})) -join ',')
315315
`$excluded = $(@(
316316
if (-not $ExcludeType) { '$null' }
317-
$(foreach ($excT in $ExcludeType) {
318-
if ($excT -is [string]) {
319-
"'$($excT -replace "'","''")'"
317+
$(foreach ($exc in $ExcludeType) {
318+
if ($exc -is [string]) {
319+
"'$($exc -replace "'","''")'"
320320
}
321-
elseif ($excT -is [type]) {
322-
"[$($excT.FullName -replace '^System\.')]"
321+
elseif ($exc -is [type]) {
322+
"[$($exc.FullName -replace '^System\.')]"
323323
}
324-
elseif ($excT -is [regex]) {
325-
"[Regex]::new('$($excT.ToString().Replace("'","''"))','$($excT.Options)','$($excT.MatchTimeout)')"
324+
elseif ($exc -is [regex]) {
325+
"[Regex]::new('$($exc.ToString().Replace("'","''"))','$($exc.Options)','$($exc.MatchTimeout)')"
326326
}
327327
})) -join ',')
328-
if (`$ast -is [Management.Automation.Language.TypeExpressionAst]) {
328+
if (`$ast -is [Management.Automation.Language.TypeExpressionAst] -or
329+
`$ast -is [Management.Automation.Language.TypeConstraintAst]) {
330+
$({
331+
$astType = $ast.TypeName
332+
$reflectionType = if ($astType) {
333+
$astType.GetReflectionType()
334+
}
335+
})
329336
$(if ($IncludeType) {
330337
{
331338
foreach ($inc in $included) {
332-
if ($inc -is [string] -and $ast.TypeName -like $inc) {
339+
if ($inc -is [string] -and $astType -like $inc) {
333340
return $true
334341
}
335-
elseif ($inc -is [Regex] -and $ast.TypeName -match $inc) {
342+
elseif ($inc -is [Regex] -and $astType -match $inc) {
336343
return $true
337344
}
338-
elseif ($inc -is [type]){
339-
$reflectionType = $ast.TypeName.GetReflectionType()
345+
elseif ($inc -is [type]){
340346
if ($inc -eq $reflectionType) { return $true}
341347
if ($inc.IsSubclassOf($reflectionType) -or $reflectionType.IsSubclassOf($inc)) {
342348
return $true
@@ -353,14 +359,13 @@ $(if ($IncludeType) {
353359
$({
354360
$throwMessage = "[$($ast.Typename)] is not allowed"
355361
foreach ($exc in $excluded) {
356-
if ($exc -is [string] -and $ast.TypeName -like $exc) {
362+
if ($exc -is [string] -and $astType -like $exc) {
357363
throw $throwMessage
358364
}
359-
elseif ($exc -is [regex] -and $ast.TypeName -match $exc) {
365+
elseif ($exc -is [regex] -and $astType -match $exc) {
360366
throw $throwMessage
361367
}
362-
elseif ($exc -is [type]) {
363-
$reflectionType = $ast.TypeName.GetReflectionType()
368+
elseif ($exc -is [type]) {
364369
if ($ecx -eq $reflectionType) {
365370
throw $throwMessage
366371
}
@@ -398,6 +403,7 @@ if ($ast -is [Management.Automation.Language.LoopStatementAst] -and
398403
$ast.GetType().Name -match '(?>do|while)') {
399404
throw "ScriptBlock cannot contain $($ast.GetType().Name)"
400405
}
406+
return $true
401407
}
402408
}
403409

Transpilers/Parameters/ValidateScriptBlock.tests.ps1

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,22 @@ describe ValidateScriptBlock {
1818
{
1919
& ({
2020
[ValidateScriptBlock(NoBlocks)]$Sb = { process { 1 } }
21-
22-
& $sb
2321
}.Transpile())
2422
} | Should -Throw
2523
}
2624

2725
it 'Can ensure a ScriptBlock may only -IncludeCommand' {
2826
{
2927
& ({
30-
[ValidateScriptBlock(IncludeCommand='*-Process')]$Sb = { Get-Command }
28+
[ValidateScriptBlock(IncludeCommand='*-Process')]$Sb = { Get-Command }
29+
}.Transpile())
30+
} | Should -Throw
31+
}
3132

32-
& $sb
33+
it 'Can ensure a ScriptBlock may not -IncludeType' {
34+
{
35+
& ({
36+
[ValidateScriptBlock(IncludeType='[int]')]$Sb = { [string]"hi" }
3337
}.Transpile())
3438
} | Should -Throw
3539
}
@@ -38,19 +42,21 @@ describe ValidateScriptBlock {
3842
{
3943
& ({
4044
[ValidateScriptBlock(NoLoop)]$Sb = { foreach ($n in 1..100) {$n} }
41-
42-
& $sb
4345
}.Transpile())
4446
} | Should -Throw
4547
}
4648

4749
it 'Can ensure a ScriptBlock has -NoWhileLoop' {
4850
{
4951
& ({
50-
[ValidateScriptBlock(NoLoop)]$Sb = { while (1) {$n} }
51-
52-
& $sb
52+
[ValidateScriptBlock(NoWhileLoop)]$Sb = { while (1) {$n} }
5353
}.Transpile())
5454
} | Should -Throw
55+
56+
& ({
57+
[ValidateScriptBlock(NoWhileLoop)]$Sb = { foreach ($n in 1..1) { $n } }
58+
& $sb
59+
}.Transpile()) |
60+
Should -be 1
5561
}
5662
}

0 commit comments

Comments
 (0)