diff --git a/packages/react-openapi/src/tryit/OpenAPITryItDialog.tsx b/packages/react-openapi/src/tryit/OpenAPITryItDialog.tsx index 287e3e8bf..7024d80c0 100644 --- a/packages/react-openapi/src/tryit/OpenAPITryItDialog.tsx +++ b/packages/react-openapi/src/tryit/OpenAPITryItDialog.tsx @@ -53,6 +53,8 @@ const PARAMETER_SECTIONS: { in: TryItParameterLocation; label: TranslationKey }[ { in: 'cookie', label: 'cookie_parameters' }, ]; +let unscopedDialogs = 0; + /** Loaded only after a reader opens Try it. */ export function OpenAPITryItDialog(props: OpenAPITryItDialogProps) { const { operation, securities, servers, withProxy, context, onClose } = props; @@ -78,7 +80,8 @@ export function OpenAPITryItDialog(props: OpenAPITryItDialogProps) { }); const [saved] = useState(() => { - const scope = getTryItApiScope(servers); + // Without a concrete host, use a scope of its own so nothing is shared or restored. + const scope = getTryItApiScope(servers) ?? `unscoped:${++unscopedDialogs}`; const savedOperation = getTryItOperationSession(scope, operation); const savedMediaTypeIndex = operation.body?.mediaTypes.findIndex( diff --git a/packages/react-openapi/src/tryit/session.test.ts b/packages/react-openapi/src/tryit/session.test.ts index a491b338f..fc356e945 100644 --- a/packages/react-openapi/src/tryit/session.test.ts +++ b/packages/react-openapi/src/tryit/session.test.ts @@ -29,9 +29,22 @@ describe('Try it session', () => { ).toBe(getTryItApiScope([{ url: 'https://a.example.com' }, { url: 'b.example.com' }])); }); + it('resolves templated servers before comparing them', () => { + const withHost = (host: string) => + getTryItApiScope([{ url: '{host}/v1', variables: { host: { default: host } } }]); + expect(withHost('https://api.gitbook.com')).toBe('api.gitbook.com'); + expect(withHost('https://api.a.com')).not.toBe(withHost('https://api.b.com')); + }); + + it('has no scope when no server resolves to a concrete host', () => { + expect(getTryItApiScope([{ url: '/relative' }])).toBeNull(); + expect(getTryItApiScope([{ url: 'https://{region}.example.com' }])).toBeNull(); + expect(getTryItApiScope([])).toBeNull(); + }); + it('never shares credentials between two APIs', () => { - const a = getTryItApiScope([{ url: 'https://a.example.com' }]); - const b = getTryItApiScope([{ url: 'https://b.example.com' }]); + const a = getTryItApiScope([{ url: 'https://a.example.com' }]) ?? ''; + const b = getTryItApiScope([{ url: 'https://b.example.com' }]) ?? ''; getTryItApiSession(a).auth['bearerAuth:value'] = 'token-for-a'; expect(getTryItApiSession(a).auth).toEqual({ 'bearerAuth:value': 'token-for-a' }); @@ -39,8 +52,8 @@ describe('Try it session', () => { }); it('keeps operations with the same method and path apart across APIs', () => { - const a = getTryItApiScope([{ url: 'https://a.example.com' }]); - const b = getTryItApiScope([{ url: 'https://b.example.com' }]); + const a = getTryItApiScope([{ url: 'https://a.example.com' }]) ?? ''; + const b = getTryItApiScope([{ url: 'https://b.example.com' }]) ?? ''; saveTryItOperationSession(a, operation, values); expect(getTryItOperationSession(a, { method: 'POST', path: '/pets' })).toEqual(values); diff --git a/packages/react-openapi/src/tryit/session.ts b/packages/react-openapi/src/tryit/session.ts index cb41183bc..db1ce316a 100644 --- a/packages/react-openapi/src/tryit/session.ts +++ b/packages/react-openapi/src/tryit/session.ts @@ -1,3 +1,6 @@ +import type { OpenAPIV3 } from '@gitbook/openapi-parser'; + +import { interpolateServerURL, isValidServerHost } from '../util/server'; import type { TryItExtraParameter } from './request'; export interface TryItOperationSession { @@ -25,16 +28,32 @@ export interface TryItApiSession { let apis = new Map(); let operations = new Map(); -/** Identify the API an operation belongs to by the origins of its servers. */ -export function getTryItApiScope(servers: { url?: string }[]): string { - const origins = new Set( - servers.map((server) => { - const url = server.url ?? ''; - const match = /^(?:https?:\/\/)?([^/]+)/i.exec(url); - return (match?.[1] ?? url).toLowerCase(); - }) - ); - return Array.from(origins).sort().join(' '); +/** + * Identify the API an operation belongs to by the hosts its servers resolve to, with their + * variables' defaults. Raw templates such as `{host}/v1` would make unrelated APIs collide. + * Returns null when no server resolves to a concrete host: nothing may be shared then. + */ +export function getTryItApiScope(servers: OpenAPIV3.ServerObject[]): string | null { + const hosts = new Set(); + for (const server of servers) { + const host = resolveHost(interpolateServerURL(server)); + if (host) { + hosts.add(host); + } + } + return hosts.size > 0 ? Array.from(hosts).sort().join(' ') : null; +} + +function resolveHost(url: string): string | null { + if (!isValidServerHost(url)) { + return null; + } + try { + const { host } = new URL(/^https?:\/\//i.test(url) ? url : `https://${url}`); + return host && !host.includes('{') ? host : null; + } catch { + return null; + } } export function getTryItApiSession(scope: string): TryItApiSession {