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

Update AntiCSRF.php #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
20 changes: 11 additions & 9 deletions src/AntiCSRF.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ class AntiCSRF
*/
protected $hmac_ip = true;

/**
* @var bool
*/
protected $hmac_ua = true;

/**
* @var bool
*/
Expand Down Expand Up @@ -269,14 +274,12 @@ public function getTokenArray(string $lockTo = ''): array

list($index, $token) = $this->generateToken($lockTo);

if ($this->hmac_ip !== false) {
if ($this->hmac_ip !== false || $this->hmac_ua !== false) {
// Use HMAC to only allow this particular IP to send this request
$token = Base64UrlSafe::encode(
\hash_hmac(
$this->hashAlgo,
isset($this->server['REMOTE_ADDR'])
? (string) $this->server['REMOTE_ADDR']
: '127.0.0.1',
(($this->hmac_ip !== false && $this->hmac_ua !== false) ? (isset($this->server['REMOTE_ADDR']) ? (string) $this->server['REMOTE_ADDR'] : '127.0.0.1').(isset($this->server['HTTP_USER_AGENT']) ? (string) $this->server['HTTP_USER_AGENT'] : 'Mozilla') : (($this->hmac_ip !== true && $this->hmac_ua !== false) ? (isset($this->server['HTTP_USER_AGENT']) ? (string) $this->server['HTTP_USER_AGENT'] : 'Mozilla') : (isset($this->server['REMOTE_ADDR']) ? (string) $this->server['REMOTE_ADDR'] : '127.0.0.1'))),
(string) Base64UrlSafe::decode($token),
true
)
Expand Down Expand Up @@ -365,7 +368,7 @@ public function validateRequest(): bool
}

// This is the expected token value
if ($this->hmac_ip === false) {
if ($this->hmac_ip === false && $this -> hmac_ua === false) {
// We just stored it wholesale
/** @var string $expected */
$expected = $stored['token'];
Expand All @@ -375,9 +378,7 @@ public function validateRequest(): bool
$expected = Base64UrlSafe::encode(
\hash_hmac(
$this->hashAlgo,
isset($this->server['REMOTE_ADDR'])
? (string) $this->server['REMOTE_ADDR']
: '127.0.0.1',
(($this->hmac_ip !== false && $this->hmac_ua !== false) ? (isset($this->server['REMOTE_ADDR']) ? (string) $this->server['REMOTE_ADDR'] : '127.0.0.1').(isset($this->server['HTTP_USER_AGENT']) ? (string) $this->server['HTTP_USER_AGENT'] : 'Mozilla') : (($this->hmac_ip !== true && $this->hmac_ua !== false) ? (isset($this->server['HTTP_USER_AGENT']) ? (string) $this->server['HTTP_USER_AGENT'] : 'Mozilla') : (isset($this->server['REMOTE_ADDR']) ? (string) $this->server['REMOTE_ADDR'] : '127.0.0.1'))),
(string) Base64UrlSafe::decode((string) $stored['token']),
true
)
Expand All @@ -403,8 +404,9 @@ public function reconfigure(array $options = []): self
case 'formToken':
case 'sessionIndex':
case 'useNativeSession':
case 'recycle_after':
case 'recycle_after':
case 'hmac_ip':
case 'hmac_ua':
case 'expire_old':
/** @psalm-suppress MixedAssignment */
$this->$opt = $val;
Expand Down