azureazure-cliazure-bicep

Consecutive "<,>" characters in bicep string parameters causing "> was unexpected at this time"


Similar to previously a answered question: Bicep/ARM XML parameter as string not parsed correctly. Which indicated you needed to escape the ">" or "<" with quotes. This is one hit a bit different. In this bicep deployment example, if the value passed in contains consecutive '>', '<' in either order, you'll receive an error similar to: "< was unexpected at this time."

the simple example bicep file is:

targetScope = 'subscription'

output esc_str string = escape_str

then deployed such as:

az deployment sub create --name test --location eastus --template-file .\BicepTest.bicep --parameters escape_str='abc<>'

my scenario is a little more complex than that. but the general idea is there.

I can get this to work if i put both the <> in quotes as such:

az deployment sub create --name test --location eastus --template-file .\BicepTest.bicep --parameters escape_str='abc"<>"'

however, that value is a variable that is passed in so it is more in the form of escape_str=$variable format (Powershell), and we don't necessarily know the combinations that will cause this to error.

I'm wondering if there is a safe way to "escape" a set of characters, or if there are other limitations. The Bicep reference doesn't indicate these characters required to be escaped.

At the moment, I have a few lines such as (also with the different combinations we have found):

$x.replace('<>','"<>"')

This works, but only for those combinations of characters, and something like ">>", ">>>" would also error as well, although the first would just give "The syntax of the command is incorrect". I'm looking for potentially a better solution for this. Any Ideas?


Solution

  • Consecutive "<,>" characters in bicep string parameters causing "> was unexpected at this time

    I agree with Thomas on the point mentioned a variable like that: $variable="abc<>" then use it like that --parameters escape_str=`""$variable"`".

    Specially when the special characters being passed to your Bicep template through PowerShell. Special characters such as <, >, or & are treated specially by PowerShell and the command shell.

    When youre using powershell it provides the special character (`)

      $variable = "abc<>"
    

    This is the case with powershell.

    and while using bicep deployement command you can use that

    az deployment sub create --name test --location eastus --template-file .\BicepTest.bicep --parameters escape_str="`"$variable"`"
    

    This should work fine as expected without any blocker.

    Refer:

    https://learn.microsoft.com/en-us/cli/azure/use-azure-cli-successfully-quoting?tabs=powershell1%2Cbash2%2Cpowershell3

    For the info related to special characters.