Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

fix(mobile): prevent invalid HTML entities from crashing markdown (#6495)

Simone83ad26c3a3ae
diff --git a/apps/mobile/modules/t3-markdown-text/src/nativeMarkdownText.ts b/apps/mobile/modules/t3-markdown-text/src/nativeMarkdownText.ts
index 719070f3d..8db904b5a 100644
--- a/apps/mobile/modules/t3-markdown-text/src/nativeMarkdownText.ts
+++ b/apps/mobile/modules/t3-markdown-text/src/nativeMarkdownText.ts
@@ -70,15 +70,22 @@ const EMPTY_CONTEXT: RunContext = {
const INLINE_HTML_TAG_PATTERN = /<\/?(?:kbd|mark|sub|sup|u)(?:\s[^>]*)?>/gi;
+function decodeCodePoint(codePoint: number, entity: string): string {
+ if (!Number.isInteger(codePoint) || codePoint < 0 || codePoint > 0x10ffff) {
+ return entity;
+ }
+ return String.fromCodePoint(codePoint);
+}
+
function decodeHtmlEntitiesOnce(value: string): string {
return value.replace(
/&(?:#(\d+)|#x([0-9a-f]+)|amp|apos|gt|lt|nbsp|quot);/gi,
(entity, decimal: string | undefined, hexadecimal: string | undefined) => {
if (decimal) {
- return String.fromCodePoint(Number.parseInt(decimal, 10));
+ return decodeCodePoint(Number.parseInt(decimal, 10), entity);
}
if (hexadecimal) {
- return String.fromCodePoint(Number.parseInt(hexadecimal, 16));
+ return decodeCodePoint(Number.parseInt(hexadecimal, 16), entity);
}
switch (entity.toLowerCase()) {
case "&amp;":
diff --git a/apps/mobile/src/lib/nativeMarkdownText.test.ts b/apps/mobile/src/lib/nativeMarkdownText.test.ts
index 5ad2cca26..867d9e983 100644
--- a/apps/mobile/src/lib/nativeMarkdownText.test.ts
+++ b/apps/mobile/src/lib/nativeMarkdownText.test.ts
@@ -126,6 +126,22 @@ describe("nativeMarkdownTextRuns", () => {
]);
});
+ it.each([
+ ["&#128512;", "😀"],
+ ["&#x1f680;", "🚀"],
+ ["&#9999999999;", "&#9999999999;"],
+ ["&#x110000;", "&#x110000;"],
+ ["&amp;#9999999999;", "&#9999999999;"],
+ ["&amp;#x110000;", "&#x110000;"],
+ ])("normalizes numeric entity %s without throwing", (content, expected) => {
+ const node: MarkdownNode = {
+ type: "paragraph",
+ children: [{ type: "text", content }],
+ };
+
+ expect(nativeMarkdownTextRuns(node)).toEqual([{ text: expected }]);
+ });
+
it("reads inline content from nested text nodes", () => {
const node: MarkdownNode = {
type: "paragraph",