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/refresh-parsers-native.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"posthog_flutter": patch
---

Fix session replay masking on iOS and Android ignoring changed `maskAllTexts`/`maskAllImages` flags when `setup()` is called again
6 changes: 6 additions & 0 deletions posthog_flutter/lib/src/posthog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'posthog_config.dart';
import 'posthog_flutter_platform_interface.dart';
import 'posthog_internal_events.dart';
import 'posthog_observer.dart';
import 'replay/mask/posthog_mask_controller.dart';
import 'utils/before_send.dart';

/// Entry point for the PostHog Flutter SDK.
Expand Down Expand Up @@ -67,6 +68,11 @@ class Posthog {

_config = config; // Store the config

// The mask controller singleton may predate this setup() (or a previous
// setup() built it with different masking flags); without a refresh the
// stale parser map would keep deciding what replay masks on every platform.
PostHogMaskController.instance.refreshParsers(config.sessionReplayConfig);

if (config.sessionReplay) {
PostHogInternalEvents.sessionRecordingActive.value = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ class ScreenshotCapturer {

ScreenshotCapturer(this._config);

/// A second `setup()` replaces the SDK config object, but the capturer is
/// built once per widget lifecycle β€” resolving the live config at capture
/// time keeps masking flags from freezing at their first-setup values.
@visibleForTesting
PostHogConfig get effectiveConfig => Posthog().config ?? _config;

void cancel() {
_cancelled = true;
}
Expand Down Expand Up @@ -448,7 +454,7 @@ class ScreenshotCapturer {
srcHeight: srcHeight,
);

final replayConfig = _config.sessionReplayConfig;
final replayConfig = effectiveConfig.sessionReplayConfig;

final postHogWidgetWrapperElements =
PostHogMaskController.instance.getPostHogWidgetWrapperElements();
Expand Down
40 changes: 40 additions & 0 deletions posthog_flutter/test/posthog_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:posthog_flutter/posthog_flutter.dart';
import 'package:posthog_flutter/src/posthog_flutter_platform_interface.dart';
import 'package:posthog_flutter/src/posthog_internal_events.dart';
import 'package:posthog_flutter/src/replay/mask/posthog_mask_controller.dart';
import 'package:posthog_flutter/src/replay/screenshot/screenshot_capturer.dart';

import 'posthog_flutter_platform_interface_fake.dart';

Expand Down Expand Up @@ -40,6 +42,44 @@ void main() {
},
);

test(
'setup after close with different masking flags rebuilds the parser map',
() async {
final controller = PostHogMaskController.instance;
addTearDown(() => controller.refreshParsers(null));

final imagesMasked = PostHogConfig('test_project_token');
await Posthog().setup(imagesMasked);
expect(controller.parsers.keys, contains('RenderImage'));

await Posthog().close();
final imagesUnmasked = PostHogConfig('test_project_token')
..sessionReplayConfig.maskAllImages = false;
await Posthog().setup(imagesUnmasked);
expect(controller.parsers.keys, isNot(contains('RenderImage')));
expect(controller.parsers.keys, contains('RenderParagraph'));
},
);

test(
'screenshot capturer resolves the live config after close and re-setup',
() async {
final first = PostHogConfig('test_project_token')
..sessionReplayConfig.maskAllImages = false;
await Posthog().setup(first);
// PostHogWidget builds its capturer once and keeps it across a
// close()/setup() reconfigure, so the capturer must follow the live
// config rather than the one it was constructed with.
final capturer = ScreenshotCapturer(first);
expect(capturer.effectiveConfig, same(first));

await Posthog().close();
final second = PostHogConfig('test_project_token');
await Posthog().setup(second);
Comment on lines +69 to +78

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw, i wouldn't be surprised if we had more gaps with duplicate setup calls, this looks like a really fringe scenario, and probably enters undefined behavior territory (unless i'm missing something)

i'm surprised you can initialize twice - imo the second should be a no-op

expect(capturer.effectiveConfig, same(second));
},
);

group('setup with blank project token', () {
const blankProjectTokens = <String, String>{
'empty string': '',
Expand Down
Loading