88
99class OneLogin_Saml2_Utils
1010{
11+
1112 /**
12- * Translates any string. Accepts args
13- *
14- * @param string $msg Message to be translated
15- * @param array|null $args Arguments
16- *
17- * @return string $translatedMsg Translated text
18- */
13+ * @var bool Control if the `Forwarded-For-*` headers are used
14+ */
15+ private static $ _proxyVars = false ;
16+
17+ /**
18+ * Translates any string. Accepts args
19+ *
20+ * @param string $msg Message to be translated
21+ * @param array|null $args Arguments
22+ *
23+ * @return string $translatedMsg Translated text
24+ */
1925 public static function t ($ msg , $ args = array ())
2026 {
2127 assert ('is_string($msg) ' );
@@ -272,6 +278,22 @@ public static function redirect($url, $parameters = array(), $stay = false)
272278 exit ();
273279 }
274280
281+ /**
282+ * @param $proxyVars bool Whether to use `X-Forwarded-*` headers to determine port/domain/protocol
283+ */
284+ public static function setProxyVars ($ proxyVars )
285+ {
286+ self ::$ _proxyVars = (bool )$ proxyVars ;
287+ }
288+
289+ /**
290+ * return bool
291+ */
292+ public static function getProxyVars ()
293+ {
294+ return self ::$ _proxyVars ;
295+ }
296+
275297 /**
276298 * Returns the protocol + the current host + the port (if different than
277299 * common ports).
@@ -290,7 +312,7 @@ public static function getSelfURLhost()
290312 $ protocol = 'http ' ;
291313 }
292314
293- if (isset ($ _SERVER ["HTTP_X_FORWARDED_PORT " ])) {
315+ if (self :: getProxyVars () && isset ($ _SERVER ["HTTP_X_FORWARDED_PORT " ])) {
294316 $ portnumber = $ _SERVER ["HTTP_X_FORWARDED_PORT " ];
295317 } else if (isset ($ _SERVER ["SERVER_PORT " ])) {
296318 $ portnumber = $ _SERVER ["SERVER_PORT " ];
@@ -342,7 +364,7 @@ public static function isHTTPS()
342364 {
343365 $ isHttps = (!empty ($ _SERVER ['HTTPS ' ]) && $ _SERVER ['HTTPS ' ] !== 'off ' )
344366 || (isset ($ _SERVER ['SERVER_PORT ' ]) && $ _SERVER ['SERVER_PORT ' ] == 443 )
345- || (isset ($ _SERVER ['HTTP_X_FORWARDED_PROTO ' ]) && $ _SERVER ['HTTP_X_FORWARDED_PROTO ' ] == 'https ' );
367+ || (self :: getProxyVars () && isset ($ _SERVER ['HTTP_X_FORWARDED_PROTO ' ]) && $ _SERVER ['HTTP_X_FORWARDED_PROTO ' ] == 'https ' );
346368 return $ isHttps ;
347369 }
348370
0 commit comments