Skip to content

fix: handle invalid selector errors - #1976

Merged
toddbaert merged 7 commits into
open-feature:mainfrom
alxckn:handle_bad_selector_errors
Jul 20, 2026
Merged

toddbaert merged 7 commits into
open-feature:mainfrom
alxckn:handle_bad_selector_errors

Conversation

@alxckn

@alxckn alxckn commented Jun 1, 2026

Copy link
Copy Markdown
Member

This PR

Invalid selector expressions in the flagd-selector header were causing unhandled errors that surfaced as 500 responses. This PR validates the selector at parse time and maps invalid input to the appropriate error code
(InvalidArgument / HTTP 400) across all service layers (flag evaluation, flag sync, OFRep).

As part of this fix, the Selector construction API has been redesigned to eliminate unchecked string-based construction entirely:

  • NewSelector(expr string) now returns an error for unknown keys
  • WithIndex is removed (breaking change) — replaced by typed methods WithSource and WithFlagSetId which cannot produce an invalid selector

Related Issues

Notes

Breaking change: (Selector).WithIndex(key, value string) has been removed from the public API. Callers should migrate to Selector{}.WithSource(source) or Selector{}.WithFlagSetId(id).

(with AI help)

@alxckn
alxckn requested review from a team as code owners June 1, 2026 08:47
@dosubot dosubot Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Jun 1, 2026
@netlify

netlify Bot commented Jun 1, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for polite-licorice-3db33c canceled.

Name Link
🔨 Latest commit b782770
🔍 Latest deploy log https://app.netlify.com/projects/polite-licorice-3db33c/deploys/6a5e631af49dbe0008cb41d9

@alxckn
alxckn force-pushed the handle_bad_selector_errors branch from 8e8b5fd to d79d9be Compare June 1, 2026 08:48

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the Selector creation and manipulation. NewSelector now validates selector keys and returns an error for invalid keys, with corresponding error handling added across evaluation and sync services. Additionally, generic WithIndex calls are replaced with type-safe methods like WithSource and WithFlagSetId. The review feedback recommends two improvements: recording the error on the tracing span when startResolveV2 fails in flag_evaluator_v2.go, and setting exitReason = "error" when returning an invalid selector error in flag-sync/handler.go to ensure accurate metrics.

Comment thread flagd/pkg/service/flag-sync/handler.go Resolved
@alxckn
alxckn force-pushed the handle_bad_selector_errors branch from 75e6aff to 9a39218 Compare June 1, 2026 11:50
@toddbaert toddbaert changed the title fix: Handle invalid selector errors fix: handle invalid selector errors Jun 1, 2026
@toddbaert

Copy link
Copy Markdown
Member

@alxckn unit test for NewSelector is good, but I'd like service-level coverage too; the whole point is "no more 500s on bad selectors". One test per surface asserting:

  • connect.CodeInvalidArgument on v0/v1/v2 eval + sync
  • HTTP 400 on OFREP (single + bulk)

Otherwise this can regress silently. WDYT?

Also FYI, some of the sonar issues should be fixed on main now; might be worth a rebase/merge before the next push.

@alxckn
alxckn force-pushed the handle_bad_selector_errors branch from 9a39218 to b353b2e Compare June 1, 2026 21:33
@sonarqubecloud

sonarqubecloud Bot commented Jun 1, 2026

Copy link
Copy Markdown

@alxckn

alxckn commented Jun 1, 2026

Copy link
Copy Markdown
Member Author

@toddbaert thanks for the review. I took care of comments, added integration tests to prevent regressions, and the sonar warnings seem resolved 👍

@alxckn

alxckn commented Jul 1, 2026

Copy link
Copy Markdown
Member Author

@toddbaert ping :) do you think this could get merged? This would be helpful to us to remove the risk of false positive alarms

alxckn added 6 commits July 20, 2026 12:40
Signed-off-by: Alexandre Chakroun <[email protected]>
Signed-off-by: Alexandre Chakroun <[email protected]>
Signed-off-by: Alexandre Chakroun <[email protected]>
Signed-off-by: Alexandre Chakroun <[email protected]>
@toddbaert
toddbaert force-pushed the handle_bad_selector_errors branch from 9dac26e to a5d8ddf Compare July 20, 2026 16:40
@coderabbitai

