File tree Expand file tree Collapse file tree 3 files changed +24
-5
lines changed
Expand file tree Collapse file tree 3 files changed +24
-5
lines changed Original file line number Diff line number Diff line change 113.x
22===
33
4+ * Fix ` req.protocol ` for proxy-direct connections
45 * Pass options from ` res.sendfile ` to ` send `
5667 - deps: body-parser@~ 1.5.0
Original file line number Diff line number Diff line change @@ -341,7 +341,9 @@ req.is = function(type){
341341 * Return the protocol string "http" or "https"
342342 * when requested with TLS. When the "trust proxy"
343343 * setting trusts the socket address, the
344- * "X-Forwarded-Proto" header field will be trusted.
344+ * "X-Forwarded-Proto" header field will be trusted
345+ * and used if present.
346+ *
345347 * If you're running behind a reverse proxy that
346348 * supplies https for you this may be enabled.
347349 *
@@ -350,17 +352,18 @@ req.is = function(type){
350352 */
351353
352354req . __defineGetter__ ( 'protocol' , function ( ) {
355+ var proto = this . connection . encrypted
356+ ? 'https'
357+ : 'http' ;
353358 var trust = this . app . get ( 'trust proxy fn' ) ;
354359
355360 if ( ! trust ( this . connection . remoteAddress ) ) {
356- return this . connection . encrypted
357- ? 'https'
358- : 'http' ;
361+ return proto ;
359362 }
360363
361364 // Note: X-Forwarded-Proto is normally only ever a
362365 // single value, but this is to be safe.
363- var proto = this . get ( 'X-Forwarded-Proto' ) || 'http' ;
366+ proto = this . get ( 'X-Forwarded-Proto' ) || proto ;
364367 return proto . split ( / \s * , \s * / ) [ 0 ] ;
365368} ) ;
366369
Original file line number Diff line number Diff line change @@ -32,6 +32,21 @@ describe('req', function(){
3232 . expect ( 'https' , done ) ;
3333 } )
3434
35+ it ( 'should default to the socket addr if X-Forwarded-Proto not present' , function ( done ) {
36+ var app = express ( ) ;
37+
38+ app . enable ( 'trust proxy' ) ;
39+
40+ app . use ( function ( req , res ) {
41+ req . connection . encrypted = true ;
42+ res . end ( req . protocol ) ;
43+ } ) ;
44+
45+ request ( app )
46+ . get ( '/' )
47+ . expect ( 'https' , done ) ;
48+ } )
49+
3550 it ( 'should ignore X-Forwarded-Proto if socket addr not trusted' , function ( done ) {
3651 var app = express ( ) ;
3752
You can’t perform that action at this time.
0 commit comments