Skip to content

Commit 0076c82

Browse files
committed
Added nonce for settings tab.
1 parent 99ae298 commit 0076c82

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

plugins/wpgraphql-logging/src/Admin/Settings/Templates/admin.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
$wpgraphql_logging_tabs_config = (array) get_query_var( 'wpgraphql_logging_main_page_config' );
1818
$wpgraphql_logging_current_tab = (string) ( $wpgraphql_logging_tabs_config['current_tab'] ?? '' );
1919
$wpgraphql_logging_tabs = (array) ( $wpgraphql_logging_tabs_config['tabs'] ?? [] );
20+
$wpgraphql_logging_nonce = (string) ( $wpgraphql_logging_tabs_config['nonce'] ?? '' );
2021
?>
2122

2223
<div class="wrap">
@@ -25,7 +26,15 @@
2526
<nav class="nav-tab-wrapper">
2627
<?php
2728
foreach ( $wpgraphql_logging_tabs as $wpgraphql_logging_tab_key => $wpgraphql_logging_tab_label ) {
28-
$wpgraphql_logging_tab_url = admin_url( 'admin.php?page=wpgraphql-logging&tab=' . $wpgraphql_logging_tab_key );
29+
// Add security nonce.
30+
$wpgraphql_logging_tab_url = add_query_arg(
31+
[
32+
'page' => 'wpgraphql-logging',
33+
'tab' => $wpgraphql_logging_tab_key,
34+
'wpgraphql_logging_settings_tab_nonce' => $wpgraphql_logging_nonce,
35+
],
36+
admin_url( 'admin.php' )
37+
);
2938
$wpgraphql_logging_tab_class = add_cssclass( $wpgraphql_logging_current_tab === $wpgraphql_logging_tab_key ? 'nav-tab-active' : '', 'nav-tab' );
3039
echo '<a href="' . esc_url( $wpgraphql_logging_tab_url ) . '" class="' . esc_attr( $wpgraphql_logging_tab_class ) . '">' . esc_html( $wpgraphql_logging_tab_label ) . '</a>';
3140
}

plugins/wpgraphql-logging/src/Admin/SettingsPage.php

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ class SettingsPage {
2727
public const PLUGIN_MENU_SLUG = 'wpgraphql-logging';
2828

2929
/**
30-
* The field collection.
30+
* The settings field collection.
3131
*
3232
* @var \WPGraphQL\Logging\Admin\Settings\Fields\SettingsFieldCollection|null
3333
*/
3434
protected ?SettingsFieldCollection $field_collection = null;
3535

3636
/**
37-
* The instance of the plugin.
37+
* The instance of the settings page.
3838
*
3939
* @var \WPGraphQL\Logging\Admin\SettingsPage|null
4040
*/
@@ -116,6 +116,7 @@ public function register_settings_page(): void {
116116
'wpgraphql_logging_main_page_config' => [
117117
'tabs' => $tab_labels,
118118
'current_tab' => $this->get_current_tab(),
119+
'nonce' => wp_create_nonce( 'wpgraphql-logging-settings-tab-action' ),
119120
],
120121
],
121122
);
@@ -158,13 +159,16 @@ public function get_current_tab( array $tabs = [] ): string {
158159
if ( empty( $tabs ) ) {
159160
return $this->get_default_tab();
160161
}
161-
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading GET parameter for tab navigation only, no form processing
162+
162163
if ( ! isset( $_GET['tab'] ) || ! is_string( $_GET['tab'] ) ) {
163164
return $this->get_default_tab();
164165
}
165166

166-
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading GET parameter for tab navigation only, no form processing
167-
$tab = sanitize_text_field( $_GET['tab'] );
167+
if ( ! isset( $_GET['wpgraphql_logging_settings_tab_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['wpgraphql_logging_settings_tab_nonce'] ) ), 'wpgraphql-logging-settings-tab-action' ) ) {
168+
return $this->get_default_tab();
169+
}
170+
171+
$tab = sanitize_text_field( wp_unslash( $_GET['tab'] ) );
168172

169173
if ( '' === $tab ) {
170174
return $this->get_default_tab();
@@ -192,7 +196,6 @@ public function get_default_tab(): string {
192196
* @param string $hook_suffix The current admin page hook suffix.
193197
*/
194198
public function load_scripts_styles( string $hook_suffix ): void {
195-
// Only load on our settings page.
196199
if ( ! str_contains( $hook_suffix, self::PLUGIN_MENU_SLUG ) ) {
197200
return;
198201
}
@@ -210,18 +213,19 @@ public function load_scripts_styles( string $hook_suffix ): void {
210213

211214
// Enqueue admin scripts if they exist.
212215
$script_path = trailingslashit( WPGRAPHQL_LOGGING_PLUGIN_URL ) . 'assets/js/settings/wp-graphql-logging-settings.js';
213-
if ( ! file_exists( trailingslashit( WPGRAPHQL_LOGGING_PLUGIN_DIR ) . 'assets/js/settings/wp-graphql-logging-settings.js' ) ) {
214-
return;
216+
if ( file_exists( trailingslashit( WPGRAPHQL_LOGGING_PLUGIN_DIR ) . 'assets/js/settings/wp-graphql-logging-settings.js' ) ) {
217+
wp_enqueue_script(
218+
'wpgraphql-logging-settings-js',
219+
$script_path,
220+
[],
221+
WPGRAPHQL_LOGGING_VERSION,
222+
true
223+
);
215224
}
216225

217-
wp_enqueue_script(
218-
'wpgraphql-logging-settings-js',
219-
$script_path,
220-
[],
221-
WPGRAPHQL_LOGGING_VERSION,
222-
true
223-
);
224-
226+
/**
227+
* Fire off action to enqueue scripts and styles.
228+
*/
225229
do_action( 'wpgraphql_logging_admin_enqueue_scripts', $hook_suffix );
226230
}
227231

plugins/wpgraphql-logging/tests/wpunit/Admin/SettingsPageTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public function get_fields(): array { return []; }
116116

117117
// Valid tab -> returns it
118118
$_GET['tab'] = 'advanced';
119+
$_GET['wpgraphql_logging_settings_tab_nonce'] = wp_create_nonce( 'wpgraphql-logging-settings-tab-action' );
119120
$this->assertSame('advanced', $page->get_current_tab($tabs));
120121
}
121122
}

0 commit comments

Comments
 (0)