internal/functions/core/Resolve-EntraServiceUrl.ps1

function Resolve-EntraServiceUrl {
    <#
    .SYNOPSIS
        Returns the service URL of a token with the service's parameters filled in - the URL its token belongs to.

    .DESCRIPTION
        A registered service can carry parameters in its URL (%VAULTNAME% in https://%VAULTNAME%.vault.azure.net);
        Invoke-EntraRequest fills them from its bound parameters, as Resolve-RequestUri does for the request URL.
        This is the URL the token guard (Get-EntraUriRefusal) compares every request with. A parameter that is not
        bound leaves its placeholder empty, which the guard then refuses rather than guessing a host.
        A parameter value that is not one ASCII host label (letters, digits and hyphens, at most 63, compared with
        regard to case) leaves no URL at all: an empty string is returned, which the guard refuses as well.

        Ported from PSExchangeOnline (Resolve-ExoServiceUrl, review finding M3, 2026-10-20).

    .PARAMETER TokenObject
        The token of the request.

    .PARAMETER ServiceObject
        The registered service of the token (if any); its Parameters name the placeholders.

    .PARAMETER BoundParameters
        The parameters of the calling command.

    .EXAMPLE
        PS C:\> Resolve-EntraServiceUrl -TokenObject $tokenObject -ServiceObject $serviceObject -BoundParameters $PSBoundParameters

        The service URL of the token, e.g. https://graph.microsoft.com/v1.0.
    #>

    [OutputType([string])]
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        $TokenObject,

        [AllowNull()]
        $ServiceObject,

        [AllowNull()]
        $BoundParameters
    )

    [string] $url = ([string]$TokenObject.ServiceUrl).Trim()
    if (-not $ServiceObject -or -not $ServiceObject.Parameters) { return $url }
    foreach ($key in $ServiceObject.Parameters.Keys) {
        [string] $value = if ($BoundParameters -and $BoundParameters.ContainsKey($key)) { [string]$BoundParameters[$key] } else { '' }
        # The value becomes part of the host the token may go to, and the request URL is built from the same value: a
        # value such as 'attacker.example/' would move both to another host and the guard would compare it with
        # itself. Anything but letters, digits and hyphens (one host label) leaves no URL, which the guard refuses.
        # -cnotmatch: case-insensitively, U+212A (Kelvin sign), U+0131 and U+0130 pass as the letters K and I.
        if ($value -and $value -cnotmatch '\A[A-Za-z0-9-]{1,63}\z') { return '' }
        # Literal, not -replace: a key is no pattern and a value no replacement text ('$&', '$1'). Case-insensitive, as
        # Resolve-RequestUri's -replace is: the Key Vault service registers the parameter VaultName for %VAULTNAME%, and a
        # case-sensitive replace left the placeholder in, so the guard refused every Key Vault request.
        $url = $url.Replace("%$key%", $value, [System.StringComparison]::OrdinalIgnoreCase)
    }
    $url
}