Make WordPress Core

Opened 5 months ago

Closed 3 months ago

#65554 closed defect (bug) (fixed)

Connectors: only validate AI provider API keys that were submitted in the request

Reported by: khokansardar Owned by: gziolo
Priority: normal Milestone: 7.2
Component: AI Version: 7.0
Severity: normal Keywords: has-patch has-unit-tests needs-testing
Cc: Focuses: rest-api, performance

Description

_wp_connectors_rest_settings_dispatch() validates connector API keys on rest_post_dispatch, but a POST /wp/v2/settings response returns the full settings object. Every unrelated settings save therefore re-validates each stored AI key, firing a live list-models request per provider — a synchronous external HTTP call on installs without a persistent object cache.
Fix: only validate keys present in the current request (WP_REST_Request::has_param()).

Change History (14)

This ticket was mentioned in ​PR #12350 on ​WordPress/wordpress-develop by ​@khokansardar.


5 months ago
#1

_wp_connectors_rest_settings_dispatch() validated every stored AI provider key on each settings save, because the /wp/v2/settings response always returns the full set of registered settings. Unrelated saves therefore triggered a live validation request to each configured provider.

Validate a key only when it is present in the current request, via WP_REST_Request::has_param(). Masking of stored keys in the response is unchanged.

Props itzmekhokan.
See #65554.

Trac ticket: https://core.trac.wordpress.org/ticket/65554

## Use of AI Tools

#2 follow-up: @sanayasir
5 months ago

Environment: WordPress Local

I tested this issue on a local WordPress installation.

While saving an unrelated setting through the POST /wp/v2/settings REST endpoint, I confirmed that _wp_connectors_rest_settings_dispatch() is still executed during rest_post_dispatch, even though the connector API key is not part of the current request.

This causes validation of the stored OpenAI API key on every unrelated settings save, resulting in unnecessary external API validation requests instead of validating only the keys included in the current request.

Expected Result:
Connector API keys should only be validated when the corresponding key is present in the current request using WP_REST_Request::has_param(). Saving unrelated settings should not trigger API key validation or external HTTP requests.

#3 in reply to: ↑ 2 @khokansardar
5 months ago

Replying to sanayasir:

Environment: WordPress Local

I tested this issue on a local WordPress installation.

While saving an unrelated setting through the POST /wp/v2/settings REST endpoint, I confirmed that _wp_connectors_rest_settings_dispatch() is still executed during rest_post_dispatch, even though the connector API key is not part of the current request.

This causes validation of the stored OpenAI API key on every unrelated settings save, resulting in unnecessary external API validation requests instead of validating only the keys included in the current request.

Expected Result:
Connector API keys should only be validated when the corresponding key is present in the current request using WP_REST_Request::has_param(). Saving unrelated settings should not trigger API key validation or external HTTP requests.

Thanks for testing, @sanayasir!

Just to clarify the behaviour you're seeing: _wp_connectors_rest_settings_dispatch() running on rest_post_dispatch is expected — it's a callback registered on that hook, so it fires for every POST /wp/v2/settings response regardless of which settings were submitted. That part isn't the bug and isn't changed by the patch.

What the patch in ​PR #12350 changes is the validation inside that callback. The key is now only validated when it's actually present in the current request, via WP_REST_Request::has_param():

if ( $is_update
    && $request->has_param( $setting_name )
    && is_string( $value ) && '' !== $value
    && 'ai_provider' === $connector_data['type']
) {
    // validate / list-models request
}

So on an unrelated settings save, has_param() returns false and the branch is skipped — no external validation request is fired for the stored key.

The report reads like the pre-patch behaviour, so it's possible the patch wasn't applied during testing. Could you re-test with PR #12350 applied and confirm whether an external request actually goes out? A reliable way to check is to hook pre_http_request (or watch your request log) and save an unrelated setting:

add_filter( 'pre_http_request', function ( $pre, $args, $url ) {
    error_log( 'Outbound HTTP during save: ' . $url );
    return $pre;
}, 10, 3 );

With the patch applied, saving an unrelated setting should produce no outbound validation request, while submitting an actual provider key should still validate it. This is also covered by the included tests: test_unsubmitted_key_is_not_validated(), test_submitted_invalid_key_is_discarded(), and test_submitted_valid_key_is_preserved_and_masked().

Let me know what you find — happy to dig further if you still see a request going out with the patch applied.

#4 @vedantere
4 months ago

Test Report

Environment: Docker-based local dev environment — PHP 8.5.6, MySQL 9.7, WordPress trunk.

Tested PR #12350 locally. The real AI provider connectors (OpenAI, Anthropic, Google) require companion plugins to be installed before their settings even register, which I don't have in this environment — so a raw end-to-end REST reproduction wasn't practical. Instead, I verified the fix using the unit tests included in the PR, which register a fake in-process connector and don't depend on those plugins.

Before the fix

Applied only the new test file (tests/phpunit/tests/connectors/wpConnectorsRestSettingsDispatch.php) on top of unpatched connectors.php. Result:

`
1) Tests_Connectors_WpConnectorsRestSettingsDispatch::test_unsubmitted_key_is_not_validated
A key absent from the request must not be validated or discarded.
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'a-valid-secret-key'
+
`

This confirms the bug: saving unrelated settings via POST /wp/v2/settings re-validates a stored AI provider key that was never submitted, and wipes it to an empty string when validation fails.

