Coder Social home page Coder Social logo

context-parser's Introduction

HTML5 Context Parser

HTML5 Context Parser is a robust and small footprint HTML5 context parser that parses HTML 5 web pages and reports the execution context of each character seen.

npm version dependency status

Overview

Execution Context

Browsers use Javascript and CSS engine in order to construct the dynamic components of a page correctly. In order to determine which engine should be used, browsers use HTML parsing algorithm to determine the context of HTML blocks (aka tokens).

Cross Site Scripting

Cross site scripting (XSS) can be prevented when input validation and filtering is performed aggressively such that it should remove all possible characters that could trigger changes in execution context in HTML. However, this has often proven as developer unfriendly and error prone.

The other way to solve XSS is to apply the filtering at the time the output is rendered, and just remove the characters that would trigger changes in context based on the current context in the HTML.

Design Principles

Secure

Parser need to be aligning with browser specification, in order to determine context accurately. One single parsing mistake would result in security exploit.

Keep It Simple and Straightforward

Keeping code simple and straightforward allows easier code review. Moreover, that would allow smaller compilation time (or JS code loading time in browser client side).

Since we are only interested in analyzing the execution context of the HTML5 page, we focused on the tokenization process and dropped other parts that are not related to context parsing logics.

Quick Start

Install the npm context-parser from the npm repo.

npm install -g context-parser

Server-side (nodejs)

Analyze the execution context of HTML 5 web page in server side.

/* create the context parser */
var Parser = require("context-parser").Parser;
var parser = new Parser();

/* read the html web page */
var file = "...";
var data = fs.readFileSync(file, 'utf-8');

/* analyze the execution context */
parser.contextualize(data);

Server-side (command line)

Run against the HTML5 file with our parser and the state defined in HTML 5 Specification and print out the state of each character.

./bin/context-dump <html file> <input preprocessing:0|1> <canonicalization:0|1>
  HTML-State { statesSize: 819 } +0ms
  HTML-State { ch: 0, state: 1, symbol: 0 } +1ms
  HTML-State { ch:   [0x20], state: 1, symbol: 0 } +1ms
  HTML-State { ch:   [0x20], state: 1, symbol: 0 } +0ms
  HTML-State { ch:   [0x20], state: 1, symbol: 0 } +0ms
  HTML-State { ch:   [0x20], state: 1, symbol: 0 } +0ms
  HTML-State { ch: < [0x3c], state: 8, symbol: 7 } +0ms
...

It reports back the execution context of each character in the format explained below.

{ch: <Character>, state: <Execution Context Number>, symbol: <Symbol Type>}

For the execution context number and character type, please refer to the state number defined in the specification and our code.

Development

How to build

npm install
npm run-script build

How to test

npm test

Build

Build Status

License

This software is free to use under the Yahoo Inc. BSD license. See the LICENSE file for license text and copyright information.

Related Works

  • parse5 is an HTML5 compliant parser implemented in native javascript. It is used by jsdom as the underlying HTML parsing engine. Parse5 has a larger code base and it exposes the parsing tree instead of execution context thus it may require some patching or trimming in order to provide context parsing functionality.

  • htmlparser2 is another HTML parser implemented in native javascript. It is used by cheerio as the underlying HTML parsing engine. HTMLparser2 is not a fully compliant parser thus it is less desirable to be used for application security related work.

context-parser's People

Contributors

adon-at-work avatar davglass avatar dmitris avatar maditya avatar neraliu avatar yukinying avatar

Watchers

 avatar

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.