Skip to content

Session.fromJson throws on wasm when expires_in crosses js_interop as a double, silently breaking cross-tab auth sync #1687

Closed

Description

@ChiefWaster

Describe the bug

Session.fromJson casts json['expires_in'] straight to int?, but it is also
called with maps that did not come from jsonDecode.

GoTrueClient._mayStartBroadcastChannel passes the payload of a web
BroadcastChannel message directly into it:

// packages/gotrue/lib/src/gotrue_client.dart
if (messageEvent['session'] != null) {
  session = Session.fromJson(messageEvent['session']);
}

Every JS number crossing that interop boundary arrives as a double. On
dart2js the cast is harmless, because Dart ints are doubles at runtime. On
dart2wasm int and double are distinct types, so
json['expires_in'] as int? throws.

Why it is more than a logged error. The throw happens inside the channel's
onMessage listener, and the surrounding try/catch guards only channel
setup, not the async callback. So all three of these are skipped:

if (session != null) {
  _saveSession(session);
} else {
  _removeSession();
}
notifyAllSubscribers(event, session: session, broadcast: false);

The receiving tab therefore silently never applies the auth change — no session
saved, none removed, no subscribers notified. In our app this surfaces as users
being unexpectedly returned to the login screen: the tab keeps a session that
another tab has already rotated away from, and the next request fails. We saw 65
occurrences across 13 different screens before locating it, because the fault is
on the global auth stream rather than on any one screen.

Suggested fix, which preserves current behaviour for the int case:

expiresIn: (json['expires_in'] as num?)?.toInt(),

Happy to open a PR with the one-line change plus the test below if that is
welcome.

Library affected

gotrue (reached via supabase_flutter)

Reproduction

No external repro app needed — it reproduces as a plain unit test, because the
Dart VM also treats int and double as distinct. Added to
packages/gotrue/test/src/types/session_test.dart, this fails on dart test
before the fix and passes after, so it needs no wasm-specific tooling in CI:

test('accepts a double expires_in, as sent by a BroadcastChannel', () {
  final json = {
    'access_token': 'test-access-token',
    'expires_in': 3600.0, // a JS number crossing js_interop
    'token_type': 'bearer',
    'user': {
      'id': '123',
      'app_metadata': <String, dynamic>{},
      'user_metadata': <String, dynamic>{},
      'aud': 'authenticated',
      'created_at': '2023-01-01T00:00:00Z',
    },
  };

  final session = Session.fromJson(json);

  expect(session, isNotNull);
  expect(session!.expiresIn, equals(3600));
});

Failure without the fix:

type 'double' is not a subtype of type 'int?' in type cast

Steps to reproduce

Requires two tabs — a BroadcastChannel never delivers a message to the tab
that posted it, so a single tab cannot trigger this.

  1. Build a Flutter web app with flutter build web --wasm (release) and serve it
  2. Sign in, and open the app in a second tab of the same browser
  3. Sign out in the second tab (a token refresh also triggers it)
  4. The first tab throws in the BroadcastChannel listener instead of applying the
    SIGNED_OUT event

System Info

  • gotrue 2.26.0, via supabase_flutter 2.16.0
  • Flutter 3.44.0 (stable), Dart 3.10
  • Also present on main as of 2026-08-12: packages/gotrue/lib/src/types/session.dart:50
    still reads expiresIn: json['expires_in'] as int?. The surrounding file has
    been refactored recently (expiresAt is now a DateTime? via
    decodeJwtPayload) but that cast is unchanged.

Platform

Web — wasm only. dart2js builds are unaffected, which is why this can hide
until a project switches to --wasm.

Logs

Stack trace from a wasm release build, captured with --no-strip-wasm so the
frames resolve (frames are bare integers otherwise):

TypeError: Runtime type check failed
  $.<obj> as int
  $.Session.fromJson
  $.closure wrapper at .../gotrue-2.26.0/lib/src/gotrue_client.dart:1390:77 trampoline
  $._rootRunUnary
  $._CustomZone.runUnary
  $._CustomZone.runUnaryGuarded
  $._BufferingStreamSubscription._sendData (body)
  $._BufferingStreamSubscription._sendData (checked entry)
  $._DelayedData.perform (checked entry)
  $.closure wrapper at .../async/stream_impl.dart:645:23 trampoline
  $._invokeCallback

Sibling casts I looked at and believe are not affected, for completeness:
session.dart's expiresAt getter parses the JWT with Dart's own jsonDecode
(so it yields a real int) and is already wrapped in catch (_) => null. The
exp/nbf/iat casts in types/jwt.dart may be worth a look if any of them
can be reached from a js_interop payload, but I have no evidence that they are.

Activity

  1. changed the title [-]fix(gotrue): Session.fromJson throws on wasm when expires_in crosses js_interop as a double, silently breaking cross-tab auth sync[/-] [+]Session.fromJson throws on wasm when expires_in crosses js_interop as a double, silently breaking cross-tab auth sync[/+] on Aug 12, 2026
  2. added a commit that references this issue on Aug 14, 2026
    5d278b9
  3. added a commit that references this issue on Aug 14, 2026
    6c8047e
  4. spydon commented on Aug 14, 2026

    @spydon
    Contributor

    @ChiefWaster the fix is now out in supabase_flutter v2.17.2, supabase v2.16.1 and gotrue 2.27.2
    I must have missed that you opened this issue, otherwise I would have got it in earlier!

  5. added a commit that references this issue on Aug 14, 2026
    a0be827
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions