Skip to content
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

PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic #871

Open
alanef opened this issue Feb 13, 2025 · 3 comments
Open

PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic #871

alanef opened this issue Feb 13, 2025 · 3 comments

Comments

@alanef
Copy link
Contributor

alanef commented Feb 13, 2025

Warns about sanitization on callbacks using array( $this, 'cb_function') when $this should not be sanitized

@swissspidy
Copy link
Member

I think the sniff is working as intended. It's a static check so it doesn't really know what the function is, hence it emits a warning telling you to ensure proper sanitization.

See

if ( false !== $next_token ) {
$next_type = $this->tokens[ $next_token ]['type'];
if ( in_array( $next_type, array( 'T_ARRAY', 'T_OPEN_SHORT_ARRAY', 'T_VARIABLE', 'T_CALLABLE' ), true ) ) {
$this->phpcsFile->addWarning(
'Dynamic argument passed in third parameter of %s(). Please ensure proper sanitization.',
$stackPtr,
$error_code . 'Dynamic',
array( $matched_content )
);
}
}

@ernilambar please correct me if I'm wrong

@alanef
Copy link
Contributor Author

alanef commented Feb 13, 2025

I get it is a warning. Maybe I dont undersatnd the wording. "Please ensure proper sanitization" of what ?

I'm not sure why the third param need sanitization in anycase?

https://developer.wordpress.org/reference/functions/register_setting/

the warning happens in all combinations i looked at

Maybe the warning has to be clearer? Otherwise it is just noise ( to me )

register_setting(
			$this->option_group,                         /* Option Group */
			"{$this->option_group}-reset",
			'function'
		);
register_setting(
			$this->option_group,                         /* Option Group */
			"{$this->option_group}-reset",
			array(
				'sanitize_callback' => 'reset_sanitize',
				'default' => NULL,
			) /* Sanitize Callback */
		);
register_setting(
			$this->option_group,                         /* Option Group */
			"{$this->option_group}-reset",
			array(
				'sanitize_callback' => array( $this, 'reset_sanitize' ),
				'default' => NULL,
			) /* Sanitize Callback */
		);

@ernilambar
Copy link
Member

The third parameter of register_setting() accepts various data types, including strings, callable functions, anonymous functions, and other dynamic arguments. Static analysis via PHPCS cannot reliably determine the parameter's type. Sanitization is recommended before storing this data in the database to ensure data integrity and security. Suggestions for improved wording are welcome.

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

No branches or pull requests

3 participants