Skip to content

Commit 44292ad

Browse files
committed
Accept unnamespaced names; make the prototype mapping exact
Operator decision, and it replaces the loosest thing in the build. wp_get_secret( 'api_key' ) is now a legal call that reports through _doing_it_wrong(). That contradicts the build brief's §5.5 ("no unnamespaced form and no escape hatch for one") deliberately: code written against the Displace prototype uses a flat keyspace, and refusing those names means every call site must be rewritten before any of it can be ported. Accepting them makes the move incremental, and the notice becomes a running list of which call sites still need a namespace. The relaxation is narrow -- it drops the "exactly one slash" rule and nothing else. Character rules, length cap, and rejection of uppercase/spaces/dots all still apply, and two-plus slashes are still invalid. That in turn removes the reason the prototype fallback store existed in its old shape. It used to drop the namespace, so wp_get_secret( 'anything/api_key' ) inherited Displace's 'api_key'. Now the correspondence is identity: an unnamespaced name reaches its own prototype key, a namespaced name reaches nothing. Nothing is rewritten or inferred, and a caller can no longer end up silently wired to prototype data through a name it did not choose. The migrator's default namespace changes from 'legacy' to none, which is not cosmetic. If it kept prefixing, `migrate-legacy` would write 'legacy/api_key' while a plain wp_get_secret( 'api_key' ) still missed, fell through to the untouched prototype row, and upgraded it a second time -- one prototype secret, two current-format records, diverging the moment either was rotated. Pinned by test_migrating_then_reading_does_not_produce_a_second_copy(), which asserts exactly one _wp_secret_ row survives both paths. --namespace and --map remain for putting a migrated secret somewhere specific. Test churn worth noting: a number of tests used an unnamespaced name as their example of an *invalid* name, which is no longer true. Those now use a name that is invalid on its own terms ('Not A Valid Name') so they keep testing rejection rather than accidentally testing the new notice path. The validate-name provider also had two array entries keyed 'two slashes', so one was silently overwriting the other -- caught while editing it, now one case plus new coverage for the accepted form. Plugin URI points at ericmann/secrets-api rather than a WordPress/secrets-api that does not exist. Recorded as open question #17, including the consequences that are easy to miss: an unnamespaced name gives a future cross-namespace access check nothing to check against, and two plugins both choosing 'api-key' collide silently. 414 tests, 937 assertions single-site, 949 multisite. Lint, compat, phpstan clean.
1 parent cc26c61 commit 44292ad

17 files changed

Lines changed: 398 additions & 109 deletions

‎cli/class-wp-cli-secret-command.php‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,9 @@ public function import_option( $args ) {
368368
* validate, e.g. --map=api_key:myplugin/api-key,other:myplugin/other-key.
369369
*
370370
* [--namespace=<namespace>]
371-
* : Namespace prefixed onto a legacy key with no --map entry.
372-
* ---
373-
* default: legacy
374-
* ---
371+
* : Namespace prefixed onto a legacy key with no --map entry. Defaults to
372+
* none, keeping the key exactly as the prototype spelled it, which is the
373+
* same name a plain wp_get_secret() would upgrade it to on first read.
375374
*
376375
* [--format=<format>]
377376
* ---
@@ -411,7 +410,7 @@ public function migrate_legacy( $args, $assoc_args ) {
411410
'dry_run' => isset( $assoc_args['dry-run'] ),
412411
'name' => isset( $assoc_args['name'] ) ? $assoc_args['name'] : null,
413412
'map' => $map,
414-
'namespace' => isset( $assoc_args['namespace'] ) ? $assoc_args['namespace'] : 'legacy',
413+
'namespace' => isset( $assoc_args['namespace'] ) ? $assoc_args['namespace'] : '',
415414
)
416415
);
417416

‎docs/migrating-from-displace.md‎

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,43 @@ and moving it into a new envelope does not undo wherever it has already been —
2727
plugin. Both systems keep working, indefinitely, on the same site — enforced, not just intended:
2828
see `test_never_writes_to_a_prototype_owned_option()`.
2929

