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

[Fizz] escape <style> textContent as css #28870

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 22 additions & 2 deletions packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -2669,6 +2669,26 @@ function pushStyle(
}
}

/**
* This escaping function is designed to work with style tag textContent only.
*
* While untrusted style content should be made safe before using this api it will
* ensure that the style cannot be early terminated or never terminated state
*/
function escapeStyleTextContent(styleText: string) {
if (__DEV__) {
checkHtmlStringCoercion(styleText);
}
return ('' + styleText).replace(styleRegex, styleReplacer);
}
const styleRegex = /(<\/|<)(s)(tyle)/gi;
const styleReplacer = (
match: string,
prefix: string,
s: string,
suffix: string,
) => `${prefix}${s === 's' ? '\\73 ' : '\\53 '}${suffix}`;
Copy link
Member

Choose a reason for hiding this comment

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

should there be the spaces here? Not exactly sure how that escaping works. '\\73 '

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes. That’s how the end of the escape sequence is encoded in CSS. It should be exactly one.


function pushStyleImpl(
target: Array<Chunk | PrecomputedChunk>,
props: Object,
Expand Down Expand Up @@ -2710,7 +2730,7 @@ function pushStyleImpl(
child !== undefined
) {
// eslint-disable-next-line react-internal/safe-string-coercion
target.push(stringToChunk(escapeTextForBrowser('' + child)));
target.push(stringToChunk(escapeStyleTextContent(child)));
}
pushInnerHTML(target, innerHTML, children);
target.push(endChunkForTag('style'));
Expand Down Expand Up @@ -2752,7 +2772,7 @@ function pushStyleContents(
child !== undefined
) {
// eslint-disable-next-line react-internal/safe-string-coercion
target.push(stringToChunk(escapeTextForBrowser('' + child)));
target.push(stringToChunk(escapeStyleTextContent(child)));
}
pushInnerHTML(target, innerHTML, children);
return;
Expand Down
50 changes: 50 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4256,6 +4256,56 @@ describe('ReactDOMFizzServer', () => {
});
});

describe('<style> textContent escaping', () => {
it('the "S" in "</?[Ss]style" strings are replaced with unicode escaped lowercase s or S depending on case, preserving case sensitivity of nearby characters', async () => {
await act(() => {
const {pipe} = renderToPipeableStream(
<style>{`
.foo::after {
content: 'sSsS</style></Style></StYlE><style><Style>sSsS'
}
body {
background-color: blue;
}
`}</style>,
);
pipe(writable);
});
expect(window.getComputedStyle(document.body).backgroundColor).toMatch(
'blue',
);
});

it('the "S" in "</?[Ss]style" strings are replaced with unicode escaped lowercase s or S depending on case, preserving case sensitivity of nearby characters inside hoistable style tags', async () => {
await act(() => {
const {pipe} = renderToPipeableStream(
<>
<style href="foo" precedence="default">{`
.foo::after {
content: 'sSsS</style></Style></StYlE><style><Style>sSsS'
}
body {
background-color: blue;
}
`}</style>
<style href="bar" precedence="default">{`
.foo::after {
content: 'sSsS</style></Style></StYlE><style><Style>sSsS'
}
body {
background-color: red;
}
`}</style>
</>,
);
pipe(writable);
});
expect(window.getComputedStyle(document.body).backgroundColor).toMatch(
'red',
);
});
});

// @gate enableFizzExternalRuntime
it('supports option to load runtime as an external script', async () => {
await act(() => {
Expand Down