Description
When using InlinePolicy with C# expressions containing string literals with double quotes, the generated XML does not properly escape the quotes as HTML entities ("). This results in invalid XML that fails Azure API Management policy validation.
Expected Behavior
String literals containing quotes in C# expressions should be automatically escaped as HTML entities in the generated XML output.
Example: StartsWith("/v1/") should become StartsWith("/v1/") in the generated XML.
Actual Behavior
String literals with quotes are left unescaped in the generated XML, causing deployment failures.
Example: StartsWith("/v1/") remains as StartsWith("/v1/") in the XML, breaking XML parsing.
Reproduction Steps
- Create a policy with
InlinePolicy containing a C# expression with quoted string literals
- Use the Azure API Management Policy Toolkit to generate XML
- Observe that quotes are not escaped as HTML entities
- Attempt to deploy to Azure API Management - fails with XML validation errors
Code Example
Policy C# Code
// In a policy class
context.InlinePolicy($"""
<choose>
<when condition="@(context.Request.Url.Path.StartsWith("/v1/"))">
<base />
</when>
<otherwise>
<return-response>
<set-status code="404" reason="Not Found" />
</return-response>
</otherwise>
</choose>
""");
Generated XML (Incorrect)
<choose>
<when condition="@(context.Request.Url.Path.StartsWith("/v1/"))">
<base />
</when>
<otherwise>
<return-response>
<set-status code="404" reason="Not Found" />
</return-response>
</otherwise>
</choose>
Expected XML (Correct)
<choose>
<when condition="@(context.Request.Url.Path.StartsWith("/v1/"))">
<base />
</when>
<otherwise>
<return-response>
<set-status code="404" reason="Not Found" />
</return-response>
</otherwise>
</choose>
Environment
- Azure API Management Policy Toolkit version: 1.0.0
- .NET version: 8.0
- Operating System: Windows
- Build Tool: dotnet CLI
Current Workaround
Using a PowerShell post-processing script to fix escaping after policy generation:
# Fix XML escaping issues in generated policies
$PolicyOutputPath = "deploy/policies"
$files = Get-ChildItem -Path $PolicyOutputPath -Filter "*.xml"
foreach ($file in $files) {
$content = Get-Content -Path $file.FullName -Raw
# Replace unescaped quotes in expressions with HTML entities
$fixed = $content -replace '(?<=@\([^)]*)"([^"]*)"(?=[^)]*\))', '"$1"'
if ($content -ne $fixed) {
Set-Content -Path $file.FullName -Value $fixed -NoNewline
Write-Host "✓ Fixed XML escaping in $($file.Name)" -ForegroundColor Green
}
}
Impact
- Deployment Failure: Prevents successful deployment of policies to Azure API Management
- Manual Intervention Required: Requires manual post-processing to fix generated XML
- Developer Experience: Affects any use of
InlinePolicy with C# expressions containing string literals
- CI/CD Pipeline Impact: Breaks automated deployment workflows without workaround
Root Cause Analysis
The issue appears to be in the XML generation process, likely in one of these components:
InlinePolicyCompiler class
RazorCodeFormatter class
CustomXmlWriter class
The toolkit correctly handles expression parsing but fails to apply proper HTML entity escaping for quotes within string literals when generating the final XML output.
Suggested Fix
- Immediate Fix: Modify the XML generation pipeline to automatically escape quotes in C# string literals as HTML entities
- Long-term Solution: Implement comprehensive HTML entity escaping for all special characters in expressions
- Testing: Add unit tests to verify proper escaping of various special characters in expressions
Technical Details
- The issue specifically affects
InlinePolicy method calls
- Other policy methods using the standard configuration approach do not seem affected
- The problem occurs during the C# to XML compilation phase
- Azure API Management requires valid XML with properly escaped entities
Related Code Locations
Based on repository analysis, the following files may need investigation:
/src/Core/Compiling/Policy/InlinePolicyCompiler.cs
/src/Core/Serialization/RazorCodeFormatter.cs
/src/Core/Serialization/CustomXmlWriter.cs
Additional Context
This issue was discovered during development of Azure API Management policies using the Policy Toolkit v1.0.0. The problem consistently occurs when using string literals containing quotes within C# expressions in InlinePolicy calls.
Description
When using
InlinePolicywith C# expressions containing string literals with double quotes, the generated XML does not properly escape the quotes as HTML entities ("). This results in invalid XML that fails Azure API Management policy validation.Expected Behavior
String literals containing quotes in C# expressions should be automatically escaped as HTML entities in the generated XML output.
Example:
StartsWith("/v1/")should becomeStartsWith("/v1/")in the generated XML.Actual Behavior
String literals with quotes are left unescaped in the generated XML, causing deployment failures.
Example:
StartsWith("/v1/")remains asStartsWith("/v1/")in the XML, breaking XML parsing.Reproduction Steps
InlinePolicycontaining a C# expression with quoted string literalsCode Example
Policy C# Code
Generated XML (Incorrect)
Expected XML (Correct)
Environment
Current Workaround
Using a PowerShell post-processing script to fix escaping after policy generation:
Impact
InlinePolicywith C# expressions containing string literalsRoot Cause Analysis
The issue appears to be in the XML generation process, likely in one of these components:
InlinePolicyCompilerclassRazorCodeFormatterclassCustomXmlWriterclassThe toolkit correctly handles expression parsing but fails to apply proper HTML entity escaping for quotes within string literals when generating the final XML output.
Suggested Fix
Technical Details
InlinePolicymethod callsRelated Code Locations
Based on repository analysis, the following files may need investigation:
/src/Core/Compiling/Policy/InlinePolicyCompiler.cs/src/Core/Serialization/RazorCodeFormatter.cs/src/Core/Serialization/CustomXmlWriter.csAdditional Context
This issue was discovered during development of Azure API Management policies using the Policy Toolkit v1.0.0. The problem consistently occurs when using string literals containing quotes within C# expressions in
InlinePolicycalls.