start	end	text
8550	10250	Hi everyone, welcome to our talk.
10250	14650	Today we'll be talking about securing front-ends at scale and how we imagine a post-XSS world.
14650	24530	As you may have heard, XSS or cross-site scripting is a really, really hard problem to solve, but this will be a tale of a journey through tools and runtime mitigations to show how we can get to a post-XSS world.
25210	28110	Before we dive into it, we'll do some introductions.
28110	29290	Hi, I'm Jen.
29710	32650	I've been a software engineer at Google for over three years now.
32650	40410	I work on a team that mainly focuses on deploying web security mitigations across all of Google's web products.
40410	48540	We have a ratio of about 1 to 100 for security to actual product engineer, so we really do care about making things work at scale.
50750	57990	Hi , my name is Aaron, and like Jen, I work on deploying the latest and the greatest security mechanisms to our end users.
57990	68670	Before my stint in security, I worked as a dev in some well-known Google products like Docs and GCP, so I come from the other side of the fence in the security world.
68670	81350	So I approach these problems with a special amount of empathy for a developer on how we can reduce as much friction as possible so that they can focus on shipping the greatest features to the user and not worry too much about security.
84170	85110	Cool.
85110	88170	Now we'll get into our agenda for the day.
88650	108530	We'll start with a quick introduction to cross-site scripting, and we'll talk about what XSS really is, and then we'll introduce some runtime mitigations, mainly within the umbrella of content security policy and its different flavors, and then we'll talk about how we can secure your apps against XSS at scale with some musings and imaginations about the post-XSS world to follow.
108950	110330	So let's get into it.
110330	112430	What is cross-site scripting?
112490	116690	We've said that XSS is common, but we'd like to show you some data to back this up.
117090	126930	Google runs a vulnerability rewards program to pay out security researchers when they report their bugs to us, and this is some data from our VRP program.
126970	131250	As you can see, over 50% of the issues were web issues.
131250	131870	This makes sense.
131870	133270	Google is a web company.
133330	137410	But out of those 50%, 35% are XSS.
137410	143870	So this proves that XSS has been really prevalent, especially within our services, and that's why we've worked so hard to mitigate against it.
144550	147370	So let's talk about what XSS really is.
147370	153430	The root cause is that user input gets mixed up with code and gets interpreted as code.
153490	160450	Because HTML is dynamic and mixes static data with logic that should execute, as you see here, is very prevalent.
160570	164330	And this leads to malicious scripts getting injected into a trusted site.
164330	169390	In this example, there's a description that's supposed to be dynamic.
171430	175750	But if this is user controlled, we can inject the following script and get it to execute.
175750	178230	Then we have code execution on the website.
179090	180990	And what does an XSS let you do?
180990	185490	An XSS, when exploited, can let the attacker take control of the account of a logged in user.
185490	190730	This means the attacker can do anything the victim can, and depending on the account type, this could be very, very dangerous.
190730	193730	And that's why we care a lot about mitigating.
194450	200310	So we also call this the client-side version of a remote code execution, as they share a similar concept.
200310	202670	And here's another format you might be used to seeing.
202670	208690	XSS, we usually pop an alert with a document domain as a proof of concept when we find an XSS vector.
210290	216730	So here's a quick example of a client-side variant of XSS, which is called DOM XSS.
216730	222490	The root cause of this is that JavaScript APIs, specifically DOM APIs, are not secure by default.
222490	228510	They can easily turn strings into code, like innerHTML is an example of this, or script.innerText.
228510	232530	So here's a very simple proof of concept example of how this might happen.
232530	240010	In the code snippet below, you see that we take the location hash, which is a user control string, and we assign it to innerHTML.
240010	244890	We'll see the onError alert statement execute if we use the payload that you see here.
244890	246990	But no one codes like this anymore, right?
246990	252590	Thanks to modern frameworks like React that abstract away DOM manipulations so we don't actually have to use those DOM APIs.
252590	254490	Except not!
254490	271230	In this fresh bit of internet outrage, we see that this enterprising developer has managed to find a way to sneak a .innerHTML assignment call into a React component, a framework that is supposed to take care of the really complex DOM interactions and the repainting for you.
271270	273130	And here's the core concept.
273130	281670	Just because a framework can abstract away the complex DOM operations does not mean that it will prevent you from using these DOM APIs directly.
281690	283670	And these issues are really easy to introduce.
283670	290690	It just takes a developer or a code reviewer having a bad day, and once introduced, it's really hard to remove from your code base.
290690	294090	So do we stand a chance against this class of problems?
294470	295990	And we think we do.
296130	304050	We've all heard of Content Security Policy, or CSP, as a defense against cross-site scripting, but we'll go into more detail about how we can use it.
304050	309990	And CSP is famous for having a lot of directives, and a lot, a lot of directives.
309990	314650	It can really turn off any sort of resource loading that happens in the browser.
314650	315910	So how do we use it?
315910	321890	How do we know that it's delivering the protections and it's not providing a workaround for our attackers?
322290	340150	So before we go there, since you're talking about CSP headers in the context of cross-site scripting protections at scale that we use at Google, we'll start off with a well-known Google app in production, and see what they do in real life, and share a little of the story of how we got there.
340590	351710	So here it is, a well-known Google web app that has a very complex user front end, and has been a champion of shipping the latest and greatest security features.
351710	357910	So let's open up Chrome DevTools in the network tab and see what headers we are sending.
357910	365090	We sort of encourage you to do this because this is a source of truth on what the actual mitigations we are sending to the end user is.
365110	370950	And we see that there are multiple content security policy headers here.
371110	380070	And we'll start out with a caveat that CSP, like we mentioned before, is really a meta feature rather than just a cross-site scripting protection mitigation.
380290	386170	And we really have a lot of different configurations that protect against different potential threats.
386170	393950	Essentially, it was designed to lock down any sort of resource loading on the page, so not all of these headers are specifically for our cross-site scripting defense.
394170	400550	And so we'll look at these two in particular, which are sort of used for cross-site scripting defense.
400710	406110	And we'll focus on these ones, nonce-only CSP and trusted types.
406110	415630	So in the next section, we will go through how these mechanisms work and work in conjunction, and why we believe they are a suitable defense against cross-site scripting.
417390	418070	Cool.
418070	421170	And we'll start with the nonce-only CSP here.
421590	425730	And the example that we saw before, it's kind of pretty self-evident.
425730	429490	It's the one that has the nonce directive, just like you can see here.
429970	434350	And more specifically, a nonce-only policy takes a form that looks like this.
434450	443730	Internally, we recommend a nonce-only or strict CSP for most of our modern apps, and only what is allowed under script source here can run on the page.
443910	446110	In this case, any script with a nonce.
446110	457490	You can think of a nonce as a one-time random value that's sent both by the HTTP headers and in the HTML, and the browser verifies that both of those values match before running any script.
457490	465370	So in this case, we use templating systems to propagate the nonce, and the header and the HTML work together to indicate which scripts are trusted.
466850	470550	Another CSP directive that we want to point out here is strict-dynamic.
470550	473950	This directive enables transitive trust for scripts.
474230	479570	So if you have one script with the right nonce, it's allowed to load other scripts, and the trust is propagated.
479630	485670	When we don't have this directive, only scripts with the right nonce can execute on the page, and they're not allowed to load other scripts.
485670	487970	If they do, it's not allowed to run on the page.
488210	494890	Although this slightly relaxes the policy, in our experience, it's also made adoptions a lot easier for complex web apps.
494890	501890	You can have one loader script that loads all the necessary scripts, and deploying content security policy becomes a lot easier.
503690	504390	Great.
504390	504950	Thanks, Jen.
504950	515650	So as Jen mentioned, a nonce-only policy, a nonce-only content security policy is great if you have the backend or the server-side rendered content working together with the frontend or the client-side code.
515650	518510	But what if we don't have control over the backend?
518510	522990	Then we can use a relative of the nonce-only policy, the hash-based CSP.
524410	526850	And a hash-based CSP looks like this.
526850	535270	So instead of having a nonce under the script source, it has a hash of the scripts that are allowed to load on the page.
535270	553110	This is more useful for scenarios where you really don't have control of the serving infrastructure, so we really need to embed this policy into the HTML response itself, and have a guarantee that it'll run on the HTML, even if we can't modify bits of it to inject the nonce at every page load.
553590	562370	And this is an especially powerful use case for SPAs because they have an initial bundle of JavaScript that they need to load, and it's a fixed value.
562370	571630	We know this at compile time, we can generate a hash knowing that it is a safe bit of JavaScript to run, and we can embed it on the page itself, as we see here.
571630	575510	So following this suggestion, we have this potential transformation of a SPA.
575510	583610	On the left, we have maybe an index.html of an app that will bootstrap some SPA, a single-page application.
583610	592970	So when we turn the script tags with source attributes into an inline loader script tag, we can hash the contents to include this CSP.
593190	598390	And we can combine this approach with a meta tag, so we don't even have to set a header in the response.
598390	602770	The HTML itself contains a CSP that we want to ship.
602770	610930	So no matter what stack you use to serve this HTML blob, you will always have this content security policy associated with it.
610930	616790	And we'll talk a bit more in the next several slides about some of the tooling that can automate this.
618780	619460	Cool.
619460	623340	Now, on to the next flavor of CSP, we have trusted types.
623340	629900	This is, again, another CSP-delivered mitigation that we think is really powerful, specifically against DOM-based XSS.
629900	635260	The example we saw before, it's this one with the require-trusted-types-for directive.
635720	640120	And we want to point out that the attack surface is very large here for DOM XSS.
640120	645000	I mentioned that JavaScript APIs, specifically DOM APIs, are insecure by default.
645000	650040	And there's over 60 of them that can take strings and turn them into code execution.
650180	655640	DOM XSS is usually very, very easy to introduce and hard to detect, as Aaron alluded to before.
655640	662120	And calls to these APIs could be going through multiple levels of indirections, and could exist in libraries that we don't have control over.
662120	664500	So we were thinking, how can we make it better?
664500	666560	And this is where trusted types comes in.
666560	672640	We need a runtime mitigation to provide some sort of type checking on top of JavaScript.
672640	674720	And this is exactly what trusted types does.
674720	685520	The main idea behind it is to make security decisions explicit and auditable as well, because usually when we use these DOM APIs, all of the security decisions seem implicit in the code.
685520	695560	So to do this, trusted types will restrict the access to dangerous DOM APIs that we mentioned before, and it'll require typed objects instead of plain strings to be passed into these DOM APIs.
695560	707300	So as you can see here, whereas before we would have had a string that represented HTML, a string that represented some sort of script, we have explicitly typed objects called trusted HTML, trusted script, or script URL.
707300	716880	We'll talk about how we generate these trusted and typed objects later in the slides, but this is the general idea behind how this protects against DOM XSS.
717580	724580	And just so we have it clear and plain on the slide, this is how the header will look like if you're enforcing trusted types.
724660	732640	This tells the browser to reject string assignments to those injection syncs, and effectively provides a runtime type checking mechanism on top of JavaScript.
733120	739240	So let's go back to the example we saw on our DOM XSS slide and see how trusted types would work in this case.
739280	743460	As you can see here, we're using a plain string and assigning it to inner HTML.
743620	747720	And if trusted types are enforced, the browser will throw an error that looks like this.
747720	752280	And it tells us that we need a trusted HTML object instead of the string that we're trying to use.
753160	754860	And here we are.
754940	758400	This is what a trusted types policy looks like.
758740	763440	This is what I mentioned previously about how we can generate these trusted objects.
763440	768140	We want to highlight that most of the security guarantees of trusted types come from a policy.
768140	778160	You can think of a policy as a wrapper that takes in a string, applies some sort of processing to it, and returns a trusted type, whether it's trusted HTML or script or script URL.
778180	782100	We apply this policy to strings before inserting them into the DOM.
782120	787860	And the processing you do within the policy should make sure that the string won't lead to any unintended code execution.
787860	790180	And that's how we can prevent DOM XSS.
790180	794160	In this example, we're specifically concerned about HTML.
794520	797720	So we use a sanitizer from DOM Purify.
797720	807340	We check for the presence of trusted types in the JavaScript namespace for browser compatibility on the if statement, and then use the correct factory if it exists.
807340	816060	But if you don't want to keep writing this verbose block, and it could be quite long, DOM Purify also works with trusted types off the shelf.
816060	822340	It'll do the browser checking under the hood if you pass in the right config here, and it'll also generate the trusted types policy for you.
822340	823360	So it's quite handy.
825960	826560	Great.
826560	827100	Thanks, Jen.
827100	840000	So now that we've talked about the security features of what we believe to be powerful tools against cross-site scripting today, we'll touch a bit on how we use them in your application and offer a bit of lessons learned from securing our apps at Google scale.
840400	847940	So before we begin, we'd like to make a quick plug for an article that presents many of the ideas that we'll discuss in this next section, but in written form.
847940	857060	This approach is used widely within our team and not just within our web scope, and the article goes into a bit more detail in some of these ideas than we have time for in this talk.
857060	858400	So please check us out.
858400	865000	It's on the Google Security Engineering blog at bughunters.google.com, and along with some other great content.
865040	866960	So yeah, we hope to see you there.
867640	871880	So a quick overview of what we are going to talk about in this section.
872020	875760	Number one, if you use the right frameworks, it'll give you superpowers.
875760	885540	Number two, we need to shift left as much as possible so that we can get feedback to developers on whether the code that they're writing is compatible or incompatible.
885540	892980	Number three, some tools that we found helpful in this process of looking for possible incompatibilities with these features.
892980	897320	And number four, a quick note about third-party dependencies.
898200	902320	So onto our first big idea, frameworks that give you superpowers.
903060	908400	So the idea here is the basis of our at-scale defense against cross-site scripting at Google.
908660	927220	Frameworks with context-aware templating, and that means we assemble the HTML in a way that we don't end up doing inadvertent injections into the HTML, and built-in compatibility with these security features, and also frameworks that are secure by default and secure by design.
930140	944620	So a part of this safe coding philosophy at Google, and this is a term that we'll use for a lot throughout this section, is giving the right tools to developers so that they don't have to worry so much about whether they have the security configurations correct,
944620	948920	and really they can focus on shipping the features to the users.
948920	952640	And we really don't want the security to get in the way of development, right?
952640	957480	Because that is how you get developers to turn against security.
957480	969160	So having frameworks that do not work against you as you try to ship wonderful and secure code is really the basis, and a big part of it is having compatibility with the features that you want to enforce.
969160	980600	And this is possible at Google because we have tight control over our internal frameworks, and also over our advice that we give to our internal developers on how to use these frameworks.
980600	984100	But what if you aren't building web apps inside of the Google ecosystem?
984100	986060	How can we get some of these benefits?
986480	994480	And thankfully, our colleagues have worked hard to ensure compatibility with these powerful security features and some other frameworks as well.
994480	1005620	For instance, all the work that we did to harden Angular and Lit, frameworks that we do also use internally as well, contribute back to the open source ecosystem of developers who also use these frameworks.
1005620	1008980	But our contributions aren't only to Google-sponsored frameworks.
1008980	1022640	Our friends at also worked hard to make React compliant with their web security strategy, and we've also submitted PRs to other React-based frameworks like Next.js to work well with their suggested security features as well.
1023140	1033140	The next point is that we really want to get feedback about whether we've potentially written code that is incompatible with these security features as early in the process as possible.
1033140	1043940	And really, in this approach of shifting left, it's about either adding a header that enforces content security policy and trust types early on, or meta tags that do the same.
1044040	1049320	So if you have control of their backend server infrastructure, we can just set our headers there, right?
1049320	1055380	And for instance, we hear that the web bundler and dev server of choice now is Vite.
1055380	1065040	So here is an example that will work in Vite, a code snippet of middleware written as a Node Express middleware because Vite accepts those.
1065040	1072160	So you can set these headers and have it enforce the content security policies.
1072360	1084760	But really, this approach is applicable to any framework-agnostic place where you do have the opportunity to change the response headers in the middleware.
1084760	1100560	But if you don't, if you're maybe building just a client-rendered single-page application, an SBA, and you don't have necessarily a lot of control over where it will eventually be served, we can also use meta tags like we discussed before.
1100560	1106120	And this HTML snippet will stay with the rest of your HTML-generated bundles.
1106120	1108640	So yeah, it really can't become unglued.
1108640	1114940	So one word of warning about using the meta tag approach, however, is that report-only mode does not work very well.
1114940	1123340	So it's really useful when used really early in the development process, when you start a new codebase to catch these violations early on.
1124240	1130880	And for the observant among you, you'll see that this example here uses the hash-based CSV policy that we talked about before.
1131140	1145540	And as we mentioned earlier, it's possible to take all of your script loading, turn it into a script block that is hashable, but it's sort of tedious to go through all of the script tags in your code and generate the hashes and add them to your CSV.
1145540	1148220	So is there a way to do this a bit more quickly?
1148300	1153740	And thankfully, we have a package that will do a lot of this for you available on GitHub and NPM.
1154920	1174060	And this is shipped as both a webpack plugin and as well as just a string-to-string transformation library that you can put in your bundling and building step to have your HTML automatically extract the script tags, hash them, and then add the meta tag back in.
1174760	1191060	And this framework agnostic approach to having these plug-and-play parts is really important because if you remember this middleware example from a couple slides ago, you can basically copy-paste the string transformation right into the middleware itself.
1191060	1199700	And this will add the meta tags or the CSP to any response body of the HTML using that library that we just talked about.
1199700	1207340	So it's really a plug-and-play approach and a framework agnostic way that we really want to build these tools around.
1209680	1210360	Cool.
1210360	1211160	Thanks, Erin.
1211320	1218920	So in addition to using the headers to spot violations, early we advocate for some other tools to help you with adopting these defenses.
1218920	1227940	And we'll introduce a bunch of helpful tools, but they can categorically be broken down into, first of all, tools that can help us understand the configurations and violations.
1228140	1231580	We mentioned that CSP is notoriously hard to configure and understand.
1231580	1234680	They have so many directives that could be combined in various different ways.
1234680	1236920	And the CSP evaluator solves that problem.
1236920	1248600	It's available as either a website or a Chrome extension, which will give you some high-level guidance on the configuration of your CSP or of the CSP of any websites that you visit if you use it as a Chrome extension.
1249320	1254920	We're also actively working on a tool called the Trusted Types Helper for streamlining the Trusted Types adoption process.
1254920	1262200	We found that one of the most time-consuming stages of a Trusted Types rollout is finding the source of the violations and fixing them.
1262200	1267940	This Chrome extension will create a DevTools tab that helps you demystify Trusted Types violations and suggest refactorings.
1267940	1270080	Stay tuned for this release.
1270360	1277020	Another category of tools that we advocate for are those that help with refactoring using Static Analysis.
1277560	1290760	At Google, we have a huge Static Analysis pipeline, and we always believe that Static Analysis goes hand-in-hand with runtime mitigations to complete the cycle and provide comprehensive protections against these attacks.
1290760	1298440	We took the most effective checks that we run on our internal linter and Static Analysis tooling and built it on top of ESLint, and you have Safety Web.
1298440	1302880	This is also under active development, but it is available on GitHub.
1303220	1314120	As you can see in the example here, it'll detect unsafe usages of risky APIs, and once it detects those, you can refactor these unsafe calls with another tool that we provide, which is Safe Values.
1314120	1317920	This is a library designed to steer developers to make safer choices.
1317920	1324320	Aaron alluded to this principle we have, which is secure by design, and this embodies all of those principles.
1324320	1334240	Safe Values will prevent unsafe assignments using type checking unless the value is trusted, either from a trusted source or by a transformation that makes it trusted, such as sanitizing.
1334240	1340960	It really tries to make doing the right thing really easy and doing the insecure thing really, really hard for developers.
1341940	1347980	Another big aspect that we want to make a warning about is third-party dependencies.
1347980	1353840	This is a big aspect of making your application compatible with the features we described before.
1355120	1358880	So third-party dependencies, quite honestly, is hard.
1358880	1364400	For both trusted types and CSP, all the code running on your site needs to be free of violations of these policies.
1364400	1371620	Otherwise, calls to APIs or running the script will be blocked by the browser, and then you might have a feature breakage, which is not what we want.
1372000	1386380	This is a hard part because we won't always know which dependencies are incompatible until we've introduced them, and it's also hard to fix because the potential refactoring sites don't exist within our code, but in code that others have written, and it could even be in transitive dependencies,
1386380	1387980	which is multiple levels deep.
1388000	1398620	So we're currently working on tooling that will make informed decisions about which dependencies might be compatible with which features in the ecosystem, but this is a part where we can use your help as well.
1398640	1407800	So as CSP and trusted types gain more widespread browser adoption, you can help us by making pull requests if you do end up patching third-party dependencies in your own refactoring journey.
1407800	1409020	We would really appreciate it.
1410260	1411620	All right, thanks, Jen.
1411620	1422900	So we talked a lot about mitigations that we run and the tools that we use today to protect our apps, but let's talk a bit more about where we go from here and the more secure world that we'd like to see in the future.
1423000	1428660	So to summarize, here are the pillars that we touched on to move towards a post-cross-site scripting world.
1428660	1435800	The running theme is abstracting security decisions away from the developer in a clear and secure by default way.
1436740	1450120	So when all of these elements work together, starting from the first stage when code is written all the way to the final point when the code is executed in the client's browser, we have protection at every stage of the development and deployment lifecycle.
1450200	1453360	And this should make cross-site scripting mostly solvable.
1453360	1458640	And we see this working at Google at scale on our most recent in-house frameworks.
1458700	1468800	We see so few cross-site scripting in these frameworks, and none of them are trivial and allow them to involve a logic bug or a legacy system and some really complex attack chain.
1468800	1471320	So where can we go from here?
1471980	1475660	So we saw that trusted types is a complex mechanism to adopt.
1475660	1480900	We can restrict the usages of dangerous DOM APIs, but we haven't gotten rid of them completely.
1480900	1485780	We still need to create policies to maybe allow list some limited usages of them.
1485780	1489680	We need to refactor away other code patterns that still use them.
1489680	1501800	But can you imagine a world where we can build apps to return off all usages of these DOM APIs to give as strong of a guarantee as possible that there are no potential DOM cross-site scripting injection points?
1502900	1505800	And that's a future vision of where we want to go from here.
1505800	1524120	If you have new platform-level APIs for operations that people use the DOM APIs for currently, say maybe a browser-included sanitizer implementation for HTML, or maybe a templating solution, then we've come full circle and have our runtime enforcements influence a new suite of secure-by-default,
1524120	1533740	secure-by-design, web platform APIs and primitives to bring decisions that nudge developers towards safer choices at the very beginning of the pipeline.
1534800	1551260	And this is a future that we can all hope to get to one day, where we can lock down all access to the DOM APIs, along with a new suite of platform APIs, so that we don't really have to rely on them to build dynamic and amazing web applications for our users.
1551260	1558520	And this approach, we've sort of nicknamed the perfect types, and we see this as the next evolution of trusted types.
1558520	1573380	So this approach of turning off access to all DOM APIs, combined with a strict CSP and other framework-based solutions so that we can still build rich user interfaces for our users, we should get us to this new post-cross-site scripting world.
1574440	1587420	And stepping out for an even bigger picture here, we'd like to make a final plug for this safe coding philosophy and safe-by-design, trying to steer developers in the right direction towards you by using the tools that we offer them.
1587420	1597020	It's something that extends beyond our work in solving just cross-site scripting or even the web sphere at Google, and is a core priority for all of Google-scale security.
1597020	1612540	You can read more about our organization's work at these links, and Google is a signer of the CSS Secure by Design pledge, along with a consortium of other industry and government leaders in the space, to commit to sharing our knowledge in how to do this better with everyone.
1614160	1616720	Cool, and that is sort of the end of our talk.
1616720	1618660	We want to thank you all for listening.
1618660	1623200	Hopefully this was a good guide on how to think about solving XSS and how to do it at scale.
1623280	1627620	Feel free to reach out to us with any questions related to our tools or some of these mitigations.
1627700	1630300	We'll be happy to have a conversation.
1630760	1636060	And I want to add a last plug to please help us fix the open source ecosystem.
1636060	1644060	We try our best to upstream any of the patches that we have internally, but the whole JS ecosystem will be safer and more usable if we all work together.
1644060	1644560	Thanks!
