FG
💻 Software🌐 Web & Full-Stack

VM env req.protocol is inaccurate

Fresh5 days ago
Mar 14, 20260 views
Confidence Score56%
56%

Problem

When running the node.js HTTPS module using express the `req.protocol` is reporting `http` when it should be reporting `https` Environment information: Host: `Ubuntu Linux jas-laptop 3.2.0-57-generic #87-Ubuntu SMP Tue Nov 12 21:35:10 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux` VM s/w: `QEMU emulator version 1.0 (qemu-kvm-1.0), Copyright (c) 2003-2008 Fabrice Bellard` VM Guest: `CentOS Linux node.dev 2.6.32-431.1.2.0.1.el6.x86_64 #1 SMP Fri Dec 13 13:06:13 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux` Relevant source; app.js & server.js. I verified that the connection between the host & guest are indeed using SSL with the following `tcpdump` commands: Guest: `tcpdump -ieth0 -s 1024 -l -A tcp port 3000` Results: `E..^I...@... ... .......!..._.. ..@h..w...+.....~..mQ_+;..B.^...............K.vm.+.O...ic.|w.+.:.tL..A..X{.o>.....9..#.x{E...D..jUZu...Q$.....uND..C....[. ..G.E....9.. .I'..5..M6.9.t@.....v...0...(.}.........a4..PsKtb.e?.=. .......u..r._.e;.sGQ.VXq.>..;p...q.#B.....C.1......i.8......1Q...P;3...M....6klik.L../..FO.kRu(..#.iA...XC..].F..in.3..pT.l......-J.8.F.>'t.6..e.........$.v$.g. '...f$<.b..pe#HE.1w...7'...v..jam...cOR........`..KGgb( W....e.t........7.%.._..a.........-..G.m.k...7lu.O.".F....V.u...... .. 10:38:40.634548 IP 10.0.2.15.hbci > 10.0.2.2.59080: Flags [P.], seq 2488:2780, ack 5311, win 25470, length 292` Host: `tcpdump -ilo -s 1024 -l -A host node.dev and tcp port 3000` Results: `E..j..@.@.Uy..............%.7=......._..... ..@h..w...+.....~..mQ_+;..

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Fix req.protocol Reporting Incorrectly in Express App

Medium Risk

The `req.protocol` is reporting `http` instead of `https` because the Express application is likely behind a reverse proxy or load balancer that is terminating SSL. In such cases, the original protocol may not be forwarded to the application unless configured correctly. Express needs to be informed about the proxy settings to accurately determine the protocol used by the client.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Enable Trust Proxy in Express

    Set the `trust proxy` setting in your Express app to ensure that it recognizes the forwarded headers from the reverse proxy. This will allow Express to correctly identify the protocol being used by the client.

    javascript
    app.set('trust proxy', true);
  2. 2

    Verify Proxy Headers

    Ensure that your reverse proxy (like Nginx or Apache) is configured to forward the necessary headers. For example, if using Nginx, you should include the following in your server block: proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    nginx
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  3. 3

    Test the Application

    After making the changes, restart your Express application and test the endpoint that uses `req.protocol`. It should now correctly report `https` when accessed via HTTPS.

    bash
    curl -k -I https://node.dev:3000/your-endpoint
  4. 4

    Check Application Logs

    Monitor the application logs to confirm that the requests are being processed with the correct protocol. Look for log entries that indicate the protocol being used.

    bash
    tail -f /path/to/your/app/logs/app.log

Validation

To confirm the fix worked, make a request to your Express application and check the response headers or logs to see if `req.protocol` now reports `https`. You can also use tools like Postman or curl to verify the output.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

expressnode.jsapi