30-
## The one thing to get right: namespace
30+
## Keep calling it what you always called it
3131

32-
A Displace key like `api_key` is unnamespaced. The upgrade path is not fussy about which
33-
namespace claims it — `wp_get_secret( 'anything/api_key' )` inherits the same row. This is
34-
deliberate, not an oversight: Displace's own keyspace was already global, so any plugin could
35-
already read any Displace secret by its bare key. Requiring an exact namespace match here would
36-
just mean the adopting plugin has to guess right, and guessing wrong reintroduces the broken-site
37-
outcome this exists to prevent. Pick whatever namespace makes sense for your plugin and use it
38-
consistently; the first read under that name is what does the upgrading.
32+
Displace keys are unnamespaced (`api_key`). The Secrets API prefers namespaced names
33+
(`plugin-slug/secret-name`), but it **accepts the unnamespaced form** precisely so that porting
34+
does not have to be all-or-nothing: `wp_get_secret( 'api_key' )` is a legal call that returns the
35+
value Displace's `get_secret( 'api_key' )` returned. It reports through `_doing_it_wrong()`, so
36+
the notice tells you which call sites still need a namespace, and nothing breaks while you work
37+
through them.
38+
39+
The correspondence is exact, in both directions:
40+
41+
| Call | Reads Displace's `_secret_api_key`? |
42+
|---|---|
43+
| `wp_get_secret( 'api_key' )` | Yes — same name, same secret |
44+
| `wp_get_secret( 'myplugin/api_key' )` | No — Displace had no namespaces, so it never owned this name |
45+
46+
Nothing is rewritten or guessed on your behalf. An earlier revision of this plugin dropped the
47+
namespace instead, so that `wp_get_secret( 'anything/api_key' )` inherited Displace's `api_key`.
48+
That was removed: it meant any namespace could silently claim any Displace row, and gave a caller
49+
no way to tell which of its names were quietly wired to prototype data.
50+
51+
**So: adopt the API first, namespace second.** Change `get_secret( 'api_key' )` to
52+
`wp_get_secret( 'api_key' )` and it works immediately. When you are ready to take a namespace,
53+
`wp secret migrate-legacy --map=api_key:myplugin/api-key` moves it, or set the new value
54+
explicitly under the name you want.
3955

4056
## If you want it done in bulk instead of lazily
4157

4258
`wp secret migrate-legacy` copies every (or one named) Displace secret into the current format
43-
up front, without waiting for a first read. It is strictly additive — it writes new-format
59+
up front, without waiting for a first read. By default it keeps each key exactly as Displace
60+
spelled it — deliberately, since that is the same name a plain `wp_get_secret()` would upgrade it
61+
to, and a different default would leave the same secret in two current-format records that
62+
diverge the moment either is rotated. It is strictly additive — it writes new-format
4463
records and never touches, and cannot delete, a Displace-owned row. `--dry-run` reports what it
45-
would do without writing anything; `--map=<old>:<new>` gives an explicit new name to a key whose
46-
derived name (`legacy/<key>`, or `--namespace=<ns>/<key>`) would not pass validation, most often
47-
because Displace allowed keys this API's naming rules do not.
64+
would do without writing anything; `--namespace=<ns>` and `--map=<old>:<new>` both put the migrated secret somewhere specific,
65+
which is also how you resolve a key whose name Displace allowed but this API's rules do not
66+
(anything with uppercase, spaces, or dots).
4867

4968
If the WordPress AI plugin's own vendored copy of the prototype's code is detected on the site,
5069
the command warns loudly: that plugin is still reading its own copy of the row you just copied,

