Issues icon indicating copy to clipboard operation
Issues copied to clipboard

ArgumentOutOfRangeException occurs when an invalid JSON array index is used when accessing an Octopus Variable

Open rhysparry opened this issue 4 months ago • 0 comments

Severity

Low

Version

2025.3.11105 (tested, but appears in previous versions)

Latest Version

I could reproduce the problem in the latest build

What happened?

When using an Octopus Variable, JSON parsing is automatically applied. For example:

Image

When accessing the out of bounds variable, an exception is thrown. This

Reproduction

  1. Create a variable with a JSON array
  2. Create another variable that reference the first variable, providing an index that is out of the bounds of the array (example in the screenshot above)
  3. Open Variable Preview

See the following error:

Error evaluating variable 'OutOfBoundsValue'. Evaluation failed with: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
  1. Create and Deploy a release with a script step (the variable does not need to be used, merely in scope)

See the following error in the logs:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') 

Error and Stacktrace

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') 
   at System.Collections.Generic.List`1.get_Item(Int32 index) 
   at Newtonsoft.Json.Linq.JContainer.GetItem(Int32 index) 
   at Newtonsoft.Json.Linq.JArray.get_Item(Int32 index) 
   at Octostache.CustomStringParsers.JsonParser.TryParseJArray(JArray jarray, String property, Binding& subBinding) 
   at Octostache.CustomStringParsers.JsonParser.TryParse(Binding parentBinding, String property, Binding& subBinding) 
   at Octostache.Templates.EvaluationContext.TryCustomParsers(Binding parentBinding, String property, Binding& subBinding) 
   at Octostache.Templates.EvaluationContext.WalkTo(SymbolExpression expression, String[]& missingTokens, String[]& nullTokens) 
   at Octostache.Templates.EvaluationContext.ResolveOptional(SymbolExpression expression, String[]& missingTokens, String[]& nullTokens) 
   at Octostache.Templates.TemplateEvaluator.Calculate(ContentExpression expression, EvaluationContext context) 
   at Octostache.Templates.TemplateEvaluator.EvaluateSubstitutionToken(EvaluationContext context, SubstitutionToken st) 
   at Octostache.Templates.TemplateEvaluator.Evaluate(TemplateToken token, EvaluationContext context) 
   at Octostache.Templates.TemplateEvaluator.Evaluate(IEnumerable`1 tokens, EvaluationContext context) 
   at Octostache.Templates.TemplateEvaluator.Evaluate(Template template, EvaluationContext context, String[]& missingTokens, String[]& nullTokens) 
   at Octostache.Templates.TemplateEvaluator.Evaluate(Template template, Binding properties, TextWriter output, Dictionary`2 extensions, String[]& missingTokens, String[]& nullTokens) 
   at Octostache.VariableDictionary.Evaluate(String expressionOrVariableOrText, String& error, Boolean haltOnError) 
   at Octostache.VariableDictionary.Get(String variableName, String& error, String defaultValue) 
   at Octostache.VariableDictionary.Get(String variableName, String defaultValue) 
   at Calamari.Common.Plumbing.Variables.CalamariVariables.Calamari.Common.Plumbing.Variables.IVariables.Get(String variableName, String defaultValue) 
   at Calamari.Common.Features.Scripting.Bash.BashScriptBootstrapper.<>c__DisplayClass11_0.<EncryptVariables>b__0(String name) in C:\BuildAgent\work\62728692c7c35200\source\Calamari.Common\Features\Scripting\Bash\BashScriptBootstrapper.cs:line 99 
   at System.Linq.Enumerable.SelectListIterator`2.ToList() 
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) 
   at Calamari.Common.Features.Scripting.Bash.BashScriptBootstrapper.EncryptVariables(IVariables variables) in C:\BuildAgent\work\62728692c7c35200\source\Calamari.Common\Features\Scripting\Bash\BashScriptBootstrapper.cs:line 96 
   at Calamari.Common.Features.Scripting.Bash.BashScriptBootstrapper.PrepareConfigurationFile(String workingDirectory, IVariables variables) in C:\BuildAgent\work\62728692c7c35200\source\Calamari.Common\Features\Scripting\Bash\BashScriptBootstrapper.cs:line 45 
   at Calamari.Common.Features.Scripting.Bash.BashScriptExecutor.PrepareExecution(Script script, IVariables variables, Dictionary`2 environmentVars)+MoveNext() in C:\BuildAgent\work\62728692c7c35200\source\Calamari.Common\Features\Scripting\Bash\BashScriptExecutor.cs:line 22 
   at Calamari.Common.Features.Scripting.ScriptExecutor.Execute(Script script, IVariables variables, ICommandLineRunner commandLineRunner, Dictionary`2 environmentVars) in C:\BuildAgent\work\62728692c7c35200\source\Calamari.Common\Features\Scripting\ScriptExecutor.cs:line 35 
   at Calamari.Common.Features.Scripting.TerminalScriptWrapper.ExecuteScript(Script script, ScriptSyntax scriptSyntax, ICommandLineRunner commandLineRunner, Dictionary`2 environmentVars) in C:\BuildAgent\work\62728692c7c35200\source\Calamari.Common\Features\Scripting\TerminalScriptWrapper.cs:line 41 
   at Calamari.Common.Features.Scripting.ScriptEngine.Execute(Script script, IVariables variables, ICommandLineRunner commandLineRunner, Dictionary`2 environmentVars) in C:\BuildAgent\work\62728692c7c35200\source\Calamari.Common\Features\Scripting\ScriptEngine.cs:line 51 
   at Calamari.Deployment.Conventions.ExecuteScriptConvention.Install(RunningDeployment deployment) in C:\BuildAgent\work\62728692c7c35200\source\Calamari\Deployment\Conventions\ExecuteScriptConvention.cs:line 29 
   at Calamari.Deployment.ConventionProcessor.RunInstallConventions(IEnumerable`1 installConventions) in C:\BuildAgent\work\62728692c7c35200\source\Calamari.Shared\Deployment\ConventionProcessor.cs:line 80 
   at Calamari.Deployment.ConventionProcessor.RunConventions(Boolean logExceptions) in C:\BuildAgent\work\62728692c7c35200\source\Calamari.Shared\Deployment\ConventionProcessor.cs:line 31 
Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

More Information

Issue is present in Octostache.

  • https://github.com/OctopusDeploy/Octostache/issues/115

Workaround

Fix any variables so that they do not reference indexes outside of the bounds of the underlying JSON array.

rhysparry avatar Sep 01 '25 23:09 rhysparry