Description
Credentials sent to Express in req.query are already in the URL when they reach the server. Even over HTTPS, URLs can remain in history, caches, proxy logs, and monitoring systems, or travel in a shared link or Referer header, depending on referrer policy.
Potential impact
Someone who obtains a valid password or token may gain account access. An exposed API key can lead to unauthorized calls and charges.
Remediation
- Change the client and route together. Submit password forms with
method="POST"over HTTPS and read the password fromreq.body. Send bearer tokens in theAuthorizationheader. - Keep credentials out of request and redirect URLs. Reject requests that supply them in the query. A proxy may already have logged the URL before Express sees it.
- Exclude secrets from body, header, and URL logs in the application, proxy, and monitoring tools.
Examples
The revised routes read the password from the body and the token from a header. Add the query check and the commented-out credential checks before returning success.
Before
javascript
const express = require("express");
const app = express();
// BAD: Read a password from the GET query
app.get("/signin", (req, res) => {
const user = req.query.user; // Non-secret example value
const password = req.query.password; // Exposed in the URL
// authenticate(user, password)
res.send("signed in");
});
app.listen(3000);
After
javascript
const express = require("express");
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// GOOD: Read from the POST body
app.post("/signin", (req, res) => {
const { user, password } = req.body; // Send the secret in the body
// authenticate(user, password)
res.send("signed in");
});
// Alternatively, send tokens in the Authorization header
app.post("/api/data", (req, res) => {
const auth = req.get("Authorization"); // For example, 'Bearer <token>'
// verifyBearer(auth)
res.json({ ok: true });
});
app.listen(3000);