Skip to content

Commit

Permalink
Add runtime check of surrounding characters to prevent corruption
Browse files Browse the repository at this point in the history
  • Loading branch information
jtran committed Jun 12, 2023
1 parent 03ed9c3 commit 36f4caa
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions web_src/js/markup/tasklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ export function initMarkupTasklist() {

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

Expand Down

0 comments on commit 36f4caa

Please sign in to comment.