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

Update escaping function #683

Merged
merged 7 commits into from
Sep 3, 2024
Merged

Conversation

matiasbenedetto
Copy link
Contributor

@matiasbenedetto matiasbenedetto commented Jul 3, 2024

What?

Use esc_attr_e to escape HTML attributes of blocks.

Why?

How?

By adding a specific function for escaping HTML attributes of blocks.

How to test:

  • Export a theme with text in HTML attributes. Example alt property of Image blocks.
    Check that the attributes are correctly escaped.

@matiasbenedetto matiasbenedetto added the bug Something isn't working label Jul 3, 2024
Copy link
Contributor

@creativecoder creativecoder left a comment

Choose a reason for hiding this comment

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

Using kses rather than an escaping function seems like the right solution to the linked issue.

However I think this needs a more nuanced change, based on how the escape_string method is used

What do you think of the following as a fix?

  • Renaming escape_string to escape_attr, replacing esc_html_e with esc_attr_e, then using it below on line 128
  • Adding kses_for_block_content (or similar name) that wraps wp_kses_post and using it below on line 112

Also, maybe these methods should be protected/private, since they aren't used outside the class.

@matiasbenedetto
Copy link
Contributor Author

@creativecoder I implemented your suggestions in the latest commits.
Thanks for the review!

@creativecoder
Copy link
Contributor

@matiasbenedetto I took a closer look at this to better understand the generated output in theme patterns and attempted an improvement in 3e53a9a to use wp_kses_post only when necessary.

My understanding is that wp_kses (and related kses functions) aren't meant to be escaping functions, though sometimes they can be used that way. That said, I don't think we should be using wp_kses_post on translated strings in theme patterns in place of esc_html.

This opens the door to translated strings containing a wide variety of html, including things like a tags. For themes released in the directory, I can imagine a kind of injection attack where translated versions of sites show injected html links, audio, videos, etc.

Potentially we could use wp_kses with a limited list of rich text tags (em, strong, br, code, etc) but having that as inline PHP seems like it would clutter pattern files considerably.

The best solution would be parsing HTML content and generating sprintf functions with placeholders for the relevant html in translated strings, but that seems very complex to get right.

What do you think?

@matiasbenedetto
Copy link
Contributor Author

@creativecoder thanks for the detailed review. I have a few comments:

I don't think we should be using wp_kses_post on translated strings in theme patterns in place of esc_html.

I see that in 3e53a9a (#683)
wp_kses_post it is still used. If there's a potential security concern it should be removed completely, right?

The best solution would be parsing HTML content and generating sprintf functions with placeholders for the relevant html in translated strings, but that seems very complex to get right.

Yes, this seems to be too complex to worth it.

I'm not sure what would be the best way to move this forward.

@creativecoder
Copy link
Contributor

I see that in 3e53a9a (#683)
wp_kses_post it is still used. If there's a potential security concern it should be removed completely, right?

Yes, I think so. That was my attempt to resolve the issue, but I don't think it's good enough.

I'm not sure what would be the best way to move this forward.

Same for me. For now, I think this needs to be worked around by manually editing the string in the theme template.

In the case of the video in the linked issue, splitting the string on the line breaks seems appropriate.

And for other inline html, using placeholders, for example:

<?php
/* translators: %1$s: start of bold text (<strong>), %2$s: end of bold text (</strong>
) */
sprintf(
    __( 'Some text %1$sthat is bold.%2$s', 'my-theme-textdomain' ),
    '<strong>',
    '</strong>'
);
?>

@matiasbenedetto
Copy link
Contributor Author

In the latest commit (75cc3d2) I removed the wp_kses_post as a escaping function. With that commit this PR only changes the way the HTML attributes are escaped by using esc_attr_e and left the rest of the html contents escaped as before using esc_html_e. In this way we would be able to merge this and fix #691. Still #682 won't be fixed by this PR.

Copy link
Contributor

@creativecoder creativecoder left a comment

Choose a reason for hiding this comment

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

Let's make sure the PR description gets updated before this lands.

tests/CbtThemeLocale/escapeString.php Show resolved Hide resolved
@matiasbenedetto
Copy link
Contributor Author

I updated the description. This PR should only fix the escaping of the HTML attributes. The text content of blocks will remain the same for now and it should be tackled in another PR.

Could you give it another review, please?

Copy link
Member

@mikachan mikachan left a comment

Choose a reason for hiding this comment

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

This seems to test well with alt text on an Image block but not with an inline link in a Paragraph block:

image image image

Should it also work with this link?

Edit: Oh I see, this PR just handles attributes - in that case I think this is working well 👏

@matiasbenedetto matiasbenedetto merged commit 6973cf8 into trunk Sep 3, 2024
2 checks passed
@matiasbenedetto matiasbenedetto deleted the fix/text-escaping-function branch September 3, 2024 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging this pull request may close these issues.

3 participants