-
Notifications
You must be signed in to change notification settings - Fork 8
Fix. Code. Editing the check_value parametere #813
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
Open
AntonV1211
wants to merge
4
commits into
dev
Choose a base branch
from
vuln_check_value_fix
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| <?php | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| /** | ||
| * Tests for the check_value salt fix in apbct_cookie() and apbct_cookies_test(). | ||
| * | ||
| * Verifies that: | ||
| * 1. check_value includes salt, so md5(api_key) alone cannot be extracted from cookies. | ||
| * 2. apbct_cookies_test() validates cookies using salt. | ||
| * 3. Cookies without salt are rejected. | ||
| */ | ||
| class TestCheckValueSalt extends TestCase | ||
| { | ||
| private $apbctBackup; | ||
| private $cookieBackup; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| global $apbct; | ||
|
|
||
| $this->apbctBackup = $apbct; | ||
| $this->cookieBackup = $_COOKIE; | ||
|
|
||
| $apbct = (object) [ | ||
| 'api_key' => 'testkey123', | ||
| 'data' => [ | ||
| 'salt' => 'test_salt_value', | ||
| 'cookies_type' => 'native', | ||
| 'key_is_ok' => 1, | ||
| ], | ||
| ]; | ||
|
|
||
| \Cleantalk\ApbctWP\Variables\Cookie::$force_alt_cookies_global = false; | ||
| } | ||
|
|
||
| protected function tearDown(): void | ||
| { | ||
| global $apbct; | ||
| $apbct = $this->apbctBackup; | ||
| $_COOKIE = $this->cookieBackup; | ||
| } | ||
|
|
||
| /** | ||
| * Test that check_value with salt differs from check_value without salt. | ||
| * This verifies the vulnerability fix: check_value must NOT equal md5(api_key). | ||
| */ | ||
| public function testCheckValueWithSaltDiffersFromWithout() | ||
| { | ||
| global $apbct; | ||
|
|
||
| $with_salt = md5($apbct->api_key . $apbct->data['salt']); | ||
| $without_salt = md5($apbct->api_key); | ||
|
|
||
| $this->assertNotEquals( | ||
| $without_salt, | ||
| $with_salt, | ||
| 'check_value must not equal md5(api_key) alone - salt must change the hash' | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Test that apbct_cookies_test() validates a cookie computed WITH salt. | ||
| */ | ||
| public function testApbctCookiesTestValidatesWithSalt() | ||
| { | ||
| global $apbct; | ||
|
|
||
| $cookie_test_value = array( | ||
| 'cookies_names' => array(), | ||
| 'check_value' => md5($apbct->api_key . $apbct->data['salt']), | ||
| ); | ||
|
|
||
| $cookie_prefix = function_exists('apbct__get_cookie_prefix') ? apbct__get_cookie_prefix() : ''; | ||
| $_COOKIE[$cookie_prefix . 'apbct_cookies_test'] = urlencode(json_encode($cookie_test_value)); | ||
|
|
||
| $result = apbct_cookies_test(); | ||
|
|
||
| $this->assertSame(1, $result, 'Cookie with md5(api_key + salt) must pass validation'); | ||
| } | ||
|
|
||
| /** | ||
| * Test that apbct_cookies_test() rejects a cookie computed WITHOUT salt. | ||
| * This is the core of the vulnerability fix verification. | ||
| */ | ||
| public function testApbctCookiesTestRejectsCookieWithoutSalt() | ||
| { | ||
| global $apbct; | ||
|
|
||
| $cookie_test_value = array( | ||
| 'cookies_names' => array(), | ||
| 'check_value' => md5($apbct->api_key), // No salt - old vulnerable value | ||
| ); | ||
|
|
||
| $cookie_prefix = function_exists('apbct__get_cookie_prefix') ? apbct__get_cookie_prefix() : ''; | ||
| $_COOKIE[$cookie_prefix . 'apbct_cookies_test'] = urlencode(json_encode($cookie_test_value)); | ||
|
|
||
| $result = apbct_cookies_test(); | ||
|
|
||
| $this->assertSame(0, $result, 'Cookie with md5(api_key) without salt must be rejected'); | ||
| } | ||
|
|
||
| /** | ||
| * Test that check_value with salt + timestamp is correctly validated. | ||
| */ | ||
| public function testApbctCookiesTestValidatesWithSaltAndTimestamp() | ||
| { | ||
| global $apbct; | ||
|
|
||
| $timestamp = '1718000000'; | ||
|
|
||
| $cookie_test_value = array( | ||
| 'cookies_names' => array('ct_ps_timestamp'), | ||
| 'check_value' => md5($apbct->api_key . $apbct->data['salt'] . $timestamp), | ||
| ); | ||
|
|
||
| $cookie_prefix = function_exists('apbct__get_cookie_prefix') ? apbct__get_cookie_prefix() : ''; | ||
| $_COOKIE[$cookie_prefix . 'apbct_cookies_test'] = urlencode(json_encode($cookie_test_value)); | ||
| $_COOKIE[$cookie_prefix . 'ct_ps_timestamp'] = $timestamp; | ||
|
|
||
| $result = apbct_cookies_test(); | ||
|
|
||
| $this->assertSame(1, $result, 'Cookie with md5(api_key + salt + timestamp) must pass validation'); | ||
| } | ||
|
|
||
| /** | ||
| * Test that check_value with timestamp but WITHOUT salt is rejected. | ||
| */ | ||
| public function testApbctCookiesTestRejectsTimestampCookieWithoutSalt() | ||
| { | ||
| global $apbct; | ||
|
|
||
| $timestamp = '1718000000'; | ||
|
|
||
| $cookie_test_value = array( | ||
| 'cookies_names' => array('ct_ps_timestamp'), | ||
| 'check_value' => md5($apbct->api_key . $timestamp), // No salt | ||
| ); | ||
|
|
||
| $cookie_prefix = function_exists('apbct__get_cookie_prefix') ? apbct__get_cookie_prefix() : ''; | ||
| $_COOKIE[$cookie_prefix . 'apbct_cookies_test'] = urlencode(json_encode($cookie_test_value)); | ||
| $_COOKIE[$cookie_prefix . 'ct_ps_timestamp'] = $timestamp; | ||
|
|
||
| $result = apbct_cookies_test(); | ||
|
|
||
| $this->assertSame(0, $result, 'Cookie with md5(api_key + timestamp) without salt must be rejected'); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.