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/pages-discussions.js b/scripts/pages-discussions.js
index 7ed3592..436b7e3 100644
--- a/scripts/pages-discussions.js
+++ b/scripts/pages-discussions.js
@@ -97,7 +97,7 @@
                 return /*html*/`

                     <div data-threadID="${discussionThread.id}" class="item">

                         <i class="fluid-icon">${discussionThread.locked ? "lock_outline" : "chat_bubble_outline"}</i>

-                        <span class="label">${discussionThread.title}</span>

+                        <span class="label">${DOMPurify.sanitize(discussionThread.title)}</span>

                     </div>

                 `;

             }).join("");

@@ -111,7 +111,7 @@
 

                     ${

                     dtps.classes[classNum].newDiscussionThreadURL ? (/*html*/`

-                            <div onclick="window.open('${dtps.classes[classNum].newDiscussionThreadURL}')" class="item back">

+                            <div onclick="window.open(${dtps.escapeAttr(JSON.stringify(dtps.safeURL(dtps.classes[classNum].newDiscussionThreadURL)))})" class="item back">

                                 <i class="fluid-icon">add</i>

                                 <span class="label">New discussion</span>

                             </div>

@@ -194,7 +194,7 @@
                     <base target="_blank" /> 

                     <link type="text/css" rel="stylesheet" href="https://cdn.jottocraft.com/CanvasCSS.css" media="screen,projection"/>

                     <style>body {background-color: ${computedBackgroundColor}; color: ${computedTextColor};</style>

-                    ${post.body}

+                    ${dtps.sanitizeContent(post.body)}

                 `], { type: 'text/html' });

             var discussionPostContentURL = window.URL.createObjectURL(blob);

 

@@ -210,17 +210,17 @@
                         <div data-post-id="${reply.id}" style="margin-left: ${(reply.depth || 0) * 50}px;" class="discussionReply">

                             <div class="discussionHeader">

                                 ${post.author ? /*html*/`

-                                    <img src="${reply.author.photoURL}" />

-                                    <h5>${reply.author.name}</h5>

+                                    <img src="${dtps.escapeAttr(reply.author.photoURL)}" />

+                                    <h5>${DOMPurify.sanitize(reply.author.name)}</h5>

                                 ` : ``}

                                 

-                                <span style="cursor: pointer;" onclick="window.open('${reply.replyURL}');">

+                                <span style="cursor: pointer;" onclick="window.open(${dtps.escapeAttr(JSON.stringify(dtps.safeURL(reply.replyURL)))});">

                                     <i class="fluid-icon">reply</i>

                                     <span>Reply</span>

                                 </span>

                             </div>

 

-                            ${reply.body}

+                            ${dtps.sanitizeContent(reply.body)}

                         </div>

                     `)

                 });

@@ -232,17 +232,17 @@
             //Create HTML for this post

             //Note that index == 0 is the initial post

             postHTML.push(/*html*/`

-                    <div class="card" data-post-id="${post.id}" style="margin-top: 20px;${index == 0 ? "margin-bottom: 75px;" : "padding: 20px 30px;"}">

+                    <div class="card" data-post-id="${dtps.escapeAttr(post.id)}" style="margin-top: 20px;${index == 0 ? "margin-bottom: 75px;" : "padding: 20px 30px;"}">

                         <!-- Thread title (Initial post) -->

                         ${index == 0 ? /*html*/`

-                            <h4 style="font-weight: bold">${thread.title}</h4>

+                            <h4 style="font-weight: bold">${DOMPurify.sanitize(thread.title)}</h4>

                         ` : ''}

 

                         <!-- Author header -->

                         <div ${index == 0 ? `style="margin-bottom: 32px;"` : ""} class="discussionHeader">

                             ${post.author ? /*html*/`

-                                <img src="${post.author.photoURL}" />

-                                <h5>${post.author.name}</h5>

+                                <img src="${dtps.escapeAttr(post.author.photoURL)}" />

+                                <h5>${DOMPurify.sanitize(post.author.name)}</h5>

                             ` : ``}

 

                             <i class="fluid-icon">calendar_today</i>

@@ -259,8 +259,8 @@
                         </div>

          

                         ${index == 0 ? /*html*/`

-                            <iframe id="classThreadIframe" onload="dtps.iframeLoad('classThreadIframe')" style="margin: 10px 0px; width: 100%; border: none; outline: none;" src="${discussionPostContentURL}" />

-                        ` : post.body}

+                            <iframe id="classThreadIframe" onload="dtps.iframeLoad('classThreadIframe')" style="margin: 10px 0px; width: 100%; border: none; outline: none;" src="${discussionPostContentURL}"></iframe>

+                        ` : dtps.sanitizeContent(post.body)}

 

                         ${replyHTML.join("")}

 

@@ -268,9 +268,9 @@
                         ${post.replyURL ? /*html*/`

                             <div ${index == 0 ? `style="margin-top: 32px;"` : ""} class="discussionFooter">

                                 ${index == 0 ? /*html*/`

-                                    <button onclick="window.open('${post.replyURL}');" class="btn small"><i class="fluid-icon">post_add</i> Add Post</button>

+                                    <button onclick="window.open(${dtps.escapeAttr(JSON.stringify(dtps.safeURL(post.replyURL)))});" class="btn small"><i class="fluid-icon">post_add</i> Add Post</button>

                                 ` : /*html*/`

-                                    <button onclick="window.open('${post.replyURL}');" class="btn small"><i class="fluid-icon">reply</i> Reply</button>

+                                    <button onclick="window.open(${dtps.escapeAttr(JSON.stringify(dtps.safeURL(post.replyURL)))});" class="btn small"><i class="fluid-icon">reply</i> Reply</button>

                                 `}

                             </div> 

                         ` : ""}

@@ -383,9 +383,9 @@
             //Loop over discusson threads array to create discussion thread HTML for the sidebar

             var pageHTML = dtps.classes[classNum].pages.map(page => {

                 return /*html*/`

-                    <div data-pageID="${page.id}" class="item ${(defaultPage && (page.id == defaultPage)) ? "active" : ""}">

+                    <div data-pageID="${dtps.escapeAttr(page.id)}" class="item ${(defaultPage && (page.id == defaultPage)) ? "active" : ""}">

                         <i class="fluid-icon">insert_drive_file</i>    

-                        <span class="label">${page.title}</span>

+                        <span class="label">${DOMPurify.sanitize(page.title)}</span>

                     </div>

                 `;

             }).join("");

@@ -467,7 +467,7 @@
                 <base target="_blank" /> 

                 <link type="text/css" rel="stylesheet" href="https://cdn.jottocraft.com/CanvasCSS.css" media="screen,projection"/>

                 <style>body {background-color: ${computedBackgroundColor}; color: ${computedTextColor};</style>

-                ${page.content}

+                ${dtps.sanitizeContent(page.content)}

             `], { type: 'text/html' });

 

             //Get URL from blob

@@ -482,13 +482,13 @@
                 ` : ""}

                 <div style="margin: 20px; padding: 20px;">

                     <!-- Page title -->

-                    <h4 style="font-weight: bold;">${page.title}</h4>

+                    <h4 style="font-weight: bold;">${DOMPurify.sanitize(page.title)}</h4>

 

                     <!-- Page header -->

                     <div style="margin-bottom: 32px;" class="discussionHeader">

                         ${page.author ? /*html*/`

-                            <img src="${page.author.photoURL}" />

-                            <h5>${page.author.name}</h5>

+                            <img src="${dtps.escapeAttr(page.author.photoURL)}" />

+                            <h5>${DOMPurify.sanitize(page.author.name)}</h5>

                         ` : ``}

 

                         <i class="fluid-icon">calendar_today</i>

@@ -497,7 +497,7 @@
 

                     <!-- Page content -->

                     <br />

-                    <iframe id="classPageIframe" onload="dtps.iframeLoad('classPageIframe')" style="margin: 10px 0px; width: 100%; border: none; outline: none;" src="${pageContentURL}" />

+                    <iframe id="classPageIframe" onload="dtps.iframeLoad('classPageIframe')" style="margin: 10px 0px; width: 100%; border: none; outline: none;" src="${pageContentURL}"></iframe>

                 </div>

             `);

         }