Skip to content

Commit 85ffaef

Browse files
authored
Merge pull request #12 from wuespace/alert-autofix-1
Potential fix for code scanning alert no. 1: Incomplete string escaping or encoding
2 parents fec0aaa + 6498084 commit 85ffaef

5 files changed

Lines changed: 24 additions & 18 deletions

File tree

example/locales/de.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"Hello world!": "Hallo Welt!",
33
"Lazy term": "Fauler Begriff",
4-
"This text contains {0} \\{} curly braces and \\\\{\\{}}\\{0}.": "Dieser Text enthält {0} \\{} geschweifte Klammern und \\\\{\\{}}\\{0}.",
5-
"Untranslated text": ""
4+
"Untranslated text": "",
5+
"Unknown Error": "",
6+
"An unexpected error occurred while processing your request.": "",
7+
"This text contains {0} \\{} curly braces and \\\\\\{\\{}}\\{0}.": "Dieser Text enthält {0} \\{} geschweifte Klammern und \\\\\\{\\{}}\\{0}."
68
}

example/locales/en.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"Hello world!": "Hello world!",
33
"Lazy term": "Lazy term",
4-
"This text contains {0} \\{} curly braces and \\\\{\\{}}\\{0}.": "This text contains {0} \\{} curly braces and \\\\{\\{}}\\{0}.",
5-
"Untranslated text": "Untranslated text"
4+
"Untranslated text": "Untranslated text",
5+
"Unknown Error": "Unknown Error",
6+
"An unexpected error occurred while processing your request.": "An unexpected error occurred while processing your request.",
7+
"This text contains {0} \\{} curly braces and \\\\\\{\\{}}\\{0}.": "This text contains {0} \\{} curly braces and \\\\\\{\\{}}\\{0}."
68
}

lib/common/escapeKey.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
export function escapeKey(key: string): string {
2-
return key.replaceAll(/{/g, "\\{");
2+
// Escape all backslashes first
3+
key = key.replace(/\\/g, "\\\\");
4+
// Then escape curly braces
5+
return key.replace(/[{]/g, "\\{");
36
}

lib/common/unescapeKey.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export function unescapeKey(key: string): string {
2-
return key.replaceAll(/\\{/g, "{");
2+
return key.replaceAll(/\\{/g, "{").replaceAll(/\\\\/g, "\\");
33
}

lib/extract/TypeScriptSourceFile.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class TypeScriptSourceFile {
6464
this.processNode(this.getSourceFile(), extractions);
6565
}
6666

67-
private processNode(node: ts.Node, extractions = this.extractions) {
67+
private processNode(node: ts.Node, extractions: Extractions) {
6868
if (TypeScriptSourceFile.isTemplateString(node)) {
6969
this.processTemplateString(node, this.getSourceFile(), extractions);
7070
}
@@ -84,18 +84,17 @@ export class TypeScriptSourceFile {
8484
sourceFile: ts.SourceFile,
8585
extractions: Extractions,
8686
) {
87-
let templateString: string;
88-
const tpl = node.template;
89-
if (ts.isNoSubstitutionTemplateLiteral(tpl)) {
90-
templateString = escapeKey(tpl.text);
91-
} else if (ts.isTemplateExpression(tpl)) {
92-
let templateParts = escapeKey(tpl.head.text);
93-
tpl.templateSpans.forEach((span, index) => {
94-
templateParts += "{" + index + "}" + escapeKey(span.literal.text);
87+
let templateString = "";
88+
const template = node.template;
89+
if (ts.isNoSubstitutionTemplateLiteral(template)) {
90+
templateString = escapeKey(template.text);
91+
} else if (ts.isTemplateExpression(template)) {
92+
let combinedTemplateParts = escapeKey(template.head.text);
93+
template.templateSpans.forEach((span, index) => {
94+
combinedTemplateParts += "{" + index + "}" +
95+
escapeKey(span.literal.text);
9596
});
96-
templateString = templateParts;
97-
} else {
98-
templateString = "";
97+
templateString = combinedTemplateParts;
9998
}
10099
const { line } = sourceFile.getLineAndCharacterOfPosition(node.getStart());
101100
extractions.addExtraction(templateString, this.fileName, line + 1);

0 commit comments

Comments
 (0)