question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Content Security Policy (CSP) compliance

See original GitHub issue

New feature motivation

Using SweetAlert2 for web extensions (built using Webpack, but relevant whatever the build tool) currently causes the web-ext linter (Mozilla Firefox) to highlight a number of (21 from sweetalert2.all[.min].js to be precise) “UNSAFE_VAR_ASSIGNMENT” warnings (using default web extension csp which I understand to be script-src 'self'; object-src 'self'). This is due mostly to the use of element.innerHTML = blah within SweetAlert’s codebase.

I understand why this is, and this is probably one of the main advantages to SweetAlert over plain js message boxes (ability to make nice-looking html versions), however it would be useful to have a solution that avoids the unsafe assignment warnings in web-ext’s linter.

One thing to note is that the linter does not make distinction between code that is actually used, and code that is there but will not ever be “used” (for instance fallback “if” statements for last resort assignment). Therefore the solution must ideally avoid element.innerHTML = altogether.

There may be an alternative, listed below. However I would be interested for anyone’s thoughts on this.

New feature description

Remove all direct assignment via element.innerHTML which is considered unsafe by the linter

New feature implementation

Idea 1 There is some guidance here from Mozilla about HTML sanitization: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Safely_inserting_external_content_into_a_page#HTML_sanitization Although I haven’t tested whether the linter is intelligent enough to know that the content has been sanitized before assignment to innerHTML.

Idea 2 Otherwise I came across the following solution, which is a little more onerous unfortunately, but removes the need for innerHTML assignment altogether by using the DOMParser library… https://devtidbits.com/2017/12/06/quick-fix-the-unsafe_var_assignment-warning-in-javascript/

Grateful for any comments about this…

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
september28commented, Mar 27, 2020

Thanks @limonte I appreciate you don’t want to overcomplicate the code base. For the rest of my js I actually wrote a quite lightweight solution (domWrap) using DOMParser which I believe is pretty neat (if I do say so myself). Included below if of interest:

function domWrap(str, tag="span") {
    if(str && typeof str === "string") {
        if(str[0] !== "<") {
            str = `<${tag}>${str}</${tag}>`;
        }
        str = `<span id="dw">${str}</span>`;
        let parser = new DOMParser();
        let dom = parser.parseFromString(str, "text/html");
        let ret = dom.getElementById("dw");
        return ret.firstChild;
    }
    return str;
}

The function returns a DOM element, so this can be used via appendChild, rather than innerHTML.

0reactions
limontecommented, Nov 26, 2020

🎉 This issue has been resolved in version 10.10.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is Content Security Policy (CSP) | Header Examples
A Content Protection Policy (CSP) adds protection measures against XSS, clickjacking, and other code injection attacks. Learn how.
Read more >
Content Security Policy - Wikipedia
Content Security Policy (CSP) is a computer security standard introduced to prevent cross-site scripting (XSS), clickjacking and other code injection ...
Read more >
Using Content Security Policy (CSP) to Secure Web Applications
Content Security Policy (CSP) is a computer security standard that provides an added layer of protection against Cross-Site Scripting (XSS), ...
Read more >
What is a Content Security Policy (CSP)? - Feroot
A Content Security Policy (CSP) is an added layer of security that helps businesses and security teams detect and mitigate certain types of...
Read more >
Content-Security-Policy Header CPS - Explained
Content Security Policy directives are defined in HTTP response headers, called CSP headers. The directions instruct the browser on trusted ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found