• Skip to main content
  • Select language
  • Skip to search
MDN Web Docs
  • Technologies
    • HTML
    • CSS
    • JavaScript
    • Graphics
    • HTTP
    • APIs / DOM
    • WebExtensions
    • MathML
  • References & Guides
    • Learn web development
    • Tutorials
    • References
    • Developer Guides
    • Accessibility
    • Game development
    • ...more docs
B2G OS
  1. MDN
  2. Archive of obsolete content
  3. B2G OS
  4. Firefox OS security
  5. Security Automation

Security Automation

In This Article
  1. ESLint Security Checks in Firefox OS Gaia
    1. Unsafe assignment to innerHTML spotted
      1. Fixing the test error
      2. Ignoring the test failure
    2. Unsafe call to insertAdjacentHTML spotted
      1. Fixing the test error
      2. Ignoring the test failure
    3. Storing and re-using the escaped HTML
    4. What if the new pattern can not be applied
    5. Implementation Details
      1. Related links

Firefox OS uses some automation to spot security errors throughout the development & build process. This article aims to documents how to work with those.

ESLint Security Checks in Firefox OS Gaia

Firefox OS Gaia uses ESLint to perform some basic security checks. We use https://github.com/mozfreddyb/eslint-plugin-no-unsafe-innerhtml. These checks are built into the Makefile as well as the git commit hook. The idea of these checks is to ensure that certain provably insecure coding patterns are avoided in our code base. We encourage developers to use the sanitizer.js library in gaia shared.

Unsafe assignment to innerHTML spotted

Fixing the test error

This error message suggests that you are using an unsafe coding pattern. Please do not assign variables to innerHTML, if at all possible. The new way requires the sanitizer.js library:

// if not included in the current project, you need require the sanitizer.js library from shared/js/
foo.innerHTML = Sanitizer.escapeHTML`<a href="${link}">click</a>`

Ignoring the test failure

If you can not apply this new coding style, please see below on how to get help.

Unsafe call to insertAdjacentHTML spotted

Fixing the test error

This error message suggests that you are using an unsafe coding pattern. Please do not simply call insertAdjacentHTML with a pure variable parameter. The new way requires the sanitizer.js library:

// if not included in the current project, you need require the sanitizer.js library from shared/js/
node.insertAdjacentHTML('afterend', Sanitizer.escapeHTML`<a href="${link}">click</a>`);

Ignoring the test failure

If you can not apply this new coding style, please see below on how to get help.

Storing and re-using the escaped HTML

The sanitizer has two basic usages. The first one is explained above, where we try to fix the most common errors. If you really need to generate your HTML somewhere else and e.g. cache it, you won't be able to run `escapeHTML` on a string that still carries the distinction between static HTML and user inputs. Thus, there is another feature in Sanitizer that allows you to create an object that contains escaped HTML which is guaranteed to be safe and thus allowed for direct innerHTML assignments (and insertAdjacentHTML calls): createSafeHTML and unwrapSafeHTML

// create the HTML object for later usage
function storeGreeting(username) {
  var safeHTML = Sanitizer.createSafeHTML`<p>Hello ${username}</p>`;
  cache.store('greeting', safeHTML)
}
// re-use the existing safe-HTML object
function useGreeting(domNode) {
  var htmlObj = cache.retrieve('greeting');
  domNode.innerHTML = Sanitizer.unwrapSafeHTML(htmlObj);
}

 

What if the new pattern can not be applied

If you cannot apply the escapeHTML pattern, try to work with the createSafeHTML and unwrapSafeHTML flow. If this does not work, don't worry. Every rule has its exceptions. If you think your example is not unsafe, you can file a bug that blocks against bug 1155131. Even better, you can submit a pull request and improve our automation, so that it will no longer complain about scripts like yours.

If you want your unsafe example to be discussed further even though it is unsafe, please raise this with the Firefox OS Security Team by requesting the sec-review? flag on your implementation bug towards fxos@security.bugs.

Implementation Details

Related links

  • implementation of our sanitizer – https://bugzilla.mozilla.org/show_bug.cgi?id=1155131
  • Bringing ESLint into our source base (including the commit hook) – https://bugzilla.mozilla.org/show_bug.cgi?id=115966
  • ESLint plugin to check the use of our sanitizer – https://github.com/mozfreddyb/eslint-plugin-no-unsafe-innerhtml

Document Tags and Contributors

 Contributors to this page: chrisdavidmills, freddyb, marumari
 Last updated by: chrisdavidmills, Feb 27, 2017, 2:06:56 AM
See also
  1. Build and install
    1. Build and install overview
    2. B2G OS build process summary
    3. Build prerequisites
    4. Preparing for your first build
    5. Building B2G OS
    6. B2G installer add-on
    7. Building B2G OS for Flame on Mac OS X
    8. Choosing how to run Gaia or B2G OS
    9. Compatible Devices
    10. Installing B2G OS on a mobile device
    11. Creating and applying B2G OS update packages
    12. Building and installing FOTA community builds
    13. B2G build variables reference sheet
  2. Porting B2G OS
    1. Porting overview
    2. Porting basics
    3. Porting on CyanogenMod
  3. Developing Gaia
    1. Developing Gaia overview
    2. Running the Gaia codebase
    3. Run Gaia on desktop using Mulet
    4. Understanding the Gaia codebase
    5. Making Gaia code changes
    6. Testing Gaia code changes
    7. Submitting a Gaia patch
    8. Gaia build system primer
    9. Different ways to run Gaia
    10. Make options reference
    11. Gaia tools reference
  4. B2G OS APIs