From 9570335f241d88b4bb826f68867a01d9d74aa4c6 Mon Sep 17 00:00:00 2001 From: Eric Peterson Date: Fri, 7 May 2021 15:18:05 +0200 Subject: [PATCH] Decode req.path before loading object from Azure Signed-off-by: Eric Peterson --- .../__mocks__/@azure/storage-blob.ts | 6 +-- .../stages/publish/azureBlobStorage.test.ts | 43 +++++++++++++++++++ .../src/stages/publish/azureBlobStorage.ts | 4 +- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/packages/techdocs-common/__mocks__/@azure/storage-blob.ts b/packages/techdocs-common/__mocks__/@azure/storage-blob.ts index 0d480a7fa9..2fad447788 100644 --- a/packages/techdocs-common/__mocks__/@azure/storage-blob.ts +++ b/packages/techdocs-common/__mocks__/@azure/storage-blob.ts @@ -71,9 +71,9 @@ export class BlockBlobClient { download() { const filePath = path.join(rootDir, this.blobName); const emitter = new EventEmitter(); - process.nextTick(() => { + setTimeout(() => { if (fs.existsSync(filePath)) { - emitter.emit('data', Buffer.from(fs.readFileSync(filePath))); + emitter.emit('data', fs.readFileSync(filePath)); emitter.emit('end'); } else { emitter.emit( @@ -81,7 +81,7 @@ export class BlockBlobClient { new Error(`The file ${filePath} does not exist !`), ); } - }); + }, 0); return Promise.resolve({ readableStreamBody: emitter, }); diff --git a/packages/techdocs-common/src/stages/publish/azureBlobStorage.test.ts b/packages/techdocs-common/src/stages/publish/azureBlobStorage.test.ts index 1160ffe49d..797ab5435c 100644 --- a/packages/techdocs-common/src/stages/publish/azureBlobStorage.test.ts +++ b/packages/techdocs-common/src/stages/publish/azureBlobStorage.test.ts @@ -21,6 +21,8 @@ import { ENTITY_DEFAULT_NAMESPACE, } from '@backstage/catalog-model'; import { ConfigReader } from '@backstage/config'; +import express from 'express'; +import request from 'supertest'; import mockFs from 'mock-fs'; import os from 'os'; import path from 'path'; @@ -329,4 +331,45 @@ describe('publishing with valid credentials', () => { ); }); }); + + describe('docsRouter', () => { + let app: express.Express; + const entity = createMockEntity(); + const entityRootDir = getEntityRootDir(entity); + + beforeEach(() => { + app = express().use(publisher.docsRouter()); + + mockFs.restore(); + mockFs({ + [entityRootDir]: { + img: { + 'with spaces.png': 'found it', + }, + 'some folder': { + 'also with spaces.js': 'found it too', + }, + }, + }); + }); + + it('should pass expected object path to bucket', async () => { + const { + kind, + metadata: { namespace, name }, + } = entity; + + // Ensures leading slash is trimmed and encoded path is decoded. + const pngResponse = await request(app).get( + `/${namespace}/${kind}/${name}/img/with%20spaces.png`, + ); + expect(Buffer.from(pngResponse.body).toString('utf8')).toEqual( + 'found it', + ); + const jsResponse = await request(app).get( + `/${namespace}/${kind}/${name}/some%20folder/also%20with%20spaces.js`, + ); + expect(jsResponse.text).toEqual('found it too'); + }); + }); }); diff --git a/packages/techdocs-common/src/stages/publish/azureBlobStorage.ts b/packages/techdocs-common/src/stages/publish/azureBlobStorage.ts index f6567abe49..60640c374d 100644 --- a/packages/techdocs-common/src/stages/publish/azureBlobStorage.ts +++ b/packages/techdocs-common/src/stages/publish/azureBlobStorage.ts @@ -254,9 +254,9 @@ export class AzureBlobStoragePublish implements PublisherBase { */ docsRouter(): express.Handler { return (req, res) => { - // Trim the leading forward slash + // Decode and trim the leading forward slash // filePath example - /default/Component/documented-component/index.html - const filePath = req.path.replace(/^\//, ''); + const filePath = decodeURI(req.path.replace(/^\//, '')); // Files with different extensions (CSS, HTML) need to be served with different headers const fileExtension = platformPath.extname(filePath); const responseHeaders = getHeadersForFileExtension(fileExtension);