Skip to content

fix(flags): guard against option index colliding with SPECIAL_INTEGERS in serialization - #590

Open
reginaldalfret wants to merge 1 commit into
vercel:mainfrom
reginaldalfret:fix/flags-serialization-max-option-length-guard
Open

reginaldalfret wants to merge 1 commit into
vercel:mainfrom
reginaldalfret:fix/flags-serialization-max-option-length-guard

Conversation

@reginaldalfret

Copy link
Copy Markdown

What?

This PR guards option indices against colliding with \SPECIAL_INTEGERS\ in \packages/flags/src/lib/serialization.ts\ by checking \matchedIndex < MAX_OPTION_LENGTH\ before returning the option index.

Why?

In \serialization.ts, \MAX_OPTION_LENGTH = 252\ was defined to reserve byte values 252–255 for \SPECIAL_INTEGERS:

  • \252: \SPECIAL_INTEGERS.NULL\
  • \253: \SPECIAL_INTEGERS.BOOLEAN_FALSE\
  • \254: \SPECIAL_INTEGERS.BOOLEAN_TRUE\
  • \255: \SPECIAL_INTEGERS.UNLISTED_VALUE\

However, in \serialize():
\\ s
const matchedIndex = matchIndex(options, value);
if (matchedIndex > -1) return matchedIndex;
\
If a flag had 252 or more options, an option at index 252 returned \252, which deserialized as
ull. An option at index 253 deserialized as \ alse, index 254 as \ rue, and index 255 attempted to read from the unlisted spillover array (resulting in \undefined\ or crashes). Furthermore, any index >= 256 would be truncated by \Uint8Array\ (\index & 0xFF), corrupting the value.

How?

  1. Add \matchedIndex < MAX_OPTION_LENGTH\ to the condition in \serialize(), ensuring option indices >= 252 fall through to the unlisted values payload so their exact values round-trip reliably.
  2. Add comprehensive unit tests in \packages/flags/src/lib/serialization.test.ts\ verifying round-trip serialization across options < 252 and >= 252 (including 252, 253, 254, 255, and beyond).

…S in serialization

When a flag has >= 252 options, option indices at or above MAX_OPTION_LENGTH (252) previously collided with SPECIAL_INTEGERS during deserialization (252 deserialized as NULL, 253 as false, 254 as true, 255 as an unlisted value). Guard matchedIndex < MAX_OPTION_LENGTH so higher option indices are safely preserved via unlisted value transport.

Signed-off-by: Reginald Alfret <reginaldalfret@gmail.com>
@vercel

vercel Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

@reginaldalfret is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

No deployments
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.

1 participant