wpseek.com
A WordPress-centric search engine for devs and theme authors
wp_secrets_validate_name › WordPress Function
Since7.2.0
Deprecatedn/a
› wp_secrets_validate_name ( $name )
| Parameters: |
|
| Returns: |
|
| Defined at: |
|
| Codex: |
Validates a secret's name.
Names take the form 'plugin-slug/secret-name': lowercase alphanumerics, hyphens, and underscores in each segment, exactly one '/' separating them, and no segment starting or ending with a hyphen or underscore. Namespacing is a matter of organization, not security. It groups secrets by owner so that listings and admin screens can be sensible. It confers no isolation: any plugin that can run PHP can read any secret, namespaced or not. An unnamespaced name ('secret-name', no '/') is accepted, but reports through _doing_it_wrong(). It exists for one reason: code written against the earlier prototype used a flat keyspace, and refusing those names outright would mean every such call site has to be rewritten before it can be ported at all. Accepting them keeps that migration incremental. Nothing else should use one. Two plugins that both choose 'api-key' collide silently, which is a real problem even though it is not a security one.Related Functions: wp_secrets_provider_label, _wp_secrets_list, _wp_secrets_delete, rest_validate_enum, wp_create_nav_menu
Source
function wp_secrets_validate_name( string $name ) {
if ( '' === $name ) {
return new WP_Error(
WP_SECRETS_ERROR_INVALID_NAME,
__( 'Secret names must not be empty.' )
);
}
if ( strlen( $name ) > WP_SECRETS_MAX_NAME_LENGTH ) {
return new WP_Error(
WP_SECRETS_ERROR_INVALID_NAME,
sprintf(
/* translators: %d: Maximum allowed length, in characters. */
__( 'Secret names must be %d characters or fewer.' ),
WP_SECRETS_MAX_NAME_LENGTH
)
);
}
$slashes = substr_count( $name, '/' );
if ( $slashes > 1 ) {
return new WP_Error(
WP_SECRETS_ERROR_INVALID_NAME,
__( 'Secret names may contain at most one "/", separating a namespace from a key.' )
);
}
$segment_pattern = '/^[a-z0-9]([a-z0-9_-]*[a-z0-9])?$/';
if ( 0 === $slashes ) {
if ( ! preg_match( $segment_pattern, $name ) ) {
return new WP_Error(
WP_SECRETS_ERROR_INVALID_NAME,
__( 'Secret names may contain only lowercase letters, numbers, hyphens, and underscores, and must not start or end with a hyphen or underscore.' )
);
}
/*
* Reported here rather than at each public entry point: this is the one
* place that decides what a name is, so a caller cannot reach the store
* with an unnamespaced name without passing through it.
*/
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: %s: The unnamespaced secret name. */
__( 'The secret name "%s" has no namespace. Namespaced names ("plugin-slug/secret-name") group secrets by owner so that listings and admin screens can be organized; unnamespaced names are supported only so that code written against the Displace prototype can be ported incrementally.' ),
$name
),
'7.2.0'
);
return true;
}
$segments = explode( '/', $name );
$namespace = $segments[0];
$key = $segments[1];
if ( '' === $namespace || '' === $key ) {
return new WP_Error(
WP_SECRETS_ERROR_INVALID_NAME,
__( 'Both segments of a secret name must be non-empty.' )
);
}
if ( ! preg_match( $segment_pattern, $namespace ) || ! preg_match( $segment_pattern, $key ) ) {
return new WP_Error(
WP_SECRETS_ERROR_INVALID_NAME,
__( 'Secret name segments may contain only lowercase letters, numbers, hyphens, and underscores, and must not start or end with a hyphen or underscore.' )
);
}
return true;
}