Update heredoc detection to handle more edge cases

This commit is contained in:
Ben Wells
2023-07-21 15:49:51 -04:00
committed by GitHub
parent 496904c0b7
commit c71eceaae3

View File

@@ -322,9 +322,21 @@ namespace GitHub.Runner.Worker
var equalsIndex = line.IndexOf("=", StringComparison.Ordinal);
var heredocIndex = line.IndexOf("<<", StringComparison.Ordinal);
// Heredoc style NAME<<EOF (where EOF is typically randomly-generated Base64 and may include an '=' character)
// Heredoc style NAME=<<EOF (where EOF is typically randomly-generated Base64 and may include an '=' character)
// see https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
if (heredocIndex >= 0 && (equalsIndex < 0 || heredocIndex < equalsIndex))
// "key=<<EOF is also considered heredoc where the key contains an =.
// Also we detect heredoc if whitespace chars exist between = and <<. (ex: "key <<EOF")
var isHeredoc = heredocIndex >= 0 &&
(
equalsIndex < 0 ||
(
heredocIndex > equalsIndex &&
OnlyContainsWhiteSpaceBetweenPositions(line, equalsIndex, heredocIndex)
) ||
heredocIndex < equalsIndex
);
if (isHeredoc)
{
var split = line.Split(new[] { "<<" }, 2, StringSplitOptions.None);
if (string.IsNullOrEmpty(split[0]) || string.IsNullOrEmpty(split[1]))
@@ -383,6 +395,34 @@ namespace GitHub.Runner.Worker
return GetEnumerator();
}
// accepts a string, two indexes, and returns true if only whitespace chars exist between those two indexes (non-inclusive)
private static bool CheckIfOnlyWhitespaceBetweenSymbols(string str, int pos1, int pos2)
{
// Check if the provided positions are valid
if (pos1 < 0 || pos2 < 0 || pos1 >= str.Length || pos2 >= str.Length)
{
throw new ArgumentOutOfRangeException("Whitespace check: Positions should be within the string length.");
}
// Ensure pos1 is always the smaller position
if (pos2 < pos1)
{
int temp = pos2;
pos2 = pos1;
pos1 = temp;
}
for (int i = pos1 + 1; i < pos2; i++)
{
if (!char.IsWhiteSpace(str[i]))
{
return false;
}
}
return true;
}
private static string ReadLine(
string text,
ref int index)