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

Fix: escape double quote of url value in CSS url() #7718

Merged

Conversation

Shinyaigeek
Copy link
Contributor

@Shinyaigeek Shinyaigeek commented Feb 15, 2022

Fixes: #7710

↪️ Pull Request

Hi team! 👋

In css transforming, CSSTransformer encloses URL value in CSS url() with " double quote, so I fixed this simply by escaping double quote in url value in url() , but honestly I'm worried that this way is too straightforward and I overlooked some context.

If there is something I missed, please be free to point out or close this PR 🙇

💻 Examples

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>CSS Double</title>
  </head>
  <body>
    <h1>CSS Double</h1>
    <div class="background"></div>
    <style>
      .background {
        background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="32" height="24" viewBox="0 0 32 24"><polygon points="0,0 32,0 16,24" style="fill: rgb%2819, 80, 91%29"></polygon></svg>');
        width: 100px;
        height: 100px;
      }
    </style>
  </body>
</html>

🚨 Test instructions

simply, I checked with the case base64 encoded url value with "

✔️ PR Todo

  • Added/updated unit tests for this change
  • Filled out test instructions (In case there aren't any unit tests)
  • Included links to related issues/PRs

@height
Copy link

height bot commented Feb 15, 2022

Link Height tasks by mentioning a task ID in the pull request title or commit messages, or description and comments with the keyword link (e.g. "Link T-123").

💡Tip: You can also use "Close T-X" to automatically close a task when the pull request is merged.

@@ -83,7 +83,8 @@ export default (new Transformer({
symbols: new Map([['*', {local: '*', isWeak: true, loc}]]),
});
} else if (dep.type === 'url') {
asset.addURLDependency(dep.url, {
// if dependency imported with url() includes ", escape " double quotes because this url value will be enclosed with double quote "
asset.addURLDependency(dep.url.replace(/"/g, '\\"'), {
Copy link
Member

Choose a reason for hiding this comment

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

hmm I think this might cause an issue for resolvers because they won't expect pre-escaped values. Perhaps this replacement should be done in the packager. The util here is what does the replacement of placeholders to resolved urls. It might need an option to escape the url.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, indeed. As you say, escaping the background image’s URL in Transformer will cause the not found error in resolver. I will fix this 👍 Thank you for your advice!

I think escaping the quote in URL is needed in anyway because

  • if URL is not enclosed with quotes, it is not a valid property that the quote exists in the URL
  • if URL is enclosed with quotes, the quote should be escaped

#7564 as you can see in this PR, I think we need to enclose the URL in quotes and then escape the quotes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking about this issue, and I notice that parcel's resolver cannot resolve the filepath of the image where the quotes are escaped though such a URL is the correct URL in the first place. So I think escape should be done with CSSTransformer and the resolver should be modified.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about making the resolver able to handle escaped quotes, but I was concerned about performance degradation due to increased computation, so I made a change to remove escaped quotes from URLs with the CSSTransformer and re-escape them with the CSSPackager. ea56294

@Shinyaigeek Shinyaigeek force-pushed the fix/escape-url-value-in-css-url branch 2 times, most recently from 7e8ae38 to ea56294 Compare March 6, 2022 10:03
@Shinyaigeek Shinyaigeek force-pushed the fix/escape-url-value-in-css-url branch from 6980ae3 to 0e3f3bd Compare March 20, 2022 04:55
@Shinyaigeek Shinyaigeek force-pushed the fix/escape-url-value-in-css-url branch from 0e3f3bd to 483258c Compare March 20, 2022 05:33
placeholder: dep.placeholder,
asset.addURLDependency(
// when URL is escaped, parcel resolver cannot resolve URL, so we need to unescape it in transformer
dep.url.replace(/\\"/g, '"').replace(/\\'/g, "'"),
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is necessary. The url will already be unescaped before it is passed to us.

@devongovett
Copy link
Member

Thanks for getting this started @Shinyaigeek! I refactored it a little so that we handle the escaping differently depending on the asset type. For example, in HTML, quotes become &quot;. Also supported escaping \ itself in CSS. Hopefully the changes make sense.

@devongovett devongovett merged commit 7d2c1ef into parcel-bundler:v2 Mar 20, 2022
@Shinyaigeek
Copy link
Contributor Author

Thank you for your help @devongovett !! I saw your commit, and feels reasonable. Sure, escaping only CSS text was short-circuited and should have been able to escape other files in the parcel pipeline! I learned a lot from that 👍

gorakong pushed a commit that referenced this pull request Nov 3, 2022
* upstream/v2:
  Upgrade Flow to 0.174.1 (#7849)
  v2.4.0
  v2.4.0 changelog
  Bump Parcel CSS
  Dynamic imports priority fix closes #6980 (#7061)
  fix(transformers): errors.map is not a function (#7672)
  Make NodeResolver check realpath before resolving with `source` entry (#7846)
  docs: fix wrong location documents (#7689)
  Fix: escape double quote of url value in CSS `url()` (#7718)
  Update @parcel/css and add diagnostic for url dependencies in custom properties (#7845)
  Use relative path for bundle labels in bundle analysis (#7737)
  Allow use react-jsx transform in React 16.14.0 (#7728)
  Move to @parcel/css by default (#7821)
  Feature: pick PORT number also from .env file (#7819)
  Enable parsing static initialization blocks (#7839)
  Bump swc and prevent pure comment removal (#7833)
  Bump swc (#7777)
  Human readable file size in bundle analyzer report (#7766)
  Improve emoji support detection (#7775)
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.

Broken url strings in CSS
4 participants