Skip to content

Commit b0a4db6

Browse files
committed
fix(console): me/localization reads the token through TokenStorage; narrow changeset scope
The seed and the provider now read the stored token through TokenStorage, so a storage-blocked embed still sends the token sign-in kept in memory (index.html stays on localStorage). Pinned with a memory-fallback case. The changesets now name the scope actually fixed (same-origin), say that split-origin requests still go without the token, and note header precedence. The field selector encodes the object name into its path. Claude-Session: https://claude.ai/code/session_01Utp6Ca6EBDpTBZk8v46Jjv Co-authored-by: Claude <noreply@anthropic.com>
1 parent f66978d commit b0a4db6

6 files changed

Lines changed: 39 additions & 17 deletions

File tree

‎.changeset/37225-chatbot-hooks-bearer.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ fix(plugin-chatbot): the agent list, the model picker, the pending-approvals inb
77
Clause-②: no
88
Metadata-protocol: none
99

10-
`useAgents`, `useAiModels`, `usePendingActions` and `useHitlInChat` asked the AI service with the session cookie only. A session held by its sign-in (bearer) token alone (a cross-site embed, a cookie the browser blocked as third-party) was refused there: no agents, no models, an empty approvals inbox, and an in-chat approval that failed. They now send through the host's authenticated fetch, the `apiFetch` a host hands `SchemaRendererProvider` (the console's sends the stored token), as the renderers' own reads already do. The session cookie is still sent, a cookie session behaves exactly as before, and in the console a refused request now reaches its session-ended check. A host that mounts no provider keeps the plain `fetch`, as before. The hooks' options, including `headers`, are unchanged.
10+
`useAgents`, `useAiModels`, `usePendingActions` and `useHitlInChat` asked the AI service with the session cookie only. A session held by its sign-in (bearer) token alone on the console's own origin (for example, a session cookie the browser blocked as third-party) was refused there: no agents, no models, an empty approvals inbox, and an in-chat approval that failed. They now send through the host's authenticated fetch, the `apiFetch` a host hands `SchemaRendererProvider` (the console's sends the stored token), as the renderers' own reads already do. The session cookie is still sent, a cookie session behaves exactly as before, and in the console a refused request now reaches its session-ended check. A host that mounts no provider keeps the plain `fetch`, as before. Where a host points these requests at another origin (an AI service or server URL configured off the page's origin), the host's fetch still sends them without the token; that layout is not fixed here. The hooks' options are unchanged in shape; in the console, a stored token now takes precedence over an `Authorization` passed in `headers`, and the host's tenant and language headers ride these requests too.

‎.changeset/37225-localization-bearer.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ fix(console): the boot read of the signed-in user's language, currency and time
77
Clause-②: no
88
Metadata-protocol: none
99

10-
`GET /api/v1/auth/me/localization` was read with the session cookie only, on all three of its legs: the request `index.html` sends during parse, the language seed before the app mounts, and the regional-defaults provider. A session held by its sign-in (bearer) token alone was answered as signed out, so the console booted in the browser's language and formatted numbers, currencies and dates without the user's own settings. All three now state the stored token, read from the same storage the other console requests read it from, and they still share one request: the three send the same request, and its identity includes the token. With no token stored the request is exactly as before. Nothing changes in what the server accepts.
10+
`GET /api/v1/auth/me/localization` was read with the session cookie only, on all three of its legs: the request `index.html` sends during parse, the language seed before the app mounts, and the regional-defaults provider. A session held by its sign-in (bearer) token alone was answered as signed out, so the console booted in the browser's language and formatted numbers, currencies and dates without the user's own settings. All three now state the stored token. The seed and the provider read it where the other console requests read it, including a token kept in memory when the browser blocks storage; the parse-time request reads storage only. They still share one request: the three send the same request, and its identity includes the token. With no token stored the request is exactly as before. Nothing changes in what the server accepts.

‎.changeset/37225-share-dialog-bearer.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ fix(components): the share dialog lists, creates and revokes share links through
77
Clause-②: no
88
Metadata-protocol: none
99

10-
`ShareDialog` asked this server for a record's share links (and created and revoked them) with the session cookie only. A session held by its sign-in (bearer) token alone (a cross-site embed, a cookie the browser blocked as third-party) was refused there: the dialog showed an error and no links. It now sends through the host's authenticated fetch, the `apiFetch` a host hands `SchemaRendererProvider` (the console's sends the stored token), as the renderers' own reads already do. The session cookie is still sent, a cookie session behaves exactly as before, and in the console a refused request now reaches its session-ended check. A host that mounts no provider keeps the plain `fetch`, as before. The `fetchHeaders` prop is unchanged.
10+
`ShareDialog` asked this server for a record's share links (and created and revoked them) with the session cookie only. A session held by its sign-in (bearer) token alone on the console's own origin (for example, a session cookie the browser blocked as third-party) was refused there: the dialog showed an error and no links. It now sends through the host's authenticated fetch, the `apiFetch` a host hands `SchemaRendererProvider` (the console's sends the stored token), as the renderers' own reads already do. The session cookie is still sent, a cookie session behaves exactly as before, and in the console a refused request now reaches its session-ended check. A host that mounts no provider keeps the plain `fetch`, as before. Where a host points these requests at another origin (an AI service or server URL configured off the page's origin), the host's fetch still sends them without the token; that layout is not fixed here. The `fetchHeaders` prop is unchanged in shape; in the console, a stored token now takes precedence over an `Authorization` passed in it, and the host's tenant and language headers ride these requests too.

‎ui/apps/console/src/__tests__/sessionCheckBootDedup.test.tsx‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import indexHtml from '../../index.html?raw';
2121
import mainSource from '../main.tsx?raw';
2222
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2323
import { render, screen, act, cleanup } from '@testing-library/react';
24-
import { AuthGuard, AuthProvider, useAuth } from '@object-ui/auth';
24+
import { AuthGuard, AuthProvider, TokenStorage, useAuth } from '@object-ui/auth';
2525
import { preflightAuth } from '../lib/auth-preflight';
2626
import { readConsoleDiscovery } from '../lib/early-discovery';
2727
import { getSharedDiscovery } from '@object-ui/data-objectstack';
@@ -1016,4 +1016,27 @@ describe('a bearer-only session: the stored Bearer rides me/localization on all
10161016
expect(authorizationOf(localizationCalls()[1])).toBe(`Bearer ${BEARER}`);
10171017
expect(readCachedLanguageSeed()).toBe('zh-CN');
10181018
});
1019+
1020+
it('blocked storage (an embed whose localStorage throws): the token sign-in kept in memory still rides the provider read', async () => {
1021+
vi.spyOn(window, 'localStorage', 'get').mockImplementation(() => {
1022+
throw new DOMException('blocked', 'SecurityError');
1023+
});
1024+
expect(() => window.localStorage.getItem('auth-session-token')).toThrow();
1025+
try {
1026+
// Sign-in stores the token; with storage blocked, `TokenStorage` keeps it in memory.
1027+
TokenStorage.set(BEARER);
1028+
expect(TokenStorage.get()).toBe(BEARER);
1029+
expect(new Headers(meLocalizationInit('zh-CN').headers).get('Authorization')).toBe(`Bearer ${BEARER}`);
1030+
render(
1031+
<LocalizationFetchProvider endpoint={ME}>
1032+
<Value />
1033+
</LocalizationFetchProvider>,
1034+
);
1035+
expect(await screen.findByText('zh-CN/CNY')).toBeTruthy();
1036+
expect(localizationCalls()).toHaveLength(1);
1037+
expect(authorizationOf(localizationCalls()[0])).toBe(`Bearer ${BEARER}`);
1038+
} finally {
1039+
TokenStorage.clear();
1040+
}
1041+
});
10191042
});

‎ui/apps/console/src/languageSeed.ts‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
* Bearer the same way, for the same parse-time hand-off.
129129
*/
130130
import { cacheLanguageSeed, readCachedLanguageSeed, readStoredLanguage } from '@object-ui/i18n';
131-
import { getSessionOwnerChangeCount } from '@object-ui/auth';
131+
import { TokenStorage, getSessionOwnerChangeCount } from '@object-ui/auth';
132132
import { inflightGetKey, sharedGetJson } from '@object-ui/types';
133133
import { resolveLocaleAmong } from '@objectstack/spec/system';
134134
import { readConsoleDiscovery } from './lib/early-discovery';
@@ -147,18 +147,16 @@ interface MeLocalizationResponse {
147147
}
148148

149149
/**
150-
* The Bearer every console request carries, `''` when there is none or storage
151-
* is blocked: the key `createAuthenticatedFetch` reads (`TokenStorage`), read
152-
* here as `index.html` and `early-discovery.ts` read it.
150+
* The Bearer every console request carries, `''` when there is none: read
151+
* through `TokenStorage`, the store `createAuthenticatedFetch` reads, so a
152+
* storage-blocked page (an embed whose `localStorage` throws, itself a
153+
* bearer-only session) still sends the in-memory token the sign-in kept.
154+
* `index.html` runs before the bundle and reads `localStorage` by hand; when
155+
* the two answers differ the inits differ and the provider sends its own read,
156+
* the "token stored after parse" shape.
153157
*/
154-
const TOKEN_KEY = 'auth-session-token';
155-
156158
function storedToken(): string {
157-
try {
158-
return localStorage.getItem(TOKEN_KEY) || '';
159-
} catch {
160-
return '';
161-
}
159+
return TokenStorage.get() || '';
162160
}
163161

164162
/**

‎ui/packages/app-shell/src/views/metadata-admin/widgets.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,10 +1204,11 @@ const EMPTY_FIELD_SELECTOR_OPTIONS: FieldSelectorOption[] = [];
12041204
* `credentials: 'include'`: a bare `fetch` with no init sends the session cookie
12051205
* alone (the default `same-origin` credentials), so a session held by its bearer
12061206
* token alone was answered as anonymous and a `401` never reached the console's
1207-
* session-ended check.
1207+
* session-ended check. The object name is encoded into the path: the request
1208+
* now carries the bearer, so it names exactly the object it was given.
12081209
*/
12091210
async function fetchFieldSelectorOptions(objectName: string): Promise<FieldSelectorOption[]> {
1210-
const res = await createAuthenticatedFetch()(`/api/v1/objects/${objectName}/fields`, {
1211+
const res = await createAuthenticatedFetch()(`/api/v1/objects/${encodeURIComponent(objectName)}/fields`, {
12111212
credentials: 'include',
12121213
});
12131214
// Read the body BEFORE branching on `ok`: a refusal usually carries the more

0 commit comments

Comments
 (0)