Skip to content

Byte order mark (BOM) breaks ability to do validation #4678

@westonruter

Description

@westonruter

Bug Description

As noted in a support topic, when a byte order mark (BOM) is present in a theme/plugin PHP file, it is prepended to the JSON that is output which then causes the AMP plugin to throw a response_not_json error since the response does not start with {. The user sees this error message:

URL validation failed due to the AMP validation request not returning JSON data. This may be due to a PHP fatal error occurring. Please check your server PHP error logs; to do this you may need to enable WP_DEBUG_LOG. If you are stuck, please search the support forum for possible related topics, or otherwise start a new support topic including the error message, the URL to your site, and your active theme/plugins. Please include your Site Health Info.

The plugin should be more forgiving about the presence of the BOM as well as other leading whitespace.

Additionally, some caching plugins append an HTML comment to the end of the response, for example LiteSpeed Cache (litespeedtech/lscache_wp#212). This also breaks JSON parsing. So any HTML comments that are appended to the response should also be stripped prior to JSON parsing.

Expected Behaviour

Validation responses that have a leading BOM or whitespace, or trailing HTML comment, should all be stripped prior to JSON parsing.

Steps to reproduce

Activate this plugin:

<?php
/**
 * Plugin Name: Add BOM and HTML Comment to End
 */

add_action(
	'init',
	function () {
		echo "\xEF\xBB\xBF ";
	}
);

add_action( 'shutdown', function () {
	if ( did_action( 'wp_footer' ) ) {
		echo "<!-- Generated by me! -->";
	}
} );

Then try to validate a page.


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

Implementation brief

QA testing instructions

Demo

Changelog entry

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions