Directory indexing enabled in Express

Directory indexing enabled in Express

Description

Registering serve-index on a production route exposes a browsable list of files in that directory.

Potential impact

The listing can disclose backup names, configuration files, logs and directory structure. Files may also be downloaded if a route serves their contents.

Remediation

Remove serve-index from production routes and serve only a dedicated public directory with express.static. Link to intended files through explicit routes or an index page. Exclude backups, logs, internal configuration and unnecessary source maps from the deployment.

Examples

Before

javascript
const express = require('express');
const serveIndex = require('serve-index');

const app = express();
app.use('/files', serveIndex('public/files'));

After

javascript
const express = require('express');

const app = express();
app.use('/assets', express.static('public'));
app.get('/files', (req, res) => res.status(403).end());

The revision returns 403 for GET /files. It still serves files under public at /assets, so check the contents of that directory.

References