Web Linguistics

Towards Higher Fluency

by Arne Brasseur / plexus

Who am I?

Arne Brasseur

Freelance Ruby Dev

Twitter / Github : @plexus

Stop using strings to handle structured data!

Why do we do this anyway?

Plain text == Universal data type

It’s “Pragmatic”!

What’s the alternative?

Input & output are formal languages

We have tools for those

Apples and Snakes

Keep the snakes out of the app

parse/generate at the app boundary

Inside the app, only apples

#langsec

Screenshot of the Langsec website (langsec.org)

http://langsec.org

WATCH THIS TALK!

Langsec


Photoshopped by Kythera of Anevern.


Photoshopped by Kythera of Anevern.

XSS

Cross site scripting

XSS

The most common vulnerability

More common than buffer overflows

Preventing XSS

“Classic” solution : escape_html

Rails 3 : SafeBuffer + html_safe

Whitelist > Blacklist

But : We’re still manually deciding what (not) to escape

The problem

What is a String?

  • a piece of text

  • a textual representation of HTML

  • a piece of text that is HTML escaped

HTML is structured data,
let’s treat it as such

Language

langue, taal, sprache, 語言

platypus s sentence subject subject s--subject action action s--action place place s--place platypus platypus subject--platypus verb verb action--verb object object action--object forest forest place--forest carry carry verb--carry chicken chicken object--chicken

A platypusis carryinga chickenin the forest

Magic of language

Identical “tree” is shared between speakers

XSS is a Failure at the Language Level

commmunication z1 c x1 y1 x1->y1 y1->z1 z2 y1->z2 z3 y1->z3 aa y1->aa serializer serializer network network serializer->network parser parser network->parser bb parser->bb aa->serializer b bb->b a a->b b->c d b->d e b->e
commmunication z1 c f c->f g c->g h g->h x1 y1 x1->y1 y1->z1 z2 y1->z2 z3 y1->z3 aa y1->aa serializer serializer network network serializer->network parser parser network->parser bb parser->bb aa->serializer b bb->b a a->b b->c d b->d e b->e

Abolish Your Templates, Burn Your Helpers

Try this at home

Create a HTML data structure
Serialize it in one pass

Benefits

HTML generation is
no longer ad-hoc

Output is
guaranteed well-formed

Output is
strict (because browsers)

A new world of programming opportunities! (a.k.a. WIN!)

Hexp

An API for generating
and manipulating
HTML syntax trees

http://github.com/plexus/hexp

What Rack does for HTTP
Hexp does for HTML

Hexp objects are immutable
API inspired by jQuery

Creating nodes

para = Hexp::Node.new(:p, "Emacs > vi")
div  = Hexp::Node.new(:div, {class: 'troll'}, [para])
puts div.to_html

Builder syntax

list = Hexp.build do
  ol do
    li "Go to Eurucamp"
    li "Meet Ruby friends"
    li "Troll the sh*t out of them"
  end
end

S-expressions

toc = H[:ol, {class: 'toc'}, [
    H[:li, 'Language Theory'],
    H[:li, 'XSS'],
    H[:li, 'Applications']
  ]
]

Reusable ‘Middleware’

  • Lint / validate

  • Add admin links

  • Inline CSS/JS/images

Reusable Widgets

  • Menus

  • Breadcrumbs

  • Forms

Example Widget

class BookWidget < Struct.new(:book, :tag)
  include Hexp

  def to_hexp
    H[tag, {class: 'book', id: "book-#{book.id}"}, [
        H[:h2, book.title],
        H[:img, src: book.image.url]
      ]
    ]
  end
end

Modules

Great for CRM / e-commerce, or for modularizing your own app

Example : discount module

doc.replace('form.checkout') do |form|
  form.add_child(discount_field)
end

discount_field = H[:div, [
    H[:label, {for: 'discount'}, 'Discount code'],
    H[:input, {type: 'text', name: 'discount'}]
  ]
]

Hexpress

http://github.com/plexus/hexpress

  • Converts Markdown to Hexp

  • Contains a bunch of ‘Middleware’

    • AddImpressJs

    • GraphvizDotToSVG

    • MakeSelfContained

  • Powers this presentation

Fork me on Github!

Thank you!

References

Blog posts

Books

  • Speaking by Willem J.M. Levelt

  • An Introduction to Formal Languages and Automata by Peter Linz

Security

Software

Ruby

  • Formless
    Completely transparent, unobtrusive form populator for web applications and content scrapers

  • Loofah
    HTML/XML manipulation and sanitization based on Nokogiri

  • Living Style Guide

Common Lisp

Software

Haskell

General