‎docs/open-questions.md‎

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ hardens**, because names are the hardest thing to change after adoption.
9393
| `secret_invalid_argument` (`WP_SECRETS_ERROR_INVALID_ARGUMENT`) | Added when #12 converted caller-error throws to WP_Error; the proposal's error list predates that decision | A genuinely new error *string*, not just a new constant name — the only one on this list that is |
9494
| `WP_SECRETS_CAP_MANAGE` / `WP_SECRETS_CAP_MANAGE_NETWORK` | Wrappers over the two published capability strings | Strings match the proposal exactly (`manage_secrets`, `manage_network_secrets`); only the constant names are new |
9595
| `WP_SECRETS_MAX_NAME_LENGTH` (172), `WP_SECRETS_RECORD_VERSION` (1) | A cap and a format version both have to exist | 172 is derived, not published — see the note under §5.5 in the build brief about subtracting the option prefix from 191 |
96+
| Accepting unnamespaced names at all | Adoption path for prototype-era code; see #17 | Contradicts the brief's §5.5 outright. The proposal never discusses naming, so this is unpublished either way -- but it is the one place the implementation deliberately loosened a stated rule rather than adding to it |
9697
| The `wp-content/secrets.php` drop-in filename, and `$GLOBALS['wp_secrets_store']` / `$GLOBALS['wp_secrets_keyring']` | The proposal describes drop-in-style replacement without naming the file or the mechanism | A drop-in filename is effectively permanent once hosts ship against it, in the same way `object-cache.php` is |
9798

9899
Not on this list, deliberately: `Secrets_API_Legacy_Reader`, `Secrets_API_Migrator`, and
@@ -350,12 +351,11 @@ returns here. So an AI-plugin site upgrades one secret at a time, on first use,
350351
nobody running anything -- which is the outcome that actually minimises broken
351352
sites. It is one-way, one-time, and flagged `needs_rotation`.
352353

353-
The mapping is namespace-agnostic: `wp_get_secret( 'ai/api_key' )` inherits the
354-
prototype's `api_key`. Requiring an exact namespace match would mean guessing which
355-
namespace the adopting plugin picks, and guessing wrong is the broken site we are
356-
trying to avoid. This is not a new exposure -- the prototype's keyspace is already
357-
global, so any plugin could read any prototype secret by bare key -- but it is worth
358-
knowing, and it is why the inherited copy is always a new row rather than a move.
354+
The mapping is exact: `wp_get_secret( 'api_key' )` inherits the prototype's
355+
`api_key`, and a namespaced name inherits nothing, because the prototype had no
356+
namespaces and so never owned that name. An earlier revision dropped the namespace
357+
instead, letting any namespace claim any prototype row; that was removed in favour
358+
of accepting the unnamespaced form outright — see #17.
359359

360360
**Non-interference is the load-bearing promise.** The prototype's rows are never
361361
written to or deleted, by anything, ever. The two option namespaces do not overlap
@@ -416,6 +416,38 @@ install` inside an already-running container) rather than this one.
416416

417417
---
418418

419+
## 17. 🟢 Unnamespaced secret names are accepted — CLOSED
420+
421+
**Operator decision: `wp_get_secret( 'api_key' )` works, and reports through
422+
`_doing_it_wrong()`.** This contradicts the build brief's §5.5 ("There is no unnamespaced form
423+
and no escape hatch for one"), deliberately.
424+
425+
The reasoning is adoption, the same as everything else in #15. Code written against the Displace
426+
prototype uses a flat keyspace. Refusing those names means every call site has to be rewritten
427+
before *any* of it can be ported, which makes the move all-or-nothing for exactly the team we are
428+
trying not to break. Accepting them makes it incremental: swap `get_secret()` for
429+
`wp_get_secret()`, keep the key, add the namespace later — and the notice is a running list of
430+
which call sites still need one.
431+
432+
What this replaced is worth recording, because it was worse. The prototype fallback store used to
433+
map `namespace/key` onto the prototype's `key` by dropping the namespace, so
434+
`wp_get_secret( 'anything/api_key' )` inherited it. That worked and was not a new exposure (the
435+
prototype's keyspace was already global), but any namespace could silently claim any prototype
436+
row, and a caller had no way to see which of its names were wired to prototype data. Making the
437+
unnamespaced form legal removed the need for the rewrite entirely.
438+
439+
**The relaxation is narrow.** It drops the "exactly one slash" requirement and nothing else: an
440+
unnamespaced name is still held to the same character rules, the same length cap, and the same
441+
rejection of anything with uppercase, spaces, or dots. Two or more slashes are still invalid.
442+
443+
**Consequences that are easy to miss:** an unnamespaced name has no namespace for a future
444+
cross-namespace access check to check against (#6), and two plugins both choosing `api-key`
445+
collide silently. Namespaced names remain the documented default everywhere, and
446+
`wp secret migrate-legacy` keeps `--namespace` and `--map` for putting a migrated secret
447+
somewhere specific.
448+
449+
---
450+
419451
## Resolved
420452

421453
Decisions that were open and are now closed, kept so the reasoning is not lost.

‎plugin/class-secrets-api-migrator.php‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ final class Secrets_API_Migrator {
5252
* - array $map Legacy key => explicit new name, for keys whose derived
5353
* name would not validate.
5454
* - string $namespace Namespace prefixed onto a legacy key with no entry in
55-
* $map. Default 'legacy'.
55+
* $map. Defaults to none, which keeps the key exactly as
56+
* the prototype spelled it.
5657
*
5758
* @param array $args Migration options, as described above.
5859
*
@@ -71,7 +72,7 @@ public function migrate( $args = array() ) {
7172
'dry_run' => false,
7273
'name' => null,
7374
'map' => array(),
74-
'namespace' => 'legacy',
75+
'namespace' => '',
7576
)
7677
);
7778

@@ -121,7 +122,7 @@ public function migrate( $args = array() ) {
121122
* @return array One report entry.
122123
*/
123124
private function migrate_one( $reader, $key, array $args ) {
124-
$new_name = isset( $args['map'][ $key ] ) ? $args['map'][ $key ] : $args['namespace'] . '/' . $key;
125+
$new_name = $this->new_name_for( $key, $args );
125126

126127
$entry = array(
127128
'legacy_key' => $key,
@@ -181,6 +182,38 @@ private function migrate_one( $reader, $key, array $args ) {
181182
return $entry;
182183
}
183184

185+
/**
186+
* Resolves the new-format name a prototype key migrates to.
187+
*
188+
* Defaults to the key unchanged, with no namespace. That deliberately matches
189+
* what Secrets_API_Prototype_Fallback_Store does on a read: if this method
190+
* prefixed a namespace by default, running the migrator would write
191+
* 'legacy/api_key' while a plain wp_get_secret( 'api_key' ) would still miss,
192+
* fall through to the untouched prototype row, and upgrade it a second time
193+
* into 'api_key'. One prototype secret, two current-format copies, diverging
194+
* from the moment either is rotated.
195+
*
196+
* --namespace and --map both remain available for an operator who wants the
197+
* migrated secret somewhere specific, which is a deliberate choice rather
198+
* than a silent default.
199+
*
200+
* @param string $key Prototype key.
201+
* @param array $args Resolved args from migrate().
202+
*
203+
* @return string
204+
*/
205+
private function new_name_for( $key, array $args ) {
206+
if ( isset( $args['map'][ $key ] ) ) {
207+
return $args['map'][ $key ];
208+
}
209+
210+
if ( '' === $args['namespace'] ) {
211+
return $key;
212+
}
213+
214+
return $args['namespace'] . '/' . $key;
215+
}
216+
184217
/**
185218
* Whether a current-format record already exists for a name.
186219
*

‎plugin/class-secrets-api-prototype-fallback-store.php‎

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
* next read hits the new record directly and never comes back here. The upgrade is
2323
* one-way and happens once per secret, on first use.
2424
*
25+
* Only the unnamespaced form participates. wp_get_secret( 'api_key' ) consults the
26+
* prototype's 'api_key'; wp_get_secret( 'myplugin/api_key' ) consults nothing,
27+
* because the prototype had no namespaces and so cannot have owned that name.
28+
* Nothing is rewritten or inferred -- see prototype_key_for().
29+
*
2530
* A note on what this is NOT. It does not implement the prototype's API, reinstate
2631
* its function names, or let prototype-era code keep running -- that would be a
2732
* compatibility layer, and there is deliberately none. It reads one option row,
@@ -166,34 +171,41 @@ public function has_current_record( $name ) {
166171
}
167172

168173
/**
169-
* Maps a current-format name onto the prototype key that would have held it.
170-
*
171-
* The prototype's keys are flat and unnamespaced ('api_key'); current names
172-
* are always 'namespace/key'. The mapping is therefore to drop the namespace,
173-
* which means any namespace can inherit a given prototype row.
174-
*
175-
* That is deliberate, and it is not the exposure it first looks like. The
176-
* prototype's keyspace is global: every plugin on the site could already read
177-
* every prototype secret by calling its get_secret() with the bare key, with
178-
* no namespace to check against. Inheriting one into 'ai/api_key' hands it to
179-
* code that could already have read it, and the inherited copy is a new row --
180-
* the prototype's own is never modified. Requiring an exact namespace match
181-
* instead would mean guessing which namespace the adopting plugin will choose,
182-
* and guessing wrong reintroduces exactly the broken-site outcome this exists
183-
* to prevent.
174+
* Maps a name onto the prototype key that would have held it, which is only
175+
* ever the identical name.
184176
*
185-
* @param string $name The secret's namespaced name.
177+
* The prototype's keyspace is flat, so an unnamespaced name corresponds to
178+
* exactly one prototype key and a namespaced one corresponds to none at all.
179+
* There is no rewriting here: 'api_key' looks at 'api_key', and
180+
* 'myplugin/api_key' looks at nothing.
181+
*
182+
* An earlier version dropped the namespace instead, so that
183+
* wp_get_secret( 'anything/api_key' ) inherited the prototype's 'api_key'.
184+
* That worked, but it meant any namespace could silently claim any prototype
185+
* row, and a caller had no way to tell which of its names were quietly wired
186+
* to prototype data. Making the unnamespaced form a legal call
187+
* (wp_secrets_validate_name() accepts it and reports through
188+
* _doing_it_wrong()) removes the need for the rewrite entirely: code being
189+
* ported off the prototype passes the same key it always passed, and gets the
190+
* value it always got, with nothing inferred on its behalf.
186191
*
187-
* @return string|null The prototype key, or null if $name is not well-formed.
192+
* @param string $name The secret's name.
193+
*
194+
* @return string|null The prototype key, or null if this name can never
195+
* correspond to one.
188196
*/
189197
private function prototype_key_for( $name ) {
190-
if ( ! is_string( $name ) || 1 !== substr_count( $name, '/' ) ) {
198+
if ( ! is_string( $name ) || '' === $name ) {
191199
return null;
192200
}
193201

194-
$key = substr( $name, strpos( $name, '/' ) + 1 );
202+
// A namespace is something the prototype never had, so a namespaced name
203+
// cannot describe a prototype row.
204+
if ( false !== strpos( $name, '/' ) ) {
205+
return null;
206+
}
195207

196-
return ( '' === $key ) ? null : $key;
208+
return $name;
197209
}
198210

199211
/**

‎secrets-api.php‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* Plugin Name: Secrets API
4-
* Plugin URI: https://github.com/WordPress/secrets-api
4+
* Plugin URI: https://github.com/ericmann/secrets-api
55
* Description: Feature plugin for the WordPress Secrets API proposed for 7.2. Encrypted, versioned credential storage with pluggable storage and keyring back ends.
66
* Version: 0.1.0
77
* Requires at least: 6.6
@@ -14,8 +14,9 @@
1414
*/
1515

1616
/*
17-
* The slug and display name above are provisional. See docs/open-questions.md #2 -- a
18-
* neutral, non-Displace-branded slug needs a human decision before any .org submission.
17+
* Plugin URI points at the current home, which is private for now. Availability of
18+
* the slug on WordPress.org is settled at submission, not here -- see
19+
* docs/open-questions.md #1.
1920
*/
2021

2122
defined( 'ABSPATH' ) || exit;

‎src/wp-includes/secrets.php‎

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,19 +186,26 @@ function wp_secrets_memzero( &$value ) {
186186
}
187187

188188
/**
189-
* Validates a secret's namespaced name.
189+
* Validates a secret's name.
190190
*
191191
* Names take the form 'plugin-slug/secret-name': lowercase alphanumerics,
192192
* hyphens, and underscores in each segment, exactly one '/' separating them,
193-
* and no segment starting or ending with a hyphen or underscore. There is no
194-
* unnamespaced form and no escape hatch for one.
193+
* and no segment starting or ending with a hyphen or underscore.
194+
*
195+
* An unnamespaced name ('secret-name', no '/') is accepted, but reports through
196+
* _doing_it_wrong(). It exists for one reason: code written against the Displace
197+
* prototype used a flat keyspace, and refusing those names outright would mean
198+
* every such call site has to be rewritten before it can be ported at all.
199+
* Accepting them keeps that migration incremental. Nothing else should use one --
200+
* without a namespace there is nothing for a future cross-namespace access check
201+
* to check against, and two plugins picking 'api-key' collide silently.
195202
*
196203
* @since 7.2.0
197204
*
198205
* @param string $name Candidate secret name.
199206
*
200-
* @return true|WP_Error True if $name is valid. Otherwise WP_Error with code
201-
* WP_SECRETS_ERROR_INVALID_NAME.
207+
* @return true|WP_Error True if $name is usable, including the unnamespaced form.
208+
* Otherwise WP_Error with code WP_SECRETS_ERROR_INVALID_NAME.
202209
*/
203210
function wp_secrets_validate_name( $name ) {
204211
if ( ! is_string( $name ) || '' === $name ) {
@@ -219,11 +226,41 @@ function wp_secrets_validate_name( $name ) {
219226
);
220227
}
221228

222-
if ( 1 !== substr_count( $name, '/' ) ) {
229+
$slashes = substr_count( $name, '/' );
230+
231+
if ( $slashes > 1 ) {
223232
return new WP_Error(
224233
WP_SECRETS_ERROR_INVALID_NAME,
225-
__( 'Secret names must contain exactly one "/", separating a namespace from a key.', 'default' )
234+
__( 'Secret names may contain at most one "/", separating a namespace from a key.', 'default' )
235+
);
236+
}
237+
238+
$segment_pattern = '/^[a-z0-9]([a-z0-9_-]*[a-z0-9])?$/';
239+
240+
if ( 0 === $slashes ) {
241+
if ( ! preg_match( $segment_pattern, $name ) ) {
242+
return new WP_Error(
243+
WP_SECRETS_ERROR_INVALID_NAME,
244+
__( 'Secret names may contain only lowercase letters, numbers, hyphens, and underscores, and must not start or end with a hyphen or underscore.', 'default' )
245+
);
246+
}
247+
248+
/*
249+
* Reported here rather than at each public entry point: this is the one
250+
* place that decides what a name is, so a caller cannot reach the store
251+
* with an unnamespaced name without passing through it.
252+
*/
253+
_doing_it_wrong(
254+
__FUNCTION__,
255+
sprintf(
256+
/* translators: %s: The unnamespaced secret name. */
257+
__( 'The secret name "%s" has no namespace. Namespaced names ("plugin-slug/secret-name") group secrets by owner and give a future access check something to check against; unnamespaced names are supported only so that code written against the Displace prototype can be ported incrementally.', 'default' ),
258+
$name
259+
),
260+
'7.2.0'
226261
);
262+
263+
return true;
227264
}
228265

229266
$segments = explode( '/', $name );
@@ -237,8 +274,6 @@ function wp_secrets_validate_name( $name ) {
237274
);
238275
}
239276

240-
$segment_pattern = '/^[a-z0-9]([a-z0-9_-]*[a-z0-9])?$/';
241-
242277
if ( ! preg_match( $segment_pattern, $namespace ) || ! preg_match( $segment_pattern, $key ) ) {
243278
return new WP_Error(
244279
WP_SECRETS_ERROR_INVALID_NAME,

0 commit comments

Comments
 (0)