Hello, my name is Aaron. And I guess I have to apologize. The resemblance is a little hard to see in that picture, because my hair is not nearly as luscious as it is in that photo. But I'm sorry, your mineral content in the water, as you've all found out, really awful here. But yeah, like Jen, I work at Google. We do web security on hundreds and thousands of different web apps. I've been at Google for a little under seven years. And before my stint in security, I came from the developer side. I've worked on products like Google Docs and GCP on customer facing stuff. So when we approach this work of trying to make our web apps as secure as possible, we approach it with a special empathy for the developer, where we want to ship the most secure products, but also we don't want to get in the way of delivering really dynamic and new user features. Cool. Thanks, Aaron. Here's a quick look at our agenda for today. So we'll start with a quick intro to XSS. I assume most of you are familiar with it, but we'll still go over some examples. And then we'll talk about runtime mitigation, specifically those that come with content security policy and its different flavors. Then we'll talk about how we use those to secure applications at scale. And we'll move on to how we imagine the post-XSS world and end with a call to action. Cool. So cross-site scripting. It's just some data to motivate that this is a big problem. Google runs a vulnerability reward program where we pay out security researchers when they report their bugs to us. As you can see here, over 50% of the bugs reported are web-related. That makes sense. Google is a web company. And then the rest, out of those 50%, almost 35% are XSS. So this was a really big problem, and that's why we've developed techniques to remediate the stats at scale. So let's talk about what XSS really is. The root cause is user input gets mixed up with code and then gets interpreted as code. Because HTML is dynamic and mixes static data with logic that should execute, as you see here, this is very prevalent. And this leads to malicious scripts getting injected into a trusted site. In this example, you see a very simple 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. And there you have an XSS. An XSS, when exploited, lets the attacker take control of the account of a logged-in user. This means the attacker can do anything the victim can, so it's pretty powerful, and that's why we try really hard to avoid it. We also call this the client-side version of remote code execution, as they share a similar concept of arbitrary code execution. And here's another format you might be used to seeing XSS. We usually pop an alert with the document domain as a proof of concept when we find an XSS record. So a quick example of DOM XSS, the client-side variant of cross-site scripting. The root cause here is DOM APIs are not secure by default. We all love JavaScript, and JavaScript loves turning strings into code through APIs like innerHTML or script.innerText. Here's a very simple example of how this might happen. In this code snippet, locationHash is a user-controlled string. We take that and happily insert it into innerHTML. In this case, with the example here, the onError alert statement will execute, and then we have XSS. But no one codes like this anymore, right? Thanks to modern frameworks like React that abstract away file manipulations, nobody has to do this. Except... not. As you can see in this fresh bit of internet outrage, an enterprising developer has managed to work in a innerHTML assignment inside a very functional React component. It's supposed to be a really advanced system that takes care of the virtual DOM dipping, all of the different re-rendering things, so that you don't have to do this yourself, but this developer didn't really want to take it into their own hands. And this is the crux of the problem. Just because a framework can abstract away the complex DOM operations doesn't mean that it can prevent a developer from using them. And these issues are really easy to introduce in your codebase. It really takes a developer, a code reviewer, having a bad day and once it sneaks into your product then it's really hard to eradicate. So do we stand a chance against this class of problems? And we think we do. We've all heard of content security policy and how it can solve all your process scripting, right? Well, we do admit that content security policy is a little bit difficult to understand and configure. It really is more like a meta feature than a feature. It has a lot of bells and whistles and knobs that you can change, a lot of directives. What do you have to set on these directives? And it is a little confusing because it tries to cover everything, any sort of resource that loads on your page. But hopefully when we go on this journey together, we can talk about some of the configurations that we found to work at scale and cover many classes of cross-site scripting and we'll see why they work together. And before we begin, since we are talking about cross-site scripting and CSP and protections that Google uses, maybe we'll take a look at what is actually working in the wild. So here we have an example of a particular Google web app that some of you may have used. You know it has a really complicated UI with a lot of interactive user features and a small call out to this team. They've been great partners in really shipping the best and greatest and the latest web protection mitigations and working really hard to keep their end users safe. So let's open up Chrome DevTools and see what we are actually shipping on a real life product. And as you can see here, we see a couple different content security policies, a lot of them as a matter of fact. But like we said before, not all of them are specifically for cross-site scripting defenses because it's a meta feature that can also turn off other potential dangerous vectors as well. We don't have time to get into the other protections, but if you have questions, we will be sticking around for a little bit after we can go into more detail after the talk. But for now, we'll focus on these two, the ones that we find to be really powerful protections against cross-site scripting, namely the nonce-only CSP and the trusted types header. Cool, and we'll go into some details. So nonce-only CSP, in the example we saw before, it's pretty self-explanatory. It's the one with the nonce directive. More specifically, a nonce-only policy takes a form that looks a bit like this. Internally, we recommend a nonce-only or a strict CSP for most of our modern web apps. What this does is only what's allow-listed under the script source directive can run on the page. Other scripts will be blocked. So in this case, any script with the right nonce, which is a random value that's generated per request, can run on the page. So the templating systems usually propagate the nonce for each script tag on the HTML, and the header has to send the right value for the nonce. So the header and the HTML work together to indicate which scripts are trusted, and the browser can act accordingly. Another CSP directive that's useful for a nonce-only policy is the strict dynamic directive. This directive enables a 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. If they load other scripts, they'll be blocked. So although this slightly relaxes the policy, we found that it's also made adoptions a lot easier for complex web apps. So that's the trade-off that we're working on. So thanks, Jen. So as Jen mentioned, a nonce-only CSP is great if you have the back-end or the server-side render content working in close conjunction with the front-end or client-side code, because you have to generate these nonces that are new for every single response. But what if you don't have control over the back-end? Then we have to use a close relative of the nonce-only CSP called the hash-based CSP. And the way it works is, it's very similar to a nonce-based policy, but as you've noticed, instead of the nonce value, we provide a hash value. And this is really powerful because we don't have to regenerate this CSP for every response load. The hash is fixed, presumably because on every response, your script is mostly the same. And this is really powerful when you don't have control over that back-end, so you can put in the logic to do this nonce generation. And this is also super useful for the SBA, single-page applications, because when you are doing the final bundling process for your final HTML, you know what script has are going to be there in your SBA bundle, and use that to generate your hash, and there you go, a fixed CSP policy that offers this protection that you can even drop into a meta tag at the top of the HTML file. And in more detail, so what you have on the left is sort of like what your bundle might look like for a particular single-page application. You've got a couple scripts loading to actually get the user interaction going. And we would actually rewrite this in this case into a dynamic loader inside an inline script block, because that is one of the characteristics of a hash-based CSP for now. You need to know exactly what's in the script content in order to make the hash. So usually you do this by actually inlining the block, and if you have resources that would load over the network, then you turn it into JavaScript that would dynamically add it to the page. And once we have that, we also added some preload tags so that we actually get the parallelization going for fetching these resources. You can take the script block, create the hash, and stick it in as a meta tag that carries a CSP, and there you go. The HTML is self-contained and offers this protection with a CSP baked in. The next CSP-delivered mitigation that we want to talk about is trusted types. Specifically, trusted types is geared towards protecting DOM-based XSS. So in the example that we saw before, it's the one that says require trusted types for, and that's the directive that you need to send. So we want to point out that the attack surface is large here for DOM XSS. There's over 60 JavaScript APIs that can turn strings into code and lead to arbitrary code execution, and DOM XSS is usually very easy to introduce and hard to detect. Calls to these APIs could be going through multiple levels of indirections, it could be in a transitive dependency, or it could exist in libraries that we don't have control over. So how can we make this better? And we need a run-time mitigation for this, and this is where trusted types comes in. The main idea behind trusted types is to make security decisions explicit and auditable. To do this, trusted types will restrict the access to the dangerous DOM APIs we mentioned previously, and it will require typed objects instead of plain strings to be passed into the over 60 APIs. So in this case, instead of strings that represent script URLs, HTML, or scripts, will require typed objects such as trusted script URL. And we'll discuss how to generate these typed objects, but this is the main idea. This is what the header looks like, again. This tells the browser to reject string assignments to injection syncs, and effectively it provides a run-time type checking mechanism on top of JavaScript. If you remember the example from our DOM XSS slide before, we're using a plain string here with innerHTML, and if trusted types were enforced, we would throw an error that looks like this. So the browser will tell you you need a trusted HTML, and it won't use a string. We want to highlight that most security guarantees of trusted types come from a policy, and the policy is what I mentioned of how we could generate a trusted object. So you can think of it as a wrapper that takes in a string and returns a trusted type. And we apply this policy to strings before inserting them into the DOM sync, and the processing you do within the policy should make sure the string won't lead to any unintended code execution, therefore preventing DOM XSS. So in this example, we use a sanitizer from DOM Purify. We check for the presence of trusted types in the JavaScript namespace for browser compatibility, and then use the correct factory if it exists. But if you don't want to keep writing this verbose if block, because it is a bit clunky, DOM Purify also works with trusted types off the shelf, so it will do the policy creation and the browser compatibility checking under the hood for you if you pass in the right config for trusted types. Thanks, Jen. So now that we've talked about our security features that we believe are powerful against cross-site scripting today, we'll touch a little bit on how to use them in your application and offer a bit of the lessons that we've learned from securing our apps at scale at Google. 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 the next section, but in written form. This approach is used widely in our team and our org, and this article goes into a little bit more detail than what we have time for in some of the ideas that we'll elaborate on next. It's on the Google Security Engineering blog at bughunters.google.com, along with other great content like this, and also similar to, I think, the theme of this village. So please check us out. So back to the main show. What are some of the tactics that we use to adopt these security features at scale on hundreds of different web apps? So some of the lessons that we've learned that we find most useful are, number one, use the frameworks that will help you in this journey. Number two, try to shift left to get developers an indication whether this feature is incompatible with code that they're writing as early as possible in the process. Number three, some tools that we've built and we also rely on throughout this process that we found useful. And number four, a quick note about third-party dependencies and how they might complicate your adoption process. So the first big idea is if you're starting a new code base from scratch, let's think about which frameworks we can use that will give us security superpowers. And the big idea behind that is that at our scale, at at-scale defense against cross-site scripting at Google, we rely on frameworks with, number one, context-aware templating, and number two, built-in compatibility with these security features that are secure by default. And as a quick aside, we haven't talked too much about context-aware templating right now, but we don't have too much time to get into that. But the gist of it is you want to have a templating system that does reasonable enough things to prevent a lot of server-side rendered cross-site scripting from happening. Catch us after the talk, and we are happy to chat about templating too, which is beyond the scope of this talk. So what we want to cover on this slide and for the rest of the talk is what we call the safe calling philosophy at Google. And this is the idea that we want to give tools to developers that take away a lot of the uncertainty from the developer on how to use and configure these security features correctly, because they are really easy to misconfigure in really subtle ways. And we want the security to not really get in the way of feature development and shipping great things to our users, so we want to abstract away some of these decisions in a clear and understandable manner. And the key to do this is through our frameworks and our APIs. And we'll say that this is possible at Google because you do have tight control over the internal frameworks that you use in-house, and also tight control over the guidance that we give to our developers on how to use these frameworks. But I imagine many of the web developers here aren't building internal web apps for Google, so how can you get some of these benefits? And thankfully, our, yeah, colleagues have worked hard to ensure compatibility with some of these security features and frameworks that are available outside of our internal ecosystem as well. For instance, some of the frameworks that are also available externally, like Angular and Lit, all the hurdling work that we do there translates to benefits for the ecosystem and for everyone in the world to use. But if you're also using frameworks that are not entirely within the Google ecosystem or very Google-inspired, we also have our colleagues at Meta who did a lot of work to make React compatible with CSV and Trusted Types, and some of the frameworks that are based on React, like Next.js, thanks to volunteers and other colleagues, have compatibility as well. Great, so on to the second idea, how to shift left and enforce early. And this is just the idea that you want to add these headers and protections as early as possible, maybe in your dev server, so that when things go wrong, your developers can see the error and act on it as soon as they've written the code, so that they can fix it while the context of what they're trying to do is fresh in their minds. And a quick, and more technically, a way to do this is, well, here in this example, we have a configuration for a Vite dev server. I hear that's what all the web developers are using nowadays. We are all on to Vite now. And here we see a Node.js express-style middleware inside this configuration for the dev server that just dumps in these headers. But if you don't have control over your dev server or your serving stack, as you mentioned before, meta tags at the top of an HTML file also can deliver these headers. And for the observant among you, you will see that here we have our hash-based CSP again, because if you are baking a CSP into a static HTML page, you really want it to be a hash-based CSP so that the nonce value isn't blown the first time you use it. And we saw an example of how to do the refactoring to make a hash-based CSP before, and it was a little cumbersome, so how can you do that more easily? And the answer is, we have an experimental tool available in GitHub and NPM that does a lot of this transformation for you. This is available as a Webpack plugin, but also as a utility library that takes in a string of the HTML and does a refactoring like this. I mean, I know there's a lot of code on the screen, but it just takes in a string and outputs a string and will shuffle around the notes, do the hashing, add the meta tags for you. And this is really powerful because this is a very framework-agnostic way that now has proven to have outlasted the popularity of Webpack plugins because they just keep changing the configurations. But this is really powerful because if we remember the example from before with the Vite middleware, it composes almost perfectly. This is just a copy-paste job of the last two slides. And I think one of the lessons we learned in building some of this tooling is if you keep it as agnostic as possible in terms of frameworks and just separate the logic from the framework, then you can just, yeah, snap the pieces together when a new framework comes along. So on to the next section. Thanks, Aaron. In addition to using the header to spot violations early, we really advocate for using some other tools to help you with adopting these defenses. We'll introduce a bunch of helpful tools, but they can be categorically be broken down into, first of all, tools for understanding features and violations. We mentioned that CSV is notoriously hard to configure and understand, and the CSV 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 CSV. It can detect directives and then kind of explain them. We're also actively working on a tool called Trusted Types Helper. This isn't released yet, but it will be soon, for streamlining the Trusted Types adoption process. One of the most time-consuming and painful stages of Trusted Types enforcement is finding the source of violations and trying to fix them. So this Chrome extension will create a DevTools tab that helps you demystify Trusted Types violations and suggest some refactoring. So stay tuned. Another category of tools are those that help with refactoring using Static Analysis. We have a huge Static Analysis pipeline at Google, and we took the most effective checks that we run on our internal Static Analysis tooling and Linter and built it on top of ESLint, and that is Safety Web. This project is also in active development, but it is available on GitHub. As you can see in the example here, it will detect unsafe usages of risky APIs, and you can refactor these unsafe calls with another tool we provide called Safe Values. So Safe Values is a library designed to steer developers to make safer choices. It will prevent unsafe assignments using TypeScript type checking unless the value is trusted, either from a trusted source or by transformation that makes it trusted. For example, sanitizing an HTML makes it trusted. We mentioned we were going to add a quick note about third-party dependencies. This is the complicated part, and it's a big aspect of making your application compatible with these features. For both Trusted Types and CSP, all code running on your site needs to be free of violations of these policies, otherwise they will be blocked by the browser. 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 in our code, but code that others have written and others control. 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 could use some help as well. 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 that you find along your journey of adopting these features. Thanks, Jen. So, I know we talked a lot about the mitigations and the tools that are used today to protect our apps, but let's talk a bit about where we want to go from here and the more secure world that we'd like to see in the future. And here are the key ideas we talked about today that form the basis of this protection, this pipeline of protection, if you will. So, the running theme is abstracting security decisions away from developers in a clear and secure by default way. When all of these elements work together, starting from the first stage when the 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 lifecycle. And this should make most cross-site scripting solvable, and we see this working at Google scale. On our most recent in-house frameworks that follow a lot of these guidelines from these pillars, we see so few cross-site scripting, and none of them are trivial attack chains, and all of them really have some weird logic bug or some legacy system involved. So, we are seeing the effects of this working at scale across billions of users and hundreds of different web apps. But how can we make this safer? Where do we go from here? What's the next logical step? And we believe that, remember how we talked about trusted types, and it blocks a lot of these inherently unsafe DOM APIs in the web platform, or at least makes it harder to use? What if we take that to the conclusion of the idea and actually like ban all of those usages at runtime of these legacy DOM APIs that are a little bit inherently unsafe as they've been designed? Can we build apps where we don't use any of those APIs? And that's our future vision of where we go from here. If we have new platform-level APIs for operations that we currently use DOM APIs for, such as maybe a browser-included sanitizer implementation for HTML, maybe a templating solution, then we've really come full circle and have our runtime enforcements influence a new suite of secure-by-default web platform APIs to bring decisions that nudge developers towards safer choices, maybe at the framework level, from the very beginning when the first line of code is written in a new code base all the way to the moment when it is executed on the client's browser. And this is the future we hope to get to, one where we can safely lock down all the accesses to many DOM APIs along with a new suite of platform APIs to replace those operations in a safe-by-default manner. And we call this approach, we've nicknamed it Perfect Types, it is the logical evolution of Trusted Types, and we see this as the next evolution of Trusted Types and a post-cross-site scripting world. This combined with the strict CSP configurations we talked about and our framework-based approaches, secure-by-design, safe-by-design, should guide us to a new post-cross-site scripting world. And taking a step back for a second, for an even bigger picture, and as a final plug, this safe coding philosophy and safe-by-design, trying to steer developers in the right direction is something that extends beyond just JavaScript, cross-site scripting, and is a core priority for all Google-scale security efforts. You can read more about our organization's work at these links, and Google is a signer of CISA's secure-by-design pledge along with other industry and government leaders in the space, and we've committed to sharing our knowledge on how to build a safer future together. All right, well, that is the end of our talk, so thank you everyone for listening. Hopefully this was a good guide on how to think about solving XSS. I'll leave you here.