Skip to content

Commit 464285f

Browse files
authored
Bugfix/snyk 2023-08-18 (#6555)
* ensure file normalization in exception handler * escape string before using it in regexp * escape exception
1 parent 3032bb5 commit 464285f

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/core/http.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import { existsSync } from "fs/mod.ts";
8-
import { basename, extname, join, posix } from "path/mod.ts";
8+
import { basename, extname, join, normalize, posix } from "path/mod.ts";
99
import { error, info } from "log/mod.ts";
1010

1111
import * as colors from "fmt/colors.ts";
@@ -150,6 +150,11 @@ export function httpFileRequestHandler(
150150
}
151151
}
152152
} catch (e) {
153+
// it's possible for an exception to occur before we've normalized the path
154+
// so we need to renormalize it here
155+
if (fsPath) {
156+
fsPath = normalize(fsPath);
157+
}
153158
response = await serveFallback(
154159
req,
155160
e,

src/core/language.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ const translationCache = cacheMap(
4848
},
4949
);
5050

51+
// https://stackoverflow.com/a/6969486
52+
function escapeRegExp(str: string) {
53+
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
54+
}
55+
5156
export async function readLanguageTranslations(
5257
translationFile: string,
5358
lang?: string,
@@ -66,7 +71,8 @@ export async function readLanguageTranslations(
6671

6772
// determine additional variations to read
6873
const ext = extname(translationFile);
69-
const [dir, stem] = dirAndStem(translationFile);
74+
let [dir, stem] = dirAndStem(translationFile);
75+
stem = escapeRegExp(stem);
7076
const variations: string[] = [];
7177
if (lang) {
7278
// enumerate variations dictated by this lang

src/resources/formats/revealjs/plugins/chalkboard/plugin.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,15 @@ console.warn( "toggleNotesButton is deprecated, use customcontrols plugin instea
671671
} );
672672
a.href = window.URL.createObjectURL( blob );
673673
} catch ( error ) {
674+
// https://stackoverflow.com/a/6234804
675+
// escape data for proper handling of quotes and line breaks
676+
// in case malicious gets a chance to craft the exception message
677+
error = String(error).replace(/&/g, "&")
678+
.replace(/</g, "&lt;")
679+
.replace(/>/g, "&gt;")
680+
.replace(/"/g, "&quot;")
681+
.replace(/'/g, "&#039;");
682+
674683
a.innerHTML += ' (' + error + ')';
675684
}
676685
a.click();

0 commit comments

Comments
 (0)