Skip to content

Commit da52eb5

Browse files
author
yihao03
committed
Refactor XML escaping logic to use strongEscapeXML for improved security and consistency
1 parent d443ffb commit da52eb5

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

i18n/controllers/translate.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ async function translate(language: string, filePath: string) {
5858

5959
parser.on("text", text => {
6060
if (isRecording) {
61-
currentSegment += `${text}`;
61+
currentSegment += strongEscapeXML(text);
6262
} else {
6363
segments.push([false, text]);
6464
}
@@ -140,7 +140,7 @@ async function translate(language: string, filePath: string) {
140140

141141
clean.on("text", text => {
142142
if (currDepth >= 1) {
143-
translated += escapeXML(text);
143+
translated += strongEscapeXML(text);
144144
}
145145
});
146146

@@ -209,7 +209,7 @@ async function translate(language: string, filePath: string) {
209209
const text = messageContent.text;
210210
// console.log(text.value);
211211

212-
const safeText = escapeXML(text.value);
212+
const safeText: String = escapeXML(text.value);
213213
const textStream = Readable.from("<WRAPPER>" + safeText + "</WRAPPER>");
214214

215215
await new Promise<void>((resolve, reject) => {
@@ -238,3 +238,14 @@ function formatAttributes(attrs) {
238238
function escapeXML(str: string): string {
239239
return str.replace(/&(?!(?:amp;|lt;|gt;|apos;|quot;))/g, "&amp;");
240240
}
241+
242+
243+
244+
function strongEscapeXML(str: string): string {
245+
return str
246+
.replace(/&(?!(?:amp;|lt;|gt;|apos;|quot;))/g, "&amp;")
247+
.replace(/</g, "&lt;")
248+
.replace(/>/g, "&gt;")
249+
.replace(/"/g, "&quot;")
250+
.replace(/'/g, "&apos;");
251+
}

0 commit comments

Comments
 (0)