Browse Source

Merge pull request #10502 from cleptric/salt-setter-getter

Split Security::salt() into getter/setter
Mark Story 9 years ago
parent
commit
40aea19088

+ 1 - 1
src/Controller/Component/CookieComponent.php

@@ -118,7 +118,7 @@ class CookieComponent extends Component
     public function initialize(array $config)
     {
         if (!$this->_config['key']) {
-            $this->setConfig('key', Security::salt());
+            $this->setConfig('key', Security::getSalt());
         }
 
         $controller = $this->_registry->getController();

+ 1 - 1
src/Controller/Component/SecurityComponent.php

@@ -382,7 +382,7 @@ class SecurityComponent extends Component
             $controller->request->here(),
             serialize($fieldList),
             $unlocked,
-            Security::salt()
+            Security::getSalt()
         ];
     }
 

+ 1 - 1
src/Error/Debugger.php

@@ -928,7 +928,7 @@ TEXT;
      */
     public static function checkSecurityKeys()
     {
-        if (Security::salt() === '__SALT__') {
+        if (Security::getSalt() === '__SALT__') {
             trigger_error(sprintf('Please change the value of %s in %s to a salt value specific to your application.', '\'Security.salt\'', 'ROOT/config/app.php'), E_USER_NOTICE);
         }
     }

+ 1 - 1
src/Http/Cookie/CookieCryptTrait.php

@@ -112,7 +112,7 @@ trait CookieCryptTrait
     public function getEncryptionKey()
     {
         if ($this->encryptionKey === null) {
-            return Security::salt();
+            return Security::getSalt();
         }
 
         return $this->encryptionKey;

+ 1 - 1
src/TestSuite/IntegrationTestCase.php

@@ -306,7 +306,7 @@ abstract class IntegrationTestCase extends TestCase
             return $this->_cookieEncryptionKey;
         }
 
-        return Security::salt();
+        return Security::getSalt();
     }
 
     /**

+ 24 - 0
src/Utility/Security.php

@@ -313,9 +313,33 @@ class Security
     }
 
     /**
+     * Gets the HMAC salt to be used for encryption/decryption
+     * routines.
+     *
+     * @return string The currently configured salt
+     */
+    public static function getSalt()
+    {
+        return static::$_salt;
+    }
+
+    /**
+     * Sets the HMAC salt to be used for encryption/decryption
+     * routines.
+     *
+     * @param string $salt The salt to use for encryption routines.
+     * @return void
+     */
+    public static function setSalt($salt)
+    {
+        static::$_salt = (string)$salt;
+    }
+
+    /**
      * Gets or sets the HMAC salt to be used for encryption/decryption
      * routines.
      *
+     * @deprecated 3.5.0 Use getSalt()/setSalt() instead.
      * @param string|null $salt The salt to use for encryption routines. If null returns current salt.
      * @return string The currently configured salt
      */

+ 1 - 1
src/View/Helper/SecureFieldTokenTrait.php

@@ -56,7 +56,7 @@ trait SecureFieldTokenTrait
             $url,
             serialize($fields),
             $unlocked,
-            Security::salt()
+            Security::getSalt()
         ];
         $fields = Security::hash(implode('', $hashParts), 'sha1');
 

+ 11 - 0
tests/TestCase/Utility/SecurityTest.php

@@ -293,6 +293,17 @@ class SecurityTest extends TestCase
     }
 
     /**
+     * Tests that the salt can be set and retrieved
+     *
+     * @return void
+     */
+    public function testGetSetSalt()
+    {
+        Security::setSalt('foobarbaz');
+        $this->assertEquals('foobarbaz', Security::getSalt());
+    }
+
+    /**
      * Test the randomBytes method.
      *
      * @return void