Skip to content

Fix XML parameter encoding for SQL Server#38462

Open
Copilot wants to merge 9 commits into
mainfrom
copilot/fix-38429-sqlxml-parameters
Open

Fix XML parameter encoding for SQL Server#38462
Copilot wants to merge 9 commits into
mainfrom
copilot/fix-38429-sqlxml-parameters

Conversation

Copilot AI commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Fixes #38429

Unicode xml SQL literal: SqlServerStringTypeMapping now treats the xml store type as Unicode (_isUtf16 = true), so GenerateNonNullSqlLiteral emits an N-prefixed literal. It also sets DbType = Xml on the parameter and sends the value as SqlXml. Previously a non-Unicode (varchar) literal was generated for xml, silently losing non-ASCII content.

Copilot AI and others added 4 commits June 18, 2026 23:55
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
…mlReader

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Comment thread src/EFCore.SqlServer/Storage/Internal/SqlServerStringTypeMapping.cs Outdated
Comment thread src/EFCore.SqlServer/Storage/Internal/SqlServerStringTypeMapping.cs Outdated
Comment thread src/EFCore.SqlServer/Storage/Internal/SqlServerStringTypeMapping.cs
Comment thread test/EFCore.SqlServer.FunctionalTests/BuiltInDataTypesSqlServerTest.cs Outdated
Comment thread test/EFCore.SqlServer.FunctionalTests/BuiltInDataTypesSqlServerTest.cs Outdated
…rateNonNullSqlLiteral test

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Comment thread test/EFCore.SqlServer.Tests/Storage/SqlServerStringTypeMappingTest.cs Outdated
Comment thread test/EFCore.SqlServer.FunctionalTests/BuiltInDataTypesSqlServerTest.cs Outdated
Comment thread test/EFCore.SqlServer.FunctionalTests/BuiltInDataTypesSqlServerTest.cs Outdated
…xml literal unit test

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI requested a review from AndriySvyryd June 19, 2026 02:53
@AndriySvyryd AndriySvyryd changed the title Move Xml_value_round_trips to BuiltInDataTypesSqlServerTest and harden the xml parameter XmlReader Fix XML parameter encoding for SQL Server Jun 19, 2026
@AndriySvyryd AndriySvyryd requested a review from Copilot June 19, 2026 04:20

Copilot AI left a comment

Copy link
Copy Markdown

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 improves SQL Server xml handling in EF Core by ensuring XML values are sent as SqlDbType.Xml (using SqlXml) and by hardening the XML parsing path against DTD/XXE-style payloads, with accompanying unit/functional test coverage.

Changes:

  • Update SQL Server string type mapping to treat xml as Unicode for SQL literal generation and to convert XML string parameters into SqlXml using secure XmlReaderSettings.
  • Update the SQL Server type mapping source so the xml mapping is explicitly configured with SqlDbType.Xml.
  • Add/extend tests to validate SqlXml parameterization, round-tripping behavior, and rejection of DTD payloads.

Reviewed changes

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

File Description
test/EFCore.SqlServer.Tests/Storage/SqlServerTypeMappingTest.cs Adds unit tests asserting xml parameters are sent as SqlDbType.Xml and values are SqlXml.
test/EFCore.SqlServer.FunctionalTests/BuiltInDataTypesSqlServerTest.cs Adds functional round-trip/query coverage for xml values and a negative test for DTD payload rejection; configures an xml-typed entity property in the fixture model.
src/EFCore.SqlServer/Storage/Internal/SqlServerTypeMappingSource.cs Configures the xml mapping with sqlDbType: SqlDbType.Xml so parameters can be sent correctly as XML.
src/EFCore.SqlServer/Storage/Internal/SqlServerStringTypeMapping.cs Adds secure XML reader settings and converts XML string parameters to SqlXml; adjusts Unicode literal handling for xml.

Comment thread test/EFCore.SqlServer.Tests/Storage/SqlServerTypeMappingTest.cs
Comment thread src/EFCore.SqlServer/Storage/Internal/SqlServerStringTypeMapping.cs Outdated
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
@AndriySvyryd AndriySvyryd marked this pull request as ready for review June 19, 2026 04:42
@AndriySvyryd AndriySvyryd requested a review from a team as a code owner June 19, 2026 04:42
Copilot AI review requested due to automatic review settings June 19, 2026 04:42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

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

Comment thread src/EFCore.SqlServer/Storage/Internal/SqlServerStringTypeMapping.cs
Comment thread test/EFCore.SqlServer.Tests/Storage/SqlServerTypeMappingTest.cs
Comment thread test/EFCore.SqlServer.Tests/Storage/SqlServerTypeMappingTest.cs Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 19, 2026 06:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comment thread src/EFCore.SqlServer/Storage/Internal/SqlServerStringTypeMapping.cs Outdated
Copilot AI review requested due to automatic review settings June 19, 2026 22:07
@AndriySvyryd AndriySvyryd force-pushed the copilot/fix-38429-sqlxml-parameters branch from 46e9376 to 3bfc384 Compare June 19, 2026 22:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comment thread src/EFCore.SqlServer/Storage/Internal/SqlServerStringTypeMapping.cs
@AndriySvyryd AndriySvyryd force-pushed the copilot/fix-38429-sqlxml-parameters branch from 3bfc384 to 9ef8296 Compare June 19, 2026 22:24
@AndriySvyryd AndriySvyryd requested a review from cincuranet June 19, 2026 22:24

if (_sqlDbType == SqlDbType.Xml
&& value is string stringValue
&& stringValue.StartsWith(Utf8XmlDeclaration, StringComparison.Ordinal))

@Charlieface Charlieface Jun 20, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is not a valid way to check a prolog. It perhaps covers a lot of cases, but the version number can now be 1.1 as well as 1.0, and there are many encodings that are not UTF-8 or UTF-16, and the standalone parameter is also optional. And whitespace is optional as well. See https://www.w3.org/TR/2006/REC-xml11-20060816/#sec-prolog-dtd for more info.

If you really want to go down the road of validating the declaration then you will definitely need an XmlReader, but maybe read it manually and dump it as soon as the declaration is confirmed correct. Might be difficult/impossible to rewind it in the case when the encoding needs to change, probably will need a new XmlReader in that case.

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.

Ok, sounds like just passing the XmlReader in SqlXml would be the more robust solution.
And perhaps you can open an issue for SqlClient to stream it instead of converting to a string.

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.

xml constants do not have the N prefix

5 participants