From 6e5aed1c9942e8456167000d49fce51f27cc7230 Mon Sep 17 00:00:00 2001 From: Phil Kuang Date: Fri, 16 Jul 2021 15:04:33 -0400 Subject: [PATCH] fix(techdocs): properly validate mkdocs docs_dir property Signed-off-by: Phil Kuang --- .changeset/angry-spies-warn.md | 5 +++++ .../generate/__fixtures__/mkdocs_valid_doc_dir.yml | 3 +++ .../src/stages/generate/helpers.test.ts | 10 ++++++++++ .../techdocs-common/src/stages/generate/helpers.ts | 6 +++++- 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 .changeset/angry-spies-warn.md create mode 100644 packages/techdocs-common/src/stages/generate/__fixtures__/mkdocs_valid_doc_dir.yml diff --git a/.changeset/angry-spies-warn.md b/.changeset/angry-spies-warn.md new file mode 100644 index 0000000000..171827748a --- /dev/null +++ b/.changeset/angry-spies-warn.md @@ -0,0 +1,5 @@ +--- +'@backstage/techdocs-common': patch +--- + +Fix validation of mkdocs.yml docs_dir diff --git a/packages/techdocs-common/src/stages/generate/__fixtures__/mkdocs_valid_doc_dir.yml b/packages/techdocs-common/src/stages/generate/__fixtures__/mkdocs_valid_doc_dir.yml new file mode 100644 index 0000000000..e75b06ada7 --- /dev/null +++ b/packages/techdocs-common/src/stages/generate/__fixtures__/mkdocs_valid_doc_dir.yml @@ -0,0 +1,3 @@ +site_name: Test site name +site_description: Test site description +docs_dir: docs/ diff --git a/packages/techdocs-common/src/stages/generate/helpers.test.ts b/packages/techdocs-common/src/stages/generate/helpers.test.ts index b6b71e3e2b..a5a6d99c29 100644 --- a/packages/techdocs-common/src/stages/generate/helpers.test.ts +++ b/packages/techdocs-common/src/stages/generate/helpers.test.ts @@ -47,6 +47,9 @@ const mkdocsYmlWithExtensions = fs.readFileSync( const mkdocsYmlWithRepoUrl = fs.readFileSync( resolvePath(__filename, '../__fixtures__/mkdocs_with_repo_url.yml'), ); +const mkdocsYmlWithValidDocDir = fs.readFileSync( + resolvePath(__filename, '../__fixtures__/mkdocs_valid_doc_dir.yml'), +); const mkdocsYmlWithInvalidDocDir = fs.readFileSync( resolvePath(__filename, '../__fixtures__/mkdocs_invalid_doc_dir.yml'), ); @@ -336,6 +339,7 @@ describe('helpers', () => { mockFs({ '/mkdocs.yml': mkdocsYml, '/mkdocs_with_extensions.yml': mkdocsYmlWithExtensions, + '/mkdocs_valid_doc_dir.yml': mkdocsYmlWithValidDocDir, '/mkdocs_invalid_doc_dir.yml': mkdocsYmlWithInvalidDocDir, }); }); @@ -351,6 +355,12 @@ describe('helpers', () => { ).resolves.toBeUndefined(); }); + it('should return true on when a valid docs_dir is present', async () => { + await expect( + validateMkdocsYaml(inputDir, '/mkdocs_valid_doc_dir.yml'), + ).resolves.toBeUndefined(); + }); + it('should return false on absolute doc_dir path', async () => { await expect( validateMkdocsYaml(inputDir, '/mkdocs_invalid_doc_dir.yml'), diff --git a/packages/techdocs-common/src/stages/generate/helpers.ts b/packages/techdocs-common/src/stages/generate/helpers.ts index 086377b4ef..590e75e7b2 100644 --- a/packages/techdocs-common/src/stages/generate/helpers.ts +++ b/packages/techdocs-common/src/stages/generate/helpers.ts @@ -19,6 +19,7 @@ import { isChildPath } from '@backstage/backend-common'; import { spawn } from 'child_process'; import fs from 'fs-extra'; import yaml, { DEFAULT_SCHEMA, Type } from 'js-yaml'; +import { resolve as resolvePath } from 'path'; import { PassThrough, Writable } from 'stream'; import { Logger } from 'winston'; import { ParsedLocationAnnotation } from '../../helpers'; @@ -178,7 +179,10 @@ export const validateMkdocsYaml = async ( schema: MKDOCS_SCHEMA, }); - if (mkdocsYml.docs_dir && !isChildPath(inputDir, mkdocsYml.docs_dir)) { + if ( + mkdocsYml.docs_dir && + !isChildPath(inputDir, resolvePath(inputDir, mkdocsYml.docs_dir)) + ) { throw new Error( `docs_dir configuration value in mkdocs can't be an absolute directory or start with ../ for security reasons. Use relative paths instead which are resolved relative to your mkdocs.yml file location.`,