Enforce DOM integrity, move 3rd party scripts to cdn.jottocraft.com, remove more dead code
Change-Id: I60aedea97116b50e0b1b7cfe3071d9eb3ea80499
Reviewed-on: https://src.jottocraft.com/c/dtps/+/138
Reviewed-by: spike <[email protected]>
diff --git a/scripts/core.js b/scripts/core.js
index 38bc218..f528850 100644
--- a/scripts/core.js
+++ b/scripts/core.js
@@ -75,7 +75,7 @@
};
//Load jQuery ASAP
-jQuery.getScript("https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js");
+jQuery.getScript("https://cdn.jottocraft.com/3rdparty/scripts/jquery.min.js");
/**
* Debugging shortcut for getting the selected class. This should only be used in the web inspector and not in actual code.
@@ -110,20 +110,20 @@
<div style="margin: 40px 0px;">
<h5>User</h5>
- <pre><code class="prettyprint">${JSON.stringify(dtps.user, null, '\t')}</code></pre>
+ <pre><code class="prettyprint">${DOMPurify.sanitize(JSON.stringify(dtps.user, null, '\t'))}</code></pre>
</div>
${dtps.classes[dtps.selectedClass] ? /*html*/`
<div style="margin: 40px 0px;">
<h5>Selected class</h5>
- <pre><code class="prettyprint">${JSON.stringify(dtps.classes[dtps.selectedClass], replacer, '\t')}</code></pre>
+ <pre><code class="prettyprint">${DOMPurify.sanitize(JSON.stringify(dtps.classes[dtps.selectedClass], replacer, '\t'))}</code></pre>
</div>
` : ``
}
`);
if (!window.PR) {
- $.getScript("https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js?lang=json");
+ $.getScript("https://cdn.jottocraft.com/3rdparty/scripts/run_prettify.js");
} else {
PR.prettyPrint();
}
@@ -143,6 +143,59 @@
console.log("[DTPS]", ...arguments);
}
+/**
+ * Escapes a string for use inside a quoted HTML attribute value.
+ * DOMPurify.sanitize does not escape quotes, so it can't be used for attribute values.
+ *
+ * @param {*} str The value to escape
+ * @return {string} Escaped string
+ */
+dtps.escapeAttr = function (str) {
+ return String(str).replace(/&/g, "&").replace(/"/g, """).replace(/'/g, "'").replace(/</g, "<").replace(/>/g, ">");
+}
+
+/**
+ * Sanitizes rich HTML content from the LMS (assignment bodies, pages, posts, etc.).
+ * Embedded iframes (e.g. videos) and link targets are allowed.
+ *
+ * @param {string} html The HTML to sanitize
+ * @return {string} Sanitized HTML
+ */
+dtps.sanitizeContent = function (html) {
+ return DOMPurify.sanitize(html, { ADD_TAGS: ["iframe"], ADD_ATTR: ["allow", "allowfullscreen", "frameborder", "scrolling", "target"] });
+}
+
+/**
+ * Ensures that a URL is safe
+ *
+ * @param {string} url The URL to check
+ * @returns {string} The URL if it is safe, otherwise "about:blank"
+ */
+dtps.safeURL = function (url) {
+ try {
+ var protocol = new URL(url, window.location.href).protocol;
+ if (["http:", "https:", "mailto:", "tel:"].includes(protocol)) return url;
+ } catch (e) { }
+ return "about:blank";
+}
+
+/**
+ * Shows an error message alert and logs to console, WITHOUT SANITIZING INPUT
+ *
+ * @param {string} msg The error to display
+ * @param {string} [devNotes] Technical error details displayed in a smaller font
+ * @param {Error} [err] An error object to log to the console. If this is null, DTPS will assume the error has been handled elsewhere.
+ */
+dtps.unsafeError = function (msg, devNotes, err) {
+ if (err !== null) {
+ var formattedDevNotes = "";
+ if (devNotes) {
+ formattedDevNotes = '<div style="font-size: 12px; color: var(--secText, gray); margin-top: 10px;">' + devNotes + '</div>';
+ }
+ console.error("[DTPS !!ERROR!!]", devNotes + ": ", err);
+ fluid.alert("Error", msg + formattedDevNotes, "error");
+ }
+}
/**
* Shows an error message alert and logs to console
@@ -152,14 +205,11 @@
* @param {Error} [err] An error object to log to the console. If this is null, DTPS will assume the error has been handled elsewhere.
*/
dtps.error = function (msg, devNotes, err) {
- if (err !== null) {
- var formattedDevNotes = "";
- if (devNotes) {
- formattedDevNotes = '<div style="font-size: 12px; color: var(--secText, gray); margin-top: 10px;">' + devNotes + '</div>';
- }
- console.error("[DTPS !!ERROR!!]", devNotes + ": ", err);
- fluid.alert("Error", msg + formattedDevNotes, "error");
- }
+ dtps.unsafeError(
+ DOMPurify.sanitize(msg),
+ DOMPurify.sanitize(devNotes),
+ err
+ );
}
/**
@@ -242,15 +292,15 @@
dtps.JS = function () {
return new Promise((resolve, reject) => {
//Moment & Fullcalendar are used for the calendar on the dashboard
- jQuery.getScript("https://cdn.jsdelivr.net/npm/[email protected]/main.min.js", function () {
- jQuery.getScript("https://cdn.jsdelivr.net/npm/[email protected]/locales-all.min.js");
+ jQuery.getScript("https://cdn.jottocraft.com/3rdparty/scripts/fullcalendar_main.min.js", function () {
+ jQuery.getScript("https://cdn.jottocraft.com/3rdparty/scripts/fullcalendar_locales-all.min.js");
});
//Lunr is used for search
- jQuery.getScript('https://unpkg.com/[email protected]/lunr.min.js');
+ jQuery.getScript('https://cdn.jottocraft.com/3rdparty/scripts/lunr.min.js');
//jQuery UI for dashboard settings page
- jQuery.getScript('https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js');
+ jQuery.getScript('https://cdn.jottocraft.com/3rdparty/scripts/jquery-ui.min.js');
//Tinycolor used for better dark mode support
jQuery.getScript("https://cdn.jottocraft.com/tinycolor.js");
@@ -258,7 +308,9 @@
//dtao/nearest-color is used for finding the nearest class color
jQuery.getScript("https://cdn.jottocraft.com/nearest-color.dtao.js", () => {
//Fluid UI for core UI elements
- jQuery.getScript(dtps.baseURL + "/fluid/fluid.js", resolve);
+ jQuery.getScript(dtps.baseURL + "/fluid/fluid.js", () => {
+ jQuery.getScript("https://cdn.jottocraft.com/3rdparty/scripts/purify.min.js", resolve);
+ });
});
});
}
@@ -593,9 +645,9 @@
//Check for login redirect
if ((err.action == "login") && err.redirectURL) {
//Redirect to login page
- window.location.href = err.redirectURL;
+ window.location.href = dtps.safeURL(err.redirectURL);
} else {
- dtps.error("Failed to get user and/or course data", "Exception in promise @ dtps.init");
+ dtps.unsafeError("Failed to get user and/or course data", "Exception in promise @ dtps.init");
}
});
@@ -653,7 +705,7 @@
for (var i = 0; i < dtps.classes.length; i++) {
//Determine letter grade HTML
var letterGradeHTML = "";
- if (dtps.classes[i].letter) letterGradeHTML = dtps.classes[i].letter;
+ if (dtps.classes[i].letter) letterGradeHTML = DOMPurify.sanitize(dtps.classes[i].letter);
if (dtps.classes[i].letter == null) letterGradeHTML = "--";
if (dtps.classes[i].letter == "...") letterGradeHTML = `<div class="shimmer" style="vertical-align:middle;display: inline-block;width: 22px;height: 22px;border-radius: 8px;"></div>`; //Show loading indicator for ...
if (dtps.classes[i].letter == "ERROR") letterGradeHTML = `<i class="fluid-icon">error</i>`;
@@ -661,9 +713,9 @@
if (dtps.classes[i].group && (dtps.classes[i].group !== previousClassGroup)) {
if (previousClassGroup) dtps.classlist.push(`</div></div>`);
dtps.classlist.push(/*html*/`
- <div id="dtpsClassListGroup-${dtps.classes[i].group}" class="group open">
+ <div id="dtpsClassListGroup-${dtps.escapeAttr(dtps.classes[i].group)}" class="group open">
<div class="name item">
- <i class="fluid-icon"></i> <span class="label">${dtps.classes[i].group}</span>
+ <i class="fluid-icon"></i> <span class="label">${DOMPurify.sanitize(dtps.classes[i].group)}</span>
</div>
<div class="items">
@@ -677,7 +729,7 @@
<div
onclick="${'dtps.selectedClass = ' + i}"
class="${'class item ' + i + ' ' + (dtps.selectedClass == i ? " active" : "")}"
- style="${'--classColor: ' + dtps.classes[i].color}"
+ style="${'--classColor: ' + dtps.escapeAttr(dtps.classes[i].color)}"
>
${dtps.classes[i].grade && dtps.classes[i].letter ? /*html*/`
<i class="fluid-icon grade">
@@ -694,7 +746,7 @@
</i>
`}
- <span class="label">${dtps.classes[i].subject}</span>
+ <span class="label">${DOMPurify.sanitize(dtps.classes[i].subject)}</span>
</div>
`);
@@ -874,32 +926,6 @@
} else {
$("#classInfo .homepage").hide();
}
-
- if (dtps.classes[classNum].videoMeetingURL) {
- $("#classInfo .videoMeeting").attr("onclick", "window.open('" + dtps.classes[classNum].videoMeetingURL + "');");
- $("#classInfo .videoMeeting").removeClass("shimmerParent");
- $("#classInfo .videoMeeting").show();
- } else if ((dtps.classes[classNum].videoMeetingURL !== null) && dtpsLMS.fetchMeetingURL) {
- $("#classInfo .videoMeeting").attr("onclick", "");
- $("#classInfo .videoMeeting").addClass("shimmerParent");
- $("#classInfo .videoMeeting").show();
-
- dtpsLMS.fetchMeetingURL(dtps.classes[classNum].lmsID).then(url => {
- dtps.classes[classNum].videoMeetingURL = url;
-
- if (dtps.selectedClass == classNum) {
- if (url) {
- $("#classInfo .videoMeeting").attr("onclick", "window.open('" + dtps.classes[classNum].videoMeetingURL + "');");
- $("#classInfo .videoMeeting").removeClass("shimmerParent");
- $("#classInfo .videoMeeting").show();
- } else {
- $("#classInfo .videoMeeting").hide();
- }
- }
- });
- } else {
- $("#classInfo .videoMeeting").hide();
- }
}
if (dtps.selectedContent !== "grades") $(".classContent").removeClass("fixedGradeSummary");
@@ -914,7 +940,7 @@
//Render loading screen
$(".card.details").html(/*html*/`
<i onclick="fluid.cards.close('.card.details')" class="fluid-icon close">close</i>
- <h3 style="font-weight: bold;">${dtps.classes[num].subject} Homepage</h3>
+ <h3 style="font-weight: bold;">${DOMPurify.sanitize(dtps.classes[num].subject)} Homepage</h3>
<br />
<p>Loading...</p>
@@ -923,6 +949,8 @@
//Fetch homepage from the LMS
dtpsLMS.fetchHomepage(dtps.classes[num].lmsID).then(homepage => {
if (homepage) {
+ homepage = dtps.sanitizeContent(homepage);
+
//Get computed background and text color to style the iFrame with
var computedBackgroundColor = getComputedStyle($(".card.details")[0]).getPropertyValue("--cards");
var computedTextColor = getComputedStyle($(".card.details")[0]).getPropertyValue("--text");
@@ -943,11 +971,11 @@
$(".card.details").html(/*html*/`
<i onclick="fluid.cards.close('.card.details')" class="fluid-icon close">close</i>
- <h4 style="font-weight: bold;">${dtps.classes[num].subject} Homepage</h4>
+ <h4 style="font-weight: bold;">${DOMPurify.sanitize(dtps.classes[num].subject)} Homepage</h4>
<br />
<div style="margin-top: 20px;" class="homepageBody">
- <iframe id="homepageIframe" onload="dtps.iframeLoad('homepageIframe')" style="margin: 10px 0px; width: 100%; border: none; outline: none;" src="${homepageURL}" />
+ <iframe id="homepageIframe" onload="dtps.iframeLoad('homepageIframe')" style="margin: 10px 0px; width: 100%; border: none; outline: none;" src="${homepageURL}"></iframe>
</div>
`);
@@ -974,7 +1002,7 @@
fluid.cards('.card.iFrameCard');
//Load new URL
- $('#CardIFrame').attr('src', url);
+ $('#CardIFrame').attr('src', dtps.safeURL(url));
}
/**
@@ -1248,9 +1276,6 @@
<p onclick="dtps.classHome(dtps.selectedClass);" class="homepage" style="cursor: pointer; display: none;">
<i class="fluid-icon">home</i> <span>Homepage</span>
</p>
- <p class="videoMeeting" style="cursor: pointer; display: none;">
- <i class="fluid-icon">videocam</i> <span>Meeting</span>
- </p>
<p class="contentLabel" style="display: none;">
<i class="fluid-icon"></i> <span></span>
</p>
@@ -1422,13 +1447,13 @@
$("#dtpsMainSearchBox").attr("data-search-type", type);
} else if (error) {
$("#dtpsSearchStatus i").text("error");
- $("#dtpsSearchStatus span").html(`Cannot search for ${type} in <b style="color: ${course.color};">${course.subject}</b>`);
+ $("#dtpsSearchStatus span").html(`Cannot search for ${DOMPurify.sanitize(type)} in <b style="color: ${dtps.escapeAttr(course.color)};">${DOMPurify.sanitize(course.subject)}</b>`);
$("#dtpsMainSearchBox").attr("placeholder", "Cannot search for " + type + " in " + course.subject);
$("#dtpsMainSearchBox").attr("data-dtps-course", "");
$("#dtpsMainSearchBox").attr("data-search-type", "");
} else if (course || true) {
$("#dtpsSearchStatus i").text(icon);
- $("#dtpsSearchStatus span").html(`Search ${type} in <b style="color: ${course.color};">${course.subject}</b>`);
+ $("#dtpsSearchStatus span").html(`Search ${DOMPurify.sanitize(type)} in <b style="color: ${dtps.escapeAttr(course.color)};">${DOMPurify.sanitize(course.subject)}</b>`);
$("#dtpsMainSearchBox").attr("placeholder", "Search " + type + " in " + course.subject);
$("#dtpsMainSearchBox").attr("data-dtps-course", course.num);
$("#dtpsMainSearchBox").attr("data-search-type", type);
@@ -1612,7 +1637,7 @@
<br /><br />
<div>
- <input id="dtpsLMSOverride" value="${window.localStorage.dtpsLMSOverride || ""}" placeholder="LMS override" />
+ <input id="dtpsLMSOverride" value="${dtps.escapeAttr(window.localStorage.dtpsLMSOverride || "")}" placeholder="LMS override" />
<button class="btn small" onclick="window.localStorage.setItem('dtpsLMSOverride', $('#dtpsLMSOverride').val())"><i class="fluid-icon">save</i> Save</button>
</div>
</div>
@@ -1639,10 +1664,10 @@
<div class="card" style="padding: 10px 20px; box-shadow: none !important; border: 2px solid var(--elements); margin-top: 20px;">
<div style="margin-top: 12px;">
- <img src="${dtps.user.photoURL}" style="height: 50px; margin-right: 10px; vertical-align: middle; border-radius: 50%;" />
+ <img src="${dtps.escapeAttr(dtps.user.photoURL)}" style="height: 50px; margin-right: 10px; vertical-align: middle; border-radius: 50%;" />
<div style="display: inline-block; vertical-align: middle;">
- <h4 style="font-weight: bold; font-size: 32px; margin: 0px;">${dtps.user.name} <span style="font-size: 12px; line-height: 0px;">${dtps.user.id}</span></h4>
+ <h4 style="font-weight: bold; font-size: 32px; margin: 0px;">${DOMPurify.sanitize(dtps.user.name)} <span style="font-size: 12px; line-height: 0px;">${DOMPurify.sanitize(dtps.user.id)}</span></h4>
</div>
</div>
@@ -1652,17 +1677,17 @@
</div>
<div class="card" style="padding: 10px 20px; box-shadow: none !important; border: 2px solid var(--elements); margin-top: 20px;">
- <img src="${dtpsLMS.logo}" style="height: 50px; margin-right: 10px; vertical-align: middle; margin-top: 20px; border-radius: 50%;" />
+ <img src="${dtps.escapeAttr(dtpsLMS.logo)}" style="height: 50px; margin-right: 10px; vertical-align: middle; margin-top: 20px; border-radius: 50%;" />
<div style="display: inline-block; vertical-align: middle;">
- <h4 style="font-weight: bold; font-size: 32px; margin-bottom: 0px;">${dtpsLMS.name}</h4>
+ <h4 style="font-weight: bold; font-size: 32px; margin-bottom: 0px;">${DOMPurify.sanitize(dtpsLMS.name)}</h4>
${dtpsLMS.description ? `
- <div style="font-size: 16px; margin-top: 5px;">${dtpsLMS.description}</div>
+ <div style="font-size: 16px; margin-top: 5px;">${DOMPurify.sanitize(dtpsLMS.description)}</div>
` : ``}
</div>
<div style="margin-top: 15px; margin-bottom: 7px;">
- <a style="color: var(--lightText); margin: 0px 5px;" href="${dtpsLMS.source}"><i class="fluid-icon" style="vertical-align: middle">code</i> LMS integration source code</a>
+ <a style="color: var(--lightText); margin: 0px 5px;" href="${dtps.escapeAttr(dtpsLMS.source)}"><i class="fluid-icon" style="vertical-align: middle">code</i> LMS integration source code</a>
</div>
</div>
@@ -1800,14 +1825,6 @@
*/
/**
-* @name dtpsLMS.fetchMeetingURL
-* @description [OPTIONAL] Fetches the videoMeetingURL for a class
-* @kind function
-* @param {string} classID The class ID to get the videoMeetingURL for
-* @return {Promise<string>} A promise which resolves to the videoMeetingURL for the class, or null if the class does not have a videoMeetingURL
-*/
-
-/**
* @name dtpsLMS.fetchHomepage
* @description [OPTIONAL] Fetches homepage HTML for a course from the LMS
* @kind function
@@ -1931,5 +1948,4 @@
* @property {string} [image] URL to the class background image
* @property {User} [teacher] Class teacher. If the class has multiple teachers, this is the primary teacher.
* @property {boolean} [hasGradebook] Automatically managed by DTPS. True if the class should show the gradebook tab.
-* @property {string} [videoMeetingURL] The URL used to join this class' online meeting. If dtpsLMS.fetchMeetingURL is implemented, DTPS will use it to get the meeting URL if this property is undefined. If this property is null, DTPS will not call dtpsLMS.fetchMeeting URL for the class.
*/