Repository navigation
Conversation
🤖 PR meta 🤖📦 Bundle sizeSize Change: 0 B Total Size: 8.58 MB
⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
🏁 Flaky testsSome tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information. Undo restores the post selection when another synced entity is loaded in
|
|
This is really interesting @juanfra ! While it's still a draft, there's a lot of really interesting ideas here, particularly around supporting composition and having safer defaults for component content. Since this came up in the context of #84036 (comment) and around Core#60229, I wonder if @dmsnell would have any thoughts on this approach. I expect there could be value in generalizing this behavior beyond just components themselves as explored in the related ticket, and this could serve as a valuable use-case, proof-of-concept, and maybe even something we could merge and build expand from. In particular, it feels to me like a good middle-ground to adopt something like Rails' SafeBuffers and treating string markup as untrusted by default except for what we know to be produced by the implementation of the components themselves. One interesting question that comes up for me in looking at the Card example is contrasting with the I also like how you approached different content options like those mentioned in #84036 (comment) by not just choosing one, but supporting consumers to mix-and-match based on their individual need, where one option may work better for specific situations. Depending on risk tolerance (and in particular question about "closures are only as safe as what they print", we could ultimately choose to drop some options if we'd rather funnel people toward one. Or we could add it later if we don't want to bind ourselves to that decision now. To your specific questions of feedback:
I see the risk. Personally it feels reasonable, insofar that breaking out of PHP delimiters assumes and understanding that you'd be working more directly with raw HTML and expected to use appropriate escaping functions when working in that context. Given my own understanding of the types of security issues we'd be trying to protect against, many of them often come in the form of concatenating strings that are assumed to be plaintext but can be manipulated maliciously to include markup. In the closure example context, I think there'd be an understanding of content in that output to be HTML. That being said, it's of course objectively "safer" to just not allow it at all as an option, and to me this feels like a question between reasonable security and developer ergonomics and flexibility.
I agree. I think the name itself can do a lot to communicate its intent. In that regard, it doesn't trouble me very much as-is. If we were especially concerned we could name it something that communicates its intent more clearly (akin to React's
I was curious about precedent for this kind of "duck typing" in WordPress, and there's a few examples (e.g. |
|
thanks @aduth for the callout here. unfortunately I do not have the cycles available to give this the attention it deserves, but I like the general idea. a couple of my explorations are linked in the Core-60229, which you linked to. there are a few things I’ve been hunting, which I think are important in a system like this:
the duck-typing note is important. a general I want this to happen and I want it supported everywhere in Core. as long as the work is slow I think we can make that happen. if wanting to experiment more, I think it’s likely more valuable to be trying to make something specific and internal and scary-sounding to others so that we don’t over-rapidly adopt something that isn’t clearly vetted and generalizable. |
|
Thanks so much for looking into this! Appreciate it :)
@aduth I agree that flat makes more sense for PHP. I believe the way to map them is by what each subcomponent does. For example:
That's more or less the idea of how it works in this PR, with the mapping in the
@aduth Agreed. I believe it'd be easier to drop an option later than adding it back.
Thanks for digging into the precedents! That's a good reference, and it looks very close, yes.
@dmsnell Thanks! That makes sense, and I hadn't considered all the
Good point 👍
That makes sense as well |
These are effectively what the Shortblocks/Bits proposal is creating, and forms the basis for my HTML templating’s context-aware auto-escaping, but in tokens that are part of HTML instead of layering a custom syntax on top of or underneath HTML. The idea for how these fit together is that the template merely defines slots into which to render content. If the values associated with those slots are strings, it should be escaped to preserve the rendering of that text verbatim. However, if the value is one of the wrapped markup containers, it will be safely combined with tags and all. <!-- profile.html -->
<div class="profile" data-user-id="</%id>" data-name="</%name>">
<a href="</%url>"></%name></a>
</div>WP_HTML::render(
file_get_contents( __DIR__ . '/profile.html' ),
array(
'id' => 14,
'name' => WP_HTML::template( '<name>Dennis Snell</name>' ),
'url' => 'https://profiles.wordpress.org/dmsnell',
)
);So the thing here is that we don’t have to send distinct values for the placeholders for each distinct context in which they render, nor do we have to escape any values (and shouldn’t!). When the template renders the <div class="profile" data-user-id="14" data-name="Dennis Snell">
<a href="https://profiles.wordpress.org/dmsnell"><name>Dennis Snell</name></a>
</div>Or the classic example I like to use, URLs which have for display and for data modes. <a href="</%url>"></%url></a>Passed <a href="https://xn-google.com">https://䕮䕵䕶䕱.com</a>Most of this work could be done, but due to various shufflings of priorities and needs it has been deferred, as also some nuanced design choices that still remain. |
|
Thanks for the links and examples @dmsnell, super helpful! It's good to see the idea already has a home. The context-aware escaping is close to what I tried, but having the template take care of that sounds like a much better foundation. It'd be a great fit for these components moving forward. |
A component can describe its React output as a render tree, like Card.Root with Card.Header and Card.Content, and its fixtures can include other components as content. Each component.json also gets an example, used by the fixtures and the preview.
Card's content can be text, another component as array( 'component' => …, 'props' => …, 'content' => … ), or a list of both. PHP renders nested components itself and escapes all text, so the HTML only comes from components and escaped text. Parity tests check Card with a Button inside against React.
8777572 to
da683c5
Compare
gutenberg_get_ui_component() returns Gutenberg_UI_Safe_HTML again, so another component's output can be passed as content and is inserted as HTML, while plain strings are escaped. Closures and sanitized() HTML are accepted too. Based on the review: trusted() is gone, so the object only comes from a component's output or from sanitized(), its constructor is private, and it can't be unserialized. __toString() stays so echo keeps working; the templating in wordpress-develop#5949 could replace this object later.
|
I've rebuilt this on top of #85759 and changed the base branch. Added an update at the top of the description with what changed based on all the feedback here, so that we don't lose any information. Sorry for all the noise with labels. I pushed before changing the base, and the automation triggered. |
…6-ui-components-php-card # Conflicts: # packages/ui/bin/build-php.mjs
…6-ui-components-php-card # Conflicts: # phpunit/experimental/ui-components-test.php
| * @param string $html HTML rendered by a component. | ||
| * @return Gutenberg_UI_Safe_HTML Safe HTML. | ||
| */ | ||
| function gutenberg_ui_components_safe_html( $html ) { |
There was a problem hiding this comment.
perhaps safe is conveying more than it should. what about just marking that it’s been sanitized?
function gutenberg_make_sanitized_html( $html ) { ... }| } | ||
| if ( $content instanceof Gutenberg_UI_Safe_HTML ) { | ||
| return (string) $content; | ||
| } |
There was a problem hiding this comment.
this condition is an example of why I suggested anonymous classes. we’ve exposed a safe primitive to the public, and I worry that this will encourage misuse of the class.
did you explore an anonymous class trapped inside a sanitizing class?
https://3v4l.org/aOOj7
it’s a bit cumbersome, but replaces these abstraction leaks with a more normative exchange with the wrapping class.
the condition here would be replaced with Gutenberg_Sanitized_HTML::render( $string_or_wrapped_value ), where it would unpack the types and keep them all in a single file. I don’t see any reason to ever call instance Private_Secured_Wrapper
class Outer {
private static $inner = null;
private function __construct() {
}
private static function ensure() {
if ( isset( self::$inner ) ) {
return;
}
self::$inner = new class () {
private $html = '';
public static function wrap( $html ) {
$self = new self();
$self->html = $html;
return $self;
}
public static function unwrap( $self ) {
return $self->html;
}
};
}
public static function wrap( $html ) {
self::ensure();
return self::$inner::wrap( $html );
}
public static function unwrap( $self ) {
return $self instanceof self::$inner
? self::$inner::unwrap( $self )
: $self;
}
}
$wrapped = Outer::wrap( '<div>just <script>a test</script> some HTML</div>' );
var_dump( $wrapped );
var_dump( Outer::unwrap( $wrapped ) );
// Can’t call this from outside `Outer` because of the anonymous class.
// var_dump( $wrapped->unwrap() );
// It’s still possible to call via a self-reference, but that’s awkward and will stand out.
var_dump( $wrapped::unwrap( $wrapped ) );
What?
Builds on #85759
#84905.Adds
Cardto try composition, and to make HTML content safe by default, as discussed in #84036How?
Content gets its own argument now,
gutenberg_get_ui_component( 'card', $props, $content ). The idea is that it's escaped unless we know it's safe:Gutenberg_UI_Safe_HTMLobject, which you can stillechoas usual.Gutenberg_UI_Safe_HTML::sanitized()(runswp_kses_post())or.trusted()To check composition, the build renders a
Cardwith aButtoninside using React, and a test makes sure PHP puts them together the same way.I also removed the unused"format": "html"path, which printed HTML as is.There are a few things I'd love thoughts on:
Update: Removedtrusted()skips escaping on purpose, for HTML that didn't come from a component (like the output of a WP function). It's explicit and easy to find in review, but nothing stops someone from misusing it.trusted().get_function in WordPress. It still works withecho, so most code shouldn't notice.Testing Instructions
npm run build.npm run test:unit:php:base -- --filter Gutenberg_UI_Components_Test.Use of AI Tools
I used Claude Code to help prototype and write parts of this code. I reviewed and tested all of it.