forked from Azure/PSRule.Rules.Azure
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes APIM policies when using embedded C# with quotes Azure#3184 (Az…
- Loading branch information
1 parent
8d81817
commit 3e2a3b7
Showing
9 changed files
with
137 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Xml; | ||
using System.Text.RegularExpressions; | ||
using System.IO; | ||
|
||
namespace PSRule.Rules.Azure.Data.APIM; | ||
|
||
/// <summary> | ||
/// A reader for APIM policy files. | ||
/// </summary> | ||
internal static class APIMPolicyReader | ||
{ | ||
// Create a regex pattern to match the `="@(` `)"` pairs. | ||
private static readonly Regex _Pattern = new(@"(?<=\=""\@\().*?(?=\)"")", RegexOptions.Singleline | RegexOptions.Compiled); | ||
|
||
/// <summary> | ||
/// Read the content of the policy file as XML. | ||
/// </summary> | ||
/// <remarks> | ||
/// This method automatically escapes quotes within policy expressions to ensure the XML is well-formed. | ||
/// </remarks> | ||
public static XmlReader ReadContent(string content) | ||
{ | ||
// For each match replace `"` characters with `"`. | ||
foreach (Match match in _Pattern.Matches(content)) | ||
{ | ||
content = content.Replace(match.Value, match.Value.Replace("\"", """)); | ||
} | ||
|
||
return XmlReader.Create(new StringReader(content)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Xml; | ||
using PSRule.Rules.Azure.Data.APIM; | ||
|
||
namespace PSRule.Rules.Azure.APIM; | ||
|
||
/// <summary> | ||
/// Test cases for APIM policy files with complex syntax. | ||
/// </summary> | ||
public sealed class APIMPolicyTests : BaseTests | ||
{ | ||
/// <summary> | ||
/// Test case for https://github.com/Azure/PSRule.Rules.Azure/issues/3184 | ||
/// </summary> | ||
[Fact] | ||
public void APIMPolicyReader_WhenExpressionIsFound_ShouldEscapeAndLoad() | ||
{ | ||
using var reader = ReadContentFromFile("APIM/Tests.Policy.1.xml"); | ||
|
||
var doc = new XmlDocument(); | ||
doc.Load(reader); | ||
|
||
var node = doc.SelectSingleNode("//set-variable"); | ||
|
||
Assert.Equal("@(context.Request.Headers.GetValueOrDefault(\"X-Original-Host\", \"NotAvailable\"))", node.Attributes["value"].Value); | ||
} | ||
|
||
#region Helper methods | ||
|
||
private static XmlReader ReadContentFromFile(string fileName) | ||
{ | ||
var content = GetContent(fileName); | ||
return APIMPolicyReader.ReadContent(content); | ||
} | ||
|
||
#endregion Helper methods | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!-- Test case for https://github.com/Azure/PSRule.Rules.Azure/issues/3184 --> | ||
<!-- Based on work contributed by @GABRIELNGBTUC --> | ||
<policies> | ||
<inbound> | ||
<base /> | ||
<set-backend-service base-url="{{backend}}" /> | ||
<authentication-managed-identity resource="{{audience}}" /> | ||
<set-variable name="originalHost" value="@(context.Request.Headers.GetValueOrDefault("X-Original-Host", "NotAvailable"))" /> | ||
</inbound> | ||
<backend> | ||
<base /> | ||
</backend> | ||
<outbound> | ||
<base /> | ||
</outbound> | ||
<on-error> | ||
<base /> | ||
</on-error> | ||
</policies> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.IO; | ||
|
||
namespace PSRule.Rules.Azure; | ||
|
||
public abstract class BaseTests | ||
{ | ||
protected static string GetSourcePath(string fileName) | ||
{ | ||
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName); | ||
} | ||
|
||
protected static string GetContent(string fileName) | ||
{ | ||
var path = GetSourcePath(fileName); | ||
return File.ReadAllText(path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters