Information exposure through static node_modules hosting

Information exposure

Description

Point express.static() at files intended for public use. Serving the whole node_modules tree can also expose package metadata, documentation and tests.

Potential impact

package.json may disclose versions or configuration, and source maps may reveal source code. Metadata such as _where can contain local paths or usernames.

Remediation

Use a bundler or build script to copy the required assets into public. If you serve package subdirectories directly, restrict them to an allow-list. Exclude unnecessary source maps, tests and documentation, and avoid directory listings through serve-index.

Examples

Before

javascript
const express = require('express');
const path = require('path');

const app = express();

// Before: map all node_modules, exposing internal package files.
app.use('/vendor', express.static(path.join(__dirname, 'node_modules')));

app.listen(3000, () => console.log('listening on 3000'));

After

javascript
const express = require('express');
const path = require('path');

const app = express();

// After: expose only required public package subdirectories.
app.use('/vendor/jquery', express.static(path.join(__dirname, 'node_modules', 'jquery', 'dist')));
app.use('/assets', express.static(path.join(__dirname, 'public'))); // Serve only built or copied public assets.

app.listen(3000, () => console.log('listening on 3000'));

The revision serves jQuery’s dist at /vendor/jquery and public at /assets. Both routes expose directories, so check which files ship in each.

References