Course outline

Express.js / Basic: request and response ownership / Lesson 7

Request identifiers and safe headers

Carry a bounded request identifier through Express middleware without treating client metadata as identity.

About 3 min readContent reviewed
On this page

Lesson goals

  • Explain header ownership and middleware order.
  • Generate or validate a request identifier.
  • Test error paths and safe logging.

Choose an identifier contract

A request identifier helps connect safe logs for one request across application components. It is not a user identity, authorization credential or proof that a client request is trustworthy. Decide whether an upstream proxy owns the identifier or whether the application generates it. Define length and character limits before accepting any caller-supplied value, and never use raw credentials or personal data as the identifier.

Generate an application identifier

The first fragment adds a server-generated UUID to each request and response before later routes run. It uses a synthetic log identifier rather than trusting a supplied header. The example assumes a Node runtime supporting randomUUID and an Express application; it is illustrative and does not configure a listener or logging sink. Avoid placing identifiers in high-cardinality metric labels unless the monitoring system is designed for that use.

javascript Generate a request identifier early
const { randomUUID } = require('node:crypto');
app.use((req, res, next) => {
  req.requestId = randomUUID();
  res.set('X-Request-Id', req.requestId);
  next();
});

The example requires an Express application and a compatible Node runtime. It does not configure logging or a listener.

Read headers deliberately

The second fragment reads a harmless client label with Express’s header accessor and returns a bounded representation. Production code should validate the expected format and avoid reflecting arbitrary data into HTML or logs. Header names are case-insensitive, but proxy behavior and duplicate header handling depend on the actual deployment. Do not infer a user or tenant from a client-supplied label without a separate trusted authentication mechanism.

javascript Read a nonsecret client label
app.get('/label', (req, res) => {
  const label = req.get('X-Client-Label') || '';
  res.json({ label: label.slice(0, 40) });
});

The value is illustrative and untrusted. Production validation and output handling depend on the destination.

Cover success and failure paths

Place identifier middleware early enough to cover the routes and errors it is intended to observe. Test a successful route, an unmatched path and a controlled exception. The public response should include only the safe identifier and stable error information, while restricted logs can record the failure category. If the proxy generates a response before Express, the application middleware will not have run.

Verify through the deployed boundary

In an isolated application, assert that each response contains an identifier with the expected shape and that separate requests receive distinct values. Test a forged incoming identifier and confirm the chosen ownership policy. Verify that logs omit tokens, cookies and request bodies, and repeat through the approved proxy path. A local middleware test cannot establish that infrastructure forwards or rewrites headers as expected.

Common errors and fixes

A forged header grants another user access

Why it happens
A correlation header was treated as authenticated identity.
How to fix it
Authenticate and authorize independently.
Prevent it next time
Test a forged identity-like header against a protected record.

An error response has no correlation identifier

Why it happens
Identifier middleware ran after the failing component.
How to fix it
Place it before the relevant routes and error handler.
Prevent it next time
Test success, unmatched route and controlled error paths.

Secrets appear in log identifiers

Why it happens
A credential or full request value was copied into logs.
How to fix it
Generate a safe identifier and remove exposed secrets.
Prevent it next time
Review log fields and test redaction.

2 practice exercises

1. Should an incoming X-Request-Id determine the authenticated user?

Show worked solution

No. It is client-controlled metadata unless a trusted infrastructure contract validates it, and even then it is not a substitute for authentication. Use a separate principal and authorization decision.

2. A proxy sends an error before Express receives the request. Why is the application identifier missing?

Show worked solution

The middleware did not execute. Correlate through the proxy’s own safe identifier and configure a reviewed end-to-end propagation contract; test each layer separately.

Key takeaways

  • Request identifiers support correlation, not identity or authorization.
  • Test middleware placement and log safety across success and failure paths.

Official references