Skip to content

Fix searchKey line detection for array-indexed paths#199

Merged
whitemerch merged 1 commit into
mainfrom
chakib.hamie/fix_search_key_array_index_detection
Jun 19, 2026
Merged

Fix searchKey line detection for array-indexed paths#199
whitemerch merged 1 commit into
mainfrom
chakib.hamie/fix_search_key_array_index_detection

Conversation

@whitemerch

@whitemerch whitemerch commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Motivation

searchKey paths that cross an array boundary (e.g. rules.0.apiGroups) were resolving to the wrong line. The Levenshtein text-matcher in defaultDetectLine has no concept of YAML list indices, when it hit a pure numeric segment like "0", it could not find matching text in the source file and broke early, reporting a line on the parent key instead of the target key inside the array element.

This is the scanner-side fix that unblocks the default-rules PR, which simplifies rule authorship by consolidating line detection to searchKey only.

Changes

  • pkg/detector/default_detect.go: filter out pure numeric segments from the dot-split searchKey path before passing it to the text-matching loop. The numeric index carries no literal text in the source file; removing it lets the detector advance to the next meaningful key (apiGroups, CidrIp, etc.) and report the correct line.

Author Checklist

  • I have reviewed my own PR.
  • I have added or updated relevant unit tests where necessary. If no tests are added, I've explained why.
  • All new and existing tests pass.
  • I have tested my changes on staging (if applicable).
  • I have updated any relevant documentation (if applicable).

QA Instruction

Run the default-rules test suite against a build with this fix. The 43 rules that currently report a line on the parent key (e.g. the rules: line instead of the apiGroups: line) should now report the precise target-field line.

Blast Radius

Only defaultDetectLine, which is the fallback detector used for all non-Terraform / non-Helm / non-Docker file kinds (YAML, JSON, BICEP, …). Terraform and Helm have dedicated detectors and are unaffected. The change only adds a pre-filter step; paths without numeric segments behave identically to before.

Additional Notes

The searchLine code path in vulnerability_builder.go (which uses GetLineBySearchLine for structured tree navigation) is not removed in this PR — that cleanup will follow once the default-rules repo stops emitting searchLine fields.

I submit this contribution under the Apache-2.0 license.

@datadog-prod-us1-4

datadog-prod-us1-4 Bot commented Jun 18, 2026

Copy link
Copy Markdown

🎯 Code Coverage (details)
Patch Coverage: 13.51%
Overall Coverage: 49.24% (-0.08%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: e921624 | Docs | Datadog PR Page | Give us feedback!

@whitemerch whitemerch changed the title [K9VULN-???] Fix searchKey line detection for array-indexed paths Fix searchKey line detection for array-indexed paths Jun 18, 2026
@whitemerch
whitemerch marked this pull request as ready for review June 19, 2026 07:43
@whitemerch
whitemerch requested a review from a team as a code owner June 19, 2026 07:43
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@whitemerch
whitemerch force-pushed the chakib.hamie/fix_search_key_array_index_detection branch from 522a641 to 7c0ccd9 Compare June 19, 2026 09:04
@whitemerch
whitemerch marked this pull request as draft June 19, 2026 09:05
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@whitemerch
whitemerch force-pushed the chakib.hamie/fix_search_key_array_index_detection branch 2 times, most recently from d391263 to c2c254a Compare June 19, 2026 10:43
@whitemerch
whitemerch marked this pull request as ready for review June 19, 2026 10:45
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@whitemerch
whitemerch force-pushed the chakib.hamie/fix_search_key_array_index_detection branch from 06ac7d9 to e921624 Compare June 19, 2026 12:09
@whitemerch

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e921624102

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".


filtered := splitSanitized[:0]
for _, s := range splitSanitized {
if _, err := strconv.Atoi(s); err != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve indices after lookup fallback

When a searchKey crosses nested arrays, GetLineBySearchLine only builds the line-info array path for the last numeric segment, so a path such as spec.template.spec.containers.1.env.0.name falls through to this fallback. Dropping every numeric segment then searches ...containers.env.name, causing the text matcher to report the first env.name under the first container rather than the indexed container/env entry. Please keep the index context or extend the structured lookup instead of stripping all indices on failure.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doubly-nested fallback limitation existed before in a more broken form.

@whitemerch
whitemerch merged commit 8cddeb1 into main Jun 19, 2026
19 checks passed
@whitemerch
whitemerch deleted the chakib.hamie/fix_search_key_array_index_detection branch June 19, 2026 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants