Skip to content

Commit 24ec475

Browse files
committed
fix: escape html entities
1 parent 265619c commit 24ec475

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

script.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,19 @@ const initImageLazyLoading = () => {
194194
}
195195
};
196196

197+
const escapeHTMLAttribute = (str) => {
198+
const map = {
199+
"&": "&",
200+
"<": "&lt;",
201+
">": "&gt;",
202+
'"': "&quot;",
203+
};
204+
205+
if (typeof str !== "string") return "";
206+
207+
return str.replace(/[&<>"]/g, (char) => map[char]);
208+
};
209+
197210
const populateLibraryList = (filterQuery = "") => {
198211
const items = [
199212
...document.getElementById("template").parentNode.children,
@@ -218,11 +231,15 @@ const populateLibraryList = (filterQuery = "") => {
218231
}
219232
const template = document.getElementById("template");
220233
const searchParams = new URLSearchParams(location.search);
221-
const referrer = searchParams.get("referrer") || "https://excalidraw.com";
234+
const referrer = escapeHTMLAttribute(
235+
searchParams.get("referrer") || "https://excalidraw.com",
236+
);
222237
const appName = getAppName(referrer);
223-
const target = decodeURIComponent(searchParams.get("target") || "_blank");
238+
const target = decodeURIComponent(
239+
escapeHTMLAttribute(searchParams.get("target")) || "_blank",
240+
);
224241
const useHash = searchParams.get("useHash");
225-
const csrfToken = searchParams.get("token");
242+
const csrfToken = escapeHTMLAttribute(searchParams.get("token"));
226243
for (let library of libraries) {
227244
const div = document.createElement("div");
228245
div.classList.add("library");
@@ -265,7 +282,9 @@ const populateLibraryList = (filterQuery = "") => {
265282
inner = inner.replace('<p class="updated">Updated: {updated}</p>', "");
266283
}
267284
inner = inner.replace(/\{appName\}/g, appName);
268-
const libraryUrl = encodeURIComponent(`${location.origin}/${source}`);
285+
const libraryUrl = encodeURIComponent(
286+
`${escapeHTMLAttribute(origin)}/${source}`,
287+
);
269288
inner = inner.replace(
270289
"{addToLib}",
271290
`${referrer}${useHash ? "#" : "?"}addLibrary=${libraryUrl}${

0 commit comments

Comments
 (0)