tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

button-untrusted-key-event.html (2499B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4 <title>Forms</title>
      5 <script src="/resources/testharness.js"></script>
      6 <script src="/resources/testharnessreport.js"></script>
      7 </head>
      8 <body>
      9 <div id="log"></div>
     10 <form id="input_form">
     11  <button name="submitButton" type="submit">Submit</button>
     12  <button name="resetButton" type="reset">Reset</button>
     13  <button name="buttonButton" type="button">Button</button>
     14 </form>
     15 <script type="module">
     16 const form = document.querySelector("form");
     17 form.addEventListener("submit", (e) => {
     18  e.preventDefault();
     19  assert_true(false, 'form should not be submitted');
     20 });
     21 
     22 for (const button of document.querySelectorAll("button")) {
     23  button.addEventListener("click", function(e) {
     24    assert_true(false, `${button.type} button should not be clicked`);
     25  });
     26 }
     27 
     28 // Create and append button elements
     29 for (const button of document.querySelectorAll("button")) {
     30  test(() => {
     31    // keyCode: Enter
     32    button.dispatchEvent(
     33      new KeyboardEvent("keypress", {
     34        keyCode: 13,
     35      })
     36    );
     37 
     38    // key: Enter
     39    button.dispatchEvent(
     40      new KeyboardEvent("keypress", {
     41        key: "Enter",
     42      })
     43    );
     44 
     45    // keyCode: Space
     46    button.dispatchEvent(
     47      new KeyboardEvent("keypress", {
     48        keyCode: 32,
     49      })
     50    );
     51 
     52    // key: Space
     53    button.dispatchEvent(
     54      new KeyboardEvent("keypress", {
     55        key: " ",
     56      })
     57    );
     58  }, `Dispatching untrusted keypress events to ${button.type} button should not cause click event`);
     59 
     60  test(() => {
     61    // keyCode: Enter
     62    button.dispatchEvent(
     63      new KeyboardEvent("keydown", {
     64        keyCode: 13,
     65      })
     66    );
     67    button.dispatchEvent(
     68      new KeyboardEvent("keyup", {
     69        keyCode: 13,
     70      })
     71    );
     72 
     73    // key: Enter
     74    button.dispatchEvent(
     75      new KeyboardEvent("keydown", {
     76        key: "Enter",
     77      })
     78    );
     79    button.dispatchEvent(
     80      new KeyboardEvent("keyup", {
     81        key: "Enter",
     82      })
     83    );
     84 
     85    // keyCode: Space
     86    button.dispatchEvent(
     87      new KeyboardEvent("keydown", {
     88        keyCode: 32,
     89      })
     90    );
     91    button.dispatchEvent(
     92      new KeyboardEvent("keyup", {
     93        keyCode: 32,
     94      })
     95    );
     96 
     97    // key: Space
     98    button.dispatchEvent(
     99      new KeyboardEvent("keydown", {
    100        key: " ",
    101      })
    102    );
    103    button.dispatchEvent(
    104      new KeyboardEvent("keyup", {
    105        key: " ",
    106      })
    107    );
    108  }, `Dispatching untrusted keyup/keydown events to ${button.type} button should not cause click event`);
    109 }
    110 </script>
    111 </body>
    112 </html>