diff --git a/.changeset/bootstrap-public-flags-only.md b/.changeset/bootstrap-public-flags-only.md new file mode 100644 index 00000000..24e1200e --- /dev/null +++ b/.changeset/bootstrap-public-flags-only.md @@ -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. diff --git a/packages/node-sdk/src/client.ts b/packages/node-sdk/src/client.ts index 52062c09..54ecec74 100644 --- a/packages/node-sdk/src/client.ts +++ b/packages/node-sdk/src/client.ts @@ -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 }), }; } @@ -580,6 +603,7 @@ export class ReflagClient { return { flags, flagStateVersion, + collection, evaluatedBeforeInitialization: !this.initialized, }; } diff --git a/packages/node-sdk/src/flag-definitions.ts b/packages/node-sdk/src/flag-definitions.ts index 8989b51c..caf022f4 100644 --- a/packages/node-sdk/src/flag-definitions.ts +++ b/packages/node-sdk/src/flag-definitions.ts @@ -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, }), diff --git a/packages/node-sdk/src/types.ts b/packages/node-sdk/src/types.ts index 1c79e7e0..548b0bd2 100644 --- a/packages/node-sdk/src/types.ts +++ b/packages/node-sdk/src/types.ts @@ -76,8 +76,12 @@ export type FlagOverrides = Partial< >; export type FlagOverridesFn = (context: Context) => FlagOverrides; -export type FlagDefinition = CompiledFlag; -export type FlagCollection = CompiledCollection & { +export type FlagDefinition = CompiledFlag & { + /** Only explicit false authorizes export to a browser bootstrap payload. */ + secret?: boolean; +}; +export type FlagCollection = { + flags: FlagDefinition[]; segments: NonNullable; }; export type FlagsAPIResponse = FlagCollection & { diff --git a/packages/node-sdk/test/bootstrap-secrecy.test.ts b/packages/node-sdk/test/bootstrap-secrecy.test.ts new file mode 100644 index 00000000..0ccdb251 --- /dev/null +++ b/packages/node-sdk/test/bootstrap-secrecy.test.ts @@ -0,0 +1,320 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +import { + fallbackProviders, + ReflagClient, + type ClientOptions, + type FlagsFallbackSnapshot, + type HttpClient, + type HttpClientResponse, +} from "../src"; +import { definition, snapshot } from "./fixtures"; + +const clients: ReflagClient[] = []; +const context = { company: { id: "acme" }, enableTracking: false }; +const privatePayload = "SERVER_ONLY_PAYLOAD"; + +function privateGraph() { + const dependency = { + ...definition("private-dependency", privatePayload, "off"), + secret: true, + }; + dependency.variants.on = privatePayload; + dependency.rules.unshift( + { + id: "private-missing-context", + filter: { + type: "context", + field: "user.plan", + operator: "IS", + values: ["pro"], + }, + result: { type: "variant", variantKey: "on" }, + }, + { + id: "private-grant", + filter: { + type: "context", + field: "company.id", + operator: "IS", + values: ["acme"], + }, + result: { type: "variant", variantKey: "on" }, + }, + ); + const root = { ...definition("public", false, "off"), secret: false }; + root.variants.on = true; + root.rules.unshift({ + id: "public-target", + filter: { type: "segment", key: "private-segment" }, + result: { type: "variant", variantKey: "on" }, + }); + return snapshot( + [root, dependency], + [ + { + key: "private-segment", + filter: { type: "flag", key: dependency.key, variantKey: "on" }, + }, + ], + ); +} + +function harness(body: unknown = privateGraph(), options: ClientOptions = {}) { + const state: { response: HttpClientResponse } = { + response: { + status: 200, + ok: true, + body: { success: true, ...(body as object) }, + etag: '"initial"', + }, + }; + const post = vi.fn(async () => ({ status: 200, ok: true, body: {} })); + const get = vi.fn(async () => state.response); + const client = new ReflagClient({ + secretKey: "syntheticSecretKeyWithMoreThan22Chars", + offline: false, + flagsSyncMode: "in-request", + flagsFetchRetries: 0, + logger: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() }, + batchOptions: { intervalMs: 1e9, flushOnExit: false }, + httpClient: { get, post } as unknown as HttpClient, + ...options, + }); + clients.push(client); + return { client, state, get, post }; +} + +async function refresh(client: ReflagClient) { + await vi.advanceTimersByTimeAsync(1001); + await client.refreshFlags(); +} + +beforeEach(() => vi.useFakeTimers()); +afterEach(() => { + for (const client of clients.splice(0)) client.destroy(); + vi.useRealTimers(); +}); + +describe("bootstrap visibility boundary", () => { + it.each(["direct", "bound"])( + "exports only public roots through %s bootstrap, retaining private dependencies on the server", + async (mode) => { + const h = harness(); + await h.client.initialize(); + const bootstrap = + mode === "direct" + ? h.client.getFlagsForBootstrap(context) + : h.client.bindClient(context).getFlagsForBootstrap(); + expect(Object.keys(bootstrap.flags)).toEqual(["public"]); + expect(bootstrap).toMatchObject({ + protocolVersion: 2, + flagStateVersion: 1, + context: { company: { id: "acme" } }, + flags: { + public: { + value: true, + resolved: true, + variantKey: "on", + sourceVersionId: "version:public", + matchedRuleId: "public-target", + ruleResults: [{ ruleId: "public-target", matched: true }], + errors: [{ code: "MISSING_CONTEXT_FIELD", field: "user.plan" }], + }, + }, + }); + const serialized = JSON.stringify(bootstrap); + for (const privateData of [ + privatePayload, + "private-dependency", + "private-segment", + ]) + expect(serialized).not.toContain(privateData); + expect(bootstrap.flags.public!.errors[0]).not.toHaveProperty("source"); + await h.client.flush(); + expect(h.post).not.toHaveBeenCalled(); + expect(h.client.getFlags(context)).toEqual({ + public: true, + "private-dependency": privatePayload, + }); + expect(h.client.getFlag(context, "private-dependency", null)).toBe( + privatePayload, + ); + expect(h.client.getFlagDefinitions().flags).toContainEqual( + expect.objectContaining({ key: "private-dependency", secret: true }), + ); + bootstrap.flags.public!.value = false; + bootstrap.flags.public!.errors.length = 0; + expect(h.client.getFlagsForBootstrap(context).flags.public).toMatchObject( + { + value: true, + errors: [{ code: "MISSING_CONTEXT_FIELD" }], + }, + ); + }, + ); + + it.each([undefined, null, "false", 0, {}, true])( + "does not treat visibility %j as an explicit public declaration", + async (secret) => { + const flag = definition("unknown", privatePayload); + Reflect.deleteProperty(flag, "secret"); + if (secret !== undefined) Reflect.set(flag, "secret", secret); + const h = harness( + snapshot([{ ...definition("public", false), secret: false }, flag]), + ); + await h.client.initialize(); + expect(h.client.getFlag(context, "unknown", null)).toBe(privatePayload); + expect(Object.keys(h.client.getFlagsForBootstrap(context).flags)).toEqual( + ["public"], + ); + }, + ); + + it("does not let base or temporary overrides publish secret or unknown keys", async () => { + const h = harness(privateGraph(), { + flagOverrides: () => ({ + public: null, + "private-dependency": "PRIVATE_OVERRIDE", + unknown: "UNKNOWN_OVERRIDE", + }), + }); + await h.client.initialize(); + const pop = h.client.pushFlagOverrides({ + public: { nested: [false] }, + extra: "EXTRA_OVERRIDE", + }); + const bootstrap = h.client.getFlagsForBootstrap(context); + expect(bootstrap.flags).toEqual({ + public: { + flagKey: "public", + value: { nested: [false] }, + resolved: false, + ruleResults: [], + errors: [], + }, + }); + pop(); + expect( + h.client.getFlagsForBootstrap(context).flags.public!.value, + ).toBeNull(); + expect(h.client.getFlags(context)).toMatchObject({ + "private-dependency": "PRIVATE_OVERRIDE", + unknown: "UNKNOWN_OVERRIDE", + }); + }); + + it.each(["offline", "unavailable", "static"])( + "does not publish value-only %s fallbacks or overrides without visibility", + async (mode) => { + const h = harness(undefined, { + offline: mode === "offline", + fallbackFlags: { fallback: privatePayload }, + flagOverrides: { override: privatePayload }, + ...(mode === "static" + ? { + flagsFallbackProvider: fallbackProviders.static({ + flags: { fallback: privatePayload }, + }), + } + : {}), + }); + h.state.response = { status: 503, ok: false, body: undefined }; + expect(h.client.getFlagsForBootstrap(context).flags).toEqual({}); + await h.client.initialize(); + expect(h.client.getFlags(context)).toMatchObject({ + fallback: privatePayload, + override: privatePayload, + }); + expect(h.client.getFlagsForBootstrap(context).flags).toEqual({}); + }, + ); + + it("keeps visibility with its snapshot through 304s, visibility-only refreshes and stale responses", async () => { + const h = harness(); + await h.client.initialize(); + expect(Object.keys(h.client.getFlagsForBootstrap(context).flags)).toEqual([ + "public", + ]); + h.state.response = { status: 304, ok: false, body: undefined }; + await refresh(h.client); + expect(Object.keys(h.client.getFlagsForBootstrap(context).flags)).toEqual([ + "public", + ]); + const changed = privateGraph(); + Reflect.set(changed.flags[0]!, "secret", true); + // Visibility is transport metadata: even the same authored versions/values must replace it. + h.state.response = { + status: 200, + ok: true, + body: { success: true, ...changed }, + etag: '"visibility-only"', + }; + await refresh(h.client); + expect(h.client.getFlagsForBootstrap(context).flags).toEqual({}); + h.state.response.body = { success: true, ...changed, flagStateVersion: 2 }; + await refresh(h.client); + h.state.response = { + status: 200, + ok: true, + body: { success: true, ...privateGraph() }, + }; + await refresh(h.client); + expect(h.client.getFlagsForBootstrap(context)).toMatchObject({ + flagStateVersion: 2, + flags: {}, + }); + h.state.response = { status: 304, ok: false, body: undefined }; + await refresh(h.client); + expect(h.client.getFlagsForBootstrap(context).flags).toEqual({}); + }); + + it("owns visibility independently of HTTP responses, definition copies and bootstrap results", async () => { + const response = privateGraph(); + const h = harness(response); + await h.client.initialize(); + Reflect.set(response.flags[1]!, "secret", false); + const definitions = h.client.getFlagDefinitions(); + Reflect.set(definitions.flags[1]!, "secret", false); + expect(Object.keys(h.client.getFlagsForBootstrap(context).flags)).toEqual([ + "public", + ]); + expect(h.client.getFlagDefinitions().flags[0]).toHaveProperty( + "secret", + false, + ); + }); + + it("persists visibility and restores safe bootstrap without pruning the private evaluation graph", async () => { + let saved: FlagsFallbackSnapshot | undefined; + const provider = { + load: vi.fn(async () => saved), + save: vi.fn(async (_context: unknown, next: FlagsFallbackSnapshot) => { + saved = JSON.parse(JSON.stringify(next)) as FlagsFallbackSnapshot; + }), + }; + const live = harness(privateGraph(), { flagsFallbackProvider: provider }); + await live.client.initialize(); + await live.client.flush(); + expect(saved!.flags).toEqual(privateGraph().flags); + const fallback = harness(undefined, { flagsFallbackProvider: provider }); + fallback.state.response = { status: 503, ok: false, body: undefined }; + await fallback.client.initialize(); + expect( + Object.keys(fallback.client.getFlagsForBootstrap(context).flags), + ).toEqual(["public"]); + expect( + fallback.client.getFlagsForBootstrap(context).flags.public!.value, + ).toBe(true); + expect(fallback.client.getFlag(context, "private-dependency", null)).toBe( + privatePayload, + ); + // Older snapshots remain useful for server evaluation, but cannot authorize client exports. + for (const flag of saved!.flags) Reflect.deleteProperty(flag, "secret"); + const legacy = harness(undefined, { flagsFallbackProvider: provider }); + legacy.state.response = { status: 503, ok: false, body: undefined }; + await legacy.client.initialize(); + expect(legacy.client.getFlag(context, "public")).toBe(true); + expect(legacy.client.getFlagsForBootstrap(context).flags).toEqual({}); + }); +}); diff --git a/packages/node-sdk/test/fixtures.ts b/packages/node-sdk/test/fixtures.ts index c290aaf0..cf40b356 100644 --- a/packages/node-sdk/test/fixtures.ts +++ b/packages/node-sdk/test/fixtures.ts @@ -13,6 +13,7 @@ export function definition( return { key, sourceVersionId: `version:${key}`, + secret: false, variants: { [variantKey]: value }, rules: [ { diff --git a/packages/react-native-sdk/test/usage.test.tsx b/packages/react-native-sdk/test/usage.test.tsx index f6708613..4ea44cc0 100644 --- a/packages/react-native-sdk/test/usage.test.tsx +++ b/packages/react-native-sdk/test/usage.test.tsx @@ -322,6 +322,7 @@ describe("React Native v2", () => { flags: Object.entries(values).map(([key, value]) => ({ key, sourceVersionId: `source:${key}`, + secret: false, variants: { chosen: value }, rules: [ {