Skip to content

feat: move Key Encryption onto Secrets API - #1107

Merged
dkotter merged 32 commits into
WordPress:developfrom
Pranjal1423:feat/secrets-api-integration
Oct 27, 2026
Merged

dkotter merged 32 commits into
WordPress:developfrom
Pranjal1423:feat/secrets-api-integration

Conversation

@Pranjal1423

@Pranjal1423 Pranjal1423 commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

What?

Closes #1012

Moves the Key Encryption experiment onto the proposed WordPress Secrets API feature plugin (slated for WordPress 7.2 Core).

Why?

The Key Encryption experiment originally vendored a subset of ericmann/displace-secrets-manager. That upstream repository has been archived and superseded by ericmann/secrets-api.

To align with the WordPress 7.2 Core roadmap and avoid data loss for existing sites:

  1. Existing stored connector keys (_secret_ai/*) need an automated migration because the new Secrets API prototype fallback only maps unnamespaced names.
  2. The AI plugin should take advantage of wp_get_secret() and friends when present, while retaining the bundled internal provider as a zero-dependency fallback so the "zero-install" experience remains intact.

How?

  1. Secrets_Bridge Integration:

    • Added get_secret(), set_secret(), and delete_secret() methods to Secrets_Bridge.
    • Delegates transparently to wp_get_secret(), wp_set_secret(), and wp_delete_secret() under the ai/ namespace with caller context ['plugin' => 'ai'].
    • Preserves backward compatibility by falling back to the bundled provider if the feature plugin or core functions are not present.
    • Includes transparent read-time promotion if a legacy secret is accessed before the upgrade routine runs.
  2. Automated Migration (V1_5_0):

    • Added V1_5_0 upgrade routine in includes/Admin/Upgrades/V1_5_0.php and registered it in Upgrades.php.
    • Safely decrypts legacy _secret_ai/* options into memory, writes to wp_set_secret(), verifies read-back matches, and only then deletes the legacy option row.
  3. Uninstall Cleanup:

    • Cleans up active secrets stored in the Secrets API via wp_delete_secret() upon plugin uninstallation.
  4. Tests & Docs:

    • Added V1_5_0Test.php and updated Key_EncryptionTest.php.
    • Updated docs/experiments/key-encryption.md and includes/Vendor/Secrets/README.md.

Use of AI Tools

AI assistance: Yes
Tool(s): Google Antigravity
Model(s): Gemini 3.8 Flash
Used for: Implementation, migration routine, and integration tests;
I have reviewed and verified it locally with unit tests and static analysis.

Testing Instructions

  1. In a clean environment with Key Encryption enabled, verify connector API keys are encrypted at rest.
  2. When the Secrets API plugin is active, verify keys are stored and retrieved via wp_get_secret().
  3. Test upgrading a site with existing _secret_ai/* rows: verify the V1_5_0 migration moves keys to the new Secrets API without data loss or user intervention.
  4. Verify deactivation / opt-out restores plaintext credentials via decrypt_all().

Changelog Entry

Changed - Move Key Encryption experiment onto the Secrets API feature plugin with backward-compatible fallback and migration.

Open WordPress Playground Preview

@Pranjal1423
Pranjal1423 requested a review from a team October 5, 2026 07:00
@Pranjal1423
Pranjal1423 requested a review from jeffpaul as a code owner October 5, 2026 07:00
@Pranjal1423
Pranjal1423 marked this pull request as draft October 5, 2026 07:00
@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: Pranjal1423 <pranjal4804@git.wordpress.org>
Co-authored-by: jeffpaul <jeffpaul@git.wordpress.org>
Co-authored-by: whyisjake <whyisjake@git.wordpress.org>
Co-authored-by: dkotter <dkotter@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@Pranjal1423
Pranjal1423 marked this pull request as ready for review October 5, 2026 07:58
@dkotter dkotter added this to the 1.5.0 milestone Oct 5, 2026

@dkotter dkotter left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm following correctly, looks like the changes here require someone to manually install the new Secrets API plugin, correct? If so, that's not the direction we were wanting to go, ideally we can bundle that plugin ourselves, matching what we already do with the older version of that plugin.

@Pranjal1423

Copy link
Copy Markdown
Contributor Author

Thanks for clarifying, @dkotter! That makes sense.

I’ll update the PR to bundle the new Secrets API (ericmann/secrets-api) under includes/Vendor/Secrets/, similar to how the older version was vendored.

I’ll:

  • Bundle the new API so the plugin works out of the box without any external dependency.
  • Update Secrets_Bridge to use the bundled API.
  • Keep the existing migration in place so the encrypted keys are safely migrated to the new format.

I’ll push the updated changes shortly!

@codecov

codecov Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.14729% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.12%. Comparing base (8288aea) to head (844f6b3).

Files with missing lines Patch % Lines
...udes/Experiments/Key_Encryption/Secrets_Bridge.php 90.17% 11 Missing ⚠️
includes/Admin/Uninstall.php 90.00% 1 Missing ⚠️
includes/Admin/Upgrades/V1_5_0.php 80.00% 1 Missing ⚠️
...udes/Experiments/Key_Encryption/Key_Encryption.php 50.00% 1 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##             develop    #1107      +/-   ##
=============================================
+ Coverage      85.11%   85.12%   +0.01%     
- Complexity      4361     4403      +42     
=============================================
  Files            172      173       +1     
  Lines          16127    16213      +86     
=============================================
+ Hits           13726    13801      +75     
- Misses          2401     2412      +11     
Flag Coverage Δ
unit 85.12% <89.14%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Pranjal1423
Pranjal1423 requested a review from dkotter October 7, 2026 08:51

@dkotter dkotter left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a quick review, this seems to be headed in the right direction, thanks for all the work here!

As I continue to test, one initial question is are we purposely leaving the old Displace plugin code behind? Is there a reason to keep that? I guess I would expect for all of those files to be removed and replaced with the new code

@dkotter

dkotter commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

One other quick thing as I'm reviewing, the Secrets API made it's way into WordPress Core just a few days ago. We should ensure the code we are shipping here includes the latest version of that, noting the code hosted at https://github.com/ericmann/secrets-api may not be the exact same code that was shipped in Core. Should look at the upstream commits to Core to see if there's anything new we need to pull in

@Pranjal1423

Copy link
Copy Markdown
Contributor Author

Thank you @dkotter and @whyisjake for the thorough review and testing against Core trunk!

All points are completely valid:

  1. Remove Old Displace Code & Sync with Core Trunk:

    • I will remove the remaining legacy Displace vendor files to avoid dead code.
    • I'll sync our bundled Secrets API under includes/Vendor/Secrets/ with the latest changes that landed in Core trunk (Trac #66187 / 5744bc5803).
  2. Handle WP_Error & Unreadable Secrets:

    • I will apply the suggestion to track unreadable secrets via get_unreadable_secrets() and warn via wp_trigger_error() rather than treating errors as empty values.
  3. Verify Read-Back Before Deleting Legacy Keys:

    • I'll introduce the write_and_verify() helper and reuse it across get_secret(), set_secret(), and migration routines to guarantee keys read back intact with hash_equals() before deleting legacy rows.
  4. Namespace-wide Uninstall Cleanup:

    • I'll update Uninstall.php to use wp_list_secrets( Secrets_Bridge::SECRET_NAMESPACE ) so drop-in stored and unregistered connector keys are cleanly removed.
  5. Tests & Coverage:

    • I will incorporate the suggested integration tests, which should also resolve the Codecov patch coverage check.

I'll push an update with these changes shortly!

@whyisjake whyisjake left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Pranjal1423, the unreadable-secret handling, the read-back check, the namespace-wide uninstall and the tests all look right. CI is green across the matrix, and locally against trunk (7.2-alpha-64345) the Key Encryption, Uninstall and Upgrade tests pass (116 tests; the 3 skips are multisite-only).

Two things are still open from the last round:

1. The bundled Secrets API isn't synced with core yet. includes/Vendor/Secrets/ is unchanged. I compared it file by file with src/wp-includes/secrets.php and src/wp-includes/secrets/ on trunk (r64337 plus the follow-up 7a47d39442).

  • Storage looks compatible. The cipher, record shape, option names and key derivation match, so a secret written by the bundled copy on 7.0.x should read under core after an upgrade. That's from reading the code; I haven't run a real upgrade.
  • Core has changes the bundled copy lacks:
    • wp_secret_changed passes a 7th $network argument on set, delete and retire.
    • decode_canonical_base64_32() replaces is_canonical_base64_32() plus base64_decode() in WP_Secrets_Config_Key_Provider.
    • The slot_fingerprint() helper in WP_Secrets_Libsodium_Provider, used by retire_previous().
    • demote_slot() type-checks created and needs_rotation.
    • list_secrets() checks that current is an array, and uses str_starts_with().
    • The key manager's root-key cache check is on cached_root_key, not cached_wrapped.
    • list_names() in the option store skips option names that aren't strings.
  • Other differences: no scalar type declarations, 'default' text domains, -- in place of en dashes, ABSPATH guards and some extra is_string() / is_int() checks. Some of these are needed for PHP 7.4 or Plugin Check, but it'd help to say in the README which are intentional.

2. Careful with removing the old Displace code. set_secret() still falls back to Secrets::set(), and get_secret() and the V1_5_0 migration both still decrypt legacy _secret_ai/* rows. Whatever goes, the legacy read path has to stay until we drop migration support, or sites that haven't migrated lose their keys.

Not blocking: turning Key Encryption off with an unreadable key keeps the stored secret and logs a warning, but the connector field just shows blank. get_unreadable_secrets() has no callers yet, so an admin notice could use it in a follow-up.

Offer: I have some time right now, so I'll open a stacked PR against your branch that does the vendor sync (item 1). You can review and merge it into this PR, or cherry-pick from it. I'll leave the Displace cleanup to you unless you'd rather I take that too.

AI assistance: Yes. Tool(s): Claude Code. Used for: comparing the bundled Secrets API against core trunk, running the PHPUnit suites, and drafting this review.

Replaces the `src/wp-includes/` copy of the Secrets API, taken from
ericmann/secrets-api, with core trunk at r64344, the follow-up to the
API's introduction in r64337 (#66187). Two changes are kept from the old
copy, for Plugin Check: the `ABSPATH` guard, and the 'default' text
domain on translation calls.

This brings in core's scalar type declarations, the `$network` argument
to `wp_secret_changed`, `decode_canonical_base64_32()`,
`slot_fingerprint()`, and the stricter record checks in
`demote_slot()`, `list_secrets()` and `list_names()`. The stored record
format is unchanged.

The `plugin/` adapters still come from ericmann/secrets-api, since they
are not part of core.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@whyisjake

Copy link
Copy Markdown
Member

@Pranjal1423 the vendor sync is up as a stacked PR against your branch: Pranjal1423#1. It's core trunk r64344 with only the ABSPATH guard and the 'default' text domain added, and it includes a 7.0.4 → trunk check that keys carry over. Merge it there and it lands in this PR. The Displace cleanup is still yours.

@Pranjal1423

Copy link
Copy Markdown
Contributor Author

Thanks @whyisjake, I've merged the vendor sync PR!

Regarding the Displace files: since Secrets::get() and the encrypted options provider are still actively required for the 1.5.0 migration and read-time fallback, I think it's safest to keep those files as-is for now until we drop migration support in a future release so existing sites don't lose their keys.

However, if you feel we should still prune or clean up parts of it for this PR, please let me know and I'd be happy to work on it!

whyisjake and others added 2 commits October 16, 2026 09:02
Resolves the conflict with WordPress#1186 in Secrets_Bridge and its tests:
- keeps this branch's get_secret()/set_secret()/delete_secret();
- on_write() keeps the plaintext and deletes a stale secret when a key
  can't be stored, as WordPress#1186 does;
- the legacy Secrets::set() fallback no longer throws when the master
  key can't be decrypted;
- WordPress#1186's tests break core's root key as well as the legacy master key,
  since writes go through the Secrets API first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e's legacy reader

Keys stored before 1.5.0 are read once with the bundled read-only
Secrets_API_Legacy_Reader and moved to the Secrets API, so the
Displace classes and their tests can go. set_secret() no longer
falls back to writing a legacy row. WordPress#1186's tests now break only
core's root key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@whyisjake

Copy link
Copy Markdown
Member

@Pranjal1423 two updates on your branch:

  1. Merged develop (5d889db), which clears the conflict with Fix: Key Encryption: undecryptable secrets cause a fatal error on every request, and deactivation fails #1186. It keeps this branch's get_secret()/set_secret(), and adopts Fix: Key Encryption: undecryptable secrets cause a fatal error on every request, and deactivation fails #1186's behavior: a key that can't be stored stays plaintext, and the stale secret is deleted. It also catches an exception the Displace fallback in set_secret() could throw when the master key can't be decrypted, the same fatal Fix: Key Encryption: undecryptable secrets cause a fatal error on every request, and deactivation fails #1186 fixed. Fix: Key Encryption: undecryptable secrets cause a fatal error on every request, and deactivation fails #1186's tests now break both the Displace master key and core's root key.
  2. The Displace cleanup is a stacked PR: Key Encryption: drop the bundled Displace Secrets Manager Pranjal1423/ai#2. You're right that the legacy read path has to stay, but the bundled Secrets_API_Legacy_Reader already reads the Displace format, so the Displace classes can go (−2,184 lines). It's tested with a real upgrade from develop on trunk and 7.0.4. Merge it there if it looks right to you.

AI assistance: Yes. Tool(s): Claude Code. Used for: resolving the merge, writing the cleanup and its tests, and running the checks.

Key Encryption: drop the bundled Displace Secrets Manager
@Pranjal1423

Copy link
Copy Markdown
Contributor Author

Thank you so much @whyisjake!

I've reviewed and merged your stacked PR (#2). Using Secrets_API_Legacy_Reader to keep pre-1.5.0 keys readable while cleanly dropping the 7 Displace classes is the perfect solution.

The branch is now fully synced with core trunk and the legacy Displace code is dropped. All checks and tests are ready for final review!

@Pranjal1423
Pranjal1423 requested a review from whyisjake October 19, 2026 06:35
@jeffpaul

Copy link
Copy Markdown
Member

@whyisjake @dkotter back to y'all for final review ahead of potential merge into 1.5.0 this week

@dkotter
dkotter merged commit c0b53b1 into WordPress:develop Oct 27, 2026
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Move the Key Encryption experiment onto the new Secrets API feature plugin

4 participants