mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-11-26 10:13:46 +00:00
Scope saved Try it values by resolved server hosts
Raw server templates like {host}/v1 made unrelated APIs share a scope,
and so their saved tokens. Resolve variables first, and share nothing
when no server resolves to a concrete host.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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<string, TryItApiSession>();
|
||||
let operations = new Map<string, TryItOperationSession>();
|
||||
|
||||
/** 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<string>();
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user