Skip to content

Add default sanitizer for resource group name in URI#2386

Open
alzimmermsft wants to merge 2 commits intomicrosoft:mainfrom
alzimmermsft:AddDefaultSanitizerForRGInUri
Open

Add default sanitizer for resource group name in URI#2386
alzimmermsft wants to merge 2 commits intomicrosoft:mainfrom
alzimmermsft:AddDefaultSanitizerForRGInUri

Conversation

@alzimmermsft
Copy link
Copy Markdown
Contributor

@alzimmermsft alzimmermsft commented Apr 10, 2026

What does this PR do?

Adds a default UriRegexSanitizer to RecordedCommandTestsBase that handles sanitizing resource group name in URIs in a way that won't have issues in playback after recording.

The previous sanitization was done using a GeneralRegexSanitizer looking for the resource base name. This might not be the same as the resource group name, which is the first issue, but generally the resource group name does contain the base name. The second issue is only the base name specifically was being sanitized, so only part of the resource group name may be sanitized, which would be fine if the third issue wasn't present. The third problem is that the base name is defaulted to Santized in playback, which would fail to match the resource group name if it was capture.

GitHub issue number?

[Link to the GitHub issue this PR addresses]

Pre-merge Checklist

  • Required for All PRs
    • Read contribution guidelines
    • PR title clearly describes the change
    • Commit history is clean with descriptive messages (cleanup guide)
    • Added comprehensive tests for new/modified functionality
    • Created a changelog entry if the change falls among the following: new feature, bug fix, UI/UX update, breaking change, or updated dependencies. Follow the changelog entry guide
  • For MCP tool changes:
    • One tool per PR: This PR adds or modifies only one MCP tool for faster review cycles
    • Updated servers/Azure.Mcp.Server/README.md and/or servers/Fabric.Mcp.Server/README.md documentation
    • Validate README.md changes running the script ./eng/scripts/Process-PackageReadMe.ps1. See Package README
    • For new or modified tool descriptions, ran ToolDescriptionEvaluator and obtained a score of 0.4 or more and a top 3 ranking for all related test prompts
    • For tools with new names, including new tools or renamed tools, update consolidated-tools.json
    • For renamed tools, follow the Tool Rename Checklist and tag the PR with the breaking-change label
    • For new tools associated with Azure services or publicly available tools/APIs/products, add URL to documentation in the PR description
  • Extra steps for Azure MCP Server tool changes:
    • Updated command list in servers/Azure.Mcp.Server/docs/azmcp-commands.md
    • Ran ./eng/scripts/Update-AzCommandsMetadata.ps1 to update tool metadata in azmcp-commands.md (required for CI)
    • Updated test prompts in servers/Azure.Mcp.Server/docs/e2eTestPrompts.md
    • 👉 For Community (non-Microsoft team member) PRs:
      • Security review: Reviewed code for security vulnerabilities, malicious code, or suspicious activities before running tests (crypto mining, spam, data exfiltration, etc.)
      • Manual tests run: added comment /azp run mcp - pullrequest - live to run Live Test Pipeline

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 updates the core recorded-test base class to improve record/playback stability by adding a default URI sanitizer that replaces Azure Resource Group names in request URIs.

Changes:

  • Add a default UriRegexSanitizer to sanitize resource group names found in URIs during recording and playback.

Comment on lines +58 to +69
public virtual List<UriRegexSanitizer> UriRegexSanitizers { get; } = [
// Sanitize Resource Group name from the URI. This is needed as there is another default GeneralRegexSanitizer for
// Settings.ResourceBaseName. But there is two issues we hit with that:
// 1. Resource group names often have other characters added to it, like prepending or appending for uniqueness.
// 2. In playback, Settings.ResourceBaseName is configured to be 'Sanitized', so it won't find the right string.
new UriRegexSanitizer(new()
{
Value = "Sanitized",
Regex = "/resource[gG]roups/(?<rgname>[\\w\\-.()]{1,90})/",
GroupForReplace = "rgname"
})
];
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

The new default UriRegexSanitizer is always registered via the property initializer, even when EnableDefaultSanitizerAdditions is set to false. That makes the opt-out flag semantics inconsistent for derived tests that disable default sanitizers (often to control sanitizer ordering). Consider only adding this default sanitizer when EnableDefaultSanitizerAdditions is enabled (e.g., populate it in PopulateDefaultSanitizers()), or update the flag/docs to reflect that URI sanitizers are always applied.

Copilot uses AI. Check for mistakes.
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.

I think this is probably pretty safe, but I also do agree with this github comment. I don't agree enough to block checkin though.

I will definitely like to kick a server run against this to make certain no one got broken by the new addition.

Copy link
Copy Markdown
Contributor

@scbedd scbedd left a comment

Choose a reason for hiding this comment

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

Going to run Azure.Server against this pr.

Copy link
Copy Markdown
Contributor

@jongio jongio left a comment

Choose a reason for hiding this comment

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

Regex looks correct - character class covers Azure's allowed RG name chars, 1-90 length constraint matches, and the lookahead handles /, ?, and end-of-string terminators.

The property-initializer approach is consistent with how HeaderRegexSanitizers already provides defaults outside EnableDefaultSanitizerAdditions. The 7 test classes that disable that flag can override UriRegexSanitizers if needed since it's virtual.

LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Untriaged

Development

Successfully merging this pull request may close these issues.

4 participants