Skip to content

Commit ad7f612

Browse files
robertcoltheartarturcic
authored andcommitted
fix: tighten parsing of label formats
1 parent 3ae1f9b commit ad7f612

5 files changed

Lines changed: 23 additions & 9 deletions

File tree

src/GitVersion.Core.Tests/Formatting/LabelTokenizerTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public class LabelTokenizerTests
3939
[TestCase("Prop ?? literal ?? ? fallback")]
4040
[TestCase("Prop ? fallback")]
4141
[TestCase("Prop ?? fall?back")]
42+
[TestCase("Prop ?? fallback ??")]
43+
[TestCase("Prop ?? fallback ?? ")]
4244
public void ParseTokens_MalformedIdentifiers_Throws(string input) => AssertThrows(input);
4345

4446
private static void AssertTokens(string input, string[] expected)

src/GitVersion.Core/Extensions/ConfigurationExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ private static Dictionary<string, object> BuildLabelPlaceholders(string? regular
155155
if (!match.Success)
156156
return placeholders;
157157

158-
foreach (var groupName in regex.GetGroupNames().Skip(1))
158+
var namedGroups = regex.GetGroupNames()
159+
.Where(name => !int.TryParse(name, out _));
160+
161+
foreach (var groupName in namedGroups)
159162
{
160163
var groupValue = match.Groups[groupName].Value;
161164

src/GitVersion.Core/Formatting/LabelTokenizer.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ internal class LabelTokenizer(string input)
99
public IEnumerable<LabelToken> ParseTokens()
1010
{
1111
var tokens = new List<LabelToken>();
12+
var separatedParsed = false;
1213

1314
SkipWhitespace();
1415

@@ -25,10 +26,16 @@ public IEnumerable<LabelToken> ParseTokens()
2526

2627
tokens.Add(ParseToken(identifier));
2728

28-
ParseSeparator();
29+
separatedParsed = ParseSeparator();
30+
2931
SkipWhitespace();
3032
}
3133

34+
if (separatedParsed)
35+
{
36+
throw new FormatException("Invalid format sequence, expected identifier after '??' separator");
37+
}
38+
3239
return tokens;
3340
}
3441

@@ -37,9 +44,9 @@ private static LabelToken ParseToken(string identifier)
3744
if (identifier.StartsWith("env:", StringComparison.OrdinalIgnoreCase))
3845
{
3946
var name = identifier[4..];
40-
var (environmentName, environemntFormat) = ParseKeyAndFormat(name);
47+
var (environmentName, environmentFormat) = ParseKeyAndFormat(name);
4148

42-
return new LabelToken(environmentName, LabelTokenType.Environment, environemntFormat);
49+
return new LabelToken(environmentName, LabelTokenType.Environment, environmentFormat);
4350
}
4451

4552
if (identifier.StartsWith('"') && identifier.EndsWith('"'))
@@ -120,7 +127,7 @@ private string ParseIdentifier()
120127
return value.ToString();
121128
}
122129

123-
private void ParseSeparator()
130+
private bool ParseSeparator()
124131
{
125132
var seen = 0;
126133

@@ -134,6 +141,8 @@ private void ParseSeparator()
134141
seen++;
135142
index++;
136143
}
144+
145+
return seen == 2;
137146
}
138147

139148
private void SkipWhitespace()

src/GitVersion.Core/Formatting/StringFormatWithExtension.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ private static string EvaluateMatch(string input, Func<string, string?> memberEv
9696
}
9797

9898
var value = token.Type == LabelTokenType.Environment
99-
? environment.GetEnvironmentVariable(token.Name)
99+
? environment.GetEnvironmentVariable(InputSanitizer.SanitizeEnvVarName(token.Name))
100100
: memberEvaluator(token.Name);
101101

102-
if (!string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(token.Format))
102+
if (value is not null && !string.IsNullOrEmpty(token.Format))
103103
{
104104
if (ValueFormatter.Default.TryFormat(value, InputSanitizer.SanitizeFormat(token.Format), out var formatted))
105105
{
@@ -109,7 +109,7 @@ private static string EvaluateMatch(string input, Func<string, string?> memberEv
109109
return value;
110110
}
111111

112-
if (!string.IsNullOrEmpty(value))
112+
if (value is not null)
113113
{
114114
return value;
115115
}

src/GitVersion.Core/VersionCalculation/VariableProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public GitVersionVariables GetVariablesFor(
8585
formattedString = formatString.FormatWith(source, this.environment)
8686
.RegexReplace(RegexPatterns.Output.SanitizeAssemblyInfoRegexPattern, "-");
8787
}
88-
catch (ArgumentException exception)
88+
catch (Exception exception) when (exception is ArgumentException or FormatException)
8989
{
9090
throw new WarningException($"Unable to format {formatVarName}. Check your format string: {exception.Message}");
9191
}

0 commit comments

Comments
 (0)