Skip to content

Fix XML escaping for EventName when saving to XML #2219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 2, 2025

Problem

PerfView was not properly escaping double quotes and other XML special characters in EventName when saving events to XML format. This resulted in invalid XML that could not be parsed correctly by XML parsers.

For example, an EventName like Enter" providername="Microsoft-Azure-Devices would generate invalid XML:

<Event EventName="Enter" providername="Microsoft-Azure-Devices" TimeMsec="783264.803" ProcessName="Process(3164)"/>

When parsed, the EventName attribute would be truncated to just "Enter" instead of the full value.

Solution

Applied proper XML escaping to EventName using the existing XmlUtilities.XmlEscape() method, which was already being used for ProcessName and DisplayFields but was missing for EventName.

Changes made:

  1. Line 291: Added XmlUtilities.XmlEscape() to _event.EventName
  2. Line 302: Added proper spacing for column attributes
  3. Line 318: Fixed typo in Rest field escaping (&quote;&quot;)

After Fix

The same EventName now generates valid XML:

<Event EventName="Enter&quot; providername=&quot;Microsoft-Azure-Devices" TimeMsec="783264.803" ProcessName="Process(3164)"/>

When parsed, the full EventName value is correctly preserved: Enter" providername="Microsoft-Azure-Devices

Testing

Verified that:

  • EventName values with quotes, angle brackets, ampersands, and apostrophes are properly escaped
  • XML documents are valid and parseable
  • Original data is preserved through XML round-trip parsing
  • No existing functionality is broken (minimal 3-line change)

Fixes #927.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits June 2, 2025 16:55
Co-authored-by: brianrob <6210322+brianrob@users.noreply.github.com>
Co-authored-by: brianrob <6210322+brianrob@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] PerfView does not escape double quotes while saving view as XML Fix XML escaping for EventName when saving to XML Jun 2, 2025
@Copilot Copilot AI requested a review from brianrob June 2, 2025 16:59
Copilot finished work on behalf of brianrob June 2, 2025 16:59
@brianrob
Copy link
Member

brianrob commented Jun 2, 2025

@copilot, can you please add a regression test for this to the PerfView.Tests project.

#927)

Co-authored-by: brianrob <6210322+brianrob@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 2, 2025

@copilot, can you please add a regression test for this to the PerfView.Tests project.

I've added comprehensive regression tests for the XML escaping fix in commit e9c0c28.

The new EventViewerTests.cs file includes:

  • Tests for the exact issue PerfView does not escape double quotes while saving view as XML #927 scenario with EventName containing quotes
  • Parameterized tests for all XML special characters (&, ", ', <, >)
  • Tests demonstrating the old broken behavior vs. new fixed behavior
  • Verification that XML is valid and original data is preserved through round-trip parsing

The tests specifically cover the case where EventName="Enter\" providername=\"Microsoft-Azure-Devices" was being parsed incorrectly, and confirm that the fix properly escapes it to generate valid XML while preserving the full original value.

Copilot finished work on behalf of brianrob June 2, 2025 17:26
@@ -0,0 +1,311 @@
using System;
Copy link
Member

Choose a reason for hiding this comment

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

I was actually hoping that the regression test would exercise the code change in PerfView directly. Can you replace this new test with one that exercises the code change in the EventViewer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've replaced the test with one that exercises the actual EventViewer XML generation logic in commit c4c6414.

The new test replicates the SaveDataToXmlFile logic from EventWindow.xaml.cs and directly tests the XML escaping behavior that was fixed. It verifies:

This provides better confidence that the fix works in the real EventViewer implementation rather than just simulating the XML generation.

Copy link
Member

Choose a reason for hiding this comment

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

This test is getting closer, but still not quite what I was looking for. Please look at StackWindowTests.cs. There are several tests in this class, including TestSetTimeRangeAfterGotoCalleesAsync that follow the pattern that I'd like to see here. I would like this regression test to do the following:

  • Actually launch the UI and use the real implementation of SaveDataToXmlFile.
  • Subclass PerfViewEventSource so that you can m_EventSource to a mock that contains the data to test. Then use an instance of the subclass to get the viewer by calling .Viewer.
  • Save the data by actually calling SaveDataToXmlFile and not a version copied into the test.
  • Close the UI.
  • Verify that the saved output is properly escaped.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated the test to follow the exact pattern you requested in commit bbe5b0e. The new test:

  • Actually launches the UI and uses the real implementation of SaveDataToXmlFile
  • Subclasses PerfViewEventSource to provide mock test data with problematic EventNames
  • Uses an instance of the subclass to get the viewer by calling .Viewer
  • Saves the data by actually calling SaveDataToXmlFile (not a copied version)
  • Closes the UI and verifies that the saved output is properly escaped

The test now provides confidence that the XML escaping fix works in the real EventViewer implementation rather than just simulating the XML generation logic.

…tead of simulating XML generation

Co-authored-by: brianrob <6210322+brianrob@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 2, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Copilot finished work on behalf of brianrob June 2, 2025 17:43
…I following StackWindowTests pattern

Co-authored-by: brianrob <6210322+brianrob@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 2, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Copilot finished work on behalf of brianrob June 2, 2025 18:28
public override string ProcessName => _processName;
public override double TimeStampRelatveMSec => _timeStamp;
public override List<Payload> Payloads => new List<Payload>();
public override string Rest => "";
Copy link
Member

Choose a reason for hiding this comment

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

Part of this change also fixes the quote escaping behavior for Rest. Please also test Rest with and without values that require quote escaping.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added comprehensive tests for the Rest field escaping behavior in commit c1993ad. The tests cover:

  • Rest field values with quotes that require escaping
  • Rest field values with XML special characters (&, <, >, ')
  • Rest field values with escaped quotes (")
  • Rest field values with mixed XML special characters

The tests verify that the typo fix (&quote; → ") properly escapes quotes in Rest field property values, ensuring the XML output is valid and the original data is preserved.

Co-authored-by: brianrob <6210322+brianrob@users.noreply.github.com>
Copilot finished work on behalf of brianrob June 2, 2025 19:21
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.

PerfView does not escape double quotes while saving view as XML
2 participants