Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(i18n): disable i18next escapeValue (React renderer is the safety boundary) (#14369)

simonrosenbergbdf3b7b44f72
diff --git a/frontend/__tests__/components/shared/modals/settings/settings-form.test.tsx b/frontend/__tests__/components/shared/modals/settings/settings-form.test.tsx
index 2e989428ec..ab478f7803 100644
--- a/frontend/__tests__/components/shared/modals/settings/settings-form.test.tsx
+++ b/frontend/__tests__/components/shared/modals/settings/settings-form.test.tsx
@@ -2,7 +2,7 @@ import userEvent from "@testing-library/user-event";
import { describe, expect, it, vi } from "vitest";
import { renderWithProviders } from "test-utils";
import { createRoutesStub } from "react-router";
-import { screen } from "@testing-library/react";
+import { screen, waitFor } from "@testing-library/react";
import SettingsService from "#/api/settings-service/settings-service.api";
import { SettingsForm } from "#/components/shared/modals/settings/settings-form";
import { DEFAULT_SETTINGS } from "#/services/settings";
@@ -28,14 +28,16 @@ describe("SettingsForm", () => {
const saveButton = screen.getByRole("button", { name: /save/i });
await user.click(saveButton);
- expect(saveSettingsSpy).toHaveBeenCalledWith(
- expect.objectContaining({
- agent_settings_diff: expect.objectContaining({
- llm: expect.objectContaining({
- model: getAgentSettingValue(DEFAULT_SETTINGS, "llm.model"),
+ await waitFor(() =>
+ expect(saveSettingsSpy).toHaveBeenCalledWith(
+ expect.objectContaining({
+ agent_settings_diff: expect.objectContaining({
+ llm: expect.objectContaining({
+ model: getAgentSettingValue(DEFAULT_SETTINGS, "llm.model"),
+ }),
}),
}),
- }),
+ ),
);
});
});
diff --git a/frontend/__tests__/i18n/escape-value.test.tsx b/frontend/__tests__/i18n/escape-value.test.tsx
new file mode 100644
index 0000000000..3c60cd6784
--- /dev/null
+++ b/frontend/__tests__/i18n/escape-value.test.tsx
@@ -0,0 +1,53 @@
+import { render } from "@testing-library/react";
+import { Trans } from "react-i18next";
+import { describe, it, expect, beforeAll } from "vitest";
+import i18n from "#/i18n";
+
+/**
+ * Regression: i18next's default ``escapeValue: true`` double-escaped
+ * interpolated values because React's text renderer also escapes β€”
+ * producing entity-encoded text like ``/tmp/foo`` in the DOM.
+ * ``index.ts`` flips it to ``false``; this test guards against accidental
+ * reversion.
+ */
+describe("i18next interpolation", () => {
+ beforeAll(async () => {
+ // Register a tiny test bundle so we don't depend on translation.json.
+ i18n.addResourceBundle(
+ "en",
+ "translation",
+ { TEST_PATH_INTERPOLATION: "Reading {{path}}" },
+ true,
+ true,
+ );
+ await i18n.changeLanguage("en");
+ });
+
+ it("renders interpolated paths verbatim (no HTML-entity escaping)", () => {
+ const { container } = render(
+ <Trans
+ i18nKey="TEST_PATH_INTERPOLATION"
+ values={{ path: "/tmp/pr14227/part_037.diff" }}
+ />,
+ );
+ expect(container.textContent).toBe(
+ "Reading /tmp/pr14227/part_037.diff",
+ );
+ // Specifically: the forward slashes must not be encoded.
+ expect(container.innerHTML).not.toContain("&#x2F;");
+ expect(container.innerHTML).not.toContain("&#x2f;");
+ });
+
+ it("still renders potentially-dangerous characters as literal text (React-side safety boundary)", () => {
+ const { container } = render(
+ <Trans
+ i18nKey="TEST_PATH_INTERPOLATION"
+ values={{ path: "<script>alert(1)</script>" }}
+ />,
+ );
+ // React's text renderer turns ``<``/``>`` into entity references β€” they
+ // appear as literal characters in textContent and do NOT execute.
+ expect(container.textContent).toContain("<script>alert(1)</script>");
+ expect(container.querySelector("script")).toBeNull();
+ });
+});
diff --git a/frontend/src/components/features/chat/mono-component.tsx b/frontend/src/components/features/chat/mono-component.tsx
index a279e85548..5b9af2b296 100644
--- a/frontend/src/components/features/chat/mono-component.tsx
+++ b/frontend/src/components/features/chat/mono-component.tsx
@@ -1,36 +1,13 @@
import { ReactNode } from "react";
-import EventLogger from "#/utils/event-logger";
-
-const decodeHtmlEntities = (text: string): string => {
- const textarea = document.createElement("textarea");
- textarea.innerHTML = text;
- return textarea.value;
-};
-
-function MonoComponent(props: { children?: ReactNode }) {
- const { children } = props;
-
- const decodeString = (str: string): string => {
- try {
- return decodeHtmlEntities(str);
- } catch (e) {
- EventLogger.error(String(e));
- return str;
- }
- };
-
- if (Array.isArray(children)) {
- const processedChildren = children.map((child) =>
- typeof child === "string" ? decodeString(child) : child,
- );
-
- return <strong className="font-mono">{processedChildren}</strong>;
- }
-
- if (typeof children === "string") {
- return <strong className="font-mono">{decodeString(children)}</strong>;
- }
+/**
+ * Renders monospaced bold text. Used as a ``<Trans>`` component override
+ * for the ``<cmd>`` tag in translation strings. Receives children verbatim
+ * β€” i18next no longer double-escapes interpolated values (see
+ * ``i18n/index.ts``'s ``escapeValue: false``), so the prior
+ * ``decodeHtmlEntities`` step had nothing to undo.
+ */
+function MonoComponent({ children }: { children?: ReactNode }) {
return <strong className="font-mono">{children}</strong>;
}
diff --git a/frontend/src/components/features/chat/path-component.tsx b/frontend/src/components/features/chat/path-component.tsx
index e431bb900a..28fec7ea2a 100644
--- a/frontend/src/components/features/chat/path-component.tsx
+++ b/frontend/src/components/features/chat/path-component.tsx
@@ -1,16 +1,4 @@
import { ReactNode } from "react";
-import EventLogger from "#/utils/event-logger";
-
-/**
- * Decodes HTML entities in a string
- * @param text The text to decode
- * @returns The decoded text
- */
-const decodeHtmlEntities = (text: string): string => {
- const textarea = document.createElement("textarea");
- textarea.innerHTML = text;
- return textarea.value;
-};
/**
* Checks if a path is likely a directory
@@ -47,40 +35,32 @@ const extractFilename = (path: string): string => {
};
/**
- * Component that displays only the filename in the text but shows the full path on hover
- * Similar to MonoComponent but with path-specific functionality
+ * ``<Trans>`` component override for the ``<path>`` tag in translation
+ * strings: displays the filename in the visible text and the full path
+ * on hover (via the ``title`` attribute).
+ *
+ * Receives children verbatim β€” i18next no longer double-escapes
+ * interpolated values (see ``i18n/index.ts``'s ``escapeValue: false``),
+ * so the prior ``decodeHtmlEntities`` step had nothing to undo.
*/
function PathComponent(props: { children?: ReactNode }) {
const { children } = props;
- const processPath = (path: string) => {
- try {
- // First decode any HTML entities in the path
- const decodedPath = decodeHtmlEntities(path);
- // Extract the filename from the decoded path
- const filename = extractFilename(decodedPath);
- return (
- <span className="font-mono" title={decodedPath}>
- {filename}
- </span>
- );
- } catch (e) {
- // Just log the error without any message to avoid localization issues
- EventLogger.error(String(e));
- return <span className="font-mono">{path}</span>;
- }
- };
+ const renderPath = (path: string) => (
+ <span className="font-mono" title={path}>
+ {extractFilename(path)}
+ </span>
+ );
if (Array.isArray(children)) {
const processedChildren = children.map((child) =>
- typeof child === "string" ? processPath(child) : child,
+ typeof child === "string" ? renderPath(child) : child,
);
-
return <strong className="font-mono">{processedChildren}</strong>;
}
if (typeof children === "string") {
- return <strong>{processPath(children)}</strong>;
+ return <strong>{renderPath(children)}</strong>;
}
return <strong className="font-mono">{children}</strong>;
diff --git a/frontend/src/i18n/index.ts b/frontend/src/i18n/index.ts
index 3234f41c11..41269286a9 100644
--- a/frontend/src/i18n/index.ts
+++ b/frontend/src/i18n/index.ts
@@ -37,6 +37,17 @@ i18n
// Do NOT set nonExplicitSupportedLngs: true as it causes 404 errors
// for region-specific codes not in supportedLngs (per i18next developer)
nonExplicitSupportedLngs: false,
+
+ interpolation: {
+ // React already escapes text content before rendering, so i18next's
+ // default ``escapeValue: true`` produces double-escaped output β€”
+ // an interpolated path like ``/tmp/foo`` surfaces as
+ // ``&#x2F;tmp&#x2F;foo`` in the rendered DOM because React's text
+ // pipeline doesn't decode entities back. Disable here; React's
+ // renderer is the proper safety boundary against XSS in interpolated
+ // strings. See: https://www.i18next.com/translation-function/interpolation
+ escapeValue: false,
+ },
});
export default i18n;