@oxyhq/core 2.3.0 → 2.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/esm/index.js CHANGED
@@ -105,7 +105,7 @@ export { CENTRAL_AUTH_URL, CENTRAL_IDP_APEX, resolveCentralAuthUrl } from './uti
105
105
  export { parseSsoReturnFragment, consumeSsoReturn } from './utils/ssoReturn.js';
106
106
  export { generateSsoState } from './mixins/OxyServices.sso.js';
107
107
  // SSO bounce — per-origin sessionStorage keys, bounce URL builder, predicates
108
- export { SSO_CALLBACK_PATH, SSO_GUARD_TTL_MS, ssoStateKey, ssoGuardKey, ssoDestKey, ssoNoSessionKey, ssoNavigate, buildSsoBounceUrl, isCentralIdPOrigin, guardActive, } from './utils/ssoBounce.js';
108
+ export { SSO_CALLBACK_PATH, SSO_GUARD_TTL_MS, ssoStateKey, ssoGuardKey, ssoDestKey, ssoNoSessionKey, ssoAttemptedKey, ssoNavigate, buildSsoBounceUrl, isCentralIdPOrigin, guardActive, } from './utils/ssoBounce.js';
109
109
  export { runColdBoot } from './utils/coldBoot.js';
110
110
  // ---------------------------------------------------------------------------
111
111
  // Constants
@@ -26,11 +26,15 @@
26
26
  * the session, then restores the original destination.
27
27
  *
28
28
  * Loop proof (logged-out): first load all steps skip → `sso-bounce` sets
29
- * guard/state/dest and navigates; the IdP (no central session) returns
29
+ * guard/state/dest + the outcome-independent attempted-flag
30
+ * ({@link ssoAttemptedKey}) and navigates; the IdP (no central session) returns
30
31
  * `#oxy_sso=none`; the callback load's `sso-return` sees `none`, sets the
31
32
  * NO_SESSION flag ({@link ssoNoSessionKey}), and `sso-bounce` is then disabled.
32
33
  * Exactly ONE bounce, no loop. An interrupted bounce (user hit back
33
34
  * mid-redirect) self-heals once the {@link SSO_GUARD_TTL_MS} guard TTL lapses.
35
+ * The attempted-flag is the definitive, outcome-INDEPENDENT loop breaker: it is
36
+ * set pre-bounce so even if the return-side NO_SESSION write never lands, the
37
+ * bounce can never re-fire this tab after the self-heal TTL lapses.
34
38
  *
35
39
  * All state lives in `sessionStorage` (per tab, cleared on tab close) and is
36
40
  * keyed per-origin so two RPs hosted in the same browser never collide. The
@@ -58,6 +62,7 @@ const STATE_KEY_PREFIX = 'oxy_sso_state:';
58
62
  const GUARD_KEY_PREFIX = 'oxy_sso_guard:';
59
63
  const DEST_KEY_PREFIX = 'oxy_sso_dest:';
60
64
  const NO_SESSION_KEY_PREFIX = 'oxy_sso_no_session:';
65
+ const ATTEMPTED_KEY_PREFIX = 'oxy_sso_attempted:';
61
66
  /** Per-origin CSRF state key (matched on return to defeat fragment forgery). */
