Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 0923dd3

Browse files
committed
Sanitize untrusted variables from message previews before translation
Fixes element-hq/element-web#18314
1 parent 1efd226 commit 0923dd3

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/languageHandler.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,17 @@ export function _t(text: string, variables?: IVariables, tags?: Tags): Translate
160160
}
161161
}
162162

163+
/**
164+
* Sanitizes unsafe text for the sanitizer, ensuring references to variables will not be considered
165+
* replaceable by the translation functions.
166+
* @param {string} text The text to sanitize.
167+
* @returns {string} The sanitized text.
168+
*/
169+
export function sanitizeForTranslation(text: string): string {
170+
// Add a non-breaking space so the regex doesn't trigger when translating.
171+
return text.replace(/\$\(([^)]*)\)/g, '$\xa0($1)');
172+
}
173+
163174
/*
164175
* Similar to _t(), except only does substitutions, and no translation
165176
* @param {string} text The text, e.g "click <a>here</a> now to %(foo)s".

src/stores/room-list/previews/MessageEventPreview.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
import { IPreview } from "./IPreview";
1818
import { TagID } from "../models";
1919
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
20-
import { _t } from "../../../languageHandler";
20+
import { _t, sanitizeForTranslation } from "../../../languageHandler";
2121
import { getSenderName, isSelf, shouldPrefixMessagesIn } from "./utils";
2222
import ReplyThread from "../../../components/views/elements/ReplyThread";
2323
import { getHtmlText } from "../../../HtmlUtils";
@@ -58,6 +58,8 @@ export class MessageEventPreview implements IPreview {
5858
body = getHtmlText(body);
5959
}
6060

61+
body = sanitizeForTranslation(body);
62+
6163
if (msgtype === 'm.emote') {
6264
return _t("* %(senderName)s %(emote)s", { senderName: getSenderName(event), emote: body });
6365
}

0 commit comments

Comments
 (0)