Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible trustedTypes bypass when assigning to script.innerHTML #238

Closed
securityMB opened this issue Nov 15, 2019 · 3 comments
Closed

Possible trustedTypes bypass when assigning to script.innerHTML #238

securityMB opened this issue Nov 15, 2019 · 3 comments

Comments

@securityMB
Copy link

Not sure if this has been covered before, but my quick search didn't yield any results. The issue is that certain webpages might assign a string (purposely or by mistake) to scriptTag.innerHTML. In this case, createHTML is called, instead of createScript (even though, effectively, a script is created). This might lead to XSS-es since HTML sanitizers won't probably sanitize alert(1) in any way.

Here's an example:

<meta http-equiv=content-security-policy
      content="trusted-types default">


<script id=magic></script>
<script>
  // simulate a good sanitizer
  const sanitize = s => s;
  
  const tt = trustedTypes.createPolicy('default', {
    createHTML(s) {
      return sanitize(s);
    },
    createScript(s) {      
      return ''; // no eval at all!
    }
  });
  
  document.querySelector('#magic').innerHTML='alert(1)'; 
  
</script>

In the case above, alert(1) gets executed even though the policy explicitly forbids creating scripts from a string.

@koto
Copy link
Member

koto commented Nov 15, 2019

Yeah, that would be covered by #236.

@koto
Copy link
Member

koto commented Dec 12, 2019

@otherdaniel - Daniel, this seems like a good example to include in the WPT test for the slot approach.

@koto
Copy link
Member

koto commented Mar 2, 2020

Confirmed that the bypass doesn't work in current Chrome 82 (Canary). The createHTML function is called on innerHTML assignment, but that value is then processed by createScript function when the script is about to execute (as outlined in https://w3c.github.io/webappsec-trusted-types/dist/spec/#prepare-script-url-and-text)

@koto koto closed this as completed Mar 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants