Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Secrets (within quotes) that are part of Variable Group linked to KeyVault are not masked in logs #1017

Closed
krsanty opened this issue Feb 7, 2024 · 2 comments

Comments

@krsanty
Copy link

krsanty commented Feb 7, 2024

Please check our current Issues to see if someone already reported this https://github.com/Microsoft/azure-pipelines-task-lib/issues

Environment

azure-pipelines-task-lib version:
Azure PowerShell V2 task
Windows (Azure VMSS)

Issue Description

Secrets (having values within quotes "" ) that are part of Variable Group linked with Azure KeyVault is not masked in logs

Expected behaviour

Secrets should be masked in logs in all cases

Actual behaviour

Secrets are not masked in all cases

Steps to reproduce

Below is a simple step that can reproduce the secret leak in logs. In the KeyVault, SecretsWithinQuotes has value "SHOULDBEHIDDEN" and SecretsWithinEscapedQuotes has value \"SHOULDBEHIDDEN\"

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
          echo 'My Secret in quotes 1: '$(SecretsWithinQuotes)
          echo 'My Secret5 in quotes 2: '"$(SecretsWithinQuotes)"
          echo "{$(SecretsWithinQuotes)}"
          echo "My Secret  in quotes 3:" {$(SecretsWithinQuotes)}
          echo "My Secret5 in quotes 4: {$(SecretsWithinQuotes)}"
          echo "My Secret  in quotes 5: {{$(SecretsWithinQuotes)}}"
          echo "*****************************************************"
          echo 'My Secret in escaped quotes 1: '$(SecretsWithinEscapedQuotes)
          echo 'My Secret5 in escaped quotes 2: '"$(SecretsWithinEscapedQuotes)"
          echo "{$(SecretsWithinEscapedQuotes)}"
          echo "My Secret  in escaped quotes 3:" {$(SecretsWithinEscapedQuotes)}
          echo "My Secret5 in escaped quotes 4: {$(SecretsWithinEscapedQuotes)}"
          echo "My Secret  in escaped quotes 5: {{$(SecretsWithinEscapedQuotes)}}"

Is there any way that I can restrict the secret being leaked out in all cases?

Logs

Here are the logs for the above step that shows leaked secret in most cases:

My Secret in quotes 1: 
SHOULDBEHIDDEN
My Secret5 in quotes 2: 

SHOULDBEHIDDEN
{
SHOULDBEHIDDEN}
My Secret  in quotes 3:
***
My Secret5 in quotes 4: {
SHOULDBEHIDDEN}
My Secret  in quotes 5: {{
SHOULDBEHIDDEN}}
*****************************************************
My Secret in escaped quotes 1: 
\SHOULDBEHIDDEN\
My Secret5 in escaped quotes 2: 
\
SHOULDBEHIDDEN\
{\
SHOULDBEHIDDEN\}
My Secret  in escaped quotes 3:
***
My Secret5 in escaped quotes 4: {\
SHOULDBEHIDDEN\}
My Secret  in escaped quotes 5: {{\
SHOULDBEHIDDEN\}}
@krsanty krsanty changed the title Secrets (within quotes) that are part of Variable Group liked to KeyVault are not masked in logs Secrets (within quotes) that are part of Variable Group linked to KeyVault are not masked in logs Feb 7, 2024
@krsanty
Copy link
Author

krsanty commented Feb 7, 2024

Just realized that I should have used BackTick as the escape character for the PowerShell script. It masked the secret properly when the secret value in KV was changed from \"SHOULDBEHIDDEN\" to `"SHOULDBEHIDDEN`".

However, since we are using non PowerShell script as well in our pipeline, adding the escape character within the secret text opens up problems in other scripts where it is not treated as escape character. It there a better way to mask the secrets in PowerShell tasks?

@krsanty krsanty closed this as completed Feb 7, 2024
@krsanty krsanty reopened this Feb 7, 2024
@krsanty
Copy link
Author

krsanty commented Feb 9, 2024

Closing the issue because I think I found the solution.
Setting new task variable and then assigning it the secret.
Write-Host "##vso[task.setvariable variable=secretwithdoublequotes;issecret=true]$(secretwithdoublequotes)"

Thereafter, using the variable in this format {$(secretwithdoublequotes)} to avoid the leak.

@krsanty krsanty closed this as completed Feb 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant