Visitar URL original
fix(compiler): incorrectly encapsulating @import containing colons and semicolons by crisbeto · Pull Request #38716 · angular/angular · GitHub
The Wayback Machine - https://web.archive.org/web/20211022015516/https://github.com/angular/angular/pull/38716
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(compiler): incorrectly encapsulating @import containing colons and semicolons #38716

Closed
wants to merge 3 commits into from

Conversation

@crisbeto
Copy link
Member

@crisbeto crisbeto commented Sep 4, 2020 •

At a high level, the current shadow DOM shim logic works by escaping the content of a CSS rule (e.g. div {color: red;} becomes div {%BLOCK%}), using a regex to parse out things like the selector and the rule body, and then re-adding the content after the selector has been modified.

The problem is that the regex has to be very broad in order capture all of the different use cases, which can cause it to match strings suffixed with a semi-colon in some places where it shouldn't, like this URL from Google Fonts https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&display=swap. Most of the time this is fine, because the logic that escapes the rule content to %BLOCK% will have converted it to something that won't be matched by the regex. However, it breaks down for rules like @import which don't have a body, but can still have quoted content with characters that can match the regex.

These changes resolve the issue by making a second pass over the escaped string and replacing all of the remaining quoted content with %QUOTED% before parsing it with the regex. Once everything has been processed, we make a final pass where we restore the quoted content.

In a previous iteration of this PR, I went with a shorter approach which narrowed down the regex so that it doesn't capture rules without a body. It fixed the issue, but it also ended up breaking some of the more contrived unit test cases. I decided not to pursue it further, because we would've ended up with a very long and brittle regex that likely would've broken in even weirder
ways.

Fixes #38587.

@crisbeto crisbeto force-pushed the 38587/ng-content-import branch from 4b3c1e5 to d2b2e83 Sep 5, 2020
@crisbeto crisbeto marked this pull request as ready for review Sep 5, 2020
@pullapprove pullapprove bot requested a review from JoostK Sep 5, 2020
@ngbot ngbot bot added this to the needsTriage milestone Sep 5, 2020
@ngbot ngbot bot added this to the needsTriage milestone Sep 5, 2020
packages/compiler/src/shadow_css.ts Outdated Show resolved Hide resolved
packages/compiler/src/shadow_css.ts Outdated Show resolved Hide resolved
crisbeto added 2 commits Oct 6, 2020
…d semicolons

At a high level, the current shadow DOM shim logic works by escaping the content of a CSS rule
(e.g. `div {color: red;}` becomes `div {%BLOCK%}`), using a regex to parse out things like the
selector and the rule body, and then re-adding the content after the selector has been modified.
The problem is that the regex has to be very broad in order capture all of the different use cases,
which can cause it to match strings suffixed with a semi-colon in some places where it shouldn't,
like this URL from Google Fonts `https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&display=swap`.
Most of the time this is fine, because the logic that escapes the rule content to `%BLOCK%` will
have converted it to something that won't be matched by the regex. However, it breaks down for rules
like `@import` which don't have a body, but can still have quoted content with characters that can
match the regex.

These changes resolve the issue by making a second pass over the escaped string and replacing all
of the remaining quoted content with `%QUOTED%` before parsing it with the regex. Once everything
has been processed, we make a final pass where we restore the quoted content.

In a previous iteration of this PR, I went with a shorter approach which narrowed down the
regex so that it doesn't capture rules without a body. It fixed the issue, but it also ended
up breaking some of the more contrived unit test cases. I decided not to pursue it further, because
we would've ended up with a very long and brittle regex that likely would've broken in even weirder
ways.

Fixes angular#38587.
@crisbeto crisbeto force-pushed the 38587/ng-content-import branch from d2b2e83 to 0c395d6 Oct 6, 2020
@crisbeto
Copy link
Member Author

@crisbeto crisbeto commented Oct 6, 2020 •

Sorry for taking WAY too long with addressing the feedback @JoostK. Can you take one more look? Everything should be addressed now.

JoostK
JoostK approved these changes Oct 6, 2020
Copy link
Member

@JoostK JoostK left a comment

LGTM, with a suggestion for adding two testcases

@atscott
Copy link
Contributor

@atscott atscott commented Oct 9, 2020

@atscott atscott closed this in 7f689a2 Oct 9, 2020
atscott added a commit that referenced this issue Oct 9, 2020
…d semicolons (#38716)

At a high level, the current shadow DOM shim logic works by escaping the content of a CSS rule
(e.g. `div {color: red;}` becomes `div {%BLOCK%}`), using a regex to parse out things like the
selector and the rule body, and then re-adding the content after the selector has been modified.
The problem is that the regex has to be very broad in order capture all of the different use cases,
which can cause it to match strings suffixed with a semi-colon in some places where it shouldn't,
like this URL from Google Fonts `https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&display=swap`.
Most of the time this is fine, because the logic that escapes the rule content to `%BLOCK%` will
have converted it to something that won't be matched by the regex. However, it breaks down for rules
like `@import` which don't have a body, but can still have quoted content with characters that can
match the regex.

These changes resolve the issue by making a second pass over the escaped string and replacing all
of the remaining quoted content with `%QUOTED%` before parsing it with the regex. Once everything
has been processed, we make a final pass where we restore the quoted content.

In a previous iteration of this PR, I went with a shorter approach which narrowed down the
regex so that it doesn't capture rules without a body. It fixed the issue, but it also ended
up breaking some of the more contrived unit test cases. I decided not to pursue it further, because
we would've ended up with a very long and brittle regex that likely would've broken in even weirder
ways.

Fixes #38587.

PR Close #38716
@angular-automatic-lock-bot
Copy link

@angular-automatic-lock-bot angular-automatic-lock-bot bot commented Nov 9, 2020

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Nov 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

4 participants