yahoo/context-parser

Name: context-parser

Owner: Yahoo Inc.

Description: HTML5 Context Parser

Created: 2015-02-09 19:35:03.0

Updated: 2016-11-11 01:58:02.0

Pushed: 2015-10-20 04:35:27.0

Homepage:

Size: 5333

Language: HTML

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

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.

install -g context-parser
Server-side (nodejs)

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

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

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

nalyze the execution context */
er.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.

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

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

 <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
install
run-script build
How to test
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

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.