Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8665311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:27:08+00:00 2026-06-12T17:27:08+00:00

My recent submission for Firefox add-on site (based on Firefox Add-on SDK 1.10) was

  • 0

My recent submission for Firefox add-on site (based on Firefox Add-on SDK 1.10) was rejected because I have not sanitized the input I use and was suggested to use nsIParserUtils.

I found the function parseHTML(doc, html, allowStyle, baseURI, isXML) in that page. I changed it to:

function parseHTML(doc, html, allowStyle, baseURI, isXML) {
    var parser = Cc["@mozilla.org/parserutils;1"].getService(Ci.nsIParserUtils);
    var f =  parser.parseFragment(html, allowStyle ? parser.SanitizerAllowStyle : 0,
                                        !!isXML, baseURI, doc);
    return f;
}

And the first parameter in that is said to be a document element. I have no idea what that is supposed to be? I tried document.createDocumentFragment() but I get “ReferenceError: document is not defined” error. Can some one help me on how to call this function?

And the function returns an nsIDOMDocumentFragment. How to convert that back to a string?


UPDATE:

As suggested by @zer0 I used:

var parser = Cc["@mozilla.org/parserutils;1"].getService(Ci.nsIParserUtils);
var sanitizedHTML = parser.sanitize(html, flags);

But it defeats the purpose of what I wanted to do. For example:

<html><head><BASE href='http://localhost/t/h.html' />
<link rel="stylesheet" type="text/css" href="h.css">
<style type="text/css">
.b{
    color:green;
}
</style>
<base href="http://foo.example.com/">
</head><body>Sample Text. No Style
<script>Hello malicious code</script>
<p class="a">External Style</p>
<p class="b">Internal Style</p>
<p style="color:blue">Inline Style</p>

<a href="sample.html">Link</a><br><br><div style='color: #666666; font-size: 12px'>Clipped on 6-October-2012, 07:37:39 PM from <a href='http://localhost/t/h.html'>http://localhost/t/h.html</a> </div></body></html>

Is converted to:

<html><head>  


<style type="text/css">
.b{

    color:green;
}
</style>



</head><body>Sample Text. No Style

<p class="a">External Style</p>
<p class="b">Internal Style</p>
<p style="color:blue">Inline Style</p>

<a>Link</a><br><br><div style="color: #666666; font-size: 12px">Clipped on 6-October-2012, 07:37:39 PM from <a href="http://localhost/t/h.html">http://localhost/t/h.html</a> </div></body></html>

As this strips the external hyperlinks and CSS, it defeats the purpose of the add-on itself. What I want is for just the scripts to be removed:

<html><head><BASE href='http://localhost/t/h.html' /> <BASE href='http://localhost/t/h.html' /> 
<link rel="stylesheet" type="text/css" href="h.css">

<style type="text/css">
.b{

    color:green;
}
</style>
<base href="http://foo.example.com/">


</head><body>Sample Text. No Style
<p class="a">External Style</p>
<p class="b">Internal Style</p>
<p style="color:blue">Inline Style</p>

<a href="sample.html">Link</a><br><br><div style='color: #666666; font-size: 12px'>Clipped on 6-October-2012, 07:37:39 PM from <a href='http://localhost/t/h.html'>http://localhost/t/h.html</a> </div></body></html>

Can someone shed some light on this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:27:09+00:00Added an answer on June 12, 2026 at 5:27 pm

    Links to external styles are removed for a reason: external styles cannot be validated and they might be dangerous (in particular, -moz-binding can be used to run code). Also, the assumption is that you could put the HTML code into a location where following relative links isn’t safe (such as mail messages in Thunderbird). Absolute links are always fine however.

    What you might want to do is preprocessing the HTML code to remove these issues – resolve relative links and inline references to external styles. Something like this:

    // Parse the HTML code into a temporary document
    var doc = Cc["@mozilla.org/xmlextras/domparser;1"]
                   .createInstance(Ci.nsIDOMParser)
                   .parseFromString(html, "text/html");
    
    // Make sure all links are absolute
    for (var i = 0; i < doc.links.length; i++)
        doc.links[i].setAttribute("href", doc.links[i].href);
    
    // Make sure all stylesheets are inlined
    var stylesheets = doc.getElementsByTagName("link");
    for (i = 0; i < stylesheets.length; i++)
    {
        try
        {
            var request = new XMLHttpRequest();
            request.open("GET", stylesheets[i].href, false);
            request.send(null);
            var style = doc.createElement("style");
            style.setAttribute("type", "text/css");
            style.textContent = request.responseText;
            stylesheets[i].parentNode.replaceChild(style, stylesheets[i]);
            i--;
        }
        catch (e)
        {
            // Ignore download errors
        }
    }
    
    // Serialize the document into a string again
    html = Cc["@mozilla.org/xmlextras/xmlserializer;1"]
             .createInstance(Ci.nsIDOMSerializer)
             .serializeToString(doc.documentElement);
    
    // Now sanizite the HTML code
    var parser = Cc["@mozilla.org/parserutils;1"].getService(Ci.nsIParserUtils);
    var sanitizedHTML = parser.sanitize(html, parser.SanitizerAllowStyle);
    

    Note that I used a synchronous XMLHttpRequest to download stylesheet contents – this has been done for simplicity, your final code should use asynchronous downloads (most likely via request module) that will not hang the user interface.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After a recent submission I have gotten the following error: Invalid Signature - the
In one of my recent SO questions, someone suggested that I use Loader for
In recent versions of python, one can use something like with open('abc.txt') as f:
The recent upgrade to Dreamweaver CS5.5 finally installs the Android SDK correctly, but when
The index page on my site shows the most recent submissions from my users.
Recent changes are forcing me to add a bunch of 301 redirects. Seems that
A recent homework assignment I have received asks us to take expressions which could
Recent JVM's have a lot of XX parameters for garbage collection (see here for
Recent versions of Z3 have decoupled the notions of Z3_context and Z3_solver . The
My recent app is like a forum, i use listview to show each thread.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.