Skip to content

Commit

Permalink
Allow for flexibility and including /wp-json in the allow/deny lists
Browse files Browse the repository at this point in the history
  • Loading branch information
srtfisher committed Jan 12, 2024
1 parent 7b40cfe commit 61a3549
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to `wp-rest-guard` will be documented in this file.
## v1.0.4 - 2024-01-12

- Fixing an issue splitting lines by `\n` instead of `\r\n` on Windows.
- Allow `/wp-json/` to be included in the allow/deny lists.

## v1.0.3 - 2023-08-28

Expand Down
12 changes: 10 additions & 2 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ function should_prevent_anonymous_access( WP_REST_Server $server, WP_REST_Reques
return true;
}

// todo: check settings.

/**
* Filter the allowlist for allowed anonymous requests.
*
Expand All @@ -108,6 +106,11 @@ function should_prevent_anonymous_access( WP_REST_Server $server, WP_REST_Reques
}

foreach ( $allowlist as $allowlist_endpoint ) {
// Strip off /wp-json from the beginning of the endpoint if it was included.
if ( 0 === strpos( $allowlist_endpoint, '/wp-json' ) ) {
$allowlist_endpoint = substr( $allowlist_endpoint, 8 );
}

if ( preg_match( '/' . str_replace( '\*', '.*', preg_quote( $allowlist_endpoint, '/' ) ) . '/', $endpoint ) ) {
return false;
}
Expand All @@ -131,6 +134,11 @@ function should_prevent_anonymous_access( WP_REST_Server $server, WP_REST_Reques
}

foreach ( $denylist as $denylist_endpoint ) {
// Strip off /wp-json from the beginning of the endpoint if it was included.
if ( 0 === strpos( $denylist_endpoint, '/wp-json' ) ) {
$denylist_endpoint = substr( $denylist_endpoint, 8 );
}

if ( preg_match( '/' . str_replace( '\*', '.*', preg_quote( $denylist_endpoint, '/' ) ) . '/', $endpoint ) ) {
return true;
}
Expand Down

0 comments on commit 61a3549

Please sign in to comment.