-
Notifications
You must be signed in to change notification settings - Fork 157
test[faustwp]: regression test for hash_equals secret-key comparison (follow-up to #2312) #2362
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
Merged
josephfusco
merged 3 commits into
canary
from
fix/faustwp-security-hardening-with-tests
May 15, 2026
+173
−0
Merged
Changes from all commits
Commits
Show all changes
3 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
173 changes: 173 additions & 0 deletions
173
plugins/faustwp/tests/integration/RestCallbacksTests.php
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,173 @@ | ||
| <?php | ||
| /** | ||
| * Class RestCallbacksTests | ||
| * | ||
| * @package FaustWP | ||
| */ | ||
|
|
||
| namespace WPE\FaustWP\Tests\Integration; | ||
|
|
||
| use function WPE\FaustWP\Settings\faustwp_update_setting; | ||
| use function WPE\FaustWP\REST\rest_authorize_permission_callback; | ||
| use function WPE\FaustWP\REST\wpac_authorize_permission_callback; | ||
|
|
||
| /** | ||
| * Regression tests for the REST permission callbacks in | ||
| * plugins/faustwp/includes/rest/callbacks.php. | ||
| * | ||
| * Specifically guards the timing-safe secret-key comparison fix for #2310, | ||
| * plus provides behavioural coverage for the wider contract (no coverage | ||
| * existed previously for either callback). | ||
| * | ||
| * @group rest | ||
| * @group auth | ||
| */ | ||
| class RestCallbacksTests extends \WP_UnitTestCase { | ||
|
|
||
| const VALID_SECRET = '00000000-0000-4000-8000-000000000001'; | ||
| const WRONG_SECRET = 'definitely-not-the-right-key'; | ||
|
|
||
| public function setUp(): void { | ||
| parent::setUp(); | ||
| faustwp_update_setting( 'secret_key', self::VALID_SECRET ); | ||
| } | ||
|
|
||
| private function make_request_with_faust_header( $value ): \WP_REST_Request { | ||
| $request = new \WP_REST_Request( 'POST', '/faustwp/v1/authorize' ); | ||
| if ( null !== $value ) { | ||
| $request->set_header( 'x-faustwp-secret', $value ); | ||
| } | ||
| return $request; | ||
| } | ||
|
|
||
| private function make_request_with_wpe_header( $value ): \WP_REST_Request { | ||
| $request = new \WP_REST_Request( 'POST', '/wpac/v1/authorize' ); | ||
| if ( null !== $value ) { | ||
| $request->set_header( 'x-wpe-headless-secret', $value ); | ||
| } | ||
| return $request; | ||
| } | ||
|
|
||
| // ----- rest_authorize_permission_callback (the current x-faustwp-secret route) ----- | ||
|
|
||
| /** | ||
| * Happy path: configured secret matches the request header → authorized. | ||
| */ | ||
| public function test_rest_authorize_returns_true_when_header_matches_secret(): void { | ||
| $request = $this->make_request_with_faust_header( self::VALID_SECRET ); | ||
|
|
||
| $this->assertTrue( rest_authorize_permission_callback( $request ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Wrong header value must be rejected. With hash_equals() this is a | ||
| * constant-time false; the previous `===` check would have returned | ||
| * the same boolean but via a fast short-circuit (the bug fixed by #2310). | ||
| */ | ||
| public function test_rest_authorize_returns_false_when_header_value_differs(): void { | ||
| $request = $this->make_request_with_faust_header( self::WRONG_SECRET ); | ||
|
|
||
| $this->assertFalse( rest_authorize_permission_callback( $request ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Missing x-faustwp-secret header → unauthorized. Guards the early return | ||
| * at `if ( $secret_key && $header_key )`. | ||
| */ | ||
| public function test_rest_authorize_returns_false_when_header_is_missing(): void { | ||
| $request = $this->make_request_with_faust_header( null ); | ||
|
|
||
| $this->assertFalse( rest_authorize_permission_callback( $request ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Empty-string header → unauthorized. Guards against a downstream caller | ||
| * sending `x-faustwp-secret: ` and accidentally being treated as authorized. | ||
| */ | ||
| public function test_rest_authorize_returns_false_when_header_is_empty_string(): void { | ||
| $request = $this->make_request_with_faust_header( '' ); | ||
|
|
||
| $this->assertFalse( rest_authorize_permission_callback( $request ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Server-side secret unset → unauthorized regardless of what the client sends. | ||
| * Guards the early return for unconfigured installs. | ||
| * | ||
| * Note: we delete the option directly rather than calling | ||
| * faustwp_update_setting('secret_key', '') because the | ||
| * sanitize_option_faustwp_settings filter (at includes/settings/callbacks.php) | ||
| * silently restores the previous value when the new one isn't a valid UUID, | ||
| * so a string clear wouldn't actually clear it. | ||
| */ | ||
| public function test_rest_authorize_returns_false_when_secret_is_unset(): void { | ||
| delete_option( 'faustwp_settings' ); | ||
| $request = $this->make_request_with_faust_header( self::VALID_SECRET ); | ||
|
|
||
| $this->assertFalse( rest_authorize_permission_callback( $request ) ); | ||
| } | ||
|
|
||
| // ----- wpac_authorize_permission_callback (the deprecated x-wpe-headless-secret route) ----- | ||
|
|
||
| /** | ||
| * The deprecated route still authorizes on a matching x-wpe-headless-secret. | ||
| * Kept under test until the route is removed so the deprecation period | ||
| * doesn't accidentally break older clients. | ||
| */ | ||
| public function test_wpac_authorize_returns_true_when_header_matches_secret(): void { | ||
| $request = $this->make_request_with_wpe_header( self::VALID_SECRET ); | ||
|
|
||
| $this->assertTrue( wpac_authorize_permission_callback( $request ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Wrong header value rejected by the deprecated route too. | ||
| */ | ||
| public function test_wpac_authorize_returns_false_when_header_value_differs(): void { | ||
| $request = $this->make_request_with_wpe_header( self::WRONG_SECRET ); | ||
|
|
||
| $this->assertFalse( wpac_authorize_permission_callback( $request ) ); | ||
| } | ||
|
|
||
| // ----- timing-safe-comparison regression guard (source-level) ----- | ||
|
|
||
| /** | ||
| * Source-level guard for #2310: the three secret-key comparisons in this | ||
| * codebase must use hash_equals(), not `===`/`!==`. Behavioural tests above | ||
| * cannot distinguish hash_equals() from `===` (both return the same boolean | ||
| * for valid/invalid inputs); the distinction is timing-safety, which can't | ||
| * be reliably asserted in unit tests. So we assert at the source level that | ||
| * the known-bad shapes are not present and hash_equals() is. | ||
| * | ||
| * Narrowly-scoped to the exact patterns that the #2310 fix removed — false | ||
| * positives are bounded to a future contributor reintroducing those exact | ||
| * literal expressions, which is exactly the regression we want to catch. | ||
| */ | ||
| public function test_secret_comparisons_use_constant_time_hash_equals(): void { | ||
| $rest_callbacks = file_get_contents( dirname( __DIR__, 2 ) . '/includes/rest/callbacks.php' ); | ||
| $graphql_callbacks = file_get_contents( dirname( __DIR__, 2 ) . '/includes/graphql/callbacks.php' ); | ||
|
|
||
| $this->assertNotFalse( $rest_callbacks, 'Failed to read includes/rest/callbacks.php for regression guard.' ); | ||
| $this->assertNotFalse( $graphql_callbacks, 'Failed to read includes/graphql/callbacks.php for regression guard.' ); | ||
|
|
||
| // The bad comparison shapes this PR replaces (two distinct patterns, three | ||
| // call sites: '=== $header_key' covers both rest_authorize_permission_callback | ||
| // and wpac_authorize_permission_callback; '!== $_SERVER[...]' covers | ||
| // filter_introspection): | ||
| $this->assertStringNotContainsString( '=== $header_key', $rest_callbacks, | ||
| 'rest_authorize_permission_callback must use hash_equals(), not ===.' ); | ||
| $this->assertStringNotContainsString( "!== \$_SERVER['HTTP_X_FAUST_SECRET']", $graphql_callbacks, | ||
| 'filter_introspection must use hash_equals(), not !==.' ); | ||
|
|
||
| // And the positive: both files must contain hash_equals(). Use >= 1 rather | ||
| // than a hardcoded count so a future contributor adding a legitimate third | ||
| // hash_equals call (e.g. for a new permission callback) doesn't trip a | ||
| // false-positive "security regression". The not-contains assertions above | ||
| // are the real revert guards; these are affirmative checks that the timing- | ||
| // safe primitive is still present somewhere. | ||
| $this->assertGreaterThanOrEqual( 1, substr_count( $rest_callbacks, 'hash_equals' ), | ||
| 'rest/callbacks.php must contain at least one hash_equals call.' ); | ||
| $this->assertStringContainsString( 'hash_equals', $graphql_callbacks, | ||
| 'filter_introspection must use hash_equals.' ); | ||
|
josephfusco marked this conversation as resolved.
|
||
| } | ||
| } | ||
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.