Skip to content

Commit

Permalink
fix: add extra security to file endpoints (#5090)
Browse files Browse the repository at this point in the history
  • Loading branch information
rephus committed Apr 14, 2023
1 parent bc1e3eb commit fcc808c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
19 changes: 9 additions & 10 deletions packages/backend/src/routers/projectRouter.ts
Expand Up @@ -242,21 +242,20 @@ projectRouter.get(

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);
Expand Down
12 changes: 9 additions & 3 deletions packages/backend/src/routers/slackRouter.ts
Expand Up @@ -58,14 +58,20 @@ slackRouter.get(

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);
Expand Down

0 comments on commit fcc808c

Please sign in to comment.