-
Notifications
You must be signed in to change notification settings - Fork 867
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue with debug output masking if it contains special symbols (#…
…3464) * Fix secrets masking in debug output * Extract logic for escaping and unescaping special symbols to `CommandStringConvertor` class * Add escape data of `##vso` commands to the secret masker to resolve the problem with debug output masking * Remove `Agent.Sdk.Knob` from `Command` class as it is not used in the code. * Fix variables names and code formatting
- Loading branch information
Alexander Smolyakov
authored
Jul 21, 2021
1 parent
43997c4
commit 390dc24
Showing
4 changed files
with
90 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using Microsoft.VisualStudio.Services.Agent.Util; | ||
|
||
namespace Agent.Sdk | ||
{ | ||
public sealed class CommandStringConvertor | ||
{ | ||
public static string Escape(string input, bool unescapePercents) | ||
{ | ||
if (string.IsNullOrEmpty(input)) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
string escaped = input; | ||
|
||
if (unescapePercents) | ||
{ | ||
escaped = escaped.Replace("%", "%AZP25"); | ||
} | ||
|
||
foreach (EscapeMapping mapping in _specialSymbolsMapping) | ||
{ | ||
escaped = escaped.Replace(mapping.Token, mapping.Replacement); | ||
} | ||
|
||
return escaped; | ||
} | ||
|
||
public static string Unescape(string escaped, bool unescapePercents) | ||
{ | ||
if (string.IsNullOrEmpty(escaped)) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
string unescaped = escaped; | ||
|
||
foreach (EscapeMapping mapping in _specialSymbolsMapping) | ||
{ | ||
unescaped = unescaped.Replace(mapping.Replacement, mapping.Token); | ||
} | ||
|
||
if (unescapePercents) | ||
{ | ||
unescaped = unescaped.Replace("%AZP25", "%"); | ||
} | ||
|
||
return unescaped; | ||
} | ||
|
||
private static readonly EscapeMapping[] _specialSymbolsMapping = new[] | ||
{ | ||
new EscapeMapping(token: ";", replacement: "%3B"), | ||
new EscapeMapping(token: "\r", replacement: "%0D"), | ||
new EscapeMapping(token: "\n", replacement: "%0A"), | ||
new EscapeMapping(token: "]", replacement: "%5D") | ||
}; | ||
|
||
private sealed class EscapeMapping | ||
{ | ||
public string Replacement { get; } | ||
public string Token { get; } | ||
|
||
public EscapeMapping(string token, string replacement) | ||
{ | ||
ArgUtil.NotNullOrEmpty(token, nameof(token)); | ||
ArgUtil.NotNullOrEmpty(replacement, nameof(replacement)); | ||
Token = token; | ||
Replacement = replacement; | ||
} | ||
} | ||
|
||
} | ||
} |
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