CVE-2023-35844: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal') (CWE-22)
value: packages/backend/src/routers in Lightdash before 0.510.3 has insecure file endpoints, e.g., they allow .. directory traversal and do not ensure that an intended file extension (.csv or .png) is used.
CWE description: The product uses external input to construct a pathname that is intended to identify a file or directory that is located underneath a restricted parent directory, but the product does not properly neutralize special elements within the pathname that can cause the pathname to resolve to a location that is outside of the restricted directory.
CVSS v3 base score: 7.5
Published: 2023-06-19T02:15Z
Repository: https://github.com/lightdash/lightdash
Language: TypeScript
Files changed: packages/backend/src/routers/projectRouter.ts, packages/backend/src/routers/slackRouter.ts
Commit date: 2023-04-14 16:42:32 +0200
Commit message: fix: add extra security to file endpoints (#5090)


Diff: diff --git a/packages/backend/src/routers/projectRouter.ts b/packages/backend/src/routers/projectRouter.ts
index c064e036..e5d3d398 100644
--- a/packages/backend/src/routers/projectRouter.ts
+++ b/packages/backend/src/routers/projectRouter.ts
@@ -240,25 +240,24 @@ projectRouter.post(
 projectRouter.get(
     '/csv/:fileId',
 
     async (req, res, next) => {
         try {
-            if (!req.params.fileId.startsWith('csv-')) {
-                throw new NotFoundError(
-                    `CSV file not found ${req.params.fileId}`,
-                );
+            const { fileId } = req.params;
+
+            if (!fileId.startsWith('csv-') || !fileId.endsWith('.csv')) {
+                throw new NotFoundError(`CSV file not found ${fileId}`);
             }
-            const filePath = path.join('/tmp', req.params.fileId);
+            const sanitizedFileId = fileId.replace('..', '');
+
+            const filePath = path.join('/tmp', sanitizedFileId);
             if (!fs.existsSync(filePath)) {
-                const error = `This file ${req.params.fileId} doesn't exist on this server, this may be happening if you are running multiple containers or because files are not persisted. You can check out our docs to learn more on how to enable cloud storage: https://docs.lightdash.com/self-host/customize-deployment/configure-lightdash-to-use-external-object-storage`;
+                const error = `This file ${fileId} doesn't exist on this server, this may be happening if you are running multiple containers or because files are not persisted. You can check out our docs to learn more on how to enable cloud storage: https://docs.lightdash.com/self-host/customize-deployment/configure-lightdash-to-use-external-object-storage`;
                 throw new NotFoundError(error);
             }
             res.set('Content-Type', 'text/csv');
-            res.set(
-                'Content-Disposition',
-                `attachment; filename=${req.params.fileId}`,
-            );
+            res.set('Content-Disposition', `attachment; filename=${fileId}`);
             res.sendFile(filePath);
         } catch (error) {
             next(error);
         }
     },
diff --git a/packages/backend/src/routers/slackRouter.ts b/packages/backend/src/routers/slackRouter.ts
index 00b5a864..d9f91314 100644
--- a/packages/backend/src/routers/slackRouter.ts
+++ b/packages/backend/src/routers/slackRouter.ts
@@ -56,18 +56,24 @@ slackRouter.get(
 slackRouter.get(
     '/image/:imageId',
 
     async (req, res, next) => {
         try {
-            if (!req.params.imageId.startsWith('slack-image')) {
+            const { imageId } = req.params;
+            if (
+                !imageId.startsWith('slack-image') ||
+                !imageId.endsWith('.png')
+            ) {
                 throw new NotFoundError(
                     `Slack image not found ${req.params.imageId}`,
                 );
             }
-            const filePath = path.join('/tmp', req.params.imageId);
+            const sanitizedImageId = imageId.replace('..', '');
+
+            const filePath = path.join('/tmp', sanitizedImageId);
             if (!fs.existsSync(filePath)) {
-                const error = `This file ${req.params.imageId} doesn't exist on this server, this may be happening if you are running multiple containers or because files are not persisted. You can check out our docs to learn more on how to enable cloud storage: https://docs.lightdash.com/self-host/customize-deployment/configure-lightdash-to-use-external-object-storage`;
+                const error = `This file ${imageId} doesn't exist on this server, this may be happening if you are running multiple containers or because files are not persisted. You can check out our docs to learn more on how to enable cloud storage: https://docs.lightdash.com/self-host/customize-deployment/configure-lightdash-to-use-external-object-storage`;
                 throw new NotFoundError(error);
             }
             res.sendFile(filePath);
         } catch (error) {
             next(error);

Vulnerable code:             if (!req.params.fileId.startsWith('csv-')) {
                throw new NotFoundError(
                    `CSV file not found ${req.params.fileId}`,
                );
            const filePath = path.join('/tmp', req.params.fileId);
                const error = `This file ${req.params.fileId} doesn't exist on this server, this may be happening if you are running multiple containers or because files are not persisted. You can check out our docs to learn more on how to enable cloud storage: https://docs.lightdash.com/self-host/customize-deployment/configure-lightdash-to-use-external-object-storage`;
            res.set(
                'Content-Disposition',
                `attachment; filename=${req.params.fileId}`,
            );
            if (!req.params.imageId.startsWith('slack-image')) {
            const filePath = path.join('/tmp', req.params.imageId);
                const error = `This file ${req.params.imageId} doesn't exist on this server, this may be happening if you are running multiple containers or because files are not persisted. You can check out our docs to learn more on how to enable cloud storage: https://docs.lightdash.com/self-host/customize-deployment/configure-lightdash-to-use-external-object-storage`;
Fixed code:             const { fileId } = req.params;

            if (!fileId.startsWith('csv-') || !fileId.endsWith('.csv')) {
                throw new NotFoundError(`CSV file not found ${fileId}`);
            const sanitizedFileId = fileId.replace('..', '');

            const filePath = path.join('/tmp', sanitizedFileId);
                const error = `This file ${fileId} doesn't exist on this server, this may be happening if you are running multiple containers or because files are not persisted. You can check out our docs to learn more on how to enable cloud storage: https://docs.lightdash.com/self-host/customize-deployment/configure-lightdash-to-use-external-object-storage`;
            res.set('Content-Disposition', `attachment; filename=${fileId}`);
            const { imageId } = req.params;
            if (
                !imageId.startsWith('slack-image') ||
                !imageId.endsWith('.png')
            ) {
            const sanitizedImageId = imageId.replace('..', '');

            const filePath = path.join('/tmp', sanitizedImageId);
                const error = `This file ${imageId} doesn't exist on this server, this may be happening if you are running multiple containers or because files are not persisted. You can check out our docs to learn more on how to enable cloud storage: https://docs.lightdash.com/self-host/customize-deployment/configure-lightdash-to-use-external-object-storage`;