coderabbitai Bot commented Jul 20, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 69379f4e-f12e-4041-9ffa-0c67f49b8b57

📥 Commits

Reviewing files that changed from the base of the PR and between a5d8ddf and b782770.

📒 Files selected for processing (5)
  • core/pkg/store/query.go
  • core/pkg/store/query_test.go
  • flagd/pkg/service/flag-evaluation/context_utils.go
  • flagd/pkg/service/flag-evaluation/flag_evaluator.go
  • flagd/pkg/service/flag-evaluation/flag_evaluator_v1.go
💤 Files with no reviewable changes (1)
  • core/pkg/store/query_test.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • flagd/pkg/service/flag-evaluation/flag_evaluator_v1.go
  • flagd/pkg/service/flag-evaluation/flag_evaluator.go

📝 Walkthrough

Walkthrough

Changes

Selectors now validate only source and flagSetId, return validation errors, and expose immutable WithSource and WithFlagSetId builders. Store queries and service handlers propagate invalid selectors as Connect/gRPC InvalidArgument or HTTP 400 responses, with coverage added across evaluation, OFREP, sync, and store tests.

Selector validation and propagation

Layer / File(s) Summary
Selector contract and builders
core/pkg/store/query.go, core/pkg/store/query_test.go
NewSelector validates selector keys and returns errors; specific immutable builders replace the generic indexing method, with tests covering valid and invalid selectors.
Store selector integration
core/pkg/store/store.go, core/pkg/store/store_test.go
Store query and update paths use withKey, WithSource, and WithFlagSetId; selector construction in store tests uses the fluent API.
Evaluation selector error handling
flagd/pkg/service/flag-evaluation/context_utils.go, flagd/pkg/service/flag-evaluation/flag_evaluator*.go, flagd/pkg/service/flag-evaluation/*_test.go
Evaluation services centralize header parsing, return invalid-argument errors before processing invalid selectors, and test old, v1, and v2 RPCs.
OFREP and sync error responses
flagd/pkg/service/flag-evaluation/ofrep/*, flagd/pkg/service/flag-sync/*
OFREP returns HTTP 400 responses and sync returns gRPC InvalidArgument errors when selector validation fails, with corresponding tests.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant EvaluationService
  participant selectorFromHeader
  participant store.NewSelector
  participant Evaluator
  Client->>EvaluationService: Send request with Flagd-Selector
  EvaluationService->>selectorFromHeader: Extract selector header
  selectorFromHeader->>store.NewSelector: Validate expression
  store.NewSelector-->>selectorFromHeader: Selector or error
  selectorFromHeader-->>EvaluationService: Valid selector or InvalidArgument
  EvaluationService->>Evaluator: Continue evaluation with selector
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 23.08% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: validating invalid selector errors.
Description check ✅ Passed The description accurately summarizes the selector validation fix and API changes in the PR.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.12.2)

level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies"


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@flagd/pkg/service/flag-evaluation/flag_evaluator.go`:
- Around line 77-80: Selector parsing failures return without updating the
active OpenTelemetry span. In
flagd/pkg/service/flag-evaluation/flag_evaluator.go at lines 77-80, 191-194,
225-228, 260-263, 295-298, and 330-333, and flag_evaluation_v1.go at lines
77-80, 217-220, 252-255, 287-290, 322-325, and 357-360, update each Resolve*
selectorFromHeader error path to record the error and set the span status to
codes.Error with the error message before returning.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 595d404c-3b47-4981-8a2d-6a5422940b3b

📥 Commits

Reviewing files that changed from the base of the PR and between d434adf and a5d8ddf.

📒 Files selected for processing (15)
  • core/pkg/store/query.go
  • core/pkg/store/query_test.go
  • core/pkg/store/store.go
  • core/pkg/store/store_test.go
  • flagd/pkg/service/flag-evaluation/context_utils.go
  • flagd/pkg/service/flag-evaluation/flag_evaluator.go
  • flagd/pkg/service/flag-evaluation/flag_evaluator_test.go
  • flagd/pkg/service/flag-evaluation/flag_evaluator_v1.go
  • flagd/pkg/service/flag-evaluation/flag_evaluator_v1_test.go
  • flagd/pkg/service/flag-evaluation/flag_evaluator_v2.go
  • flagd/pkg/service/flag-evaluation/flag_evaluator_v2_test.go
  • flagd/pkg/service/flag-evaluation/ofrep/handler.go
  • flagd/pkg/service/flag-evaluation/ofrep/handler_test.go
  • flagd/pkg/service/flag-sync/handler.go
  • flagd/pkg/service/flag-sync/handler_test.go

Signed-off-by: Todd Baert <[email protected]>
@sonarqubecloud

Copy link
Copy Markdown

@toddbaert

toddbaert commented Jul 20, 2026 •

Copy link
Copy Markdown
Member

@alxckn I made some formatting changes, and also added span errors in these cases as CodeRabbit suggested here.

Will merge and release soon.

@toddbaert
toddbaert merged commit 409a62e into open-feature:main Jul 20, 2026
13 checks passed
@github-actions github-actions Bot mentioned this pull request Jul 20, 2026
toddbaert pushed a commit that referenced this pull request Jul 27, 2026
🤖 I have created a release *beep* *boop*
---


<details><summary>flagd: 0.16.1</summary>

##
[0.16.1](flagd/v0.16.0...flagd/v0.16.1)
(2026-07-27)


### 🐛 Bug Fixes

* add server ReadTimeouts, update security deps
([#1980](#1980))
([0e1e8b3](0e1e8b3))
* handle invalid selector errors
([#1976](#1976))
([409a62e](409a62e))
* **security:** update module google.golang.org/grpc to v1.82.1
[security] ([#2005](#2005))
([4281adf](4281adf))
* **security:** update vulnerability-updates [security]
([#1984](#1984))
([2a5a6b6](2a5a6b6))


### ✨ New Features

* **sync:** configurable gRPC keepalive enforcement on sync server
([#1999](#1999))
([19e57b9](19e57b9))
</details>

<details><summary>flagd-proxy: 0.9.7</summary>

##
[0.9.7](flagd-proxy/v0.9.6...flagd-proxy/v0.9.7)
(2026-07-27)


### 🐛 Bug Fixes

* add server ReadTimeouts, update security deps
([#1980](#1980))
([0e1e8b3](0e1e8b3))
* **security:** update module google.golang.org/grpc to v1.82.1
[security] ([#2005](#2005))
([4281adf](4281adf))
* **security:** update vulnerability-updates [security]
([#1984](#1984))
([2a5a6b6](2a5a6b6))
</details>

<details><summary>core: 0.16.1</summary>

##
[0.16.1](core/v0.16.0...core/v0.16.1)
(2026-07-27)


### 🐛 Bug Fixes

* **certreloader:** re-check reload condition under write lock
([#1994](#1994))
([86489da](86489da))
* handle invalid selector errors
([#1976](#1976))
([409a62e](409a62e))
* readyz never true of multiple gRPC syncs are used
([#1985](#1985))
([29833e8](29833e8))
* **security:** update module google.golang.org/grpc to v1.82.1
[security] ([#2005](#2005))
([4281adf](4281adf))
* **security:** update vulnerability-updates [security]
([#1984](#1984))
([2a5a6b6](2a5a6b6))
* **sync:** avoid send on closed channel in fileinfo watcher Close
([#1992](#1992))
([a7339ea](a7339ea))
* **sync:** handle non-string YAML map keys in config parsing
([#1990](#1990))
([3c72e6f](3c72e6f))
* **sync:** re-establish file watch after delete and restore
([#2001](#2001))
([d434adf](d434adf))
* **sync:** synchronize readiness state
([#2006](#2006))
([317a7e0](317a7e0))


### ✨ New Features

* improve ETag and body hash support to blob/http sync
([#1991](#1991))
([1e210d4](1e210d4))


### 🧹 Chore

* fix flaky test
([7ff137d](7ff137d))
</details>

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Signed-off-by: OpenFeature Bot <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants