SlideShare a Scribd company logo
Trusted Types
Securing the DOM from the bottom up
Krzysztof Kotowicz, Google
@kkotowicz
github.com/koto
koto@google.com
DOM XSS
Google Vulnerability Reward Program
payouts in 2018
XSS 35.6%
Non-web issues 49.1%
Mobile app vulnerabilities
Business logic (authorization)
Server /network misconfigurations
...
Source: HackerOne report, 2018
Consumer
Goods
Financial services &
insurance Government Healthcare Media &
Entertainment
Professional
services
Retail &
Ecommerce
Technology Telecom Transportation Travel &
Hospitality
Figure 5: Listed are the top 15 vulnerability types platform wide, and the percentage of vulnerabilities received per industry
Cross Site scripting (XSS)
Information disclosure
Improper authentication
Violation of secure
design principles
Cross-site request
forgery (CSRF)
Open redirect
Privilege Escalation
Improper access control
Cryptographic issues
Denial of service
Business logic errors
Code injection
SQL injection
Vulnerabilities by industry
Paid bounties by vulnerability
on Mozilla websites in 2016 and 2017
Source: @jvehent, Mozilla
CountofVulnerability
w
sec-xss
w
sec-applogic
w
sec-disclosure
w
sec-im
personation
w
sec-objref
w
sec-injection
w
sec-appm
isconfig
w
sec-authentication
w
sec-redirect
w
sec-oscm
d
w
sec-http-header-inject
w
sec-serverm
isconfig
w
sec-sqli
w
sec-authorization
w
sec-crossdom
ain
w
sec-csrf
DOM XSS is a client-side XSS variant caused by the DOM API not being secure by
default.
● User controlled strings get converted into code
● via dangerous and error-prone DOM sinks like:
innerHTML, window.open(), ~60 other DOM sinks
var foo = location.hash.slice(1);
document.querySelector('#foo').innerHTML = foo;
How does DOM XSS happen?
Example: https://example.com/#<img src=x onerror=alert('xss')>
HTMLFormElement.action
Element.innerHTML
window.open
HTMLAreaElement.href
HTMLMediaElement.src
HTMLFrameElement.src
HTMLSourceElement.src
HTMLTrackElement.src
HTMLInputElement.src
location.assign
location.hrefdocument.write
HTMLButtonElement.formAction
HTMLFrameElement.srcdoc
HTMLImageElement.src
HTMLEmbededElement.src
HTMLScriptElement.textContent
HTMLInputElement.formAction
HTMLScriptElement.InnerText
HTMLBaseElement.href
Growing problem
● DOM sinks can be used by your own code
○ … or the libraries you use
○ … or the scripts you load (analytics?)
○ … or the script that they load at runtime.
● Each of those potentially introduces DOM XSS
● Static analysis for JS does not work reliably
● At Google, DOM XSS is already the most common XSS variant.
We know how to address it!
Safe Types (link)
● 6+ years of experience
● Protects Gmail and almost all other
Google applications
● Evidence of efficacy
● Securely produce values that end up in DOM
● Implemented as Java{Script},Go, … libraries
● We're porting this approach directly to the browsers
Trusted Types
Strongly typed DOM API.
● Don't pass (HTML, URL, script URL) strings to the DOM sinks
● Use objects instead
● DOM already supports it:
● Web platform (or polyfill) provides typed objects:
TrustedHTML, TrustedScript, TrustedURL, TrustedScriptURL
● Security rules & controls are based on types
The idea behind Trusted Types
el.innerHTML = {toString: () => 'hello'};
el.innerHTML // 'hello'
When Trusted Types are not enforced:
DOM sinks accepts strings as usual
DOM sinks accept typed objects
element.innerHTML = aTrustedHTML;
The idea behind Trusted Types
When Trusted Types are enforced:
DOM sinks reject strings
DOM sinks accept typed objects
Content-Security-Policy: trusted-types myPolicy
element.innerHTML = location.hash.slice(1); // a string
element.innerHTML = aTrustedHTML; // created via a TrustedTypes policy
The idea behind Trusted Types
When Trusted Types are in reporting mode:
DOM sinks accept & report strings
DOM sinks accept typed objects
Content-Security-Policy-Report-Only: trusted-types myPolicy; report-uri /report
element.innerHTML = location.hash.slice(1); // a string
element.innerHTML = aTrustedHTML; // created via a TrustedTypes policy
The idea behind Trusted Types
1. Create policies with validation rules
2. Use the policies to create Trusted Type objects
3. Enforce "myPolicy" by setting a Content Security Policy header
Creating Trusted Types
Content-Security-Policy: trusted-types myPolicy myOtherPolicy
const SanitizingPolicy = TrustedTypes.createPolicy('myPolicy', {
createHTML(s: string) => myCustomSanitizer(s)
}, false);
// Calls myCustomSanitizer(foo).
const trustedHTML = SanitizingPolicy.createHTML(foo);
element.innerHTML = trustedHTML;
Control policy creation:
● Policy name whitelist:
● No duplicate policy names
Control policy usage:
● Policies are JavaScript objects
● Lock them in a module, inside a local function variable etc.
Control over policies
Content-Security-Policy: trusted-types myPolicy myOtherPolicy
The "default" policy is called as a fallback when a string is assigned to a sink.
Good way to get started and to identify risky DOM assignments.
Trusted Types - default policy
Content-Security-Policy: trusted-types myPolicy default
TrustedTypes.createPolicy('default', {
createHTML(s) {
console.log("Please fix! Insecure string assignment:", s);
return s;
}
}, true);
Demo
bit.ly/trusted-types-demo2
→
Benefits
Source ... Policy Trusted Type→ → → ... DOM sink→
● Reduced attack surface - The risky data flow will always be:
● Isolates security-sensitive code from the rest of the applications
● No DOM XSS if policies are secure and access restricted
● Compile time & runtime security validation
Trusted Types in practice
● Policies in a few trusted components
○ Frameworks
○ Templating engines
○ HTML sanitizers (DOMPurify)
● It's easy to add support to modern frameworks (sample patch for Angular)
● Code size overhead negligible
○ 66 bytes of the smallest polyfill
○ ~300 bytes in Google Closure
Porting modern applications is easy (we're already doing this for our apps!)
● Active development, looking for your feedback!
● Support in Chromium browsers (origin trial - tinyurl.com/try-trusted-types)
● Discussion group - trusted-types@googlegroups.com
● W3C specification draft - wicg.github.io/trusted-types/
● Polyfills & documentation at bit.ly/trusted-types
Working on external integrations:
● DOMPurify
● Modern JS frameworks
● TypeScript type definitions - definitelytyped.org
● Secure policies library & other tooling (coming soon!)
Project status
Try Trusted Types now!
bit.ly/trusted-types

More Related Content

PDF
Trusted Types and the end of DOM XSS
PDF
Trusted Types @ W3C TPAC 2018
PDF
Locking the Throne Room - How ES5+ might change views on XSS and Client Side ...
PDF
Scriptless Attacks - Stealing the Pie without touching the Sill
PDF
Securing your Node.js App
PDF
Generic Attack Detection - ph-Neutral 0x7d8
PDF
Rethinking The Policy Agent
PDF
Locking the Throneroom 2.0
Trusted Types and the end of DOM XSS
Trusted Types @ W3C TPAC 2018
Locking the Throne Room - How ES5+ might change views on XSS and Client Side ...
Scriptless Attacks - Stealing the Pie without touching the Sill
Securing your Node.js App
Generic Attack Detection - ph-Neutral 0x7d8
Rethinking The Policy Agent
Locking the Throneroom 2.0

What's hot (20)

PDF
The innerHTML Apocalypse
PDF
In the DOM, no one will hear you scream
PDF
ECMAScript 6 from an Attacker's Perspective - Breaking Frameworks, Sandboxes,...
PDF
The Image that called me - Active Content Injection with SVG Files
PDF
The Ultimate IDS Smackdown
PPTX
SMIMP Lightning Talk - DEFCON CryptoVillage
PDF
The Future of Web Attacks - CONFidence 2010
PDF
Dev and Blind - Attacking the weakest Link in IT Security
PDF
A XSSmas carol
PDF
Security in PHP Applications: An absolute must!
PPTX
XML Encryption
PDF
Cryptography In The Browser Using JavaScript
PPTX
XML Security
PPT
Lock Interface in Java
PDF
Security Vulnerabilities: How to Defend Against Them
PDF
Dot Net Training in Chennai
PPT
Cookie mechanism and attacks on web-client
PDF
Developer's Guide to JavaScript and Web Cryptography
PDF
Java script and web cryptography (cf.objective)
PPTX
Less is More by Matt Christensen
The innerHTML Apocalypse
In the DOM, no one will hear you scream
ECMAScript 6 from an Attacker's Perspective - Breaking Frameworks, Sandboxes,...
The Image that called me - Active Content Injection with SVG Files
The Ultimate IDS Smackdown
SMIMP Lightning Talk - DEFCON CryptoVillage
The Future of Web Attacks - CONFidence 2010
Dev and Blind - Attacking the weakest Link in IT Security
A XSSmas carol
Security in PHP Applications: An absolute must!
XML Encryption
Cryptography In The Browser Using JavaScript
XML Security
Lock Interface in Java
Security Vulnerabilities: How to Defend Against Them
Dot Net Training in Chennai
Cookie mechanism and attacks on web-client
Developer's Guide to JavaScript and Web Cryptography
Java script and web cryptography (cf.objective)
Less is More by Matt Christensen
Ad

Similar to Trusted Types - Securing the DOM from the bottom up (JSNation Amsterdam) (20)

PDF
[OPD 2019] Trusted types and the end of DOM XSS
PDF
パフォーマンスを考慮したプリミティブなTrusted TypesによるClient-Side XSS防御手法
PPTX
Web security: Securing Untrusted Web Content in Browsers
PPTX
Web security: Securing untrusted web content at browsers
PPTX
Webinar–Reviewing Modern JavaScript Applications
PPTX
Cos 432 web_security
PPTX
W3 conf hill-html5-security-realities
PDF
Webinar–OWASP Top 10 for JavaScript for Developers
PPTX
Confidence web
PDF
AppSec Tel Aviv - OWASP Top 10 For JavaScript Developers
PDF
BsidesDelhi 2018: DomGoat - the DOM Security Playground
PPT
Reversing JavaScript
PDF
Why Traditional Web Security Technologies no Longer Suffice to Keep You Safe
PPTX
Pentesting Modern Web Apps: A Primer
PPTX
Dmk sb2010 web_defense
PDF
Secure java script-for-developers
PDF
Waf.js: How to Protect Web Applications using JavaScript
PDF
SBA Live Academy: A Primer in Single Page Application Security by Thomas Konrad
PPTX
How to React to JavaScript Insecurity
PPT
(In)Security Implication in the JS Universe
[OPD 2019] Trusted types and the end of DOM XSS
パフォーマンスを考慮したプリミティブなTrusted TypesによるClient-Side XSS防御手法
Web security: Securing Untrusted Web Content in Browsers
Web security: Securing untrusted web content at browsers
Webinar–Reviewing Modern JavaScript Applications
Cos 432 web_security
W3 conf hill-html5-security-realities
Webinar–OWASP Top 10 for JavaScript for Developers
Confidence web
AppSec Tel Aviv - OWASP Top 10 For JavaScript Developers
BsidesDelhi 2018: DomGoat - the DOM Security Playground
Reversing JavaScript
Why Traditional Web Security Technologies no Longer Suffice to Keep You Safe
Pentesting Modern Web Apps: A Primer
Dmk sb2010 web_defense
Secure java script-for-developers
Waf.js: How to Protect Web Applications using JavaScript
SBA Live Academy: A Primer in Single Page Application Security by Thomas Konrad
How to React to JavaScript Insecurity
(In)Security Implication in the JS Universe
Ad

More from Krzysztof Kotowicz (15)

PDF
Biting into the forbidden fruit. Lessons from trusting Javascript crypto.
PDF
Hacking HTML5 offensive course (Zeronights edition)
PDF
I'm in ur browser, pwning your stuff - Attacking (with) Google Chrome Extensions
PDF
HTML5: Atak i obrona
PDF
I'm in your browser, pwning your stuff
PDF
Advanced Chrome extension exploitation
PDF
Html5: Something wicked this way comes (Hack in Paris)
PDF
Something wicked this way comes - CONFidence
PDF
Html5: something wicked this way comes - HackPra
PDF
Html5: something wicked this way comes
PDF
Creating, obfuscating and analyzing malware JavaScript
PDF
Tworzenie, zaciemnianie i analiza złośliwego kodu JavaScript
PDF
Jak ocalić swoje dane przed SQL injection?
PDF
SQL Injection: complete walkthrough (not only) for PHP developers
PPT
Kompletny przewodnik po SQL injection dla developerów PHP (i nie tylko)
Biting into the forbidden fruit. Lessons from trusting Javascript crypto.
Hacking HTML5 offensive course (Zeronights edition)
I'm in ur browser, pwning your stuff - Attacking (with) Google Chrome Extensions
HTML5: Atak i obrona
I'm in your browser, pwning your stuff
Advanced Chrome extension exploitation
Html5: Something wicked this way comes (Hack in Paris)
Something wicked this way comes - CONFidence
Html5: something wicked this way comes - HackPra
Html5: something wicked this way comes
Creating, obfuscating and analyzing malware JavaScript
Tworzenie, zaciemnianie i analiza złośliwego kodu JavaScript
Jak ocalić swoje dane przed SQL injection?
SQL Injection: complete walkthrough (not only) for PHP developers
Kompletny przewodnik po SQL injection dla developerów PHP (i nie tylko)

Recently uploaded (20)

PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Cloud computing and distributed systems.
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Machine learning based COVID-19 study performance prediction
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
cuic standard and advanced reporting.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Cloud computing and distributed systems.
Mobile App Security Testing_ A Comprehensive Guide.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Unlocking AI with Model Context Protocol (MCP)
Machine learning based COVID-19 study performance prediction
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Network Security Unit 5.pdf for BCA BBA.
20250228 LYD VKU AI Blended-Learning.pptx
Empathic Computing: Creating Shared Understanding
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Encapsulation theory and applications.pdf
Review of recent advances in non-invasive hemoglobin estimation
cuic standard and advanced reporting.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
“AI and Expert System Decision Support & Business Intelligence Systems”

Trusted Types - Securing the DOM from the bottom up (JSNation Amsterdam)

  • 1. Trusted Types Securing the DOM from the bottom up Krzysztof Kotowicz, Google @kkotowicz github.com/koto koto@google.com
  • 3. Google Vulnerability Reward Program payouts in 2018 XSS 35.6% Non-web issues 49.1% Mobile app vulnerabilities Business logic (authorization) Server /network misconfigurations ...
  • 4. Source: HackerOne report, 2018 Consumer Goods Financial services & insurance Government Healthcare Media & Entertainment Professional services Retail & Ecommerce Technology Telecom Transportation Travel & Hospitality Figure 5: Listed are the top 15 vulnerability types platform wide, and the percentage of vulnerabilities received per industry Cross Site scripting (XSS) Information disclosure Improper authentication Violation of secure design principles Cross-site request forgery (CSRF) Open redirect Privilege Escalation Improper access control Cryptographic issues Denial of service Business logic errors Code injection SQL injection Vulnerabilities by industry
  • 5. Paid bounties by vulnerability on Mozilla websites in 2016 and 2017 Source: @jvehent, Mozilla CountofVulnerability w sec-xss w sec-applogic w sec-disclosure w sec-im personation w sec-objref w sec-injection w sec-appm isconfig w sec-authentication w sec-redirect w sec-oscm d w sec-http-header-inject w sec-serverm isconfig w sec-sqli w sec-authorization w sec-crossdom ain w sec-csrf
  • 6. DOM XSS is a client-side XSS variant caused by the DOM API not being secure by default. ● User controlled strings get converted into code ● via dangerous and error-prone DOM sinks like: innerHTML, window.open(), ~60 other DOM sinks var foo = location.hash.slice(1); document.querySelector('#foo').innerHTML = foo; How does DOM XSS happen? Example: https://example.com/#<img src=x onerror=alert('xss')>
  • 8. Growing problem ● DOM sinks can be used by your own code ○ … or the libraries you use ○ … or the scripts you load (analytics?) ○ … or the script that they load at runtime. ● Each of those potentially introduces DOM XSS ● Static analysis for JS does not work reliably ● At Google, DOM XSS is already the most common XSS variant.
  • 9. We know how to address it! Safe Types (link) ● 6+ years of experience ● Protects Gmail and almost all other Google applications ● Evidence of efficacy ● Securely produce values that end up in DOM ● Implemented as Java{Script},Go, … libraries ● We're porting this approach directly to the browsers
  • 11. Strongly typed DOM API. ● Don't pass (HTML, URL, script URL) strings to the DOM sinks ● Use objects instead ● DOM already supports it: ● Web platform (or polyfill) provides typed objects: TrustedHTML, TrustedScript, TrustedURL, TrustedScriptURL ● Security rules & controls are based on types The idea behind Trusted Types el.innerHTML = {toString: () => 'hello'}; el.innerHTML // 'hello'
  • 12. When Trusted Types are not enforced: DOM sinks accepts strings as usual DOM sinks accept typed objects element.innerHTML = aTrustedHTML; The idea behind Trusted Types
  • 13. When Trusted Types are enforced: DOM sinks reject strings DOM sinks accept typed objects Content-Security-Policy: trusted-types myPolicy element.innerHTML = location.hash.slice(1); // a string element.innerHTML = aTrustedHTML; // created via a TrustedTypes policy The idea behind Trusted Types
  • 14. When Trusted Types are in reporting mode: DOM sinks accept & report strings DOM sinks accept typed objects Content-Security-Policy-Report-Only: trusted-types myPolicy; report-uri /report element.innerHTML = location.hash.slice(1); // a string element.innerHTML = aTrustedHTML; // created via a TrustedTypes policy The idea behind Trusted Types
  • 15. 1. Create policies with validation rules 2. Use the policies to create Trusted Type objects 3. Enforce "myPolicy" by setting a Content Security Policy header Creating Trusted Types Content-Security-Policy: trusted-types myPolicy myOtherPolicy const SanitizingPolicy = TrustedTypes.createPolicy('myPolicy', { createHTML(s: string) => myCustomSanitizer(s) }, false); // Calls myCustomSanitizer(foo). const trustedHTML = SanitizingPolicy.createHTML(foo); element.innerHTML = trustedHTML;
  • 16. Control policy creation: ● Policy name whitelist: ● No duplicate policy names Control policy usage: ● Policies are JavaScript objects ● Lock them in a module, inside a local function variable etc. Control over policies Content-Security-Policy: trusted-types myPolicy myOtherPolicy
  • 17. The "default" policy is called as a fallback when a string is assigned to a sink. Good way to get started and to identify risky DOM assignments. Trusted Types - default policy Content-Security-Policy: trusted-types myPolicy default TrustedTypes.createPolicy('default', { createHTML(s) { console.log("Please fix! Insecure string assignment:", s); return s; } }, true);
  • 19. → Benefits Source ... Policy Trusted Type→ → → ... DOM sink→ ● Reduced attack surface - The risky data flow will always be: ● Isolates security-sensitive code from the rest of the applications ● No DOM XSS if policies are secure and access restricted ● Compile time & runtime security validation
  • 20. Trusted Types in practice ● Policies in a few trusted components ○ Frameworks ○ Templating engines ○ HTML sanitizers (DOMPurify) ● It's easy to add support to modern frameworks (sample patch for Angular) ● Code size overhead negligible ○ 66 bytes of the smallest polyfill ○ ~300 bytes in Google Closure Porting modern applications is easy (we're already doing this for our apps!)
  • 21. ● Active development, looking for your feedback! ● Support in Chromium browsers (origin trial - tinyurl.com/try-trusted-types) ● Discussion group - trusted-types@googlegroups.com ● W3C specification draft - wicg.github.io/trusted-types/ ● Polyfills & documentation at bit.ly/trusted-types Working on external integrations: ● DOMPurify ● Modern JS frameworks ● TypeScript type definitions - definitelytyped.org ● Secure policies library & other tooling (coming soon!) Project status
  • 22. Try Trusted Types now! bit.ly/trusted-types