Skip to content

Commit bb690c1

Browse files
committed
Added nonce to download action.
1 parent b027cf8 commit bb690c1

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

plugins/wpgraphql-logging/src/Admin/View/List/ListTable.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,9 @@ public function column_cb( $item ): string {
299299
* @return string The rendered ID column or null.
300300
*/
301301
public function column_id( WordPressDatabaseEntity $item ): string {
302-
$url = \WPGraphQL\Logging\Admin\ViewLogsPage::ADMIN_PAGE_SLUG;
303-
$actions = [
302+
$url = \WPGraphQL\Logging\Admin\ViewLogsPage::ADMIN_PAGE_SLUG;
303+
$download_nonce = wp_create_nonce( 'wpgraphql-logging-download_' . $item->get_id() );
304+
$actions = [
304305
'view' => sprintf(
305306
'<a href="?page=%s&action=%s&log=%d">%s</a>',
306307
esc_attr( $url ),
@@ -309,10 +310,11 @@ public function column_id( WordPressDatabaseEntity $item ): string {
309310
esc_html__( 'View', 'wpgraphql-logging' )
310311
),
311312
'download' => sprintf(
312-
'<a href="?page=%s&action=%s&log=%d">%s</a>',
313+
'<a href="?page=%s&action=%s&log=%d&_wpnonce=%s">%s</a>',
313314
esc_attr( $url ),
314315
'download',
315316
$item->get_id(),
317+
esc_attr( $download_nonce ),
316318
esc_html__( 'Download', 'wpgraphql-logging' )
317319
),
318320
];

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public function enqueue_admin_scripts( string $hook_suffix ): void {
128128

129129
wp_enqueue_style( 'jquery-ui-style', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css', [], '1.12.1' );
130130

131+
// Add inline script to initialize the datetimepicker.
131132
wp_add_inline_script(
132133
'jquery-ui-timepicker-addon',
133134
'jQuery(document).ready(function($){ $(".wpgraphql-logging-datepicker").datetimepicker({ dateFormat: "yy-mm-dd", timeFormat: "HH:mm:ss" }); });'
@@ -351,7 +352,10 @@ protected function process_log_download(): void {
351352
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'wpgraphql-logging' ) );
352353
}
353354

354-
$log_id = isset( $_GET['log'] ) ? absint( $_GET['log'] ) : 0; // @phpcs:ignore WordPress.Security.NonceVerification.Recommended
355+
$log_id = isset( $_GET['log'] ) ? absint( $_GET['log'] ) : 0;
356+
if ( $log_id > 0 ) {
357+
check_admin_referer( 'wpgraphql-logging-download_' . $log_id );
358+
}
355359
$downloader = new DownloadLogService( $this->get_log_service() );
356360
$downloader->generate_csv( $log_id );
357361
}

plugins/wpgraphql-logging/tests/wpunit/Admin/View/ViewLogsPageTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,20 @@ public function test_get_redirect_url_constructs_correct_url(): void {
214214
$url
215215
);
216216
}
217+
218+
public function test_process_log_download_dies_without_nonce(): void {
219+
$this->set_as_admin();
220+
$instance = ViewLogsPage::init();
221+
$_GET['action'] = 'download';
222+
$_GET['log'] = 'nonexistent-log-id';
223+
ob_start();
224+
$this->expectException(\WPDieException::class);
225+
$this->expectExceptionMessage('Invalid log ID.');
226+
227+
// Use reflection to call the protected method
228+
$reflection = new \ReflectionClass($instance);
229+
$method = $reflection->getMethod('process_log_download');
230+
$method->setAccessible(true);
231+
$method->invoke($instance);
232+
}
217233
}

0 commit comments

Comments
 (0)