Skip to content

Block Bindings: Don't throw on non-string values bound to a url attribute - #81533

Open
Mayank-Tripathi32 wants to merge 2 commits into
WordPress:trunkfrom
Mayank-Tripathi32:fix/bindings-url-non-string-crash
Open

Mayank-Tripathi32 wants to merge 2 commits into
WordPress:trunkfrom
Mayank-Tripathi32:fix/bindings-url-non-string-crash

Conversation

@Mayank-Tripathi32

Copy link
Copy Markdown
Member

What

A bindings source value bound to a url attribute is passed to isURLLike, which starts with val.includes( ' ' ). When the value isn't a string that throws a TypeError and the block renders the crash warning instead of its content.

Why

Sources return raw values. core/post-meta hands back the meta value untouched, so a field registered as integer, array or object and bound to an image or button url breaks the block. The falsy check before isURLLike hid this for 0, '', null and false, so only truthy non-strings hit it.

The bindings UI only offers fields whose type matches the attribute, but bindings set in block markup, in patterns, or by third-party sources aren't filtered. This is reachable when the binding comes from somewhere other than that UI.

How

Return false from isURLLike for anything that isn't a string. Such values then take the existing "not a valid URL" branch and the attribute is set to null, which is what already happened for the falsy ones.

The guard is in isURLLike rather than at the call site because the rest of that function is already written defensively (val?.startsWith) and the fix covers the other LinkControl callers too. Happy to move it into BlockEdit instead if reviewers would rather the source contract be enforced there.

Testing Instructions

  • register_post_meta( 'post', 'my_number', [ 'show_in_rest' => true, 'type' => 'integer', 'default' => 123 ] );
  • Add an Image block and bind its url to my_number in the block markup:
    <!-- wp:image {"metadata":{"bindings":{"url":{"source":"core/post-meta","args":{"key":"my_number"}}}}} -->
  • On trunk the block shows "This block has encountered an error and cannot be previewed". With this change it renders the image placeholder.
  • Unit tests cover integer, float, boolean, array and object values, plus the falsy cases.

Use Of AI

Yes, Verified with claude opus.

…bute

isURLLike immediately calls val.includes( ' ' ), so a bound source value
that is a truthy non-string threw a TypeError and crashed the block. The
preceding falsy check masked this for 0, '', null and false, leaving only
truthy non-strings, e.g. post meta registered as integer, array or object.

Return false for anything that isn't a string, so such values fall through
to the existing 'not a valid URL' branch and null the attribute.
@github-actions

Copy link
Copy Markdown

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: Mayank-Tripathi32 <mayanktripathi32@git.wordpress.org>

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

@github-actions github-actions Bot added the [Package] Block editor /packages/block-editor label Aug 12, 2026
@Mayank-Tripathi32 Mayank-Tripathi32 added the [Type] Code Quality Issues or PRs that relate to code quality label Aug 12, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Block editor /packages/block-editor [Type] Code Quality Issues or PRs that relate to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant