Skip to content

Commit

Permalink
fix: prevent directory traversal on SSR (#1241)
Browse files Browse the repository at this point in the history
  • Loading branch information
jometzner authored Aug 11, 2022
1 parent ace4c77 commit c7d8d00
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,19 @@ export function app() {
server.get(/\/.*\.(js|css)$/, (req, res) => {
// remove all parameters
const path = req.originalUrl.substring(1).replace(/[;?&].*$/, '');

fs.readFile(join(BROWSER_FOLDER, path), { encoding: 'utf-8' }, (err, data) => {
if (err) {
res.sendStatus(404);
} else {
res.set('Content-Type', `${path.endsWith('css') ? 'text/css' : 'application/javascript'}; charset=UTF-8`);
res.send(setDeployUrlInFile(DEPLOY_URL, path, data));
}
});
const filename = join(BROWSER_FOLDER, path);
if (filename.startsWith(BROWSER_FOLDER)) {
fs.readFile(filename, { encoding: 'utf-8' }, (err, data) => {
if (err) {
res.sendStatus(404);
} else {
res.set('Content-Type', `${path.endsWith('css') ? 'text/css' : 'application/javascript'}; charset=UTF-8`);
res.send(setDeployUrlInFile(DEPLOY_URL, path, data));
}
});
} else {
res.sendStatus(404);
}
});
server.get(/\/ngsw\.json/, (_, res) => {
fs.readFile(join(BROWSER_FOLDER, 'ngsw.json'), { encoding: 'utf-8' }, (err, data) => {
Expand Down

0 comments on commit c7d8d00

Please sign in to comment.