Skip to content

Fix integrity hash not rendering due to quote mismatch - #34

Merged
rmccue merged 3 commits into
humanmade:masterfrom
mehulkaklotar:fix/integrity-hash-quote-mismatch
Oct 2, 2026
Merged

rmccue merged 3 commits into
humanmade:masterfrom
mehulkaklotar:fix/integrity-hash-quote-mismatch

Conversation

@mehulkaklotar

Copy link
Copy Markdown
Contributor

Problem

The output_integrity_for_script() and output_integrity_for_style() functions use str_replace() looking for single-quoted attributes (src=' and href='), but WordPress core and plugins may output script/style tags with double quotes.

This causes the integrity hash to be generated but never rendered in the HTML output.

Solution

Replace str_replace() with preg_replace() using a pattern that captures the quote character (['"]) and reuses it in the replacement string. This supports both single and double quote styles.

Before

$tag = str_replace(
    " src='",
    sprintf( " integrity='%s' src='", esc_attr( $hash ) ),
    $tag
);

After

$tag = preg_replace(
    '/( src=)(['\''"])/',
    sprintf( ' integrity=$2%s$2$1$2', esc_attr( $hash ) ),
    $tag,
    1
);

Testing

  1. Enqueue a script with a local URL
  2. Verify the integrity attribute is added regardless of whether the tag uses single or double quotes

The output_integrity_for_script() and output_integrity_for_style() functions
use str_replace() looking for single-quoted attributes (src=' and href='),
but WordPress and plugins may output tags with double quotes.

This change uses preg_replace() with a pattern that captures the quote
character and reuses it in the replacement, supporting both quote styles.

@jerico jerico left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Regex correctly handles both quote styles.

Note: Travis is failing because of PHP version, unrelated to this PR

@mikelittle

Copy link
Copy Markdown
Contributor

I'd like to see some automated tests for the cases this fix covers, please. We missed them in the existing tests, so it would be great to know we can confirm the functionality and easily spot any regression.

Cover single- and double-quoted src/href for scripts and styles, and the
no-hash passthrough case.
@mehulkaklotar
mehulkaklotar force-pushed the fix/integrity-hash-quote-mismatch branch from 7f07e28 to eb2b47a Compare September 30, 2026 12:06
@mehulkaklotar

Copy link
Copy Markdown
Contributor Author

Thanks for the review. @jerico @mikelittle I've added the unit tests in tests/class-test-integrity-quote-styles.php:

  • Single-quoted and double-quoted src for output_integrity_for_script()
  • Single-quoted and double-quoted href for output_integrity_for_style()
  • A script and a style with no hash set, which return the tag unchanged

The full suite passes locally in the humanmade/plugin-tester image (15 tests, 16 assertions). The old str_replace code only handled single quotes, so the double-quote cases cover the bug this PR fixes.

@rmccue rmccue left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The regexes are overly complex with the capture groups here for no reason, which hurts readability of the code; in both cases, the attribute is fixed so there's no point capturing it.

Comment thread inc/namespace.php Outdated
Comment on lines +356 to +358
'/( src=)([\'"])/',
sprintf(
" integrity='%s' src='",
' integrity=$2%s$2$1$2',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
'/( src=)([\'"])/',
sprintf(
" integrity='%s' src='",
' integrity=$2%s$2$1$2',
'/ src=([\'"])/',
sprintf(
' integrity=$1%s$1 src=$1',

Comment thread inc/namespace.php Outdated
Comment on lines +385 to +387
'/( href=)([\'"])/',
sprintf(
" integrity='%s' href='",
' integrity=$2%s$2$1$2',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
'/( href=)([\'"])/',
sprintf(
" integrity='%s' href='",
' integrity=$2%s$2$1$2',
'/ href=([\'"])/',
sprintf(
' integrity=$1%s$1 href=$1',

@mehulkaklotar

Copy link
Copy Markdown
Contributor Author

Thanks @rmccue, good catch. I've applied both suggestions.

@jerico
jerico requested a review from rmccue October 1, 2026 05:56

@rmccue rmccue left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@rmccue
rmccue merged commit 70d7fdd into humanmade:master Oct 2, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants