- make this point up front
- talk in 3 parts : What do I mean? Why do I say this? How do we that?
Language
langue, taal, sprache, 語言
I have an idea in my head : "A platypus is carrying a chicken through the forest".
Before I can say this, or even have the words in my mind, I need to have this as an idea in my mind.
In order to communicate this idea, I need to pull it into individual concepts
At this point we have identified the components that make up our message, and have determined how they relate to each other. Now we can go on and serialize this message.
A platypusis carryinga chickenin the forest
Now we can turn this tree into a linear list of words, ready to be uttered.
So what exactly is XSS? This is an example of a 'persisted' XSS attack. Anyone who can sneak HTML in our pages can do nasty stuff.
This gives an attacker all privileges the current user of your app has.
session hijacking
attacker can surf the site with user credentials
Escape!
The common wisdom is to "escape" the inserted value
This is a real life shipping label. This may seem silly but it happens all the time, we either escape too much or too little.
I actually noticed this myself although less extreme, when sending a parcel to a friend I wanted to add a middle name between quotes, these ended up on the parcel as HTML entity.
Manual escaping? hard
Let's automate!
# using HTML::SafeBuffer
<div><%= @post.body %></div>
And it just works
This is (at least in Ruby) the state of the art of XSS prevention, it's part of Rails 'secure by default' philosophy.
We've turned the problem around
Whitelist instead of blacklist
def helper
"<p> haikus are pretty <p>".html_safe
end
We're still manually deciding what (not) to escape
This is better because whitelist > blacklist. It is a step forward, less strings will be left unescaped, but you can hardly call this a structural solution.
The problem
Semantics of string are twofold
a string
a textual representation of HTML
this is not a new concept
we already do this for SQL
@users = User.where(name: params[:query])
Now we can revisit our injection attacks. In this case a syntax tree is built, this can then be used to generate the SQL dialect we need.
This is good because we can express our intent (semantics), and the data structure reflects those semantics.
def helper
"<p> haikus are pretty <p>".html_safe
end
The default for doing these kind of things, but ; tedious to work with ; only HTML 4.01 (because of libxml2), optimized for parsing.
Builder syntax
HTML::Builder.new do
html do
body do
p 'hello, world'
end
end
end
implementations vary
need to return a Node/Tree, not a string
what is
self
?
Can be a pain to keep track of `self`, some magic involved, but feels natural.
Important that the result can be recombined, not the case with Nokogiri::HTML::Builder.
Templating
%div
.big
= "Hello RuLu"
HAML isn't Apples and Snakes
but it could be
disallow inserting HTML strings
recognize Node/Tree types
S-expressions
('p ('em "hello, world"))
[:p, [:em, "hello, world"]]
simple
lightweight
easy to program / reason
Simple, lightweight, easy to reason about.
Now you can actually program your HTML
class MyController
def index
page = SignupPage.new
if request.post?
page.rewrite(PopulateFormFields.new(params))
end
render Layout.new(page)
end
end
Other ideas : add file/line numbers in dev mode ; structural validation ; presentation vs data separation.