Skip to content

Commit 4cb4fb2

Browse files
committed
Mitigate potential xss.
1 parent a9c51a7 commit 4cb4fb2

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

examples/device/webusb_serial/website/application.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@
3434

3535
const THEME_STATES = ['auto', 'light', 'dark'];
3636

37+
/// https://stackoverflow.com/a/6234804/4479969
38+
const escapeHtml = unsafe => {
39+
if (typeof unsafe !== 'string') unsafe = String(unsafe);
40+
return unsafe
41+
.replaceAll("&", "&")
42+
.replaceAll("<", "&lt;")
43+
.replaceAll(">", "&gt;")
44+
.replaceAll('"', "&quot;")
45+
.replaceAll("'", "&#039;");
46+
};
47+
3748
class CommandHistoryEntry {
3849
constructor(text) {
3950
this.text = text;
@@ -211,9 +222,9 @@
211222
commandHistoryEntryBtn.type = 'button';
212223
let time_str = new Date(commandHistoryEntry.time).toLocaleString();
213224
commandHistoryEntryBtn.innerHTML = `
214-
<span class="command-history-entry-time">${time_str}</span>
215-
<span class="command-history-entry-text">${commandHistoryEntry.text}</span>
216-
<span class="command-history-entry-count">×${commandHistoryEntry.count}</span>
225+
<span class="command-history-entry-time">${escapeHtml(time_str)}</span>
226+
<span class="command-history-entry-text">${escapeHtml(commandHistoryEntry.text)}</span>
227+
<span class="command-history-entry-count">×${escapeHtml(commandHistoryEntry.count)}</span>
217228
`;
218229
commandHistoryEntryBtn.addEventListener('click', () => {
219230
if (uiCommandLineInput.disabled) return;
@@ -247,7 +258,7 @@
247258

248259
clearCommandHistory() {
249260
this.commandHistory = [];
250-
uiCommandHistoryScrollbox.innerHTML = '';
261+
uiCommandHistoryScrollbox.textContent = '';
251262
localStorage.removeItem('commandHistory');
252263
this.setStatus('Command history cleared', 'info');
253264
}
@@ -322,8 +333,8 @@
322333
let receivedDataEntryBtn = document.createElement('div');
323334
receivedDataEntryBtn.className = 'received-data-entry';
324335
receivedDataEntryBtn.innerHTML = `
325-
<span class="received-data-entry-time">${new Date(entry.time).toLocaleString()}</span>
326-
<span class="received-data-entry-text">${entry.text}</span>
336+
<span class="received-data-entry-time">${escapeHtml(new Date(entry.time).toLocaleString())}</span>
337+
<span class="received-data-entry-text">${escapeHtml(entry.text)}</span>
327338
`;
328339
documentFragment.appendChild(receivedDataEntryBtn);
329340
}
@@ -352,7 +363,7 @@
352363

353364
clearReceivedData() {
354365
this.receivedData = [];
355-
uiReceivedDataScrollbox.innerHTML = '';
366+
uiReceivedDataScrollbox.textContent = '';
356367
localStorage.removeItem('receivedData');
357368
this.setStatus('Received data cleared', 'info');
358369
}

0 commit comments

Comments
 (0)