Skip to content

Add the xsd validation back for CIM cmdlets - #27869

Merged
Dongbo Wang (daxian-dbw) merged 1 commit into
PowerShell:masterfrom
anamnavi:cim_validation
Aug 20, 2026
Merged

Dongbo Wang (daxian-dbw) merged 1 commit into
PowerShell:masterfrom
anamnavi:cim_validation

Conversation

@anamnavi

@anamnavi Anam Navied (anamnavi) commented Aug 20, 2026

Copy link
Copy Markdown
Member

PR Summary

This pull request improves the security and reliability of CDXML cmdlet XML schema validation in PowerShell. The main changes include embedding the cmdlets-over-objects.xsd schema as a resource, updating the XML schema validation logic to use this embedded resource, and adding a test to ensure that XML is valid.

Schema validation improvements:

  • The cmdlets-over-objects.xsd XML schema file is now embedded as a resource in the System.Management.Automation assembly, ensuring it is always available at runtime and not dependent on external files.
  • The static constructor of ScriptWriter in ScriptWriter.cs is refactored to load the embedded schema resource for XML validation, replacing the previous approach that relied on resource strings and conditional compilation. This ensures proper schema validation and improved security.

Testing:

  • A new test is added to Cdxml.Tests.ps1 to verify that attempts to use invalid schema via the Verb attribute in a CDXML file are correctly blocked.

Minor cleanup:

  • An unused System.Windows.Forms assembly reference is removed from CmdletizationResources.resx.

PR Context

PR Checklist

Copilot AI lite review requested due to automatic review settings August 20, 2026 15:39
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request restores and hardens CDXML schema validation for CIM cmdlets by embedding the cmdlets-over-objects.xsd schema into System.Management.Automation, updating ScriptWriter to load the schema from the embedded resource, and adding a regression test to ensure malformed CDXML is rejected.

Changes:

  • Embed cmdlets-over-objects.xsd into System.Management.Automation and load it via GetManifestResourceStream for schema validation.
  • Refactor ScriptWriter XML reader settings to consistently enable schema validation.
  • Add a CDXML test asset and a Pester test to validate rejection of an injected Verb attribute.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
test/powershell/engine/Cdxml/Cdxml.Tests.ps1 Adds a regression test to ensure CDXML import rejects injected/malformed Verb values.
test/powershell/engine/Cdxml/assets/invalid_verb/invalid_verb.cdxml Adds a malicious/invalid CDXML asset used by the new regression test.
src/System.Management.Automation/System.Management.Automation.csproj Embeds cmdlets-over-objects.xsd as an assembly resource for runtime availability.
src/System.Management.Automation/cimSupport/cmdletization/ScriptWriter.cs Loads the embedded XSD and configures XML schema validation for cmdletization metadata.
src/Microsoft.PowerShell.Commands.Management/resources/CmdletizationResources.resx Removes an explicit System.Windows.Forms assembly alias entry from the RESX.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +56 to +61
using Stream xsdStream = typeof(ScriptWriter).Assembly.GetManifestResourceStream("cmdlets-over-objects.xsd");
XmlReader cmdletizationSchemaReader = XmlReader.Create(xsdStream, s_xmlReaderSettings);

s_xmlReaderSettings.Schemas = new XmlSchemaSet();
s_xmlReaderSettings.Schemas.Add(null, cmdletizationSchemaReader);
s_xmlReaderSettings.Schemas.XmlResolver = null; // do not fetch external documents

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not necessary. It's better to add a test to make sure the embedded resource is available.
I will add the test in a separate PR.

Comment thread test/powershell/engine/Cdxml/Cdxml.Tests.ps1 Outdated
@TravisEz13 Travis Plunk (TravisEz13) added CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log CL-Engine Indicates that a PR should be marked as an engine change in the Change Log and removed CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log labels Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-Engine Indicates that a PR should be marked as an engine change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants