@@ -473,6 +473,62 @@ console.log('status', packet);
473473 else
474474 if ( self . status === "init" )
475475 {
476+ // Authorize connecting debugger engine by IP if applicable
477+ if ( self . options . ips && self . options . ips . length > 0 )
478+ {
479+ var found = self . options . ips . indexOf ( self . socket . remoteAddress ) ;
480+ if ( found === - 1 )
481+ {
482+ console . log ( "Error: IDEKEY mismatch!" , self . socket . remoteAddress , self . options . ips ) ;
483+ return ;
484+ }
485+ }
486+
487+ // Fix the init packet. This is needed due to a bug in Xdebug. Fixing it in Xdebug
488+ // at this stage may break a lot of clients using the DBGP protocol.
489+ // 1) When debugging CLI scripts and environment variables are set to:
490+ // export XDEBUG_CONFIG="idekey=SESSION"
491+ // export XDEBUG_CONFIG="idekey=,session=SESSION"
492+ // export XDEBUG_CONFIG="idekey=IDEKEY,session=SESSION"
493+ // the `packet["@"].idekey` property is set to (respectively):
494+ // `packet["@"].idekey == "SESSION"`
495+ // `packet["@"].idekey == ",session=SESSION"`
496+ // `packet["@"].idekey == "IDEKEY,session=SESSION"`
497+ // 2) If debugging a MOD_APACHE script and cookies are set to:
498+ // `XDEBUG_SESSION_START=SESSION`
499+ // the `packet["@"].idekey` property is set to:
500+ // `packet["@"].idekey == "SESSION"`
501+ // and the `xdebug.idekey` php.ini config option is ignored.
502+ // Because we do not always get an `idekey` we need to restrict debug engines
503+ // from connecting by IP whitelist and/or use a hash for the session ID.
504+
505+ var idekey = packet [ "@" ] . idekey . split ( "," ) ;
506+ packet [ "@" ] . idekey = undefined ;
507+ if ( idekey . length === 1 ) {
508+ packet [ "@" ] . session = idekey [ 0 ] ;
509+ } else {
510+ if ( idekey . length != 2 )
511+ throw new Error ( "`idekey` property in init packet does not have correct format (1)!" ) ;
512+ if ( idekey [ 0 ] ) {
513+ packet [ "@" ] . idekey = idekey [ 0 ] ;
514+ }
515+ var session = idekey [ 1 ] . split ( "=" ) ;
516+ if ( session . length != 2 || session [ 0 ] !== "session" )
517+ throw new Error ( "`idekey` property in init packet does not have correct format (2)!" ) ;
518+ packet [ "@" ] . session = session [ 1 ] ;
519+ }
520+
521+ // If `idekey` is set we authorize it
522+ if ( packet [ "@" ] . idekey && self . options . idekeys && self . options . idekeys . length > 0 )
523+ {
524+ var found = self . options . idekeys . indexOf ( packet [ "@" ] . idekey ) ;
525+ if ( found === - 1 )
526+ {
527+ console . log ( "Error: IDEKEY mismatch!" , packet [ "@" ] . idekey , self . options . idekeys ) ;
528+ return ;
529+ }
530+ }
531+
476532 // 5.2 Connection Initialization
477533 // @see http://www.xdebug.org/docs-dbgp.php#id18
478534 self . id = "session-" + ( ++ sessionCounter ) ;
0 commit comments