@unboundcx/sdk 4.13.112-security.1 → 4.13.112-security.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/base.js CHANGED
@@ -568,14 +568,23 @@ export class BaseSDK {
568
568
  return handleUnauthorized(this, {
569
569
  status: response.status,
570
570
  endpoint,
571
+ method,
571
572
  alreadyRetried: retryCtx?.originalParams?.__skipAutoRefresh === true,
572
573
  originalError: httpError,
573
574
  retry: () =>
575
+ // forceFetch: true (not retryCtx.forceFetch) -- the retry must
576
+ // always go over plain HTTP, never the original transport. A
577
+ // socket transport call that 401'd still holds the pre-rotation
578
+ // JWT (the rotation reaches the socket asynchronously via NATS),
579
+ // so retrying over the same socket would 401 again and trigger
580
+ // onUnauthorized even though the refresh above just succeeded.
581
+ // See _getAvailableTransport (base.js:~185): forceFetch skips
582
+ // transports entirely and goes straight to HTTP.
574
583
  this.#request(
575
584
  endpoint,
576
585
  method,
577
586
  { ...(retryCtx.originalParams || {}), __skipAutoRefresh: true },
578
- retryCtx.forceFetch,
587
+ true,
579
588
  ),
580
589
  });
581
590
  }
@@ -17,15 +17,26 @@
17
17
  // still single-flights its own concurrent 401s against itself.
18
18
  const inFlightRefreshBySdk = new WeakMap();
19
19
 
20
- function isLoginRoute(endpoint) {
21
- return typeof endpoint === 'string' && endpoint.startsWith('/login');
20
+ // Only the routes that WOULD cause a refresh loop (or make no sense to
21
+ // refresh) are excluded: refreshing itself (POST /login/refresh), the
22
+ // initial password login (POST /login), and logout (DELETE /login). Every
23
+ // other /login/* call -- notably GET /login/validate, which a page reload
24
+ // hits with a stale access cookie -- is refreshable: a valid refresh cookie
25
+ // must be able to recover it instead of bouncing the user to the password
26
+ // form.
27
+ function isNonRefreshableLoginRoute(method, endpoint) {
28
+ if (typeof endpoint !== 'string') return false;
29
+ const m = typeof method === 'string' ? method.toUpperCase() : '';
30
+ if (endpoint === '/login/refresh' && m === 'POST') return true;
31
+ if (endpoint === '/login' && (m === 'POST' || m === 'DELETE')) return true;
32
+ return false;
22
33
  }
23
34
 
24
- export function shouldAttemptRefresh(sdk, status, endpoint) {
35
+ export function shouldAttemptRefresh(sdk, status, endpoint, method) {
25
36
  return (
26
37
  status === 401 &&
27
38
  sdk?._autoRefresh === true &&
28
- !isLoginRoute(endpoint) &&
39
+ !isNonRefreshableLoginRoute(method, endpoint) &&
29
40
  typeof sdk?.login?.refresh === 'function'
30
41
  );
31
42
  }
@@ -47,7 +58,16 @@ async function runRefresh(sdk) {
47
58
  // token back in the body; cookie clients rely on the Set-Cookie
48
59
  // pair the refresh response already sent -- there is no token
49
60
  // string to apply to the SDK instance for them.
50
- if (result?.token) sdk.setToken(result.token);
61
+ if (result?.token) {
62
+ sdk.setToken(result.token);
63
+ } else if (typeof sdk?.setToken === 'function') {
64
+ // Cookie-mode refresh: no token string comes back (the Set-Cookie
65
+ // pair already rotated the browser's cookies), but a stale
66
+ // in-memory access token left on the SDK would keep going out as
67
+ // an Authorization header, which checkApiAuth prefers over the
68
+ // fresh cookie. Clear it so the next request relies on the cookie.
69
+ sdk.setToken(null);
70
+ }
51
71
  if (result?.refreshToken) sdk._refreshToken = result.refreshToken;
52
72
  return result;
53
73
  })
@@ -76,6 +96,7 @@ function notifyUnauthorized(sdk, err) {
76
96
  * @param {object} ctx
77
97
  * @param {number} ctx.status
78
98
  * @param {string} ctx.endpoint
99
+ * @param {string} [ctx.method]
79
100
  * @param {() => Promise<any>} ctx.retry
80
101
  * @param {Error} ctx.originalError -- rethrown whenever refresh isn't
81
102
  * attempted, fails, or the retry itself still comes back 401 (never loop
@@ -84,9 +105,9 @@ function notifyUnauthorized(sdk, err) {
84
105
  */
85
106
  export async function handleUnauthorized(
86
107
  sdk,
87
- { status, endpoint, retry, originalError, alreadyRetried = false },
108
+ { status, endpoint, method, retry, originalError, alreadyRetried = false },
88
109
  ) {
89
- if (alreadyRetried || !shouldAttemptRefresh(sdk, status, endpoint)) {
110
+ if (alreadyRetried || !shouldAttemptRefresh(sdk, status, endpoint, method)) {
90
111
  throw originalError;
91
112
  }
92
113
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unboundcx/sdk",
3
- "version": "4.13.112-security.1",
3
+ "version": "4.13.112-security.2",
4
4
  "description": "Official JavaScript SDK for the Unbound API - A comprehensive toolkit for integrating with Unbound's communication, AI, and data management services",
5
5
  "main": "index.js",
6
6
  "type": "module",