Coder Social home page Coder Social logo

guarded-string's Introduction

guarded`string`

Prevent accidentally introducing XSS holes with the strings in your app

Hold your friends close, and your strings closer

Installation

yarn add guarded-string

Usage

Important! This should be used for things like preventing XSS attacks, not for hiding sensitive information.

import guardedString from 'guarded-string';

const myString = guardedString`My very important (but not too important) string`;

guardedString.isGuarded(myString); // >> boolean
guardedString.assertGuarded(myString); // >> maybe throws
guardedString.toUnguarded(myString); // >> unguarded string (throws on other value types)

myString + ''; // 'My very important (but not too important) string'

guardedString.freeze(myString);
guardedString.isFrozen(myString);
guardedString.assertFrozen(myString);

myString + ''; // Error!
JSON.stringify(myString); // Error!
// etc.

API

guardedString

Create a guarded string. This must be used as a tagged template literal with no interpolations. You cannot construct a guarded string that is not statically written in your code.

let str = guardedString`Hello World`;

You can continue using this as a string, but when you modify it, the result is an unguarded (regular) string.

let str1 = guardedString`Hello World`;
let str2 = str1 + '!';

guardedString.isGuarded(str1); // true
guardedString.isGuarded(str2); // false

If you want to using string methods, you can wrap your string with String(str) or guardedString.toUnguarded(str).

let str1 = guardedString`Hello World`;
let str2 = String(str1).replace('World', 'Universe');
let str3 = guardedString.toUnguarded(str1).replace('World', 'Universe');

guardedString.isGuarded(val)

This just returns a boolean if the value you pass in is a guarded string or not.

guardedString.assertGuarded(val)

This will throw an error if the value you pass in is not a guarded string.

guardedString.freeze(str)

If you want to make sure that your string is not accidentally stringified, you can call guardedString.freeze(str) on your guarded string and it will prevent code from accidentally stringifying it.

let str = guardedString.freeze(guardedString`Hello World`);

String(str); // Error!
str + '!'; // Error!
JSON.stringify(str); // Error!

See test cases for more

Note that you can still call guardedString.toUnguarded(str) to convert it back to a plain string.

guardedString.isFrozen(val)

This just returns a boolean if the value you pass in is a frozen string or not.

guardedString.assertFrozen(val)

This will throw an error if the value you pass in is not a frozen string.

guardedString.toUnguarded(str)

This will convert any guarded string (including frozen strings).

let str1 = guardedString.freeze(guardedString`Hello World`);
let str2 = guardedString.toUnguarded(str1);

console.log(typeof str1); // 'object'
console.log(typeof str2); // 'string'

guarded-string's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

poeschko petetnt

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.