Page MenuHomePhabricator

D6686.id14988.diff
No OneTemporary

D6686.id14988.diff

Index: src/applications/people/storage/PhabricatorUser.php
===================================================================
--- src/applications/people/storage/PhabricatorUser.php
+++ src/applications/people/storage/PhabricatorUser.php
@@ -157,7 +157,7 @@
const EMAIL_CYCLE_FREQUENCY = 86400;
const EMAIL_TOKEN_LENGTH = 24;
- public function getCSRFToken($offset = 0) {
+ private function getRawCSRFToken($offset = 0) {
return $this->generateToken(
time() + (self::CSRF_CYCLE_FREQUENCY * $offset),
self::CSRF_CYCLE_FREQUENCY,
@@ -165,12 +165,31 @@
self::CSRF_TOKEN_LENGTH);
}
- public function validateCSRFToken($token) {
+ public function getCSRFToken() {
+ // Generate a token hash to mitigate BREACH attacks against SSL. See
+ // discussion in T3684.
+ $token = $this->getRawCSRFToken();
+ $salt = Filesystem::readRandomCharacters(16);
+ $hash = PhabricatorHash::digest($token, $salt);
+ return 'B@'.$salt.$hash;
+ }
+ public function validateCSRFToken($token) {
if (!$this->getPHID()) {
return true;
}
+ $salt = null;
+
+ $version = 'plain';
+
+ // This is a BREACH-mitigating token. See T3684.
+ if (!strncmp($token, 'B@', 2)) {
+ $version = 'breach';
+ $salt = substr($token, 2, 16);
+ $token = substr($token, 18);
+ }
+
// When the user posts a form, we check that it contains a valid CSRF token.
// Tokens cycle each hour (every CSRF_CYLCE_FREQUENCY seconds) and we accept
// either the current token, the next token (users can submit a "future"
@@ -199,9 +218,22 @@
$csrf_window = 6;
for ($ii = -$csrf_window; $ii <= 1; $ii++) {
- $valid = $this->getCSRFToken($ii);
- if ($token == $valid) {
- return true;
+ $valid = $this->getRawCSRFToken($ii);
+ switch ($version) {
+ // TODO: We can remove this after the BREACH version has been in the
+ // wild for a while.
+ case 'plain':
+ if ($token == $valid) {
+ return true;
+ }
+ break;
+ case 'breach':
+ if (PhabricatorHash::digest($valid, $salt) == $token) {
+ return true;
+ }
+ break;
+ default:
+ throw new Exception("Unknown CSRF token format!");
}
}
Index: src/infrastructure/util/PhabricatorHash.php
===================================================================
--- src/infrastructure/util/PhabricatorHash.php
+++ src/infrastructure/util/PhabricatorHash.php
@@ -9,8 +9,11 @@
* @param string Input string.
* @return string 32-byte hexidecimal SHA1+HMAC hash.
*/
- public static function digest($string) {
- $key = PhabricatorEnv::getEnvConfig('security.hmac-key');
+ public static function digest($string, $key = null) {
+ if ($key === null) {
+ $key = PhabricatorEnv::getEnvConfig('security.hmac-key');
+ }
+
if (!$key) {
throw new Exception(
"Set a 'security.hmac-key' in your Phabricator configuration!");

File Metadata

Mime Type
text/plain
Expires
Sun, Mar 23, 6:58 AM (1 d, 12 h ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7718048
Default Alt Text
D6686.id14988.diff (2 KB)

Event Timeline