mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-11-25 09:43:43 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6d062f1d0d | |||
| 1c6652ae9a |
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"gitbook": patch
|
||||
---
|
||||
|
||||
Drop description HTML supplied by OpenAPI specs and sanitize example descriptions that could render HTML.
|
||||
@@ -35,4 +35,61 @@ describe('#enrichFilesystem', () => {
|
||||
expect(paths['/a'].get['x-gitbook-description-html']).toBe('<p>constructor</p>');
|
||||
expect(paths['/b'].get['x-gitbook-description-html']).toBe('<p>constructor</p>');
|
||||
});
|
||||
|
||||
async function enrichSchemas(schemas: Record<string, unknown>) {
|
||||
const { filesystem } = await parseOpenAPI({
|
||||
value: JSON.stringify({
|
||||
openapi: '3.0.0',
|
||||
info: { title: 'Test', version: '1.0.0' },
|
||||
paths: {},
|
||||
components: { schemas },
|
||||
}),
|
||||
rootURL: null,
|
||||
});
|
||||
const enriched = await enrichFilesystem(filesystem);
|
||||
return enriched[0]?.specification.components.schemas;
|
||||
}
|
||||
|
||||
it('drops description fields supplied by the specification', async () => {
|
||||
const schemas = await enrichSchemas({
|
||||
Pet: {
|
||||
type: 'object',
|
||||
'x-gitbook-description-html': '<b>spec</b>',
|
||||
'x-gitbook-description-document': { nodes: [] },
|
||||
},
|
||||
});
|
||||
expect(schemas.Pet['x-gitbook-description-html']).toBeUndefined();
|
||||
expect(schemas.Pet['x-gitbook-description-document']).toBeUndefined();
|
||||
});
|
||||
|
||||
it('sanitizes descriptions of schemas named example', async () => {
|
||||
const schemas = await enrichSchemas({
|
||||
example: { type: 'string', description: '<img src=x onerror=alert(1)>' },
|
||||
});
|
||||
const html = schemas.example['x-gitbook-description-html'];
|
||||
expect(html).toContain('<img');
|
||||
expect(html).not.toContain('onerror');
|
||||
});
|
||||
|
||||
it('sanitizes descriptions in example values that could render HTML', async () => {
|
||||
const schemas = await enrichSchemas({
|
||||
Pet: {
|
||||
type: 'object',
|
||||
example: {
|
||||
description: '<img src=x onerror=alert(1)>',
|
||||
'x-gitbook-description-html': '<img src=x onerror=alert(2)>',
|
||||
},
|
||||
},
|
||||
});
|
||||
const html = schemas.Pet.example['x-gitbook-description-html'];
|
||||
expect(html).toBeDefined();
|
||||
expect(html).not.toContain('onerror');
|
||||
});
|
||||
|
||||
it('keeps example values without HTML as written', async () => {
|
||||
const schemas = await enrichSchemas({
|
||||
Pet: { type: 'object', example: { description: 'A **good** dog' } },
|
||||
});
|
||||
expect(schemas.Pet.example).toEqual({ description: 'A **good** dog' });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -8,19 +8,21 @@ import { parseMarkdown } from '../markdown';
|
||||
export async function enrichFilesystem(filesystem: Filesystem) {
|
||||
const parseMarkdownWithCache = createMarkdownParser();
|
||||
return traverse(filesystem, async (node, path) => {
|
||||
// Examples included: a `$ref` can render an example value as a schema.
|
||||
delete node['x-gitbook-description-html'];
|
||||
delete node['x-gitbook-description-document'];
|
||||
|
||||
if (
|
||||
path !== undefined &&
|
||||
'description' in node &&
|
||||
typeof node.description === 'string' &&
|
||||
node.description
|
||||
) {
|
||||
const key = path[path.length - 1];
|
||||
// Avoid parsing descriptions in examples.
|
||||
if (key !== 'example') {
|
||||
const description = node.description.trim();
|
||||
if (description) {
|
||||
node['x-gitbook-description-html'] = await parseMarkdownWithCache(description);
|
||||
}
|
||||
const description = node.description.trim();
|
||||
// Examples stay as written, except where their raw description could render a tag.
|
||||
const isExample = path[path.length - 1] === 'example';
|
||||
if (description && (!isExample || description.includes('<'))) {
|
||||
node['x-gitbook-description-html'] = await parseMarkdownWithCache(description);
|
||||
}
|
||||
}
|
||||
return node;
|
||||
|
||||
Reference in New Issue
Block a user