Skip to content

Commit c2f1c4f

Browse files
Fix inputs trimming for variables (#4940)
* Testing fix for input trimming * Added runtime knob source for DisableInputTrimming knob and cleaned up the code * Add knob for variables trimming and refactor the logic to use it * Fix bug where the variable input trimming logic was placed incorrectly in the ExpandValues method --------- Co-authored-by: v-levockina <undefined>
1 parent f60062e commit c2f1c4f

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

src/Agent.Sdk/Knob/AgentKnobs.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,16 @@ public class AgentKnobs
403403
nameof(DisableInputTrimming),
404404
"By default, the agent trims whitespace and new line characters from all task inputs. Setting this to true disables this behavior.",
405405
new EnvironmentKnobSource("DISABLE_INPUT_TRIMMING"),
406+
new RuntimeKnobSource("DISABLE_INPUT_TRIMMING"),
406407
new BuiltInDefaultKnobSource("false"));
407408

409+
public static readonly Knob EnableVariableInputTrimming = new Knob(
410+
nameof(EnableVariableInputTrimming),
411+
"By default, the agent does not trim whitespace and new line characters if an input comes from a variable. Setting this to true enables this behavior.",
412+
new EnvironmentKnobSource("AGENT_ENABLE_VARIABLE_INPUT_TRIMMING"),
413+
new RuntimeKnobSource("AGENT_ENABLE_VARIABLE_INPUT_TRIMMING"),
414+
new BuiltInDefaultKnobSource("false"));
415+
408416
public static readonly Knob DecodePercents = new Knob(
409417
nameof(DecodePercents),
410418
"By default, the agent does not decodes %AZP25 as % which may be needed to allow users to work around reserved values. Setting this to true enables this behavior.",

src/Agent.Worker/TaskRunner.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ private async Task RunAsyncInternal()
230230

231231
// Expand the inputs.
232232
Trace.Verbose("Expanding inputs.");
233-
runtimeVariables.ExpandValues(target: inputs);
233+
bool enableVariableInputTrimmingKnob = AgentKnobs.EnableVariableInputTrimming.GetValue(ExecutionContext).AsBoolean();
234+
runtimeVariables.ExpandValues(target: inputs, enableVariableInputTrimmingKnob);
234235

235236
// We need to verify inputs of the tasks that were injected by decorators, to check if they contain secrets,
236237
// for security reasons execution of tasks in this case should be skipped.

src/Agent.Worker/Variables.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public bool Retain_Default_Encoding
281281
Constants.Variables.Agent.Name,
282282
};
283283

284-
public void ExpandValues(IDictionary<string, string> target)
284+
public void ExpandValues(IDictionary<string, string> target, bool enableVariableInputTrimming = false)
285285
{
286286
ArgUtil.NotNull(target, nameof(target));
287287
_trace.Entering();
@@ -292,7 +292,7 @@ public void ExpandValues(IDictionary<string, string> target)
292292
source[variable.Name] = value;
293293
}
294294

295-
VarUtil.ExpandValues(_hostContext, source, target);
295+
VarUtil.ExpandValues(_hostContext, source, target, enableVariableInputTrimming);
296296
}
297297

298298
public string ExpandValue(string name, string value)

src/Microsoft.VisualStudio.Services.Agent/Util/VarUtil.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace Microsoft.VisualStudio.Services.Agent.Util
1414
{
15-
public static class VarUtil
15+
public static class VarUtil
1616
{
1717
public static StringComparer EnvironmentVariableKeyComparer
1818
{
@@ -142,7 +142,7 @@ public static JToken ExpandValues(IHostContext context, IDictionary<string, stri
142142
return target.Map(mapFuncs);
143143
}
144144

145-
public static void ExpandValues(IHostContext context, IDictionary<string, string> source, IDictionary<string, string> target)
145+
public static void ExpandValues(IHostContext context, IDictionary<string, string> source, IDictionary<string, string> target, bool enableVariableInputTrimming = false)
146146
{
147147
ArgUtil.NotNull(context, nameof(context));
148148
ArgUtil.NotNull(source, nameof(source));
@@ -183,6 +183,10 @@ public static void ExpandValues(IHostContext context, IDictionary<string, string
183183
variableValue ?? string.Empty,
184184
targetValue.Substring(suffixIndex + Constants.Variables.MacroSuffix.Length));
185185

186+
targetValue = enableVariableInputTrimming
187+
? targetValue?.Trim() ?? string.Empty
188+
: targetValue ?? string.Empty;
189+
186190
// Bump the start index to prevent recursive replacement.
187191
startIndex = prefixIndex + (variableValue ?? string.Empty).Length;
188192
}

0 commit comments

Comments
 (0)