Direkt zum Inhalt [Alt + SHIFT + 2] Direkt zum Inhalt [Alt + SHIFT + 1]

PHP

June 04, 2010

Dealing with SSL redirects under nginx

"Why are secure pages in SSL mode redirected to non-secure pages?" a client of mine asked. This is a big security risk. Imagine completing a bank transaction, being redirected to another transaction page and you don't notice that the browser isn't in the SSL mode anymore. This can happen if the code behind the proxy server doesn't read the global variables properly.

Let me explain you the case. For PHP the nginx proxy server sets following global variable to 'https'

$_SERVER['HTTP_SCHEME'] = 'https';

Normally PHP doesn't set this global variable for SSL connections but this one by default:

$_SERVER['HTTPS'] = 'https';

Most PHP frameworks generate SSL links by reading $_SERVER['HTTPS'] only, for example the constructor of the Zend_View_Helper_ServerUrl class from the Zend Framework Version 1.10.4 is doing following:

    /**
     * Constructor of Zend_View_Helper_ServerUrl
     *
     * @return void
     */
    public function __construct()
    {
        if (isset($_SERVER['HTTPS']) &&
           ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] === true)) {
               
            $scheme = 'https';
        } else {
            $scheme = 'http';
        }

        $this->setScheme($scheme);

        // Do more stuff

    }

You can see, it doesn't read from the $_SERVER['HTTP_SCHEME'] global variable but $_SERVER['HTTPS']. A solution would be to overwrite the constructor by your own subclass but this is too hackish and not clean object oriented design. There is no such protected method to alter that behaviour. Therefore I reported that problem to the Zend Frameworks Issue Tracker.

For now I have coded a workaround. In the bootstrapper class I added a method called _populateGlobalVariables() which translates the global variable nginx is setting ($_SERVER['HTTP_SCHEME'])  to the default one Zend Framework can deal with ($_SERVER['HTTPS']) and the problem has been solved. All links on SSL pages are pointing to other SSL pages with the HTTPS scheme.

Diskutieren: Dealing with SSL redirects under nginx

Antwort 1 von Wil Moore III

Interesting article.

You can actually side-step this issue by simply using scheme-relative URLs.

For instance, if the resource is located at http(s)://example.com then you can write:

//example.com and it will be translated to http or https by the user agent (e.g. browser).

See RFC1808:
http://freesoft.org/CIE/RFC/1808/18.htm

and check out this SO post:
http://stackoverflow.com/questions/2458735/using-in-a-scripts-source/2458861#2458861

Abgeschickt am September 23, 2010 um 09:32 Uhr

Antwort 2 von Michael Heuberger

Good trick wilmoore, I didn't know about //example.com ... many thanks!

Abgeschickt am September 23, 2010 um 11:52 Uhr

Zeige Resultate 1 bis 2 von total 2.

Schreiben Sie Ihre Meinung

Diese Frage wurde ausnahmesweise gestellt, um Spam herauszufiltern und um zu beweisen, dass Sie menschlich sind.

Hält Sie am Laufenden, wenn jemand anders geantwortet hat.