CVE-2020-15128 (published 2020-07-31T18:15Z)

In OctoberCMS before version 1.0.468, encrypted cookie values were not tied to the name of the cookie the value belonged to. This meant that certain classes of attacks that took advantage of other theoretical vulnerabilities in user facing code (nothing exploitable in the core project itself) had a higher chance of succeeding. Specifically, if your usage exposed a way for users to provide unfiltered user input and have it returned to them as an encrypted cookie (ex. storing a user provided search query in a cookie) they could then use the generated cookie in place of other more tightly controlled cookies; or if your usage exposed the plaintext version of an encrypted cookie at any point to the user they could theoretically provide encrypted content from your application back to it as an encrypted cookie and force the framework to decrypt it for them. Issue has been fixed in build 468 (v1.0.468).

Severity: LOW
CVSS v2 base score: 3.5 (AV:N/AC:M/Au:S/C:N/I:P/A:N)
CVSS v3 base score: 6.3, MEDIUM (CVSS:3.1/AV:N/AC:H/PR:L/UI:N/S:C/C:N/I:H/A:N)

Weakness information:
cwe_id: CWE-327
cwe_name: Use of a Broken or Risky Cryptographic Algorithm
description: The product uses a broken or risky cryptographic algorithm or protocol.
extended_description: 
	   Cryptographic algorithms are the methods by which data is scrambled to prevent observation or influence by unauthorized actors. Insecure cryptography can be exploited to expose sensitive information, modify data in unexpected ways, spoof identities of other users or devices, or other impacts.
           It is very difficult to produce a secure algorithm, and even high-profile algorithms by accomplished cryptographic experts have been broken. Well-known techniques exist to break or weaken various kinds of cryptography. Accordingly, there are a small number of well-understood and heavily studied algorithms that should be used by most products. Using a non-standard or known-insecure algorithm is dangerous because a determined adversary may be able to break the algorithm and compromise whatever data has been protected.
           Since the state of cryptography advances so rapidly, it is common for an algorithm to be considered "unsafe" even if it was once thought to be strong. This can happen when new attacks are discovered, or if computing power increases so much that the cryptographic algorithm no longer provides the amount of protection that was originally thought.
	   For a number of reasons, this weakness is even more challenging to manage with hardware deployment of cryptographic algorithms as opposed to software implementation. First, if a flaw is discovered with hardware-implemented cryptography, the flaw cannot be fixed in most cases without a recall of the product, because hardware is not easily replaceable like software. Second, because the hardware product is expected to work for years, the adversary's computing power will only increase over time.
	 
         

Fix information:
repo_url: https://github.com/octobercms/library
commit_details: repo_url: https://github.com/octobercms/library
author: Luke Towers
author_date: 2020-07-28 21:05:38-06:00
msg: Improvements to cookie value validation (#508)
repository: repo_url: https://github.com/octobercms/library
repo_name: octobercms/library
description: October Rain Library
repo_language: PHP
owner: octobercms
file_changes: filename: CookieValuePrefix.php
change_type: ModificationType.ADD
diff: @@ -0,0 +1,47 @@
+<?php namespace October\Rain\Cookie;
+
+/**
+ * Helper class to prefix, unprefix, and verify cookie values
+ */
+class CookieValuePrefix
+{
+    /**
+     * Create a new cookie value prefix for the given cookie name.
+     *
+     * @param  string $name The name of the cookie
+     * @param  string $key The encryption key
+     * @return string
+     */
+    public static function create($name, $key)
+    {
+        return hash_hmac('sha1', $name . 'v2', $key) . '|';
+    }
+
+    /**
+     * Remove the cookie value prefix.
+     *
+     * @param  string  $cookieValue
+     * @return string
+     */
+    public static function remove($cookieValue)
+    {
+        return substr($cookieValue, 41);
+    }
+
+    /**
+     * Verify the provided cookie's value
+     *
+     * @param string $name The name of the cookie
+     * @param string $value The decrypted value of the cookie to be verified
+     * @param string $key The encryption key used to encrypt the cookie originally
+     * @return string|null $verifiedValue The unprefixed value if it passed verification, otherwise null
+     */
+    public static function getVerifiedValue($name, $value, $key)
+    {
+        $verifiedValue = null;
+        if (starts_with($value, static::create($name, $key))) {
+            $verifiedValue = static::remove($value);
+        }
+        return $verifiedValue;
+    }
+}

programming_language: PHP
filename: EncryptCookies.php
change_type: ModificationType.MODIFY
diff: @@ -1,6 +1,11 @@
 <?php namespace October\Rain\Cookie\Middleware;
 
 use Config;
+use Session;
+use October\Rain\Cookie\CookieValuePrefix;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+use Illuminate\Contracts\Encryption\DecryptException;
 use Illuminate\Contracts\Encryption\Encrypter as EncrypterContract;
 use Illuminate\Cookie\Middleware\EncryptCookies as EncryptCookiesBase;
 
@@ -57,4 +62,72 @@ class EncryptCookies extends EncryptCookiesBase
 
         return $decrypted;
     }
+
+    /**
+     * Decrypt the cookies on the request.
+     *
+     * @param  \Symfony\Component\HttpFoundation\Request  $request
+     * @return \Symfony\Component\HttpFoundation\Request
+     */
+    protected function decrypt(Request $request)
+    {
+        foreach ($request->cookies as $key => $cookie) {
+            if ($this->isDisabled($key)) {
+                continue;
+            }
+
+            try {
+                // Decrypt the request-provided cookie
+                $decryptedValue = $this->decryptCookie($key, $cookie);
+
+                // Verify that the decrypted value belongs to this cookie key, use null if it fails
+                $value = CookieValuePrefix::getVerifiedValue($key, $decryptedValue, $this->encrypter->getKey());
+
+                /**
+                 * If the cookie is for the session and the value is a valid Session ID,
+                 * then allow it to pass through even if the validation failed (most likely
+                 * because the upgrade just occurred)
+                 *
+                 * The cookie will be adjusted on the next request
+                 * @todo Remove if year >= 2021 or build >= 475
+                 */
+                if (empty($value) && $key === Config::get('session.cookie') && Session::isValidId($decryptedValue)) {
+                    $value = $decryptedValue;
+                }
+
+                // Set the verified cookie value on the request
+                $request->cookies->set($key, $value);
+            } catch (DecryptException $e) {
+                $request->cookies->set($key, null);
+            }
+        }
+
+        return $request;
+    }
+
+    /**
+     * Encrypt the cookies on an outgoing response.
+     *
+     * @param  \Symfony\Component\HttpFoundation\Response  $response
+     * @return \Symfony\Component\HttpFoundation\Response
+     */
+    protected function encrypt(Response $response)
+    {
+        foreach ($response->headers->getCookies() as $cookie) {
+            if ($this->isDisabled($cookie->getName())) {
+                continue;
+            }
+
+            $response->headers->setCookie($this->duplicate(
+                $cookie,
+                $this->encrypter->encrypt(
+                    // Prefix the cookie value to verify that it belongs to the current cookie
+                    CookieValuePrefix::create($cookie->getName(), $this->encrypter->getKey()) . $cookie->getValue(),
+                    static::serialized($cookie->getName())
+                )
+            ));
+        }
+
+        return $response;
+    }
 }

programming_language: PHP