62
67
  export function ssoStateKey(origin) {
63
68
  return `${STATE_KEY_PREFIX}${origin}`;
@@ -78,6 +83,18 @@ export function ssoDestKey(origin) {
78
83
  export function ssoNoSessionKey(origin) {
79
84
  return `${NO_SESSION_KEY_PREFIX}${origin}`;
80
85
  }
86
+ /**
87
+ * Per-origin, OUTCOME-INDEPENDENT once-guard. Set in `sessionStorage` BEFORE
88
+ * the terminal SSO bounce navigates. Gates the bounce so the silent
89
+ * cross-domain probe fires AT MOST ONCE per tab session — independent of
90
+ * whether the return-side NO_SESSION flag ever lands. The definitive loop
91
+ * breaker; survives the 30s self-heal `ssoGuardKey` TTL. Cleared only on an
92
+ * explicit sign-out/clear so a later cold boot (after the user signs in
93
+ * centrally) can probe again.
94
+ */
95
+ export function ssoAttemptedKey(origin) {
96
+ return `${ATTEMPTED_KEY_PREFIX}${origin}`;
97
+ }
81
98
  /**
82
99
  * Perform the terminal top-level SSO bounce navigation.
83
100
  *
@@ -20,7 +20,7 @@
20
20
  * an oxy_sso fragment at all (i.e. `oxy_sso` is absent or an unrecognised
21
21
  * value), so the caller can ignore unrelated fragments without special-casing.
22
22
  */
23
- import { SSO_CALLBACK_PATH, ssoStateKey, ssoGuardKey, ssoDestKey, ssoNoSessionKey, } from './ssoBounce.js';
23
+ import { SSO_CALLBACK_PATH, ssoStateKey, ssoGuardKey, ssoDestKey, ssoNoSessionKey, ssoAttemptedKey, } from './ssoBounce.js';
24
24
  const VALID_KINDS = new Set(['ok', 'none', 'error']);
25
25
  /**
26
26
  * Parse an SSO return fragment.
@@ -86,14 +86,20 @@ export function parseSsoReturnFragment(hash) {
86
86
  * or a `Referer` header even if a later step throws.
87
87
  * - `state` must match (CSRF). A mismatch or a missing code sets the
88
88
  * NO_SESSION flag so `sso-bounce` is disabled (no rebounce loop).
89
- * - `none`/`error` outcomes set the NO_SESSION flag (the load2 half of the
90
- * loop proof).
89
+ * - `none`/`error` outcomes set BOTH the NO_SESSION flag and the
90
+ * outcome-independent attempted-flag (the load2 half of the loop proof).
91
91
  * - A throwing exchange is caught, reported via `onExchangeError`, and
92
92
  * treated exactly like "no session" (never loops, never rethrows).
93
- * - After a successful exchange landing on {@link SSO_CALLBACK_PATH}, the real
94
- * destination is restored from the DEST key same-origin only (an
95
- * attacker-planted cross-origin or relative-evil dest is rejected). The
96
- * DEST key is removed unconditionally.
93
+ * - On EVERY consumed outcome (ok, none, error, state-mismatch, no-code,
94
+ * failed-exchange, no-sessionId) not just okif the page landed on
95
+ * {@link SSO_CALLBACK_PATH}, the real pre-bounce destination is restored
96
+ * from the DEST key so the user is never stranded on the internal callback
97
+ * path. Same-origin only (an attacker-planted cross-origin or relative-evil
98
+ * dest is rejected). The DEST key is removed unconditionally.
99
+ * - After a same-origin dest restore (which uses `history.replaceState`, that
100
+ * does NOT itself emit `popstate`), a synthetic `popstate` is dispatched so
101
+ * URL-driven routers (Expo Router / React Navigation web) re-sync to the
102
+ * restored route. It is NOT dispatched when the dest is rejected/absent.
97
103
  *
98
104
  * Total: this function NEVER throws. Off-web it is a no-op returning `null`.
99
105
  *
@@ -112,6 +118,21 @@ export async function consumeSsoReturn(oxy, deps = {}) {
112
118
  const location = deps.location ?? window.location;
113
119
  const history = deps.history ?? window.history;
114
120
  const onExchangeError = deps.onExchangeError;
121
+ // Default: emit a synthetic `popstate` so URL-driven routers re-sync after a
122
+ // `history.replaceState` (which does NOT emit `popstate` on its own). Feature-
123
+ // detected end to end so it never throws in any environment.
124
+ const dispatchPopState = deps.dispatchPopState ??
125
+ (() => {
126
+ if (typeof window === 'undefined' || typeof window.dispatchEvent !== 'function') {
127
+ return;
128
+ }
129
+ if (typeof PopStateEvent !== 'undefined') {
130
+ window.dispatchEvent(new PopStateEvent('popstate'));
131
+ }
132
+ else if (typeof Event !== 'undefined') {
133
+ window.dispatchEvent(new Event('popstate'));
134
+ }
135
+ });
115
136
  const ret = parseSsoReturnFragment(location.hash);
116
137
  if (!ret) {
117
138
  // Not an oxy_sso fragment — nothing to do (do NOT touch any flags).
@@ -129,17 +150,47 @@ export async function consumeSsoReturn(oxy, deps = {}) {
129
150
  storage.removeItem(ssoGuardKey(origin));
130
151
  const markNoSession = () => {
131
152
  storage.setItem(ssoNoSessionKey(origin), '1');
153
+ // A return was consumed, so the probe definitively happened. Set the
154
+ // outcome-independent attempted-flag too so the bounce can never re-fire
155
+ // even if some consumer path skipped setting it pre-bounce.
156
+ storage.setItem(ssoAttemptedKey(origin), '1');
157
+ };
158
+ // Restore the user's real pre-bounce destination so they are never stranded
159
+ // on the internal callback path — invoked on EVERY consumed outcome, not just
160
+ // success. Same-origin only — never honour a cross-origin/protocol-relative
161
+ // dest that could have been planted to redirect the user. The DEST key is
162
+ // removed unconditionally. After a successful same-origin restore a synthetic
163
+ // `popstate` is dispatched so URL-driven routers re-sync.
164
+ const restoreDest = () => {
165
+ if (location.pathname === SSO_CALLBACK_PATH) {
166
+ const dest = storage.getItem(ssoDestKey(origin));
167
+ if (dest) {
168
+ try {
169
+ const destUrl = new URL(dest, origin);
170
+ if (destUrl.origin === origin) {
171
+ history.replaceState(null, '', destUrl.pathname + destUrl.search + destUrl.hash);
172
+ dispatchPopState();
173
+ }
174
+ }
175
+ catch {
176
+ // Malformed stored destination — leave the URL on the callback path.
177
+ }
178
+ }
179
+ }
180
+ storage.removeItem(ssoDestKey(origin));
132
181
  };
133
182
  if (ret.kind === 'none' || ret.kind === 'error') {
134
183
  // The central IdP had no session (or the bounce failed). Record it so we do
135
184
  // not bounce again this tab — the definitive loop breaker.
136
185
  markNoSession();
186
+ restoreDest();
137
187
  return null;
138
188
  }
139
189
  if (!stateOk || !ret.code) {
140
190
  // Forged / replayed / stale fragment, or a malformed ok with no code. Treat
141
191
  // exactly like "no session": never exchange, never loop.
142
192
  markNoSession();
193
+ restoreDest();
143
194
  return null;
144
195
  }
145
196
  let session;
@@ -149,31 +200,14 @@ export async function consumeSsoReturn(oxy, deps = {}) {
149
200
  catch (error) {
150
201
  onExchangeError?.(error);
151
202
  markNoSession();
203
+ restoreDest();
152
204
  return null;
153
205
  }
154
206
  if (!session?.sessionId) {
155
207
  markNoSession();
208
+ restoreDest();
156
209
  return null;
157
210
  }
158
- // If we landed on the internal callback path, restore the user's real
159
- // destination (captured at bounce time). Same-origin only — never honour a
160
- // cross-origin destination that could have been planted to redirect the
161
- // freshly signed-in user. `new URL(dest, origin)` tolerates relative dests
162
- // and is still re-checked against the page origin.
163
- if (location.pathname === SSO_CALLBACK_PATH) {
164
- const dest = storage.getItem(ssoDestKey(origin));
165
- if (dest) {
166
- try {
167
- const destUrl = new URL(dest, origin);
168
- if (destUrl.origin === origin) {
169
- history.replaceState(null, '', destUrl.pathname + destUrl.search + destUrl.hash);
170
- }
171
- }
172
- catch {
173
- // Malformed stored destination — leave the URL on the callback path.
174
- }
175
- }
176
- }
177
- storage.removeItem(ssoDestKey(origin));
211
+ restoreDest();
178
212
  return session;
179
213
  }