Skip to content

Commit 36f4caa

Browse files
committed
Add runtime check of surrounding characters to prevent corruption
1 parent 03ed9c3 commit 36f4caa

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

web_src/js/markup/tasklist.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ export function initMarkupTasklist() {
2929

3030
const encoder = new TextEncoder();
3131
const buffer = encoder.encode(oldContent);
32+
// Indexes may fall off the ends and return undefined.
33+
if (buffer[position - 1] !== '['.codePointAt(0) ||
34+
buffer[position] !== ' '.codePointAt(0) && buffer[position] !== 'x'.codePointAt(0) ||
35+
buffer[position + 1] !== ']'.codePointAt(0)) {
36+
// Position is probably wrong. Revert and don't allow change.
37+
checkbox.checked = !checkbox.checked;
38+
throw new Error(`Expected position to be space or x and surrounded by brackets, but it's not: position=${position}`);
39+
}
3240
buffer.set(encoder.encode(checkboxCharacter), position);
3341
const newContent = new TextDecoder().decode(buffer);
3442

0 commit comments

Comments
 (0)