Skip to content

Manifest rules throw NullReferenceException on a .psd1 with a non-string key #2305

Backlog

Description

@Dashtid

Steps to reproduce

PSAvoidUsingDeprecatedManifestFields, PSMissingModuleManifestField and PSUseToExportFieldsInManifest each throw on a .psd1 data file whose top-level hashtable has a key that is not a string:

"@{ 0 = 'OK' }" | Set-Content -Path data.psd1
Invoke-ScriptAnalyzer -Path ./data.psd1

Expected behavior

No output, as for @{ a = 'OK' }. The file is valid data (Import-PowerShellDataFile reads the key as the integer 0) and not a module manifest, so the manifest rules have nothing to report.

Actual behavior

One error from each manifest rule:

Invoke-ScriptAnalyzer: Object reference not set to an instance of an object.
Invoke-ScriptAnalyzer: Object reference not set to an instance of an object.
Invoke-ScriptAnalyzer: Object reference not set to an instance of an object.

Any key that is not a string constant does it, wherever it sits in the top-level hashtable (1 = 'a', $true = 'x', (1 + 1) = 'x'), and with -ErrorAction Stop the first error ends the run. $error[0] | Select-Object *, trimmed and with the path shortened:

Exception             : System.NullReferenceException: Object reference not set to an instance of an object.
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper.IsModuleManifest(String filepath, Version powershellVersion)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseToExportFieldsInManifest.AnalyzeScript(Ast ast, String fileName)+MoveNext()
                           at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
                           at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass84_1.<AnalyzeSyntaxTree>b__2()
TargetObject          : C:\repro\data.psd1
CategoryInfo          : InvalidOperation: (C:\repro\data.psd1:String) [Invoke-ScriptAnalyzer], NullReferenceException
FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand

GetMapFromHashtableAst in Engine/Helper.cs returns null for such a key, and IsModuleManifest uses the map without a check. PSDSCUseIdenticalMandatoryParametersForDSC reaches the same code when such a file is listed before the manifest in the module folder of a DSC resource.

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.6.9
PSEdition                      Core
GitCommitId                    7.6.9
OS                             Microsoft Windows 10.0.26300
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.4
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }

1.25.0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions