Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use putenv() in InitialisationMiddleware #61

Merged
merged 5 commits into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/Control/InitialisationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ class InitialisationMiddleware implements HTTPMiddleware

public function process(HTTPRequest $request, callable $delegate)
{
$response = $delegate($request);

if ($this->config()->get('egress_proxy_default_enabled')) {
$this->configureEgressProxy();
}

$this->configureProxyDomainExclusions();

$response = $delegate($request);

if ($this->config()->get('xss_protection_enabled') && $response) {
$response->addHeader('X-XSS-Protection', '1; mode=block');
}
Expand All @@ -80,8 +80,15 @@ protected function configureEgressProxy()
$proxy = Environment::getEnv('SS_OUTBOUND_PROXY');
$proxyPort = Environment::getEnv('SS_OUTBOUND_PROXY_PORT');

Environment::setEnv('http_proxy', $proxy . ':' . $proxyPort);
Environment::setEnv('https_proxy', $proxy . ':' . $proxyPort);
/*
* This sets the environment variables so they are available in
* external calls executed by exec() such as curl.
* Environment::setEnv() would only availabe in context of SilverStripe.
* Environment::getEnv() will fallback to getenv() and will therefore
* fetch the variables
*/
putenv('http_proxy=' . $proxy . ':' . $proxyPort);
putenv('https_proxy=' . $proxy . ':' . $proxyPort);
}

/**
Expand All @@ -103,6 +110,10 @@ protected function configureProxyDomainExclusions()
$noProxy = array_merge(explode(',', Environment::getEnv('NO_PROXY')), $noProxy);
}

Environment::setEnv('NO_PROXY', implode(',', array_unique($noProxy)));
/*
* Set the environment varial for NO_PROXY the same way the
* proxy variables are set above
*/
putenv('NO_PROXY=' . implode(',', array_unique($noProxy)));
}
}
5 changes: 2 additions & 3 deletions tests/Control/InitialisationMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function setUp()

Environment::setEnv('SS_OUTBOUND_PROXY', '');
Environment::setEnv('SS_OUTBOUND_PROXY_PORT', '');
Environment::setEnv('NO_PROXY', '');
putenv('NO_PROXY=');
}

public function testDoNotConfigureProxyIfNoEnvironmentVarsAreSet()
Expand Down Expand Up @@ -79,8 +79,7 @@ public function testConfigureEgressProxyDomainExclusions()
'example.com'
);

Environment::setEnv('NO_PROXY', 'foo.com,bar.com');

putenv('NO_PROXY=foo.com,bar.com');
$this->runMiddleware();

$this->assertSame(
Expand Down