2020-03-24 11:08:37 -07:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
2018-02-13 09:23:53 -08:00
|
|
|
# Licensed under the MIT License.
|
|
|
|
|
Describe "Test restricted language check method on scriptblocks" -Tags "CI" {
|
2016-09-12 10:43:53 -07:00
|
|
|
BeforeAll {
|
2020-05-07 13:00:30 +01:00
|
|
|
Set-StrictMode -v 2
|
2016-09-12 10:43:53 -07:00
|
|
|
function list {
|
|
|
|
|
|
|
|
|
|
$l = [System.Collections.Generic.List[String]]::new()
|
2017-07-22 05:03:49 +01:00
|
|
|
$args | ForEach-Object {$l.Add($_)}
|
2016-09-12 10:43:53 -07:00
|
|
|
, $l
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
AfterAll {
|
|
|
|
|
Set-StrictMode -Off
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It 'Check basic expressions' {
|
|
|
|
|
|
2018-03-21 10:47:08 -07:00
|
|
|
{{2+2}.CheckRestrictedLanguage($null, $null, $false) } | Should -Not -Throw # Succeed with no variables
|
2016-09-12 10:43:53 -07:00
|
|
|
}
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2016-09-12 10:43:53 -07:00
|
|
|
It 'Check default variables' {
|
|
|
|
|
|
2018-03-21 10:47:08 -07:00
|
|
|
{{ $PSCulture, $PSUICulture, $true, $false, $null}.CheckRestrictedLanguage($null, $null, $false) } | Should -Not -Throw
|
2016-09-12 10:43:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It 'Check default variables' {
|
2018-05-17 14:42:04 -07:00
|
|
|
{ {2+$a}.CheckRestrictedLanguage($null, $null, $false) } | Should -Throw -ErrorId 'ParseException'
|
2016-09-12 10:43:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It 'Check union of default + one allowed variables' {
|
|
|
|
|
|
2020-01-11 15:41:59 +00:00
|
|
|
{ { 2 + $a }.CheckRestrictedLanguage($null, (list a), $false) } | Should -Not -Throw # succeed
|
2016-09-12 10:43:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It 'Check union of default + two allowed variables' {
|
|
|
|
|
|
2018-03-21 10:47:08 -07:00
|
|
|
{ { $a + $b }.CheckRestrictedLanguage($null, (list a b), $false) } | Should -Not -Throw # succeed
|
2016-09-12 10:43:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It 'Check union of default + allowed variables' {
|
|
|
|
|
|
2020-01-11 15:41:59 +00:00
|
|
|
{ { $PSCulture, $PSUICulture, $true, $false, $null, $a, $b}.CheckRestrictedLanguage($null, (list a b), $false) } | Should -Not -Throw
|
2016-09-12 10:43:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It 'Check union of default + one disallowed variables' {
|
2018-05-17 14:42:04 -07:00
|
|
|
{ { $a + $b + $c }.CheckRestrictedLanguage($null, (list a b), $false) } | Should -Throw -ErrorId 'ParseException'
|
2016-09-12 10:43:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It 'Check union of default + one allowed variable and but not allow environment variable' {
|
2018-05-17 14:42:04 -07:00
|
|
|
{ { 2 + $a + $env:foo }.CheckRestrictedLanguage($null, (list a), $false) } | Should -Throw -ErrorId 'ParseException'
|
2016-09-12 10:43:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It 'Check union of default + one allowed variable name and allow environment variable ' {
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2018-03-21 10:47:08 -07:00
|
|
|
{{ 2 + $a + $env:foo }.CheckRestrictedLanguage($null, (list a), $true)} | Should -Not -Throw # succeed
|
2016-09-12 10:43:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It 'Check that wildcard allows env even if the flag is set to false' {
|
|
|
|
|
|
2018-03-21 10:47:08 -07:00
|
|
|
{ { 2 + $a + $b + $c + $env:foo }.CheckRestrictedLanguage($null, (list *), $false)} | Should -Not -Throw # succeed
|
2016-09-12 10:43:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It 'Check for restricted commands' {
|
2020-05-07 13:00:30 +01:00
|
|
|
{ {Get-Date}.CheckRestrictedLangauge($null, $null, $false) } | Should -Throw -ErrorId 'MethodNotFound'
|
2016-09-12 10:43:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It 'Check for allowed commands and variables' {
|
|
|
|
|
|
2020-05-07 13:00:30 +01:00
|
|
|
{ { Get-Process | where name -Match $pattern | foreach $prop }.CheckRestrictedLanguage(
|
2016-09-12 10:43:53 -07:00
|
|
|
(list get-process where foreach),
|
|
|
|
|
(list prop pattern)
|
2020-01-11 15:41:59 +00:00
|
|
|
, $false) } | Should -Not -Throw
|
2016-09-12 10:43:53 -07:00
|
|
|
}
|
|
|
|
|
}
|