npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

snabbdom-signature

v0.0.3

Published

Protects against vnode injection.

Downloads

174

Readme

Snabbdom-Signature

Protects your app against vnode injection.

const text = userInput.potentiallyMaliciousInput;
// Thanks to Snabbdom-Signature this is XSS-free.
const vnode = h('p', text);

The problem Snabbdom-Signature solves

Snabbdom vnodes are just data structures. It's impossible to distinguish vnodes created by the programmer from user-supplied objects. Malicious vnodes may come from sources like web requests or document-oriented databases. It is a type of XSS attacks.

How does Snabbdom-Signature work?

Snabbdom-Signature ships with two essential parts.

  1. A h vnode factory function which marks the vnodes it creates with a Symbol.
const h = snabbdomSignature.signingH(require('snabbdom/h').default);
const vnode = h('p', text); // {sel: "p", data: {snabbdom_signature: Symbol(snabbdom_signature)}, /* ... */ }

Please bear in mind that in order for this mechanism to work, the browser must support Symbols.

  1. A module which allows DOM elements to be created only based on signed (marked with a Symbol) vnodes.
const patch = snabbdom.init([
  snabbdomSignature.denyUnlessSigned,
  // other modules
]);

Since now the patch function throws an error (Error: Patching with a vnode which is not correctly signed!) when it encounters an unsigned vnode.

Getting Snabbdom-Signature

First, install the package.

npm i snabbdom-signature

Then, add the denyUnlessSigned module to your snabbdom init call and get the vnode signing version of the h function.

const snabbdom = require('snabbdom');
// Include the Snabbdom-Signature package
const snabbdomSignature = require('snabbdom-signature');
const patch = snabbdom.init([
  // Add the snabbdomSignature.denyUnlessSigned module 
  // to make sure that every vnode has been created by the programmer 
  // and not a malicious user.
  snabbdomSignature.denyUnlessSigned,
  // other modules
]);
// This helper function signs vnodes. 
// Use it like you use the default h helper.
const h = snabbdomSignature.signingH(require('snabbdom/h').default); 

Transferring vnodes (postMessage, Worker, JSON etc.)

  1. Remove the signature from a tree

Let's say signedVnode is a complex tree with many signed vnodes.

In order to be able to do things like sending vnodes to another window, we need to remove all the signatures, because Symbols don't survive structured cloning.

const snabbdomSignature = require('snabbdom-signature');
const removeSignature = snabbdomSignature.removeSignature;

// This form is ready for serialization/structured cloning.
const unsigned = removeSignature(signedVnode);

Because signature validation usually happens during the patching phase, there's a chance that the tree we pass to the removeSignature function consists of an injected, malicious vnode. That's why the removeSignature function verifies the signature and throws an error if it's invalid. You can be sure that if the signature is removed successfully, you can trust this tree again.

  1. Sign an unsigned tree coming from a trusted source

In the previous step we built a potentially dangerous tree of vnodes. Then, we used the removeSignature function to remove automatically added signatures (after verifying if all nodes were correctly signed).

Let's say that unsigned is that signature-less tree received from a trusted source. In order to be able to use it, we need to add all the signatures again.

const snabbdomSignature = require('snabbdom-signature');
const sign = snabbdomSignature.sign;

// This form is accepted by the patch function.
const signedAgain = sign(unsigned);

Please, make sure that you're not calling trust on trees coming from untrusted sources, like user-supplied JSON etc. Otherwise, there's a risk of XSS. Also, make sure that the unsigned node you're trusting has been somehow verified earlier, either by the removeSignature function or by successfully patching the DOM with it.

Development

If you found a security vulnerability in this package, please write to me at [email protected].

If you know a way to improve the performance or fix a bug which is not related to security, feel free to create an issue or submit a pull request.