From 7dad379a164fa03f8e61533dcf20d1ebb2fc791b Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 6 Dec 2026 06:16:02 +0000 Subject: [PATCH 1/2] fix: reject an empty userId in setUserId instead of logging the user out [ENG-1499] setUserId('') treated the empty string as a switch to a different user: it tore down the identified user's state, cleared the Embedded Data bag, and then identified no one. It now returns Err(MissingFieldError('userId')) and changes nothing. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011A96cvjgJLErFw37fziM7Z --- packages/formbricks/README.md | 3 ++ packages/formbricks/lib/src/user/user.dart | 14 ++++++ .../lib/src/widgets/formbricks_widget.dart | 6 ++- packages/formbricks/test/user/user_test.dart | 49 +++++++++++++++++++ 4 files changed, 70 insertions(+), 2 deletions(-) diff --git a/packages/formbricks/README.md b/packages/formbricks/README.md index 3ed5cb2..4097397 100644 --- a/packages/formbricks/README.md +++ b/packages/formbricks/README.md @@ -85,6 +85,9 @@ Each returns a `Future>`. | `Formbricks.setAppearance(FormbricksAppearance)` | Surveys render `light` (default), `dark`, or `system`. See Dark mode. | | `Formbricks.logout()` | Reset to an anonymous session. | +`setUserId('')` returns an error and changes nothing, so an identified user +stays identified. Call `logout()` to clear the user. + Attribute keys must be lowercase letters, numbers, and underscores, and start with a letter. `DateTime` values are sent as UTC ISO-8601 strings; numbers stay numbers. Identity/attribute changes are debounced (~500 ms) and coalesced into a diff --git a/packages/formbricks/lib/src/user/user.dart b/packages/formbricks/lib/src/user/user.dart index 903ad33..bc95156 100644 --- a/packages/formbricks/lib/src/user/user.dart +++ b/packages/formbricks/lib/src/user/user.dart @@ -16,6 +16,8 @@ import 'update_queue.dart'; /// Identifies the current contact as [userId]. /// +/// - Empty [userId] → `Err(MissingFieldError)` and nothing changes, so an +/// identified user stays identified. Use [logout] to clear the user. /// - Same value already set → idempotent `Ok` no-op. /// - A *different* userId already set → [tearDown] the previous user state /// first, then queue the new id. @@ -31,6 +33,18 @@ Future> setUserId( final cfg = config ?? FormbricksConfig.instance; final q = queue ?? UpdateQueue.instance; + // Validate before touching any state: an empty id is not an identity, and + // treating it as a switch would tear down (log out) the current user and + // then identify no one. + if (userId.isEmpty) { + final error = MissingFieldError( + 'userId', + message: 'userId must not be empty. Call logout() to clear the user.', + ); + Logger.error(error.message); + return Result.err(error); + } + final currentUserId = cfg.get().user.data.userId; if (currentUserId == userId) { diff --git a/packages/formbricks/lib/src/widgets/formbricks_widget.dart b/packages/formbricks/lib/src/widgets/formbricks_widget.dart index bcf298a..d7a06a9 100644 --- a/packages/formbricks/lib/src/widgets/formbricks_widget.dart +++ b/packages/formbricks/lib/src/widgets/formbricks_widget.dart @@ -148,8 +148,10 @@ class Formbricks extends StatefulWidget { /// Identifies the current contact as [userId] (`checkSetup: true`). /// /// Idempotent for the already-set value; switching to a different id resets - /// the prior user state first. The backend sync runs from the debounced - /// update queue, so this resolves before the network call completes. + /// the prior user state first. An empty [userId] returns + /// `Err(MissingFieldError)` and changes nothing; use [logout] to clear the + /// user. The backend sync runs from the debounced update queue, so this + /// resolves before the network call completes. static Future> setUserId(String userId) { return _queue.add>( () => user.setUserId(userId), diff --git a/packages/formbricks/test/user/user_test.dart b/packages/formbricks/test/user/user_test.dart index ae78d5a..5022416 100644 --- a/packages/formbricks/test/user/user_test.dart +++ b/packages/formbricks/test/user/user_test.dart @@ -4,7 +4,9 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:formbricks/src/common/api_client.dart'; import 'package:formbricks/src/common/config.dart'; import 'package:formbricks/src/common/logger.dart'; +import 'package:formbricks/src/common/result.dart'; import 'package:formbricks/src/survey/embedded_data.dart'; +import 'package:formbricks/src/types/errors.dart'; import 'package:formbricks/src/user/update_queue.dart'; import 'package:formbricks/src/user/user.dart'; import 'package:http/http.dart' as http; @@ -122,6 +124,53 @@ void main() { expect(result.isOk, isTrue); expect(queue.pendingUserId, 'u1'); }); + + test( + 'empty string when one set → MissingFieldError, user kept, nothing queued', + () async { + final config = await _seed(userId: 'u1'); + final queue = UpdateQueue.instance + ..configOverride = config + ..apiClientOverride = _noopApi(); + EmbeddedDataStore.instance + ..clear() + ..set({'plan': 'pro'}); + addTearDown(EmbeddedDataStore.instance.clear); + + final result = await setUserId('', config: config, queue: queue); + + expect(result, isA>()); + final error = (result as Err).error; + expect(error, isA()); + expect((error as MissingFieldError).field, 'userId'); + // No teardown: the identified user, their segments and the Embedded + // Data bag all survive. + expect(config.get().user.data.userId, 'u1'); + expect(config.get().user.data.segments, ['seg_1']); + expect(EmbeddedDataStore.instance.snapshot(), {'plan': 'pro'}); + expect(queue.isEmpty, isTrue); + }, + ); + + test( + 'empty string from anonymous → MissingFieldError, nothing queued', + () async { + final config = await _seed(); + final queue = UpdateQueue.instance + ..configOverride = config + ..apiClientOverride = _noopApi(); + + final result = await setUserId('', config: config, queue: queue); + + expect(result.isErr, isTrue); + expect( + (result as Err).error, + isA(), + ); + expect(config.get().user.data.userId, isNull); + expect(queue.isEmpty, isTrue); + }, + ); }); group('logout', () { From c2b5984e4f3924b3f77944e7fddcaa31048271dc Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 6 Dec 2026 06:17:58 +0000 Subject: [PATCH 2/2] fix: also reject a whitespace-only userId and correct the setUserId docs [ENG-1499] Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011A96cvjgJLErFw37fziM7Z --- packages/formbricks/README.md | 4 ++-- packages/formbricks/lib/src/user/user.dart | 11 +++++---- .../lib/src/widgets/formbricks_widget.dart | 2 +- packages/formbricks/test/user/user_test.dart | 23 ++++++++++++++++++- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/packages/formbricks/README.md b/packages/formbricks/README.md index 4097397..0b1f3ec 100644 --- a/packages/formbricks/README.md +++ b/packages/formbricks/README.md @@ -85,8 +85,8 @@ Each returns a `Future>`. | `Formbricks.setAppearance(FormbricksAppearance)` | Surveys render `light` (default), `dark`, or `system`. See Dark mode. | | `Formbricks.logout()` | Reset to an anonymous session. | -`setUserId('')` returns an error and changes nothing, so an identified user -stays identified. Call `logout()` to clear the user. +`setUserId('')` (or a whitespace-only id) returns an error and changes nothing, +so an identified user stays identified. Call `logout()` to clear the user. Attribute keys must be lowercase letters, numbers, and underscores, and start with a letter. `DateTime` values are sent as UTC ISO-8601 strings; numbers stay diff --git a/packages/formbricks/lib/src/user/user.dart b/packages/formbricks/lib/src/user/user.dart index bc95156..46e1a51 100644 --- a/packages/formbricks/lib/src/user/user.dart +++ b/packages/formbricks/lib/src/user/user.dart @@ -16,14 +16,15 @@ import 'update_queue.dart'; /// Identifies the current contact as [userId]. /// -/// - Empty [userId] → `Err(MissingFieldError)` and nothing changes, so an -/// identified user stays identified. Use [logout] to clear the user. +/// - Empty or whitespace-only [userId] → `Err(MissingFieldError)` and nothing +/// changes, so an identified user stays identified. Use [logout] to clear +/// the user. /// - Same value already set → idempotent `Ok` no-op. /// - A *different* userId already set → [tearDown] the previous user state /// first, then queue the new id. /// - Anonymous → queue the id directly (no teardown). /// -/// Returns immediately with `Ok`; the backend `createOrUpdateUser` fires from +/// Otherwise returns immediately with `Ok`; the backend `createOrUpdateUser` fires from /// the debounced [UpdateQueue]. Future> setUserId( String userId, { @@ -33,10 +34,10 @@ Future> setUserId( final cfg = config ?? FormbricksConfig.instance; final q = queue ?? UpdateQueue.instance; - // Validate before touching any state: an empty id is not an identity, and + // Validate before touching any state: a blank id is not an identity, and // treating it as a switch would tear down (log out) the current user and // then identify no one. - if (userId.isEmpty) { + if (userId.trim().isEmpty) { final error = MissingFieldError( 'userId', message: 'userId must not be empty. Call logout() to clear the user.', diff --git a/packages/formbricks/lib/src/widgets/formbricks_widget.dart b/packages/formbricks/lib/src/widgets/formbricks_widget.dart index d7a06a9..0d8c4aa 100644 --- a/packages/formbricks/lib/src/widgets/formbricks_widget.dart +++ b/packages/formbricks/lib/src/widgets/formbricks_widget.dart @@ -148,7 +148,7 @@ class Formbricks extends StatefulWidget { /// Identifies the current contact as [userId] (`checkSetup: true`). /// /// Idempotent for the already-set value; switching to a different id resets - /// the prior user state first. An empty [userId] returns + /// the prior user state first. An empty or whitespace-only [userId] returns /// `Err(MissingFieldError)` and changes nothing; use [logout] to clear the /// user. The backend sync runs from the debounced update queue, so this /// resolves before the network call completes. diff --git a/packages/formbricks/test/user/user_test.dart b/packages/formbricks/test/user/user_test.dart index 5022416..696821a 100644 --- a/packages/formbricks/test/user/user_test.dart +++ b/packages/formbricks/test/user/user_test.dart @@ -162,7 +162,7 @@ void main() { final result = await setUserId('', config: config, queue: queue); - expect(result.isErr, isTrue); + expect(result, isA>()); expect( (result as Err).error, isA(), @@ -171,6 +171,27 @@ void main() { expect(queue.isEmpty, isTrue); }, ); + + test( + 'whitespace-only when one set → MissingFieldError, user kept', + () async { + final config = await _seed(userId: 'u1'); + final queue = UpdateQueue.instance + ..configOverride = config + ..apiClientOverride = _noopApi(); + + final result = await setUserId(' ', config: config, queue: queue); + + expect(result, isA>()); + expect( + (result as Err).error, + isA(), + ); + expect(config.get().user.data.userId, 'u1'); + expect(config.get().user.data.segments, ['seg_1']); + expect(queue.isEmpty, isTrue); + }, + ); }); group('logout', () {