mirror of
https://github.com/silverstripe/silverstripe-environmentcheck
synced 2024-10-22 17:05:40 +02:00
NEW: Session check.
This commit is contained in:
parent
e323674adf
commit
df59195634
@ -91,6 +91,7 @@ SilverStripe\EnvironmentCheck\EnvironmentCheckSuite:
|
||||
* `ExternalURLCheck`: Checks that one or more URLs are reachable via HTTP.
|
||||
* `SMTPConnectCheck`: Checks if the SMTP connection configured through PHP.ini works as expected.
|
||||
* `SolrIndexCheck`: Checks if the Solr cores of given class are available.
|
||||
* `SessionCheck`: Checks that a given URL does not generate a session.
|
||||
|
||||
## Monitoring Checks
|
||||
|
||||
|
86
src/Checks/SessionCheck.php
Normal file
86
src/Checks/SessionCheck.php
Normal file
@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace SilverStripe\EnvironmentCheck\Checks;
|
||||
|
||||
use SilverStripe\Control\Director;
|
||||
use SilverStripe\Control\Controller;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use SilverStripe\Core\Config\Configurable;
|
||||
use SilverStripe\EnvironmentCheck\Traits\Fetcher;
|
||||
use SilverStripe\EnvironmentCheck\EnvironmentCheck;
|
||||
|
||||
/**
|
||||
* Check that a given URL does not generate a session.
|
||||
*
|
||||
* @author Adrian Humphreys
|
||||
* @package environmentcheck
|
||||
*/
|
||||
class SessionCheck implements EnvironmentCheck
|
||||
{
|
||||
use Configurable;
|
||||
use Fetcher;
|
||||
|
||||
/**
|
||||
* URL to check
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $url;
|
||||
|
||||
/**
|
||||
* Set up check with URL
|
||||
*
|
||||
* @param string $url The route, excluding the domain
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function __construct($url = '')
|
||||
{
|
||||
$this->url = $url;
|
||||
$this->clientConfig = [
|
||||
'base_uri' => Director::absoluteBaseURL(),
|
||||
'timeout' => 10.0,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the response for URL does not create a session
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function check(): array
|
||||
{
|
||||
$response = $this->fetchResponse($this->url);
|
||||
$cookie = $this->getCookie($response);
|
||||
$fullURL = Controller::join_links(Director::absoluteBaseURL(), $this->url);
|
||||
|
||||
if ($cookie) {
|
||||
return [
|
||||
EnvironmentCheck::ERROR,
|
||||
"Sessions are being set for {$fullURL} : Set-Cookie => " . $cookie,
|
||||
];
|
||||
}
|
||||
return [
|
||||
EnvironmentCheck::OK,
|
||||
"Sessions are not being created for {$fullURL} 👍",
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get PHPSESSID or SECSESSID cookie set from the response if it exists.
|
||||
*
|
||||
* @param ResponseInterface $response
|
||||
* @return string|null Cookie contents or null if it doesn't exist
|
||||
*/
|
||||
public function getCookie(ResponseInterface $response): ?string
|
||||
{
|
||||
$result = null;
|
||||
$cookies = $response->getHeader('Set-Cookie');
|
||||
|
||||
foreach ($cookies as $cookie) {
|
||||
if (strpos($cookie, 'SESSID') !== false) {
|
||||
$result = $cookie;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user