github.com/google/go-safeweb@v0.0.0-20231219055052-64d8cfc90fbb/examples/trustedtypes/safe.html (about)

     1  <!--
     2  Copyright 2020 Google LLC
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  	
     7  https://www.apache.org/licenses/LICENSE-2.0
     8  Unless required by applicable law or agreed to in writing, software
     9  distributed under the License is distributed on an "AS IS" BASIS,
    10  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  See the License for the specific language governing permissions and
    12  limitations under the License.
    13  -->
    14  <!DOCTYPE html>
    15  <html lang="en">
    16  
    17  <head>
    18      <title>Trusted Types - example</title>
    19      <!-- Install polyfill for browsers that do not support Trusted Types yet. -->
    20      <script src="https://w3c.github.io/webappsec-trusted-types/dist/es5/trustedtypes.build.js"
    21          data-csp="require-trusted-types-for 'script'"></script>
    22      <script src="https://cdnjs.cloudflare.com/ajax/libs/dompurify/2.2.6/purify.min.js"
    23          integrity="sha512-rXAHWSMciPq2KsOxTvUeYNBb45apbcEXUVSIexVPOBnKfD/xo99uUe5M2OOsC49hGdUrkRLYsATkQQHMzUo/ew=="
    24          crossorigin="anonymous"></script>
    25  </head>
    26  
    27  <body>
    28      <p>This document installs the following script which appends sanitized user input into the DOM:</p>
    29      <code>
    30          document.getElementById("hash").innerHTML = DOMPurify.sanitize(location.hash, {RETURN_TRUSTED_TYPE: true});
    31      </code>
    32      <p>location hash: <span id="hash"></span></p>
    33      <script>
    34          window.addEventListener('load', function () {
    35              // This piece of code is safe from DOM XSS.
    36              document.getElementById("hash").innerHTML = DOMPurify.sanitize(location.hash, { RETURN_TRUSTED_TYPE: true });
    37          })
    38      </script>
    39      <p>Unsafe usage of DOM APIs will be blocked. The code snippet below will not be executed and will
    40          trigger a warning in the console:</p>
    41      <code>
    42          document.getElementById("unsafehash").innerHTML = location.hash;
    43      </code>
    44      <p>location hash (unsafe): <span id="unsafehash"></span></p>
    45      <script>
    46          window.addEventListener('load', function () {
    47              // This piece of code is vulnerable to DOM XSS.
    48              document.getElementById("unsafehash").innerHTML = location.hash;
    49          })
    50      </script>
    51  </body>
    52  
    53  </html>