Skip to content

Commit

Permalink
Add more escape characters to the inline stream component (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanto authored Oct 26, 2024
1 parent b7dcfaf commit 9c425c7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
5 changes: 5 additions & 0 deletions examples/kitchen-sink/src/pages/nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ export default function Nav() {
description="Dedupe data requests with React's cache helper."
href="/react/cache"
/>
<ExampleLink
title="Logging"
description="RSC server to client logging."
href="/react/logging"
/>
</ExampleGroup>

<ExampleGroup name="Server actions" path="/server-actions">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use client";

export function DisplayText({ text }: { text: string }) {
return <p>{text}</p>;
}
25 changes: 25 additions & 0 deletions examples/kitchen-sink/src/pages/react/logging/index.page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { DisplayText } from "./client-component";

export default function LoggingPage() {
console.log("Hello world!");
console.log("Hello <b>html</b> world!");
console.log("Hello <script></script> world!");

return (
<div className="space-y-4">
<div>
<h2>These should be escaped</h2>
<p>{"A string with some HTML: <b>Hello</b><script></script>"}</p>
<DisplayText text="Hello <b>world!</b><script></script>" />
</div>
<div>
<h2>This should contain HTML</h2>
<p
dangerouslySetInnerHTML={{
__html: "A string with some <b>HTML</b><script></script>",
}}
></p>
</div>
</div>
);
}
12 changes: 7 additions & 5 deletions packages/framework/src/components/inline-rsc-stream.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,21 @@ async function Inline({

// From https://github.com/cyco130/vite-rsc/blob/2e3d0ad9915e57c4b2eaa3ea24b46c1b477a4cce/packages/fully-react/src/server/htmlescape.ts#L25C1-L38C2

const TERMINATORS_LOOKUP: Record<string, string> = {
let ESCAPE_LOOKUP: Record<string, string> = {
">": "\\u003e",
"<": "\\u003c",
"\u2028": "\\u2028",
"\u2029": "\\u2029",
};

const TERMINATORS_REGEX = /[\u2028\u2029]/g;
let ESCAPE_REGEX = /[><\u2028\u2029]/g;

function sanitizer(match: string | number) {
return TERMINATORS_LOOKUP[match];
function escaper(match: string | number) {
return ESCAPE_LOOKUP[match];
}

function sanitize(str: string) {
return str.replace(TERMINATORS_REGEX, sanitizer);
return str.replace(ESCAPE_REGEX, escaper);
}

export class TeardownOnError extends Component<
Expand Down

0 comments on commit 9c425c7

Please sign in to comment.