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

Interactivity API: Prevent wrong written directives from killing the runtime #61249

Merged
merged 8 commits into from May 7, 2024

Conversation

cbravobernal
Copy link
Contributor

@cbravobernal cbravobernal commented Apr 30, 2024

What?

Prevent wrong written directives killing the runtime of the Interactivity API.

Why?

A directive like data-wp-on-window-- can kill the runtime process in the Interactivity API.

How?

Refactored the code that gets the directives and parse them.

Testing Instructions

  1. Create an Interactive block with a wrong directive like data-wp-on-document--
  2. Check that the rest of directives are processed.

Screenshots or screencast

Before:
Screenshot 2024-04-29 at 18 01 14

After:

Screenshot 2024-04-30 at 18 50 19

Copy link

Flaky tests detected in 03c093d.
Some 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.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/8895797255
📝 Reported issues:

@cbravobernal cbravobernal changed the title Prevent error on vdom Interactivity API: Prevent wrong written directives from killing the runtime Apr 30, 2024
@cbravobernal cbravobernal self-assigned this Apr 30, 2024
@cbravobernal cbravobernal added [Type] Bug An existing feature does not function as intended [Packages] Interactivity /packages/interactivity labels Apr 30, 2024
@cbravobernal cbravobernal force-pushed the fix/prevent-runtime-error-iapi branch from b407b49 to 204c420 Compare April 30, 2024 17:19
@cbravobernal cbravobernal marked this pull request as ready for review April 30, 2024 17:21
Copy link

github-actions bot commented Apr 30, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: cbravobernal <cbravobernal@git.wordpress.org>
Co-authored-by: sirreal <jonsurrell@git.wordpress.org>
Co-authored-by: DAreRodz <darerodz@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Member

@sirreal sirreal left a comment

Choose a reason for hiding this comment

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

Should we update the regexp instead so it doesn't match the bad suffix, requiring an alphanumeric character to start?

// Regular expression for directive parsing.
const directiveParser = new RegExp(
`^data-${ p }-` + // ${p} must be a prefix string, like 'wp'.
// Match alphanumeric characters including hyphen-separated
// segments. It excludes underscore intentionally to prevent confusion.
// E.g., "custom-directive".
'([a-z0-9]+(?:-[a-z0-9]+)*)' +
// (Optional) Match '--' followed by any alphanumeric charachters. It
// excludes underscore intentionally to prevent confusion, but it can
// contain multiple hyphens. E.g., "--custom-prefix--with-more-info".
'(?:--([a-z0-9_-]+))?$',
'i' // Case insensitive.
);

This line matching the suffix:

'(?:--([a-z0-9_-]+))?$',

Could add a required alphanumeric initial character to the suffix like this:

'(?:--([a-z0-9][a-z0-9_-]*))?$',

For the warning, then we could check early if .exec() returns null, show the warning, and just skip the rest of the processing for a bad directive?

@DAreRodz
Copy link
Contributor

DAreRodz commented May 3, 2024

Should we update the regexp instead so it doesn't match the bad suffix, requiring an alphanumeric character to start?

Hey @sirreal, we need to support dashes at the beginning of "suffixes". 😅 For instance, the data-wp-style directive accepts CSS variables:

<button
  style="--button-color:blue;"
  data-wp-style----button-color="state.buttonColor"
>A fancy button</button>

@sirreal
Copy link
Member

sirreal commented May 3, 2024

Hey @sirreal, we need to support dashes at the beginning of "suffixes". 😅 For instance, the data-wp-style directive accepts CSS variables:

Good spot 👍

Copy link
Member

@sirreal sirreal left a comment

Choose a reason for hiding this comment

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

The bugfix is good, but I would like to see some better naming generally for the parts of a directive. It doesn't need to happen in this PR.

@@ -12,6 +12,8 @@

- Hooks useMemo and useCallback should return a value. ([#60474](https://github.com/WordPress/gutenberg/pull/60474))

- Prevent wrong written directives from killing the runtime ([#61249](https://github.com/WordPress/gutenberg/pull/61249))
Copy link
Member

Choose a reason for hiding this comment

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

This needs to go up under the unreleased section.

Comment on lines +134 to +137
const prefix = directiveMatch[ 1 ] || '';
const suffix = directiveMatch[ 2 ] || 'default';

obj[ prefix ] = obj[ prefix ] || [];
Copy link
Member

Choose a reason for hiding this comment

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

This section could probably use some comments about how strings are being split up and what we expect to find in each part. We've even got a constant wp that's the directivePrefix (that also appears in the middle of the data attribute).

Honestly, some kind of description of what the parts of a directive are and a refactor to rename things accordingly would be helpful. prefix and suffix don't really make sense:


nothing?       suffix is everything after `prefix--`… I guess this makes sense
   |              |
vvvvvvv       vvvvvvvvvv   
data-wp-bind--on--change
        ^^^^
          |
      "prefix" in the middle?

Maybe we could talk about these things like the "directive name" or "directive type", then the rest of it could be some kind of input to the directive? I think we use these prefix/suffix names across the package and it seems confusing.


The bug fix here where we try to access a null match is good 👍

@cbravobernal cbravobernal force-pushed the fix/prevent-runtime-error-iapi branch from 5a9a7d5 to 1df720c Compare May 7, 2024 18:17
@cbravobernal cbravobernal enabled auto-merge (squash) May 7, 2024 18:24
@cbravobernal cbravobernal merged commit 7f4adc2 into trunk May 7, 2024
61 checks passed
@cbravobernal cbravobernal deleted the fix/prevent-runtime-error-iapi branch May 7, 2024 18:54
@github-actions github-actions bot added this to the Gutenberg 18.4 milestone May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Packages] Interactivity /packages/interactivity [Type] Bug An existing feature does not function as intended
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants