Fix · Express / Node
Fix “Exposed .git directory” on Express / Node
Block access to /.git at the server, and deploy build artifacts without the .git directory. Rotate any secrets found in history.
app.use('/.git', (req, res) => res.sendStatus(404));
// better: deploy build artifacts without the .git directory
Verify the fix
Re-run the Exposed .git directory check after deploying. Test config changes in staging first and keep a rollback: a wrong header can break a site faster than a missing one.
Other stacks: nginx · Apache · Caddy · Cloudflare