Directory

add common functions that return int/float to escaping functions by kkmuffme · Pull Request #2082 · WordPress/WordPress-Coding-Standards · GitHub
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

add common functions that return int/float to escaping functions #2082

Closed

Conversation

kkmuffme
Copy link

@kkmuffme kkmuffme commented Sep 4, 2022

#1969

count
rand
random_int
ceil
floor
round

WordPress/Sniff.php Outdated Show resolved Hide resolved
@dingo-d
Copy link
Member

dingo-d commented Sep 5, 2022

Looks good, but I'd like to go through the added functions, just to be safe (no pun intended) 😅

@kkmuffme
Copy link
Author

kkmuffme commented Sep 5, 2022

Yes please, take your time. I grabbed them off of psalm only taking global, non-class functions

Copy link
Member

@dingo-d dingo-d left a comment

Choose a reason for hiding this comment

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

Ok, I've gone through all of the additions and left some comments.

I'm wondering if we should split the PHP ones into a separate array and then just array_merge it with functions specific to WP?

Could be easier for later maintenance.

'bzerrno' => true,
'cal_days_in_month' => true,
'cal_to_jd' => true,
'calendar_week_mod' => true,
Copy link
Member

Choose a reason for hiding this comment

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

Turns out this function is not type safe, so the results can result in a fatal error on PHP 8+, and a warning on PHP < 8 (albeit it returns 0 along the warning).

I'd remove this from the list.

Copy link
Author

Choose a reason for hiding this comment

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

This function is already there in the list right now, I didn't add this function.

Also it always returns a float, so it's safe/auto-escaped?

Copy link
Member

Choose a reason for hiding this comment

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

Try passing it a non numerical string and see what will happen. It's not type safe 😅

Copy link
Author

Choose a reason for hiding this comment

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

https://3v4l.org/H5evB returns float 0 in PHP 7 and fatal in PHP 8, both is safe for output. Am I missing something?

Copy link
Member

Choose a reason for hiding this comment

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

I wasn't aware that throwing an error was considered safe. 👀

Copy link
Author

Choose a reason for hiding this comment

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

It is. "Safe" is defined as any value that does not allow for an XSS attack vector - that's why we're escaping in the first place, isn't it?

WordPress/Sniff.php Outdated Show resolved Hide resolved
Comment on lines +144 to +151
'curl_errno' => true,
'curl_multi_add_handle' => true,
'curl_multi_errno' => true,
'curl_multi_exec' => true,
'curl_multi_remove_handle' => true,
'curl_multi_select' => true,
'curl_pause' => true,
'curl_share_errno' => true,
Copy link
Member

Choose a reason for hiding this comment

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

I think all of these would throw a Using cURL functions is highly discouraged. Use wp_remote_get() instead. warning, not sure if we should include them in this list.

In theory, they are escaped (safe to use because they return integer), but not sure if the above error disqualifies them from being on this list 🤷🏼‍♂️

Copy link
Author

Choose a reason for hiding this comment

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

Since these are mostly for "features" not supported by wp_remote_get I think it makes sense to keep those in.

'do_shortcode' => true,
'do_shortcode_tag' => true,
'doubleval' => true,
'easter_date' => true,
Copy link
Member

Choose a reason for hiding this comment

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

As noted on the docs page, this function will generate a warning if the year is outside of the range for Unix timestamps, and if this isn't covered by tests or a sniff, I'd leave it out of this list.

Copy link
Author

Choose a reason for hiding this comment

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

But a warning doesn't mean the returned value isn't escaped? The returned value is always a safe int.

WordPress/Sniff.php Outdated Show resolved Hide resolved
WordPress/Sniff.php Outdated Show resolved Hide resolved
WordPress/Sniff.php Outdated Show resolved Hide resolved
'mb_ereg_search_getpos' => true,
'mb_strwidth' => true,
'mb_substr_count' => true,
'mhash_count' => true,
Copy link
Member

Choose a reason for hiding this comment

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

This function is deprecated as of 8.1.0, should we keep it in the list?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, bc WP will support PHP < 8.1 for at least 5 more years it seems 😂

WordPress/Sniff.php Show resolved Hide resolved
WordPress/Sniff.php Outdated Show resolved Hide resolved
@kkmuffme
Copy link
Author

kkmuffme commented Dec 7, 2023

The code was moved to a different file with WPCS 3.0.0 and no more feedback given, will probably not get merged.

@dingo-d
Copy link
Member

dingo-d commented Dec 8, 2023

@kkmuffme you could have rebased this PR, and we could have given it another look, no need to close it 🤷🏼‍♂️

@kkmuffme
Copy link
Author

kkmuffme commented Dec 8, 2023

It's easier to create a new PR than rebasing this one, bc there are tons of rebase conflicts since the files this code is in has changed and also they're sorted alphabetically now already (which this PR did too).

If you still want a new PR, that's no problem, I can PR it :-)

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.

None yet

2 participants