Skip to content

Add support for inline XML format data in Update-FormatData#27713

Open
eevaal wants to merge 3 commits into
PowerShell:masterfrom
eevaal:feat/update-formatdata-xml
Open

Add support for inline XML format data in Update-FormatData#27713
eevaal wants to merge 3 commits into
PowerShell:masterfrom
eevaal:feat/update-formatdata-xml

Conversation

@eevaal

@eevaal eevaal commented Jul 22, 2026

Copy link
Copy Markdown

PR Summary

This PR allows the Update-FormatData cmdlet to accept formatting configuration (PS1XML) directly from an in-memory string. It introduces two new string array parameters: -PrependFormatData and -AppendFormatData.

Under the hood:

  • Adds a new FormatDataXml property and an XmlDocument constructor to SessionStateFormatEntry for secure encapsulation of the raw string.
  • Updates TypeInfoDataBaseLoader to parse inline XML strings without requiring physical disk I/O.
  • Plumbs the inline XML capabilities into UpdateFormatDataCommand via the TypeInfoDataBaseManager.
  • Adds Pester unit tests to ensure that -PrependFormatData and -AppendFormatData correctly load views.

PR Context

Resolves #7845

Currently, module authors who use tools like ModuleBuilder to pack their modules into a single .psm1 file for better performance cannot easily embed their format data (.ps1xml file contents). They are forced to ship external .ps1xml files alongside the module, which also complicates code signing (since the XML file must be signed separately).

By allowing Update-FormatData to accept raw XML strings, module authors can now natively store and apply formatting rules entirely in memory.

PR Checklist

Addresses PowerShell#7845 by allowing Update-FormatData to accept format data directly as XML strings using the -PrependFormatData and -AppendFormatData parameters.

- Adds FormatDataXml property and constructor to SessionStateFormatEntry.

- Updates TypeInfoDataBaseLoader to parse format data XML from strings without disk I/O.

- Plumbs the inline XML capabilities into UpdateFormatDataCommand through TypeInfoDataBaseManager.

- Adds unit tests covering new parameters.
Copilot AI review requested due to automatic review settings July 22, 2026 16:19
@eevaal
eevaal requested a review from a team as a code owner July 22, 2026 16:19
@azure-pipelines

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

- Add blank lines before doc comments

- Group fields before properties

- Add Justification to SuppressMessage attributes

- Use 'Gets or sets' in property summaries

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 PR extends Update-FormatData to accept PS1XML formatting configuration as in-memory XML strings, enabling module authors to embed format data without shipping external .ps1xml files (addressing #7845).

Changes:

  • Adds new Update-FormatData parameters -PrependFormatData / -AppendFormatData for inline format XML input.
  • Introduces SessionStateFormatEntry.FormatDataXml and plumbing through TypeInfoDataBaseManager/loader to load format data from XML strings.
  • Adds Pester coverage intended to validate loading views from inline XML.

Reviewed changes

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

Show a summary per file
File Description
test/powershell/Modules/Microsoft.PowerShell.Utility/Update-FormatData.Tests.ps1 Adds tests for loading format views from inline XML strings.
src/Microsoft.PowerShell.Commands.Utility/commands/utility/Update-TypeData.cs Adds cmdlet parameters and populates InitialSessionState.Formats with inline XML entries.
src/System.Management.Automation/engine/InitialSessionState.cs Adds FormatDataXml storage on SessionStateFormatEntry and cloning support.
src/System.Management.Automation/utils/FormatAndTypeDataHelper.cs Adds support for carrying inline format XML through PSSnapInTypeAndFormatErrors.
src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader.cs Adds LoadXmlString(...) to parse/load formatting data from an XML string.
src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataManager.cs Hooks inline XML loading into the format database load path.

Comment on lines +978 to +980
var doc = new System.Xml.XmlDocument();
doc.LoadXml(PrependFormatData[i]);
newFormats.Add(new SessionStateFormatEntry(doc));
Comment on lines +1018 to +1020
var doc = new System.Xml.XmlDocument();
doc.LoadXml(appendFormatDataItem);
newFormats.Add(new SessionStateFormatEntry(doc));
Comment on lines +369 to +373
else if (FormatDataXml != null)
{
var doc = new XmlDocument();
doc.LoadXml(FormatDataXml);
entry = new SessionStateFormatEntry(doc);
Comment on lines +48 to +53
internal PSSnapInTypeAndFormatErrors(string psSnapinName, string formatDataXml, bool isXml)
{
this.psSnapinName = psSnapinName;
FormatDataXml = formatDataXml;
Errors = new ConcurrentBag<string>();
}
Security (High): Replace unsafe XmlDocument.LoadXml() with XmlReader using DtdProcessing.Prohibit and XmlResolver=null to prevent XXE and DTD entity expansion attacks. Fixed in Update-TypeData.cs (Prepend + Append) and InitialSessionState.Clone().

Tests (Medium): Fix variable scoping in Pester tests by passing xmlContent via param()/AddArgument() instead of string interpolation.
@eevaal
eevaal force-pushed the feat/update-formatdata-xml branch from ad795c7 to 9b555f4 Compare July 22, 2026 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Supported way to load ExtendedTypeDefinition

2 participants