Skip to content

fix(Utils): coerce non-string values before split in validateRule - #8198

Open
bardock-2393 wants to merge 2 commits into
Flagsmith:mainfrom
bardock-2393:fix/create-segment-condition-value-split-crash
Open

fix(Utils): coerce non-string values before split in validateRule#8198
bardock-2393 wants to merge 2 commits into
Flagsmith:mainfrom
bardock-2393:fix/create-segment-condition-value-split-crash

Conversation

@bardock-2393

Copy link
Copy Markdown
Contributor

Thanks for submitting a PR! Please check the boxes below:

  • I have read the Contributing Guide.
  • I have added information to docs/ if required so people know about the feature.
  • I have filled in the "Changes" section below.
  • I have filled in the "How did you test this code" section below.

Changes

Closes #7536

Utils.validateRule (used by both the create-segment and edit-segment forms to decide whether a condition row is valid) crashes with TypeError: Cannot read properties of undefined (reading 'split') for some segment conditions.

#7779 previously added a guard so a missing (null/undefined) condition value returns false instead of crashing for the semver and Modulo operators. That guard only covers a missing value, though — it still calls .split() directly on rule.value once the value is present. If a condition reaches validateRule with a truthy value that isn't a string (for example a boolean, which can happen while a condition row's value is mid-update), .split doesn't exist on it and the same class of crash occurs, which is why this issue stayed open after #7779 merged.

This PR coerces the value to a string (`${rule.value}`) before calling .split() in both the semver and Modulo branches, so validateRule never throws regardless of what type the value happens to be, while keeping the existing pass/fail validation behaviour for well-formed input unchanged.

How did you test this code?

Added unit tests in frontend/common/utils/__tests__/utils.test.ts covering validateRule for the Modulo and semver operators with missing (undefined/null/empty string) and non-string (boolean) values, plus the IS_SET/IS_NOT_SET "hideValue" operator, and existing valid/invalid value cases. The two non-string-value tests fail on the code prior to this fix (confirmed by reverting the fix locally and re-running) and pass afterwards.

Ran the full frontend unit test suite (npm run test:unit): 368 tests passed across 28 suites. Also ran npx eslint and npm run typecheck against the changed files — no new lint or type errors introduced.

The semver and MODULO branches of validateRule guarded against a
missing rule value but still called .split() directly on rule.value,
so a truthy non-string value (e.g. a boolean) reached .split and threw.
Coerce to a string first so validation degrades to false instead of
crashing CreateSegment/EditSegment's isValid check.
@bardock-2393
bardock-2393 requested a review from a team as a code owner July 31, 2026 19:28
@bardock-2393
bardock-2393 requested review from talissoncosta and removed request for a team July 31, 2026 19:28
@vercel

vercel Bot commented Jul 31, 2026

Copy link
Copy Markdown

@bardock-2393 is attempting to deploy a commit to the Flagsmith Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jul 31, 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: ASSERTIVE

Plan: Pro Plus

Run ID: c9683f68-8015-471d-82ad-8357332d6367

📥 Commits

Reviewing files that changed from the base of the PR and between 52ade97 and 0a02692.

📒 Files selected for processing (1)
  • frontend/common/utils/__tests__/utils.test.ts

📝 Walkthrough

Walkthrough

validateRule now stringifies semver and modulo values before validation. New Jest tests mock browser-dependent modules and cover falsy, deleted, missing, null, non-string, valid, and invalid rule values.

Estimated code review effort: 2 (Simple) | ~10 minutes

✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch

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.

@github-actions github-actions Bot added the front-end Issue related to the React Front End Dashboard label Jul 31, 2026

@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


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: f878f4cf-e85a-4366-88a4-0078f2dc7264

📥 Commits

Reviewing files that changed from the base of the PR and between fc33002 and 52ade97.

📒 Files selected for processing (2)
  • frontend/common/utils/__tests__/utils.test.ts
  • frontend/common/utils/utils.tsx

Comment thread frontend/common/utils/__tests__/utils.test.ts
Strengthens the MODULO and semver regression tests per CodeRabbit review
feedback on PR Flagsmith#8198 — they previously only checked for no throw.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

front-end Issue related to the React Front End Dashboard

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeError: Cannot read properties of undefined (reading 'split')

1 participant