Hi everyone, welcome to our talk. Today we'll be talking about securing front-ends at scale and how we imagine a post-XSS world. 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. Before we dive into it, we'll do some introductions. Hi, I'm Jen. I've been a software engineer at Google for over three years now. I work on a team that mainly focuses on deploying web security mitigations across all of Google's web products. 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. Hi , my name is Aaron, and like Jen, I work on deploying the latest and the greatest security mechanisms to our end users. 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. 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. Cool. Now we'll get into our agenda for the day. 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. So let's get into it. What is cross-site scripting? We've said that XSS is common, but we'd like to show you some data to back this up. 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. As you can see, over 50% of the issues were web issues. This makes sense. Google is a web company. But out of those 50%, 35% are XSS. 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. So let's talk about what XSS really is. The root cause is that user input gets mixed up with code and gets interpreted as code. Because HTML is dynamic and mixes static data with logic that should execute, as you see here, is very prevalent. And this leads to malicious scripts getting injected into a trusted site. In this example, there's a description that's supposed to be dynamic. But if this is user controlled, we can inject the following script and get it to execute. Then we have code execution on the website. And what does an XSS let you do? An XSS, when exploited, can let the attacker take control of the account of a logged in user. This means the attacker can do anything the victim can, and depending on the account type, this could be very, very dangerous. And that's why we care a lot about mitigating. So we also call this the client-side version of a remote code execution, as they share a similar concept. And here's another format you might be used to seeing. XSS, we usually pop an alert with a document domain as a proof of concept when we find an XSS vector. So here's a quick example of a client-side variant of XSS, which is called DOM XSS. The root cause of this is that JavaScript APIs, specifically DOM APIs, are not secure by default. They can easily turn strings into code, like innerHTML is an example of this, or script.innerText. So here's a very simple proof of concept example of how this might happen. 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. We'll see the onError alert statement execute if we use the payload that you see here. But no one codes like this anymore, right? Thanks to modern frameworks like React that abstract away DOM manipulations so we don't actually have to use those DOM APIs. Except not! 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. And here's the core concept. 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. And these issues are really easy to introduce. 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. So do we stand a chance against this class of problems? And we think we do. 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. And CSP is famous for having a lot of directives, and a lot, a lot of directives. It can really turn off any sort of resource loading that happens in the browser. So how do we use it? How do we know that it's delivering the protections and it's not providing a workaround for our attackers? 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. 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. So let's open up Chrome DevTools in the network tab and see what headers we are sending. 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. And we see that there are multiple content security policy headers here. 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. And we really have a lot of different configurations that protect against different potential threats. 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. And so we'll look at these two in particular, which are sort of used for cross-site scripting defense. And we'll focus on these ones, nonce-only CSP and trusted types. 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. Cool. And we'll start with the nonce-only CSP here. And the example that we saw before, it's kind of pretty self-evident. It's the one that has the nonce directive, just like you can see here. And more specifically, a nonce-only policy takes a form that looks like this. 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. In this case, any script with a nonce. 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. 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. Another CSP directive that we want to point out here is strict-dynamic. This directive enables transitive trust for scripts. So if you have one script with the right nonce, it's allowed to load other scripts, and the trust is propagated. 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. If they do, it's not allowed to run on the page. Although this slightly relaxes the policy, in our experience, it's also made adoptions a lot easier for complex web apps. You can have one loader script that loads all the necessary scripts, and deploying content security policy becomes a lot easier. Great. Thanks, Jen. 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. But what if we don't have control over the backend? Then we can use a relative of the nonce-only policy, the hash-based CSP. And a hash-based CSP looks like this. 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. 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. 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. 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. So following this suggestion, we have this potential transformation of a SPA. On the left, we have maybe an index.html of an app that will bootstrap some SPA, a single-page application. 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. And we can combine this approach with a meta tag, so we don't even have to set a header in the response. The HTML itself contains a CSP that we want to ship. So no matter what stack you use to serve this HTML blob, you will always have this content security policy associated with it. And we'll talk a bit more in the next several slides about some of the tooling that can automate this. Cool. Now, on to the next flavor of CSP, we have trusted types. This is, again, another CSP-delivered mitigation that we think is really powerful, specifically against DOM-based XSS. The example we saw before, it's this one with the require-trusted-types-for directive. And we want to point out that the attack surface is very large here for DOM XSS. I mentioned that JavaScript APIs, specifically DOM APIs, are insecure by default. And there's over 60 of them that can take strings and turn them into code execution. DOM XSS is usually very, very easy to introduce and hard to detect, as Aaron alluded to before. 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. So we were thinking, how can we make it better? And this is where trusted types comes in. We need a runtime mitigation to provide some sort of type checking on top of JavaScript. And this is exactly what trusted types does. 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. 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. 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. 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. 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. This tells the browser to reject string assignments to those injection syncs, and effectively provides a runtime type checking mechanism on top of JavaScript. 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. As you can see here, we're using a plain string and assigning it to inner HTML. And if trusted types are enforced, the browser will throw an error that looks like this. And it tells us that we need a trusted HTML object instead of the string that we're trying to use. And here we are. This is what a trusted types policy looks like. This is what I mentioned previously about how we can generate these trusted objects. We want to highlight that most of the security guarantees of trusted types come from a policy. 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. We apply this policy to strings before inserting them into the DOM. And the processing you do within the policy should make sure that the string won't lead to any unintended code execution. And that's how we can prevent DOM XSS. In this example, we're specifically concerned about HTML. So we use a sanitizer from DOM Purify. 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. 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. 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. So it's quite handy. Great. Thanks, Jen. 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. 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. 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. So please check us out. It's on the Google Security Engineering blog at bughunters.google.com, and along with some other great content. So yeah, we hope to see you there. So a quick overview of what we are going to talk about in this section. Number one, if you use the right frameworks, it'll give you superpowers. 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. Number three, some tools that we found helpful in this process of looking for possible incompatibilities with these features. And number four, a quick note about third-party dependencies. So onto our first big idea, frameworks that give you superpowers. So the idea here is the basis of our at-scale defense against cross-site scripting at Google. 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. 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, and really they can focus on shipping the features to the users. And we really don't want the security to get in the way of development, right? Because that is how you get developers to turn against security. 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. 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. But what if you aren't building web apps inside of the Google ecosystem? How can we get some of these benefits? And thankfully, our colleagues have worked hard to ensure compatibility with these powerful security features and some other frameworks as well. 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. But our contributions aren't only to Google-sponsored frameworks. 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. 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. 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. So if you have control of their backend server infrastructure, we can just set our headers there, right? And for instance, we hear that the web bundler and dev server of choice now is Vite. 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. So you can set these headers and have it enforce the content security policies. 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. 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. And this HTML snippet will stay with the rest of your HTML-generated bundles. So yeah, it really can't become unglued. So one word of warning about using the meta tag approach, however, is that report-only mode does not work very well. 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. And for the observant among you, you'll see that this example here uses the hash-based CSV policy that we talked about before. 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. So is there a way to do this a bit more quickly? And thankfully, we have a package that will do a lot of this for you available on GitHub and NPM. 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. 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. 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. So it's really a plug-and-play approach and a framework agnostic way that we really want to build these tools around. Cool. Thanks, Erin. So in addition to using the headers to spot violations, early we advocate for some other tools to help you with adopting these defenses. 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. We mentioned that CSP is notoriously hard to configure and understand. They have so many directives that could be combined in various different ways. And the CSP evaluator solves that problem. 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. We're also actively working on a tool called the Trusted Types Helper for streamlining the Trusted Types adoption process. 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. This Chrome extension will create a DevTools tab that helps you demystify Trusted Types violations and suggest refactorings. Stay tuned for this release. Another category of tools that we advocate for are those that help with refactoring using Static Analysis. 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. 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. This is also under active development, but it is available on GitHub. 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. This is a library designed to steer developers to make safer choices. Aaron alluded to this principle we have, which is secure by design, and this embodies all of those principles. 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. It really tries to make doing the right thing really easy and doing the insecure thing really, really hard for developers. Another big aspect that we want to make a warning about is third-party dependencies. This is a big aspect of making your application compatible with the features we described before. So third-party dependencies, quite honestly, is hard. For both trusted types and CSP, all the code running on your site needs to be free of violations of these policies. 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. 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, which is multiple levels deep. 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. 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. We would really appreciate it. All right, thanks, Jen. 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. So to summarize, here are the pillars that we touched on to move towards a post-cross-site scripting world. The running theme is abstracting security decisions away from the developer in a clear and secure by default way. 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. And this should make cross-site scripting mostly solvable. And we see this working at Google at scale on our most recent in-house frameworks. 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. So where can we go from here? So we saw that trusted types is a complex mechanism to adopt. We can restrict the usages of dangerous DOM APIs, but we haven't gotten rid of them completely. We still need to create policies to maybe allow list some limited usages of them. We need to refactor away other code patterns that still use them. 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? And that's a future vision of where we want to go from here. 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, secure-by-design, web platform APIs and primitives to bring decisions that nudge developers towards safer choices at the very beginning of the pipeline. 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. And this approach, we've sort of nicknamed the perfect types, and we see this as the next evolution of trusted types. 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. 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. 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. 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. Cool, and that is sort of the end of our talk. We want to thank you all for listening. Hopefully this was a good guide on how to think about solving XSS and how to do it at scale. Feel free to reach out to us with any questions related to our tools or some of these mitigations. We'll be happy to have a conversation. And I want to add a last plug to please help us fix the open source ecosystem. 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. Thanks!