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

Warn on double prepare? #36

Open
iandunn opened this issue Apr 7, 2021 · 1 comment
Open

Warn on double prepare? #36

iandunn opened this issue Apr 7, 2021 · 1 comment
Labels
enhancement New feature or request question Further information is requested

Comments

@iandunn
Copy link
Member

iandunn commented Apr 7, 2021

It's not an active vulnerability anymore, but maybe it should still be considered a bad practice, since it makes the code harder to reason about? Like escaping, a single prepare() at the point of query execution is more straight-forward.

https://blog.ircmaxell.com/2017/10/disclosure-wordpress-wpdb-sql-injection-technical.html
https://make.wordpress.org/security/2017/11/13/the-war-on-sqli-or-what-happened-in-4-8-2-and-4-8-3/

$where = $wpdb->prepare(
	" WHERE foo = %s",
	$_GET['data']
);

$wpdb->get_results( $wpdb->prepare(
	"SELECT * FROM something $where LIMIT %d, %d",
	1,
	2
) );
@iandunn iandunn added enhancement New feature or request question Further information is requested labels Apr 7, 2021
@iandunn
Copy link
Member Author

iandunn commented Apr 14, 2021

I think double prepares often result from building a query conditionally, but passing a static list of arguments to prepare(). A better practice IMO would be to pass an array of arguments, which is also built conditionally:

$where = '';
$prepare_values = array();

if ( $foo ) {
	$where = " WHERE foo = %s";
	$prepare_values[] = $_GET['data'] );
}

$prepare_values[] = 1;
$prepare_values[] = 2;

$wpdb->get_results( $wpdb->prepare(
	"SELECT * FROM something $where LIMIT %d, %d",
	$prepare_values
) );

That way the scanner (and humans) can easily see that $where is a literal string, and therefore safe.

That should definitely just be a warning, though, since there's plenty of existing secure code that double-prepares, including in Core.

@iandunn iandunn added this to the 1: Create Plugin Standard milestone Apr 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant