Amp-wp: AMP causes gutenberg editor crash on very long posts

Created on 1 Sep 2020  路  9Comments  路  Source: ampproject/amp-wp

Bug Description

On a very long post (more than 120000 characters in total), when the AMP plugin is active, the gutenberg editor will fail to load with the following error on the console:

index.js?ver=e60bd1460738ba2b6388866b5ee392db:1 Uncaught (in promise) TypeError: Cannot read property 'length' of null 

Screen Shot 2020-09-01 at 6 04 39 PM

When creating text content, I recommend this online string generator, some others actually have a silent limit on the total number of characters generated

https://onlinerandomtools.com/generate-random-string

The classic editor will still be able to load the post, the published post still renders correctly on the live site, but the gutenberg editor will completely fail to load with the above error on the console.

The error seems to be thrown from within gutenberg code, but I found that after disabling the AMP plugin, the editor loads correctly

Sep-01-2020 18-34-12

Bug Changelogged Editor Core

Most helpful comment

@mdrovdahl @roo2 v2.0.1 is now published on WordPress.org.

All 9 comments

Here's the stacktrace with source maps:

image

The error seems to be due to document.length being undefined:

image

I haven't found the reason as to how the plugin causes this error as yet, however.

So the issue lies within this block of code: https://github.com/ampproject/amp-wp/blob/a6514231d6e15568d7698c86eb5f404b552e3ff2/src/ObsoleteBlockAttributeRemover.php#L95-L100

This issue is that the preg_replace_callback is returning null instead of the original string. According to the docs that only happens when an error has occurred:

preg_replace_callback() returns an array if the subject parameter is an array, or a string otherwise. On errors the return value is NULL.

In this case the error is due to a catastrophic amount of backtracing being done on the input. For example, if you have:

preg_replace(
    '#(?P<block_comment><!--\s*+wp:\w+.*?-->\s*+)(?P<start_tag><[a-z][a-z0-9_:-]*+\s[^>]*+>)#s',
    'replacement',
    '<!-- wp:paragraph -->' . str_repeat('a', 111101)
);

It would take 1000054 steps for the regex engine to figure out that the there is no < character after <!-- wp:paragraph -->. Here's a 3V4L demoing the issue.

I see. And so this PHP error is causing content.raw to be null and not a string, and thus length is not defined.

Good investigation. We'll ship a fix for this in v2.0.1.

Hi @westonruter =)

Just adding a +1 as we have a popular site that ran into this today. We're going to rollback to 1.5.5 until the fix lands.

Fix should be out this week. Tomorrow even.

@roo2 @mdrovdahl Actually, the fix (#5313) has landed just now. We have a 2.0.1 pre-release build for you to test if you're willing: https://storage.googleapis.com/ampwp_github_artifacts/refs/heads/2.0/prod/amp.zip (URL to ZIP reflects current state of 2.0 branch)

hey thanks for getting this fix out so quickly @westonruter , and yep I can confirm that the 2.0.1-pre-release fixes the issue we've been seeing 馃憤

Thank you for testing!

Moving to QA Passed.

@mdrovdahl @roo2 v2.0.1 is now published on WordPress.org.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alexhaller picture alexhaller  路  5Comments

miina picture miina  路  5Comments

westonruter picture westonruter  路  4Comments

westonruter picture westonruter  路  3Comments

miina picture miina  路  5Comments