Skip to content
Open
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 and their remote config out of browser data returned by `getFlagsForBootstrap()`, including bound clients. They remain available on the server. The v1 data format does not change.
31 changes: 24 additions & 7 deletions packages/node-sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,11 @@ type BulkEvent =
function compileFlagDefinitions(
flags: FlagAPIResponse[],
): CachedFlagDefinition[] {
return flags.map((flagDef) => {
return structuredClone(flags).map((flagDef) => {
return {
...flagDef,
// Only an explicit public marker may authorize browser bootstrap export.
secret: flagDef.secret !== false,
enabledEvaluator: newEvaluator(
flagDef.targeting.rules.map((rule) => ({
filter: rule.filter,
Expand Down Expand Up @@ -204,7 +206,7 @@ function createFlagsFallbackSnapshot(
return {
version: 1,
savedAt: new Date().toISOString(),
flags,
flags: structuredClone(flags),
};
}

Expand Down Expand Up @@ -652,6 +654,8 @@ export class ReflagClient {
snapshot: FlagsCacheSnapshot,
etag?: string,
) {
// Own the response before asynchronous persistence can observe caller mutation.
const fallbackSnapshot = createFlagsFallbackSnapshot(flags);
// Serialize background writes so an older save cannot overwrite a newer one.
this.flagsFallbackSavePromise = this.flagsFallbackSavePromise.then(
async () => {
Expand All @@ -660,7 +664,7 @@ export class ReflagClient {
if (provider) {
await provider.save(
this._config.flagsFallbackProviderContext,
createFlagsFallbackSnapshot(flags),
fallbackSnapshot,
);
}
// Enable revalidation only after saving. A late save updates only its
Expand Down Expand Up @@ -1025,6 +1029,7 @@ export class ReflagClient {
const flags = this.flagsCache.get() || [];
return flags.map((f) => ({
key: f.key,
secret: f.secret,
description: f.description,
flag: f.targeting,
config: f.config,
Expand Down Expand Up @@ -1083,8 +1088,7 @@ export class ReflagClient {
}

/**
* Gets the evaluated flags for the current context without wrapping them in getters.
* This method returns raw flag data suitable for bootstrapping client-side applications.
* Get public flag data to send to a browser.
*
* @param options - The options for the context.
* @param options.enableTracking - Whether to enable tracking for the context.
Expand All @@ -1097,18 +1101,31 @@ export class ReflagClient {
*
* @remarks
* Call `initialize` before calling this method to ensure the flag definitions are cached, no flags will be returned otherwise.
* This method returns RawFlag objects without wrapping them in getters, making them suitable for serialization.
* Only flags marked `secret: false` are included. Secret flags, unknown overrides
* and fallback values without definitions are left out. Older definitions without
* the `secret` field are also left out. Include only browser-safe context attributes.
**/
public getFlagsForBootstrap({
enableTracking = true,
...context
}: ContextWithTracking): BootstrappedFlags {
const contextWithTracking = { enableTracking, ...context };
const flagStateVersion = this.flagsCache.getFlagStateVersion();
const publicKeys = new Set(
this.flagsCache
.get()
?.filter((flag) => flag.secret === false)
.map((flag) => flag.key),
);
const flags = this._getFlags(contextWithTracking);

return {
context: contextWithTracking,
flags: this._getFlags(contextWithTracking),
flags: structuredClone(
Object.fromEntries(
Object.entries(flags).filter(([key]) => publicKeys.has(key)),
),
),
...(flagStateVersion !== undefined ? { flagStateVersion } : {}),
};
}
Expand Down
6 changes: 6 additions & 0 deletions packages/node-sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ export type FlagOverride =
* Describes a feature definition.
*/
export type FlagDefinition = {
/** Only explicit false authorizes browser bootstrap export. */
secret?: boolean;

/**
* The key of the feature.
*/
Expand Down Expand Up @@ -348,6 +351,9 @@ export type FlagConfigVariant = {
* @internal
*/
export type FlagAPIResponse = {
/** Missing visibility in older responses is not a public declaration. */
secret?: boolean;

/**
* The key of the feature.
*/
Expand Down
Loading
Loading