CVE-2023-35844 (TypeScript, published 2023-06-19T02:15Z)
Description: 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.
Severity: nan (CVSS v3 base score: 7.5)
Weakness: CWE-22 \u2014 Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
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.
Repository: https://github.com/lightdash/lightdash
Files changed:
packages/backend/src/routers/projectRouter.ts
packages/backend/src/routers/slackRouter.ts
Commit message: fix: add extra security to file endpoints (#5090)



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`;