Skip to content

Commit 50bcdbf

Browse files
committed
fix: prevent reference key set for users with no capability Codeinwp/themeisle#1618
1 parent 8df4a81 commit 50bcdbf

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

src/Modules/Promotions.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ public function load_available() {
173173
* @return void
174174
*/
175175
public function register_reference() {
176+
if ( ! current_user_can( 'activate_plugins' ) ) {
177+
return;
178+
}
179+
176180
if ( isset( $_GET['reference_key'] ) ) {
177181
update_option( 'otter_reference_key', sanitize_key( $_GET['reference_key'] ) );
178182
}
@@ -269,12 +273,12 @@ public function is_writeable() {
269273

270274
/**
271275
* Third-party compatibility.
272-
*
276+
*
273277
* @return boolean
274278
*/
275279
private function has_conflicts() {
276280
global $pagenow;
277-
281+
278282
// Editor notices aren't compatible with Enfold theme.
279283
if ( defined( 'AV_FRAMEWORK_VERSION' ) && in_array( $pagenow, array( 'post.php', 'post-new.php' ) ) ) {
280284
return true;

tests/promotion-test.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Promotion module feature test.
4+
*
5+
* @package ThemeIsleSDK
6+
*/
7+
8+
/**
9+
* Test Promotion feature.
10+
*/
11+
class Promotion_Test extends WP_UnitTestCase {
12+
/**
13+
* Author user ID.
14+
*
15+
* @var int $author_id
16+
*/
17+
private $author_id;
18+
19+
/**
20+
* Set up.
21+
* Create a test user.
22+
*
23+
* @return void
24+
*/
25+
public function setUp() {
26+
parent::setUp();
27+
$this->author_id = $this->factory->user->create( array( 'role' => 'editor' ) );
28+
}
29+
30+
/**
31+
* Tear down.
32+
* Remove the user.
33+
*
34+
* @return void
35+
*/
36+
public function tearDown() {
37+
parent::tearDown();
38+
wp_delete_user( $this->author_id, true );
39+
}
40+
41+
/**
42+
* Test the CSRF protection when setting the reference_key
43+
*
44+
* @return void
45+
*/
46+
public function testCSRFOptionUpdate() {
47+
$promotions = new \ThemeisleSDK\Modules\Promotions();
48+
$option_key = 'otter_reference_key';
49+
50+
$option = get_option( $option_key );
51+
$this->assertEmpty( $option );
52+
53+
wp_set_current_user( $this->author_id );
54+
55+
// Check non-capable users can not update the option.
56+
$_GET['reference_key'] = 'test';
57+
$promotions->register_reference();
58+
$option = get_option( $option_key );
59+
$this->assertEmpty( $option );
60+
61+
wp_set_current_user( 1 );
62+
63+
// Check capable users can update the option.
64+
$promotions->register_reference();
65+
$option = get_option( $option_key );
66+
$this->assertEquals( 'test', $option );
67+
}
68+
}

0 commit comments

Comments
 (0)