@@ -266,6 +266,23 @@ function next($er) {
266266 Write-Host $er - ForegroundColor DarkRed
267267}
268268
269+ function Get-CheckverScriptPrelude () {
270+ return @ (
271+ ' $ErrorActionPreference = "Stop"' ,
272+ ' $ProgressPreference = "SilentlyContinue"' ,
273+ # Set allowed env vars, `SystemRoot` is required for irm/iwr
274+ ' $allowedEnvs = @("SystemRoot","TEMP","TMP")' ,
275+ ' Get-ChildItem Env: | Where-Object { $allowedEnvs -notcontains $_.Name } | Remove-Item' ,
276+ # Use `UseBasicParsing` for Invoke-WebRequest by default (WindowsPowerShell)
277+ ' $PSDefaultParameterValues["Invoke-WebRequest:UseBasicParsing"] = $true' ,
278+ # Use ConstrainedLanguage language mode to restrict checkver.script capabilities
279+ ' $ExecutionContext.SessionState.LanguageMode = "ConstrainedLanguage"' ,
280+ # Available variables/functions for checkver.script
281+ # - `$page`: the retrieved webpage content
282+ ' $page = $using:page'
283+ )
284+ }
285+
269286# wait for all to complete
270287while ($in_progress -gt 0 ) {
271288 $ev = Wait-Event
@@ -318,12 +335,22 @@ while ($in_progress -gt 0) {
318335 $page = (New-Object System.IO.StreamReader($ms , (Get-Encoding $wc ))).ReadToEnd()
319336 }
320337
338+ # checkver.script evaluation
321339 if ($script ) {
322- $page = Invoke-Command ([scriptblock ]::Create($script -join " `r`n " ))
323- $source = ' the output of script'
340+ # Restrict the capabilities of accessing outer variables via `$using:`
341+ # by replacing possible `$using:` uses with `$` before evaluation
342+ $script = $script -replace ' \$using:' , ' $'
343+ $prelude = Get-CheckverScriptPrelude
344+
345+ $script = $prelude + @ ($script )
346+ $scriptBlock = [scriptblock ]::Create($script -join " `n " )
347+
348+ $page = (Start-Job - ScriptBlock $scriptBlock | Receive-Job - Wait - AutoRemoveJob | Out-String ).Trim()
349+ Write-Verbose " Output from evaluating checkver.script:`r`n $page "
350+ $source = ' the output of checkver.script'
324351 }
325352
326- if ($null -eq $page ) {
353+ if (! $page ) {
327354 next " couldn't retrieve content from $source "
328355 continue
329356 }
0 commit comments