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

Late escape site blocks #37880

Merged
merged 6 commits into from
Jan 17, 2022
Merged

Late escape site blocks #37880

merged 6 commits into from
Jan 17, 2022

Conversation

getdave
Copy link
Contributor

@getdave getdave commented Jan 11, 2022

Description

This is not a security problem. This PR simply moves escaping of all PHP output to be as "late" as possible. This means we avoid escaping variables until they are output in the HTML markup.

This is a WP Core best practice.

How has this been tested?

  • add both blocks
  • check functionality is "as was"
  • check all tests pass

Screenshots

Types of changes

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR (please manually search all *.native.js files for terms that need renaming or removal).
  • I've updated related schemas if appropriate.

@getdave getdave added [Type] Code Quality Issues or PRs that relate to code quality [Block] Site Title Affects the Site Title Block [Block] Site Tagline Affects the Site Tagline Block [Block] Site Logo Affects the Site Logo Block [Type] Security Related to security concerns or efforts labels Jan 11, 2022
@getdave getdave requested a review from ntsekouras January 11, 2022 14:53
@getdave getdave requested a review from ajitbohra as a code owner January 11, 2022 14:53
@getdave getdave self-assigned this Jan 11, 2022
@@ -40,7 +40,7 @@ function render_block_core_site_logo( $attributes ) {
// Add the link target after the rel="home".
// Add an aria-label for informing that the page opens in a new tab.
$aria_label = 'aria-label="' . esc_attr__( '(Home link, opens in a new tab)' ) . '"';
$custom_logo = str_replace( 'rel="home"', 'rel="home" target="' . $attributes['linkTarget'] . '"' . $aria_label, $custom_logo );
$custom_logo = str_replace( 'rel="home"', 'rel="home" target="' . esc_attr( $attributes['linkTarget'] ) . '"' . $aria_label, $custom_logo );
Copy link
Member

Choose a reason for hiding this comment

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

This is under a '_blank' === $attributes['linkTarget'] check, so we already know what the value is, and it's hardcoded without any user-input involved. Escaping this string is not necessary, it can just be _blank

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMHO, if data is coming from a variable I believe we should escape at point of output. Who's to say what the variable might contain in future. It's hardcoded for now but that's no guarantee it will stay that way.

This is minor overhead for the sake of long term peace of mind and resilience against XSS.

Copy link
Member

Choose a reason for hiding this comment

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

I agree... But why not just make it target="_blank"? It can't be anything else. That way we make it easier to read, and at the same time it's a performance micro-optimization 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me look again. If we can hard code inline when we build the markup then let's do that,

Copy link
Contributor Author

@getdave getdave Jan 13, 2022

Choose a reason for hiding this comment

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

Ok I've looked again. Yes this block of code is behind the following conditional:

if ( $attributes['isLink'] && '_blank' === $attributes['linkTarget'] ) {

Whilst I recognise this is how it is now, that could easily change in the future.

What if there's a refactor and the $custom_logo markup ends up outside this conditional? What if the developer forgets to escape the linkTarget as part of the refactor?

I appreciate this is a "what if" scenario but given the low overhead (how many site logo blocks are going to be on one page?) I believe it is better to be safe than sorry when it comes to XSS. I believe it's a good trade off.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What's the next step here? Is the addition of this esc_attr() a blocker? If it absolutely is I will revert the change.

packages/block-library/src/site-title/index.php Outdated Show resolved Hide resolved
@getdave getdave requested a review from aristath January 12, 2022 12:36
@getdave getdave requested a review from Mamaduka January 14, 2022 16:58
@Rockstar907
Copy link

Rockstar907 commented Jan 14, 2022 via email

@getdave
Copy link
Contributor Author

getdave commented Jan 17, 2022

@Rockstar907 As you mention, this is a Github issue and not specifically related to this repository. I recommend taking a look at their support article on notifications to learn how to avoid getting notified.

Copy link
Member

@aristath aristath left a comment

Choose a reason for hiding this comment

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

I may not agree with some of the choices, but I don't see any regressions.

@getdave getdave merged commit 52f6290 into trunk Jan 17, 2022
@getdave getdave deleted the update/late-escape-site-blocks branch January 17, 2022 11:52
@github-actions github-actions bot added this to the Gutenberg 12.5 milestone Jan 17, 2022
@getdave getdave mentioned this pull request Feb 7, 2022
17 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Site Logo Affects the Site Logo Block [Block] Site Tagline Affects the Site Tagline Block [Block] Site Title Affects the Site Title Block [Type] Code Quality Issues or PRs that relate to code quality [Type] Security Related to security concerns or efforts
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants