mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-12-03 14:37:10 +00:00
Only follow own keys when resolving OpenAPI schema references (#4887)
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@gitbook/react-openapi": patch
|
||||
---
|
||||
|
||||
Only follow own properties when resolving schema references, so a spec can't point at `Object.prototype`.
|
||||
@@ -0,0 +1,41 @@
|
||||
import { describe, expect, it } from 'bun:test';
|
||||
|
||||
import { decycle, retrocycle } from './decycle';
|
||||
|
||||
function roundTrip(value: unknown): any {
|
||||
return JSON.parse(JSON.stringify(value, decycle()), retrocycle());
|
||||
}
|
||||
|
||||
describe('retrocycle', () => {
|
||||
it('restores circular references', () => {
|
||||
const schema: any = { type: 'object', properties: {} };
|
||||
schema.properties.self = schema;
|
||||
|
||||
const result = roundTrip(schema);
|
||||
|
||||
expect(result.properties.self).toBe(result);
|
||||
});
|
||||
|
||||
it('resolves references through an own `constructor` key', () => {
|
||||
const team = { type: 'object' };
|
||||
const schema = { properties: { constructor: team, owner: team } };
|
||||
|
||||
const result = roundTrip(schema);
|
||||
|
||||
expect(result.properties.owner).toBe(result.properties.constructor);
|
||||
});
|
||||
|
||||
it('does not resolve references to inherited members', () => {
|
||||
const schema = {
|
||||
properties: {
|
||||
a: { $reference: '#/__proto__' },
|
||||
b: { $reference: '#/constructor/constructor' },
|
||||
},
|
||||
};
|
||||
|
||||
const result = roundTrip(schema);
|
||||
|
||||
expect(result.properties.a).toEqual({ $reference: '#/__proto__' });
|
||||
expect(result.properties.b).toEqual({ $reference: '#/constructor/constructor' });
|
||||
});
|
||||
});
|
||||
@@ -38,12 +38,17 @@ export function retrocycle() {
|
||||
|
||||
function dereference(this: { [k: string]: any }, ref: { $reference: string }) {
|
||||
const parts = ref.$reference.slice(1).split('/');
|
||||
let key: any;
|
||||
let value = this;
|
||||
let value: any = this;
|
||||
|
||||
for (let i = 0; i < parts.length; i++) {
|
||||
key = parts[i]?.replace(/~1/g, '/').replace(/~0/g, '~');
|
||||
value = value[key];
|
||||
const key = parts[i]?.replace(/~1/g, '/').replace(/~0/g, '~') ?? '';
|
||||
// `$reference` paths can be written in the spec itself, so only walk through keys owned
|
||||
// by the object, never inherited ones like `__proto__` that lead to a prototype.
|
||||
// Owned keys with such names (e.g. a `constructor` property) are spec data and stay valid.
|
||||
if (!isObject(value) || !Object.hasOwn(value, key)) {
|
||||
return;
|
||||
}
|
||||
value = (value as Record<string, unknown>)[key];
|
||||
}
|
||||
|
||||
const parent = parents.get(ref);
|
||||
|
||||
Reference in New Issue
Block a user