kubescape / vscode-kubescape

Kubescape extension for Visual Studio Code
Apache License 2.0
17 stars 10 forks source link

Fix Paths ending with array produce wrong YAML highlight #15

Closed angad-singhh closed 3 months ago

angad-singhh commented 5 months ago

What is the issue about There is an error while parsing the Fix Path that ends with an array value. This error occurs when the YAML parse function tries to parse a path which is not present in YAML file and the path ends with an array value. For example spec.template.spec.containers[0].securityContext.capabilities.drop[0].

Example for path spec.template.spec.containers[0].securityContext.capabilities.drop[0], the last item is drop, if it is present in our YAML file, no issue will occur and scan will run just fine. But in case there is no drop in our YAML file, the function should have returned -1 but it does not.

Why this issue occur There is a small condition check missing that should ensure that when handleArrayMatch is called for isLastItem, the condition should be that it should only call when startIndex is greater than -1 means that lastItem should be present in our YAML file.

  startIndexAcc.tempMatch = step.match(regExpForArrayIndex);

  const isLastItem = stepIndex === (steps.length - 1);

  if (isLastItem && startIndexAcc.tempMatch) {          // This part is where we need a change in condition 
    handleArrayMatch(startIndexAcc, lines, indentArray, indentArray);  
  }

Solution We only need to add a condition to check if ( startIndexAcc.startIndex > -1 ), So that it does not call the handleArrayMatch for path values which does not exist in YAML file.