Skip to content

Commit

Permalink
🐛 Fix issue with Katas' verification script (#328)
Browse files Browse the repository at this point in the history
CheckRestrictedLanguage() is far too stringent.
It prevents usage of basic array indexing and member access.

Switch to detecting command names in the AST.
This also lets users define sub-functions if they want.

Only functions defined in the test function itself will be allowed.
  • Loading branch information
vexx32 authored Nov 24, 2019
1 parent 87b2585 commit 9b028d1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
25 changes: 21 additions & 4 deletions PSKoans/Koans/Katas/ProcessingStrings.Koans.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using module PSKoans
using namespace System.Management.Automation.Language
using namespace System.Collections.Generic
[Koan(Position = 150)]
param()
<#
Expand Down Expand Up @@ -51,10 +53,25 @@ Describe "The Stock Challenge" {
#>

$Verification = {
[string[]]$AllowedCommands = @()
[string[]]$AllowedVariables = @("*")
$Function = Get-Item -Path 'Function:Get-GreatestVarianceDate'
$Function.ScriptBlock.CheckRestrictedLanguage($AllowedCommands, $AllowedVariables, $false)
$Functions = [Hashset[string]]::new([StringComparer]::OrdinalIgnoreCase)
$Ast = (Get-Command 'Get-GreatestVarianceDate' -CommandType Function).ScriptBlock.Ast
$Ast.FindAll(
{
param($node)
if ($node -is [CommandAst] -and ($name = $node.GetCommandName()) -and !$Functions.Contains($name)) {
throw 'Usage of external cmdlets and functions is not permitted.'
}

if ($node -is [FunctionDefinitionAst]) {
$Functions.Add($node.Name) > $null
return
}

if ($node.Left -is [VariableExpressionAst] -and $node.Left.VariablePath.DriveName -eq 'Function') {
$Functions.Add($node.Left.VariablePath.UserPath -replace '^function:') > $null
}
}, $true
)
}

function Get-GreatestVarianceDate {
Expand Down
23 changes: 19 additions & 4 deletions PSKoans/Koans/Katas/SortingCharacters.Koans.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,25 @@ param()
Describe 'Kata - Sorting Characters' {
BeforeAll {
$Verification = {
[string[]]$AllowedCommands = @()
[string[]]$AllowedVariables = @("*")
$Function = Get-Item -Path 'Function:Get-SortedString'
$Function.ScriptBlock.CheckRestrictedLanguage($AllowedCommands, $AllowedVariables, $false)
$Functions = [Hashset[string]]::new([StringComparer]::OrdinalIgnoreCase)
$Ast = (Get-Command 'Get-GreatestVarianceDate' -CommandType Function).ScriptBlock.Ast
$Ast.FindAll(
{
param($node)
if ($node -is [CommandAst] -and ($name = $node.GetCommandName()) -and !$Functions.Contains($name)) {
throw 'Usage of external cmdlets and functions is not permitted.'
}

if ($node -is [FunctionDefinitionAst]) {
$Functions.Add($node.Name) > $null
return
}

if ($node.Left -is [VariableExpressionAst] -and $node.Left.VariablePath.DriveName -eq 'Function') {
$Functions.Add($node.Left.VariablePath.UserPath -replace '^function:') > $null
}
}, $true
)
}

function Get-SortedString {
Expand Down

0 comments on commit 9b028d1

Please sign in to comment.