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

Fingerprinting + PostProcess + HTML minify causes HTML validation errors #8884

Closed
internetfreak opened this issue Aug 12, 2021 · 5 comments · Fixed by #8908
Closed

Fingerprinting + PostProcess + HTML minify causes HTML validation errors #8884

internetfreak opened this issue Aug 12, 2021 · 5 comments · Fixed by #8908
Milestone

Comments

@internetfreak
Copy link

internetfreak commented Aug 12, 2021

What version of Hugo are you using (hugo version)?

0.87

Does this issue reproduce with the latest release?

Yes

What is happening?

When building a site that uses fingerprints for the resources while also minifying the final output with the option keepQuotes = false the output will cause HTML validation errors due to the fact that the fingerprint is base64 encoded and can contain = which break HTML when unquoted.
From my observations the final fingerprint is added to the site after the minification took process so the minifier cannot detect the base64 string and keep the quotes.

What to expect?

Minification should take place after all other build steps are done so the output processed by the minifier is definitely the final output and does not get processed any further after minification.
I tested the issue with the online version of the used minifier and it in fact keeps quotes when it encounters a base64 encoded string while removing all other quotes which then allows the document to pass validation

How to reproduce?

  1. Create a minimal hugo site
  2. Add some fingerprinted resource to the site, e.g. {{§css = resources.Get "my/styles.css" | fingerprint}}
  3. Output that file and fingerprint: <link rel="stylesheet" href="{{ $css.RelPermalink }}" integrity="{{ $css.Data.Integrity }}" crossorigin="anonymous">
  4. Set the following config option: keepQuotes = false
  5. Call hugo with hugo --minify
  6. The final build will now have unquoted = characters in its source (if the hash requires them)
@jmooring
Copy link
Member

jmooring commented Aug 13, 2021

Unable to reproduce with v0.87.0.

git clone --single-branch -b hugo-github-issue-8884 https://github.com/jmooring/hugo-testing hugo-github-issue-8884
cd hugo-github-issue-8884
hugo server --minify

@internetfreak
Copy link
Author

I have tested your repo and indeed I am also unable to reproduce it so I went again to my site to test it further and I found the most likely culprit: PostProcess.
I checked the HTML output and noticed that only one stylesheet reference is affected which is my reference of the TailwindCSS styles. In production mode, I call it like this: {{ $tailwindCSS := $tailwindCSS | minify | fingerprint | resources.PostProcess }}
When I watch the html generated by hugo I see that it fills and minifies the output but for the stylesheet that has yet to process there's only a placeholder which is then later replaced by the correct reference but as there's no quotes due to the minification, the error occurs.
Removing resources.PostProcess from the build chain yields the expected result.

@jmooring
Copy link
Member

Verified. Updated MRE:

git clone --single-branch -b hugo-github-issue-8884 https://github.com/jmooring/hugo-testing hugo-github-issue-8884
cd hugo-github-issue-8884
hugo server --minify

Changing PostProcessSuffix = "__e" to PostProcessSuffix = "__e=" in postpub.go fixes the problem, but affects more than just the integrity attribute (a heavy-handed fix).

@jmooring jmooring changed the title Fingerprinting + HTML Minify causes HTML validation errors Fingerprinting + PostProcess + HTML minify causes HTML validation errors Aug 13, 2021
@bep bep added the Bug label Aug 13, 2021
@bep bep added this to the v0.88 milestone Aug 13, 2021
@bep
Copy link
Member

bep commented Aug 19, 2021

@jmooring I think that with minification as part of the publisher chain, the best we currently can do is to make sure the attribute values stays quoted. I don't see any harm in adding a "=" to the suffix. We could do it just for the integrity attribute, but that would be a little "what's next?"

I do have a improved (but little bit more involved design for these post processings) somewhere, with a new defer template keyword. Which would be really cool, but will not happen today.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants