start	end	text
2040	8100	Hello, my name is Aaron. And I guess I have to apologize. The resemblance is a little hard to
8100	12340	see in that picture, because my hair is not nearly as luscious as it is in that photo.
12380	17800	But I'm sorry, your mineral content in the water, as you've all found out, really awful here.
18700	25040	But yeah, like Jen, I work at Google. We do web security on hundreds and thousands of different
25040	29760	web apps. I've been at Google for a little under seven years. And before my stint in security,
29760	34780	I came from the developer side. I've worked on products like Google Docs and GCP on customer
34780	40780	facing stuff. So when we approach this work of trying to make our web apps as secure as possible,
40780	45400	we approach it with a special empathy for the developer, where we want to ship the most secure
45400	50160	products, but also we don't want to get in the way of delivering really dynamic and new user
50160	57720	features. Cool. Thanks, Aaron. Here's a quick look at our agenda for today. So we'll start with a
57720	61500	quick intro to XSS. I assume most of you are familiar with it, but we'll still go over some
61500	65660	examples. And then we'll talk about runtime mitigation, specifically those that come with
65660	70400	content security policy and its different flavors. Then we'll talk about how we use those to secure
70400	76180	applications at scale. And we'll move on to how we imagine the post-XSS world and end with a call
76180	83100	to action. Cool. So cross-site scripting. It's just some data to motivate that this is a big
83100	87980	problem. Google runs a vulnerability reward program where we pay out security researchers
87980	94980	when they report their bugs to us. As you can see here, over 50% of the bugs reported are web-related.
94980	103080	That makes sense. Google is a web company. And then the rest, out of those 50%, almost 35% are XSS.
103080	107400	So this was a really big problem, and that's why we've developed techniques to remediate the stats
107400	113520	at scale. So let's talk about what XSS really is. The root cause is user input gets mixed up with
113520	119600	code and then gets interpreted as code. Because HTML is dynamic and mixes static data with logic
119600	124480	that should execute, as you see here, this is very prevalent. And this leads to malicious scripts
124480	130740	getting injected into a trusted site. In this example, you see a very simple example, there's a
130740	134720	description that's supposed to be dynamic, but if this is user-controlled, we can inject the following
134720	141360	script and get it to execute. And there you have an XSS. An XSS, when exploited, lets the attacker
141360	145960	take control of the account of a logged-in user. This means the attacker can do anything the victim
145960	150840	can, so it's pretty powerful, and that's why we try really hard to avoid it. We also call this the
150840	156240	client-side version of remote code execution, as they share a similar concept of arbitrary code
156240	162180	execution. And here's another format you might be used to seeing XSS. We usually pop an alert
162180	169020	with the document domain as a proof of concept when we find an XSS record. So a quick example of
169020	175560	DOM XSS, the client-side variant of cross-site scripting. The root cause here is DOM APIs are
175560	180680	not secure by default. We all love JavaScript, and JavaScript loves turning strings into code
181320	187340	through APIs like innerHTML or script.innerText. Here's a very simple example of how this might
187340	191840	happen. In this code snippet, locationHash is a user-controlled string. We take that and
191840	198080	happily insert it into innerHTML. In this case, with the example here, the onError alert statement
198080	203000	will execute, and then we have XSS. But no one codes like this anymore, right? Thanks to modern
203000	207200	frameworks like React that abstract away file manipulations, nobody has to do this.
209580	215960	Except... not. As you can see in this fresh bit of internet outrage, an enterprising developer has
215960	221980	managed to work in a innerHTML assignment inside a very functional React component. It's supposed
221980	226320	to be a really advanced system that takes care of the virtual DOM dipping, all of the different
226320	230020	re-rendering things, so that you don't have to do this yourself, but this developer didn't really
230020	235180	want to take it into their own hands. And this is the crux of the problem. Just because a framework
235180	240240	can abstract away the complex DOM operations doesn't mean that it can prevent a developer
240240	245220	from using them. And these issues are really easy to introduce in your codebase. It really takes
245500	250500	a developer, a code reviewer, having a bad day and once it sneaks into your product then
251140	255740	it's really hard to eradicate. So do we stand a chance against this class of problems?
256060	260780	And we think we do. We've all heard of content security policy and how it can solve all your
260780	266620	process scripting, right? Well, we do admit that content security policy is a little bit difficult
266620	272800	to understand and configure. It really is more like a meta feature than a feature. It has a lot
272800	277220	of bells and whistles and knobs that you can change, a lot of directives. What do you have
277220	282140	to set on these directives? And it is a little confusing because it tries to cover everything,
282140	286700	any sort of resource that loads on your page. But hopefully when we go on this journey together,
286700	292560	we can talk about some of the configurations that we found to work at scale and cover many
292560	299780	classes of cross-site scripting and we'll see why they work together. And before we begin,
299780	306960	since we are talking about cross-site scripting and CSP and protections that Google uses,
306960	312040	maybe we'll take a look at what is actually working in the wild. So here we have an example
312040	316520	of a particular Google web app that some of you may have used. You know it has a really complicated
316520	322740	UI with a lot of interactive user features and a small call out to this team. They've been great
322740	329200	partners in really shipping the best and greatest and the latest web protection mitigations and
329200	333860	working really hard to keep their end users safe. So let's open up Chrome DevTools and see
333860	340280	what we are actually shipping on a real life product. And as you can see here, we see a couple
340280	344540	different content security policies, a lot of them as a matter of fact. But like we said before,
344540	350120	not all of them are specifically for cross-site scripting defenses because it's a meta feature
350120	355100	that can also turn off other potential dangerous vectors as well. We don't have time to get into
355100	359120	the other protections, but if you have questions, we will be sticking around for a little bit after
359120	364740	we can go into more detail after the talk. But for now, we'll focus on these two, the ones that
364740	370660	we find to be really powerful protections against cross-site scripting, namely the nonce-only CSP
371280	378720	and the trusted types header. Cool, and we'll go into some details. So nonce-only CSP,
378720	383800	in the example we saw before, it's pretty self-explanatory. It's the one with the nonce
383800	390000	directive. More specifically, a nonce-only policy takes a form that looks a bit like this.
390000	394820	Internally, we recommend a nonce-only or a strict CSP for most of our modern web apps.
394820	399960	What this does is only what's allow-listed under the script source directive can run on the page.
399960	403900	Other scripts will be blocked. So in this case, any script with the right nonce,
403900	409220	which is a random value that's generated per request, can run on the page. So the
409220	412920	templating systems usually propagate the nonce for each script tag on the HTML,
412920	418000	and the header has to send the right value for the nonce. So the header and the HTML work
418000	421840	together to indicate which scripts are trusted, and the browser can act accordingly.
422480	427680	Another CSP directive that's useful for a nonce-only policy is the strict dynamic directive.
427680	432840	This directive enables a transitive trust for scripts. So if you have one script with the
432840	437820	right nonce, it's allowed to load other scripts, and the trust is propagated. When we don't have
437820	441640	this directive, only scripts with the right nonce can execute. If they load other scripts,
441640	447320	they'll be blocked. So although this slightly relaxes the policy, we found that it's also
447320	452540	made adoptions a lot easier for complex web apps. So that's the trade-off that we're working on.
453820	458280	So thanks, Jen. So as Jen mentioned, a nonce-only CSP is great if you have the
458280	462220	back-end or the server-side render content working in close conjunction with the front-end
462220	466940	or client-side code, because you have to generate these nonces that are new for every single
466940	471700	response. But what if you don't have control over the back-end? Then we have to use a close
471700	476180	relative of the nonce-only CSP called the hash-based CSP. And the way it works is,
476180	480800	it's very similar to a nonce-based policy, but as you've noticed, instead of the nonce value,
480800	485980	we provide a hash value. And this is really powerful because we don't have to regenerate
485980	492980	this CSP for every response load. The hash is fixed, presumably because on every response,
492980	499480	your script is mostly the same. And this is really powerful when you don't have control
499480	506600	over that back-end, so you can put in the logic to do this nonce generation. And this is also
506600	512560	super useful for the SBA, single-page applications, because when you are doing the final bundling
512560	518120	process for your final HTML, you know what script has are going to be there in your SBA bundle,
518120	524000	and use that to generate your hash, and there you go, a fixed CSP policy that offers this
524000	531420	protection that you can even drop into a meta tag at the top of the HTML file. And in more detail,
531420	535060	so what you have on the left is sort of like what your bundle might look like for a particular
535060	539960	single-page application. You've got a couple scripts loading to actually get the user interaction
539960	547040	going. And we would actually rewrite this in this case into a dynamic loader inside an inline script
547040	551580	block, because that is one of the characteristics of a hash-based CSP for now. You need to know
551580	557160	exactly what's in the script content in order to make the hash. So usually you do this by actually
557160	563220	inlining the block, and if you have resources that would load over the network, then you
563220	568780	turn it into JavaScript that would dynamically add it to the page. And once we have that,
568780	574480	we also added some preload tags so that we actually get the parallelization going for
574480	579700	fetching these resources. You can take the script block, create the hash, and stick it in as a meta
579700	585640	tag that carries a CSP, and there you go. The HTML is self-contained and offers this protection
585640	595300	with a CSP baked in. The next CSP-delivered mitigation that we want to talk about is
595300	600520	trusted types. Specifically, trusted types is geared towards protecting DOM-based XSS. So in
600520	605480	the example that we saw before, it's the one that says require trusted types for, and that's the
605480	611840	directive that you need to send. So we want to point out that the attack surface is large here
611840	617000	for DOM XSS. There's over 60 JavaScript APIs that can turn strings into code and lead to arbitrary
617000	622640	code execution, and DOM XSS is usually very easy to introduce and hard to detect. Calls to these
622640	626660	APIs could be going through multiple levels of indirections, it could be in a transitive
626660	630920	dependency, or it could exist in libraries that we don't have control over. So how can we make
630920	636280	this better? And we need a run-time mitigation for this, and this is where trusted types comes in.
636280	640680	The main idea behind trusted types is to make security decisions explicit and auditable.
640680	645900	To do this, trusted types will restrict the access to the dangerous DOM APIs we mentioned previously,
645900	652520	and it will require typed objects instead of plain strings to be passed into the over 60 APIs.
652580	656900	So in this case, instead of strings that represent script URLs, HTML, or scripts,
656900	661200	will require typed objects such as trusted script URL. And we'll discuss how to generate
661200	667040	these typed objects, but this is the main idea. This is what the header looks like, again.
667100	671160	This tells the browser to reject string assignments to injection syncs, and effectively
671160	676340	it provides a run-time type checking mechanism on top of JavaScript. If you remember the example
676340	681240	from our DOM XSS slide before, we're using a plain string here with innerHTML, and if trusted
681240	686620	types were enforced, we would throw an error that looks like this. So the browser will tell you you
686620	692640	need a trusted HTML, and it won't use a string. We want to highlight that most security guarantees
692640	698480	of trusted types come from a policy, and the policy is what I mentioned of how we could generate a
698480	704800	trusted object. So you can think of it as a wrapper that takes in a string and returns a trusted type.
704800	708720	And we apply this policy to strings before inserting them into the DOM sync, and the
708720	713280	processing you do within the policy should make sure the string won't lead to any unintended
713280	719740	code execution, therefore preventing DOM XSS. So in this example, we use a sanitizer from DOM Purify.
719740	724500	We check for the presence of trusted types in the JavaScript namespace for browser compatibility,
724500	728740	and then use the correct factory if it exists. But if you don't want to keep writing this verbose
728740	734340	if block, because it is a bit clunky, DOM Purify also works with trusted types off the shelf, so
734340	739340	it will do the policy creation and the browser compatibility checking under the hood for you
739340	741840	if you pass in the right config for trusted types.
745280	749420	Thanks, Jen. So now that we've talked about our security features that we believe are powerful
749420	753560	against cross-site scripting today, we'll touch a little bit on how to use them in your application
753560	758060	and offer a bit of the lessons that we've learned from securing our apps at scale at Google.
758060	761900	So before we begin, we'd like to make a quick plug for an article that presents many of the
761900	766560	ideas that we'll discuss in the next section, but in written form. This approach is used widely in
766560	770260	our team and our org, and this article goes into a little bit more detail than what we have time for
770260	775700	in some of the ideas that we'll elaborate on next. It's on the Google Security Engineering blog
775700	781880	at bughunters.google.com, along with other great content like this, and also similar to, I think,
781880	786760	the theme of this village. So please check us out. So back to the main show. What are some of
786760	792020	the tactics that we use to adopt these security features at scale on hundreds of different web
792020	795960	apps? So some of the lessons that we've learned that we find most useful are, number one,
795960	801020	use the frameworks that will help you in this journey. Number two, try to shift left to get
801020	806180	developers an indication whether this feature is incompatible with code that they're writing as
806180	812240	early as possible in the process. Number three, some tools that we've built and we also rely on
812240	817040	throughout this process that we found useful. And number four, a quick note about third-party
817040	822760	dependencies and how they might complicate your adoption process. So the first big idea is if
822760	827460	you're starting a new code base from scratch, let's think about which frameworks we can use
827460	833000	that will give us security superpowers. And the big idea behind that is that at our scale,
833000	837640	at at-scale defense against cross-site scripting at Google, we rely on frameworks with, number one,
837640	842880	context-aware templating, and number two, built-in compatibility with these security features that
842880	847700	are secure by default. And as a quick aside, we haven't talked too much about context-aware
847700	852420	templating right now, but we don't have too much time to get into that. But the gist of it is you
852420	857200	want to have a templating system that does reasonable enough things to prevent a lot of
857200	861340	server-side rendered cross-site scripting from happening. Catch us after the talk,
861340	864920	and we are happy to chat about templating too, which is beyond the scope of this talk.
866820	871380	So what we want to cover on this slide and for the rest of the talk is what we call the safe
871380	876640	calling philosophy at Google. And this is the idea that we want to give tools to developers
876640	881920	that take away a lot of the uncertainty from the developer on how to use and configure these
881920	886340	security features correctly, because they are really easy to misconfigure in really subtle
886340	892300	ways. And we want the security to not really get in the way of feature development and shipping
892300	896540	great things to our users, so we want to abstract away some of these decisions in a clear and
896540	903320	understandable manner. And the key to do this is through our frameworks and our APIs. And we'll
903320	908540	say that this is possible at Google because you do have tight control over the internal frameworks
908540	912900	that you use in-house, and also tight control over the guidance that we give to our developers
912900	917840	on how to use these frameworks. But I imagine many of the web developers here aren't building
917840	923140	internal web apps for Google, so how can you get some of these benefits? And thankfully, our,
923140	927820	yeah, colleagues have worked hard to ensure compatibility with some of these security
927820	932520	features and frameworks that are available outside of our internal ecosystem as well.
932520	937740	For instance, some of the frameworks that are also available externally, like Angular and Lit,
937740	942860	all the hurdling work that we do there translates to benefits for the ecosystem and for everyone in
942860	948280	the world to use. But if you're also using frameworks that are not entirely within the
948280	954720	Google ecosystem or very Google-inspired, we also have our colleagues at Meta who did a lot of work
954720	959260	to make React compatible with CSV and Trusted Types, and some of the frameworks that are based
959260	964940	on React, like Next.js, thanks to volunteers and other colleagues, have compatibility as well.
964940	970320	Great, so on to the second idea, how to shift left and enforce early. And this is just the idea
970320	974740	that you want to add these headers and protections as early as possible, maybe in your dev server,
974740	979480	so that when things go wrong, your developers can see the error and act on it as soon as they've
979480	984420	written the code, so that they can fix it while the context of what they're trying to do is fresh
984420	990340	in their minds. And a quick, and more technically, a way to do this is, well, here in this example,
990340	995780	we have a configuration for a Vite dev server. I hear that's what all the web developers are
995780	1002560	using nowadays. We are all on to Vite now. And here we see a Node.js express-style middleware
1002560	1006960	inside this configuration for the dev server that just dumps in these headers.
1008220	1013660	But if you don't have control over your dev server or your serving stack, as you mentioned before,
1013660	1019340	meta tags at the top of an HTML file also can deliver these headers. And for the observant
1019340	1024260	among you, you will see that here we have our hash-based CSP again, because if you are
1024260	1030260	baking a CSP into a static HTML page, you really want it to be a hash-based CSP so that
1030260	1036680	the nonce value isn't blown the first time you use it. And we saw an example of how to do the
1036680	1041580	refactoring to make a hash-based CSP before, and it was a little cumbersome, so how can you do that
1041580	1047340	more easily? And the answer is, we have an experimental tool available in GitHub and
1047340	1053620	NPM that does a lot of this transformation for you. This is available as a Webpack plugin,
1053620	1059320	but also as a utility library that takes in a string of the HTML and does a refactoring like
1059320	1063800	this. I mean, I know there's a lot of code on the screen, but it just takes in a string and
1063800	1069300	outputs a string and will shuffle around the notes, do the hashing, add the meta tags for you.
1069300	1074160	And this is really powerful because this is a very framework-agnostic way that now has proven
1074160	1079220	to have outlasted the popularity of Webpack plugins because they just keep changing the
1079220	1085820	configurations. But this is really powerful because if we remember the example from before
1085820	1091080	with the Vite middleware, it composes almost perfectly. This is just a copy-paste job of the
1091080	1096460	last two slides. And I think one of the lessons we learned in building some of this tooling is
1096460	1102160	if you keep it as agnostic as possible in terms of frameworks and just separate the logic from
1102160	1107060	the framework, then you can just, yeah, snap the pieces together when a new framework comes along.
1108120	1110340	So on to the next section.
1111560	1117120	Thanks, Aaron. In addition to using the header to spot violations early, we really advocate
1117120	1121480	for using some other tools to help you with adopting these defenses. We'll introduce a
1121480	1126600	bunch of helpful tools, but they can be categorically be broken down into, first of all,
1126600	1131520	tools for understanding features and violations. We mentioned that CSV is notoriously hard to
1131520	1136500	configure and understand, and the CSV evaluator solves that problem. It's available as either
1136780	1140640	a website or a Chrome extension, which will give you some high-level guidance on the configuration
1140640	1147780	of your CSV. It can detect directives and then kind of explain them. We're also actively working
1147780	1153600	on a tool called Trusted Types Helper. This isn't released yet, but it will be soon, for streamlining
1153600	1158120	the Trusted Types adoption process. One of the most time-consuming and painful stages of Trusted
1158120	1162240	Types enforcement is finding the source of violations and trying to fix them. So this Chrome
1162240	1167220	extension will create a DevTools tab that helps you demystify Trusted Types violations and suggest
1167220	1173100	some refactoring. So stay tuned. Another category of tools are those that help with refactoring
1173100	1179040	using Static Analysis. We have a huge Static Analysis pipeline at Google, and we took the
1179040	1184320	most effective checks that we run on our internal Static Analysis tooling and Linter and built it
1184320	1190100	on top of ESLint, and that is Safety Web. This project is also in active development, but it is
1190100	1195980	available on GitHub. As you can see in the example here, it will detect unsafe usages of risky APIs,
1195980	1200900	and you can refactor these unsafe calls with another tool we provide called Safe Values. So
1200900	1207060	Safe Values is a library designed to steer developers to make safer choices. It will prevent
1207060	1211960	unsafe assignments using TypeScript type checking unless the value is trusted, either from a trusted
1211960	1217980	source or by transformation that makes it trusted. For example, sanitizing an HTML makes it trusted.
1219000	1223500	We mentioned we were going to add a quick note about third-party dependencies. This
1223500	1228740	is the complicated part, and it's a big aspect of making your application compatible with these
1228740	1234080	features. For both Trusted Types and CSP, all code running on your site needs to be free of
1234080	1239160	violations of these policies, otherwise they will be blocked by the browser. This is a hard part
1239160	1244000	because we won't always know which dependencies are incompatible until we've introduced them,
1244000	1248080	and it's also hard to fix because the potential refactoring sites don't exist in our code, but
1248080	1252800	code that others have written and others control. We're currently working on tooling that will make
1252800	1257540	informed decisions about which dependencies might be compatible with which features in the ecosystem,
1257540	1262600	but this is a part where we could use some help as well. As CSP and Trusted Types gain more widespread
1262600	1267060	browser adoption, you can help us by making pull requests if you do end up patching third-party
1267060	1270920	dependencies that you find along your journey of adopting these features.
1272900	1278100	Thanks, Jen. So, I know we talked a lot about the mitigations and the tools that are used today to
1278100	1282720	protect our apps, but let's talk a bit about where we want to go from here and the more secure world
1282720	1288660	that we'd like to see in the future. And here are the key ideas we talked about today that form the
1288660	1296200	basis of this protection, this pipeline of protection, if you will. So, the running theme is
1296200	1300840	abstracting security decisions away from developers in a clear and secure by default way.
1301180	1304900	When all of these elements work together, starting from the first stage when the code is written,
1304900	1308780	all the way to the final point when the code is executed in the client's browser,
1308780	1313600	we have protection at every stage of the development lifecycle. And this should make
1313600	1319240	most cross-site scripting solvable, and we see this working at Google scale. On our most recent
1319240	1324040	in-house frameworks that follow a lot of these guidelines from these pillars, we see so few
1324040	1330020	cross-site scripting, and none of them are trivial attack chains, and all of them really have some
1330020	1335220	weird logic bug or some legacy system involved. So, we are seeing the effects of this working
1335220	1340180	at scale across billions of users and hundreds of different web apps. But how can we make this
1340180	1345320	safer? Where do we go from here? What's the next logical step? And we believe that, remember how
1345320	1351820	we talked about trusted types, and it blocks a lot of these inherently unsafe DOM APIs in the web
1351820	1357280	platform, or at least makes it harder to use? What if we take that to the conclusion of the idea and
1357280	1363180	actually like ban all of those usages at runtime of these legacy DOM APIs that are a little bit
1363180	1370580	inherently unsafe as they've been designed? Can we build apps where we don't use any of those APIs?
1371060	1375740	And that's our future vision of where we go from here. If we have new platform-level APIs
1375740	1381720	for operations that we currently use DOM APIs for, such as maybe a browser-included sanitizer
1381720	1386480	implementation for HTML, maybe a templating solution, then we've really come full circle
1386480	1392020	and have our runtime enforcements influence a new suite of secure-by-default web platform APIs
1392020	1397180	to bring decisions that nudge developers towards safer choices, maybe at the framework level,
1397180	1400760	from the very beginning when the first line of code is written in a new code base
1401280	1407960	all the way to the moment when it is executed on the client's browser.
1408520	1412860	And this is the future we hope to get to, one where we can safely lock down all the accesses
1412860	1417680	to many DOM APIs along with a new suite of platform APIs to replace those operations in
1417780	1421860	a safe-by-default manner. And we call this approach, we've nicknamed it Perfect Types,
1421860	1426940	it is the logical evolution of Trusted Types, and we see this as the next evolution of Trusted
1426940	1431520	Types and a post-cross-site scripting world. This combined with the strict CSP configurations
1431520	1436120	we talked about and our framework-based approaches, secure-by-design, safe-by-design,
1436120	1440540	should guide us to a new post-cross-site scripting world. And taking a step back for a second,
1440540	1446660	for an even bigger picture, and as a final plug, this safe coding philosophy and safe-by-design,
1446660	1451260	trying to steer developers in the right direction is something that extends beyond just JavaScript,
1451260	1456620	cross-site scripting, and is a core priority for all Google-scale security efforts. You can read
1456620	1461760	more about our organization's work at these links, and Google is a signer of CISA's secure-by-design
1461760	1465920	pledge along with other industry and government leaders in the space, and we've committed to
1465920	1471940	sharing our knowledge on how to build a safer future together. All right, well, that is the
1471940	1475460	end of our talk, so thank you everyone for listening. Hopefully this was a good guide
1475460	1478160	on how to think about solving XSS. I'll leave you here.
