Title: JavaScript: The Good Parts Part Seven: Security
1JavaScript The Good PartsPart Seven Security
- Douglas Crockford
- douglas_at_crockford.com
2The SAMY Saga
3In a week Samy learned
- How to hide JavaScript in a URL in CSS.
- How to generate MySpace crumbs.
- How to submit a message to MySpace to update the
friends list. - How to submit a message that contains a script
that sends a message containing a script.
4But most of all, Samy is my hero
- Within 20 hours, his script was in one million
accounts. - He immediately stepped forward because he
intended no harm. - He was charged with a felony.
- Three years probation.
- 90 days community service.
- Banned from the internet.
5Security
- The number 1 biggest problem with the whole World
Wide Web.
6The browser is not a safe programming environment.
- It is inherently insecure.
7Security is a big issue.
- The web is an exploit waiting to happen.
8Same Origin Policy
- Intended to defend sites from each other.
- Inconsistently applied.
- Script tags are exempt.
- The Origin of the Same Origin Policy Netscape 2
included frames and JavaScript. - Oops.
9Prevent useful things.Allow dangerous things.
10The Web Is Underspecified
- Much of the browser's functionality is not
defined by any standard. - The standards require unsafe constructions.
- The standards are interpreted very differently by
the many implementations, often with diverging
security vulnerabilities.
11Insecure Network
- Millionaire computers and ARPA contracts.
12WWW had no security model
- Unless you think that passwords embedded in URLs
and sent in the clear is a security model
13They had the wrong threat model
- "Nobody would ever do that."
14Security architecture in the web was reactive
- After an exploit was detected, a patch was added
15Marginal Security
- Security is sometimes obtained, but surprises can
occur.
16Cookies
- Cookies were not designed to be an authentication
mechanism. - Cookies are used as an authentication mechanism.
17JavaScript
- Not a secure programming language.
- (Virtually all programming languages are not
secure programming languages.) - All scripts on a page run with exactly the same
privileges, regardless of source. - Dependence on global variables.
18Document Object Model
- All nodes are linked to all other nodes and to
the network
19Never trust the browser.
- Use a rigorous validator on all input reject
dangerous text. - Everything it submits must be validated.
- Only whitebox filtering works.
20User Generated Content
- Blogs, groups, reviews, email...
- Malicious JavaScript can be hidden.
- Only whitelist filters are effective at rejecting
it.
21Hygienic Construction
- Don't just concatenate strings together to form
HTML or JSON payloads. - Use encoding methods that ensure that everything
is properly quoted.
22Excellent Code Quality
- If code is clean and readable, it is less likely
to contain insecurities.
23There is no security in obscurity
- Tricks and puzzles are not effective.
- Speed bumps are not effective.
24False security increases the danger.
- Ineffective measures make things worse.
25What can an attacker do if he gets some script
into your page?
26An attacker can request additional scripts from
any server in the world.
- Once it gets a foothold, it can obtain all of the
scripts it needs.
27An attacker can read the document.
- The attacker can see everything the user sees.
28An attacker can make requests of your server.
- Your server cannot detect that the request did
not originate with your application.
29An attacker has control over the display and can
request information from the user.
- The user cannot detect that the request did not
originate with your application.
30An attacker can send information to servers
anywhere in the world.
31The browser does not prevent any of these.
- Web standards require these weaknesses.
32The consequences of a successful attack are
horrible.
- Harm to customers.
- Loss of trust.
- Legal liabilities.
33This is not a Web 2.0 problem.
- All of these problems came with Netscape 2 in
1995.
34If the web as been totally screwed up from the
beginning, why should we worry about it now?
- 1. Escalating legal penalties
- 2. Mashups
- 3. Competition
35Competition to displace the web.
- Silverlight. AIR.
- JavaFX. Curl. Rebol.
36That wouldn't be the end of the world.
- It would just be the end of the WWW.
37Mashups
- The most interesting innovation in software
development in 20 years.
38Mashups are insecure.
- Mashups must not have access to any confidential
information.
39Advertising is a mashup.
40If there is script from two or more sources, the
application is not secure.
41HTML Is Not Secure
- Browser implementations use standards as a point
of departure. - In the current environment, a browser that only
supports standards would be worthless. - Browsers allow JavaScript to be hidden.
42Many nested languages with different quoting,
escapement, and commenting conventions
43The Turducken Problem
- Many Languages
- HTTP, HTML, URL, CSS, JavaScript, XML, JSON,
plaintext, PHP, SQL... - Each language has different quoting and
commenting conventions. - The languages can be nested inside each other.
44A text that is benign in one context might be
dangerous in another.
- Sloppy encoding allows injection of evil scripts
45A Simple Attack
- http//yoursite.com/ltscriptgt...lt/scriptgt
- lthtmlgtltbodygt
- ltpgt404 File not found ltscriptgt...lt/scriptgt
- lt/pgtlt/bodygtlt/htmlgt
- The script runs with the authority of your site.
46Never stuff raw text into a page.
- Every insertion must be properly encoded for its
role.
47Coding hygiene is critical for avoiding turducken
attacks.
- Use good encoders.
- Do not use simple concatenation or text
insertion. - Never trust the browser.
- Validate all input.
48Turducken
49PC Operating Systems confused the interests of
the user and the program.
50The Source of Perpetual Woe
- The browser is able to handle two interests The
Site and the User. Sort of. - It is unable to go safely beyond two interests.
- The browser's model does not allow for the
protection of multiple interests. - Mashups require support for multiple interests.
- Interactive advertising requires support for
multiple interests.
51XSS
- XSS Attacks are a direct consequence of the
browser's inability to manage multiple interests. - Any script that can trick its way onto the page
receives that same rights a privileges as the
site's own scripts. - An interactive ad or a mashup is an intentional,
self-inflicted XSS attack.
52JavaScript's Security Model Is Intolerable
- The global object-based design subjects all
applications to XSS attacks. - Third party scripts get first party privileges.
- New mashup-based application patterns call for
intentional injection of third party scripts. - This is dangerous.
53JavaScript Is the Problem
- We need to replace JavaScript with a similar
language that has the same expressive power, but
that provides cooperation under mutual suspicion
at the language level. - The new language must break compatibility with
ES3. - The ES4 proposal is attempting to solve a
different problem.
54A Three Prong Strategy to Fix the Web
- Safe JavaScript subsets.
- Small browser improvements.
- Massive browser improvements.
- This could take a while, so we should proceed on
all three immediately.
551. Safe JavaScript Subsets.
- The easiest way to improve JavaScript is to make
it smaller. - Caja. Cajita. ADsafe.
562. Add Simple Features to the Browsers.
- Even simple improvements can take a long time to
distribute.
57JSONRequest for safe data interchange.
58Vats
Communicating computational containment vessels
59HTML Provides No Modules
- It was conceived to be a document format.
- We are using it as an application format.
- Applications requires modules.
- Modules protect their contents.
- Modules communicate by exposing clean interfaces.
- Iframes are not safe enough.
60Vats
- Adapting Google's Gears or Adobe's AIR to provide
communicating containment vessels is a partial
solution. - Provides cooperation under mutual suspicion.
- Heavyweight.
- Deployment is difficult.
- Still subject to XSS attacks.
613. We need to replace JavaScript and the DOM.
- As long as we are using insecure languages, we
will be subject to XSS attacks.
62Ultimately
- We need to replace JavaScript with a secure
language. - We need to replace HTML and the DOM with a secure
application delivery system.
63Most security architecture adds a lot of
inconvenience and little real safety.
- The TSA model isn't working.
64Object Capability Model
- Better living through object oriented security
65A is an Object. Object A has state and behavior.
66has-a
Object A has a reference to Object B.
An object can have references to other objects.
67Object A can communicate with Object B...
...because it has a reference to Object B.
68Object B provides an interface that constrains
access to its own state and references.
Object A does not get access to Object B's
innards.
69Object A does not have a reference to Object C,
so Object A cannot communicate with Object C.
In an Object Capability System, an object can
only communicate with objects that it has
references to.
70An Object Capability System is produced by
constraining the ways that references are
obtained.
- A reference cannot be obtained simply by knowing
the name of a global variable or a public class.
71There are exactly three ways to obtain a
reference.
- By Creation.
- By Construction.
- By Introduction.
721. By Creation
- If a function creates an object, it gets a
reference to that object.
732. By Construction
- An object may be endowed by its constructor with
references. - This can include references in the constructor's
context and inherited references.
743. By Introduction
A has a references to B and C. B has no
references, so it cannot communicate with A or
C. C has no references, so it cannot communicate
with A or B.
753. By Introduction
A calls B, passing a reference to C.
Miller Diagram
763. By Introduction
B is now able to communicate with C.
It has the capability.
77If references can only be obtained by Creation,
Construction, or Introduction, then you may have
a safe system.
78If references can be obtained in any other way,
you do not have a safe system.
79Potential weaknesses include
- Arrogation.
- Corruption.
- Confusion.
- Collusion.
-
801. Arrogation
- To take or claim for oneself without right.
- Global variables.
- public static variables.
- Standard libraries that grant powerful
capabilities like access to the file system or
the network or the operating system to all
programs or objects. - Address generation.
812. Corruption
- It should not be possible to tamper with or
circumvent the system or other objects.
823. Confusion
- It should be possible to create objects that are
not subject to confusion. A confused object can
be tricked into misusing its capabilities.
834. Collusion
- It must not be possible for two objects to
communicate until they are introduced. - If two independent objects can collude, they
might be able to pool their capabilities to cause
harm. - For example, I can give gasoline to one object,
and matches to another. I need to be confident
that they cannot collude.
84Stepping down the power
- Some objects are too powerful to give to guest
code. - We can give those capabilities to intermediate
objects that will constrain the capability. - For example, an intermediate object for a file
system might limit access to a particular device
or directory, or limit the size of files, or the
number of files, or the longevity of files, or
the types of files.
85Ultimately, every object should be given exactly
the capabilities it needs to do its work.
- Capabilities should be granted on a need-to-do
basis. - Information Hiding - Capability Hiding.
86Intermediate objects, or facets, can be very
light weight.
- Class-free languages can be especially effective.
87The Facet object limits the Guest object's access
to the Dangerous object.
The Guest object cannot tamper with the Facet to
get a direct reference to the Dangerous object.
88References are not revocable.
- Once you introduce an object, you can't ask it to
forget it. - You can ask, but you should not depend on your
request being honored.
89The Guest object has a reference to an Agency
object. The Guest asks for an introduction to the
Dangerous object.
90The Agency object makes a Facet, and gives it to
the Guest.
The Facet might be a simple pass through.
91When the Agency wants to revoke the capability,
it tells the Facet to forget its capability.
The Facet is now useless to the Guest.
92A Facet can mark requests so that the Dangerous
object can know where the request came from.
93Facets
- Very expressive.
- Easy to construct.
- Lightweight.
- Rights Attenuation.
- Revocation.
- Notification.
- Delegation.
- Accountability.
- The best OO patterns are also capability patterns
94Good Object Capability Design is Good Object
Oriented Design
95The web needs a secure programming language.
- JavaScript is not that language.
96Meanwhile
- Never trust the browser.
- Formally validate everything you receive from the
browser. - Properly encode everything you send to the
browser or database. - Do not circumvent what little security the
browser offers. - Never put data on the wire unless you want it to
be delivered. - Don't take ineffective measures.
97Be Rigorous
- Sloppiness aids the Enemy.
- Neatness counts.
- Use good encoders.
- Avoid concatenation.
- Be paranoid.