After the fix

Applied the code fix (the $request->has_param( $setting_name ) guard). All 3 new tests passed:

`
OK (3 tests, 6 assertions)
`

Also ran the full connectors suite for regressions:

`
OK (84 tests, 216 assertions)
`

Conclusion

The patch fixes the reported issue — unrelated settings saves no longer touch stored AI provider keys — and doesn't break anything else in the connectors suite. I did not test against a live AI provider (real HTTP call), so I can't confirm the outbound HTTP behavior directly, only the code path that gates it.

#5 @gziolo
3 months ago

#65867 was marked as a duplicate.

This ticket was mentioned in ​PR #13031 on ​WordPress/wordpress-develop by ​@hbhalodia.


3 months ago
#6

Trac ticket: https://core.trac.wordpress.org/ticket/65554

## Description

  • Fixed the issue related to settings check for API key for all connectors, even if the request does not changes or have connector key.
  • This leads to unnecessary calls to provider to check for API key on every settings save via wp/v2/settings.

## Use of AI Tools

  • Yes, Claude Code, Opus 5.
  • Used for drafting the ticket mentioned.
  • Used for adding the unit test for the changes mentioned. Tested locally and reviewed tests by me.

#7 @gziolo
3 months ago

  • Milestone Awaiting Review → 7.2
  • Owner set to gziolo
  • Status new → reviewing

​@gziolo commented on ​PR #12350:


3 months ago
#8

Thanks for working on this. The implementation looks correct, and WP_REST_Request::has_param() clearly expresses that validation should only happen when the API-key setting is present in the request.

PR #13031 independently addresses the same issue. Since this branch was created, Tests_Connectors_WpConnectorsRestSettingsDispatch has been added to trunk, and #13031 extends that existing test suite instead of introducing the file.

Could you rebase this PR on current trunk and integrate these test cases into the existing test class, preserving its application-password coverage? The broader coverage here for omitted, submitted-invalid, and submitted-valid keys is valuable and should be retained. The exact masking assertion from #13031 would also strengthen the omitted-key case.

Given that the two PRs and Trac tickets cover the same behavior, they should also be cross-referenced and consolidated before commit.

​@gziolo commented on ​PR #13031:


3 months ago
#9

Thanks for working on this. I noticed that ​PR #12350, opened earlier for Trac #65554, appears to address the same underlying issue: AI provider API keys should only be validated when they were submitted in the current /wp/v2/settings request.

The regression coverage added here to the existing connectors test suite is valuable and could be carried over to #12350. Could we reconcile the two implementations and continue with one PR to avoid maintaining duplicate fixes?

​@hbhalodia commented on ​PR #13031:


3 months ago
#10

Thanks for working on this. I noticed that ​PR #12350, opened earlier for Trac #65554, appears to address the same underlying issue: AI provider API keys should only be validated when they were submitted in the current /wp/v2/settings request.

The regression coverage added here to the existing connectors test suite is valuable and could be carried over to #12350. Could we reconcile the two implementations and continue with one PR to avoid maintaining duplicate fixes?

Thanks @gziolo, I overlooked and thinked that the issue was not created, hence gone ahead and created the new ticket. Sorry for that.

For the PRs, yes we should accomodate it to the single PR. I would ask PR author if the changes can be ported over to it, so that I can close this and work on the new one.

Unfortunately, I do not have sufficient permission to update directly to that PR.

Cc: @itzmekhokan

​@hbhalodia commented on ​PR #12350:


3 months ago
#11

Hi @itzmekhokan, Could you please check the comment from @gziolo and update the PR with the the regression coverage added here in the PR - ​https://github.com/WordPress/wordpress-develop/pull/13031, I would then close mine and will work on this one.

Thanks,

#12 @hbhalodia
3 months ago

Hi @khokansardar, Do you have bandwidth to continue work on the above PR? Please let us know so that we can accomodate the changes of this PR - ​https://github.com/WordPress/wordpress-develop/pull/13031 into ​https://github.com/WordPress/wordpress-develop/pull/12350 this.

Thanks,

​@khokansardar commented on ​PR #12350:


3 months ago
#13

Thanks @hbhalodia, @gziolo. Rebased on trunk and folded the tests into the existing Tests_Connectors_WpConnectorsRestSettingsDispatch, keeping the application-password coverage and carrying over the regression test from #13031 including its masking assertion. Kept WP_REST_Request::has_param() for the guard. Confirmed the settings route carries no default in its endpoint args, so has_param() reflects only what the request actually submitted.

#14 @gziolo
3 months ago

  • Resolution → fixed
  • Status reviewing → closed

In 63348:

Connectors: Only validate submitted AI provider API keys

_wp_connectors_rest_settings_dispatch() validated every stored AI provider API key on each /wp/v2/settings update, because the response always contains the full set of registered settings. Saving an unrelated setting therefore triggered a live validation request to each configured provider, and a key that failed validation at that moment was silently reset.

Validate a key only when it is present in the current request, using WP_REST_Request::has_param(). Masking of stored keys in the response is unchanged.

Add test coverage for omitted, submitted-invalid, and submitted-valid keys to the existing Tests_Connectors_WpConnectorsRestSettingsDispatch suite.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/12350

Props khokansardar, hbhalodia, gziolo.
Fixes #65554. See #65867.

Note: See TracTickets for help on using tickets.