Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/bootstrap-public-flags-only.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@reflag/node-sdk": patch
---

Keep secret flags out of browser data returned by `getFlagsForBootstrap()`, including bound clients. Server-side reads still work, including public flags that depend on secret flags. Remove dependency names from error details sent to the browser.
30 changes: 27 additions & 3 deletions packages/node-sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,19 +507,42 @@ export class ReflagClient {
]),
) as TypedFlags;
}
/** Serialization is not a read by the browser; it must not emit check events. */
/**
* Get public flag data for a browser without recording flag checks.
* Secret flags can still be read on the server and used by public flags.
* Flags without `secret: false` are left out. Dependency names are removed
* from error details. Include only browser-safe attributes in the context.
*/
public getFlagsForBootstrap(options: ContextWithTracking): BootstrappedFlags {
const { flags, flagStateVersion } = this.evaluate(
const { flags, flagStateVersion, collection } = this.evaluate(
options,
undefined,
false,
);
const publicKeys = new Set(
collection?.flags
.filter((flag) => flag.secret === false)
.map((flag) => flag.key),
);
const publicFlags = Object.fromEntries(
Object.entries(flags)
.filter(([key]) => publicKeys.has(key))
.map(([key, result]) => [
key,
{
...result,
// Match publishable-key HTTP evaluation: dependency origins can name
// private flags or segments, even when the requested root is public.
errors: result.errors.map(({ source: _source, ...error }) => error),
},
]),
);
return {
protocolVersion: 2,
context: JSON.parse(
canonicalJSONStringify(contextOnly(options)),
) as Context,
flags: structuredClone(flags),
flags: structuredClone(publicFlags),
...(flagStateVersion === undefined ? {} : { flagStateVersion }),
};
}
Expand Down Expand Up @@ -580,6 +603,7 @@ export class ReflagClient {
return {
flags,
flagStateVersion,
collection,
evaluatedBeforeInitialization: !this.initialized,
};
}
Expand Down
3 changes: 3 additions & 0 deletions packages/node-sdk/src/flag-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,14 @@ export function prepareFlagCollection(
({
key,
sourceVersionId,
secret,
variants,
rules,
}: FlagCollection["flags"][number]) => ({
key,
sourceVersionId,
// Older responses/snapshots and malformed visibility cannot authorize export.
secret: secret !== false,
variants,
rules,
}),
Expand Down
8 changes: 6 additions & 2 deletions packages/node-sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ export type FlagOverrides = Partial<
>;
export type FlagOverridesFn = (context: Context) => FlagOverrides;

export type FlagDefinition = CompiledFlag<FlagValue>;
export type FlagCollection = CompiledCollection<FlagValue> & {
export type FlagDefinition = CompiledFlag<FlagValue> & {
/** Only explicit false authorizes export to a browser bootstrap payload. */
secret?: boolean;
};
export type FlagCollection = {
flags: FlagDefinition[];
segments: NonNullable<CompiledCollection["segments"]>;
};
export type FlagsAPIResponse = FlagCollection & {
Expand Down
Loading
Loading