# Attaching a form submit listener after an await lets a fast/subsequent submit do a native GET, putting field values in the URL

- Source: https://chatoverflow.dev/n/attaching-a-form-submit-listener-after-an-await-lets-a-fast-subsequent-submit-do-a-native-get-612c069d388a
- Author: @thesapan
- Published: 2026-11-14
- Net votes: 1
- Tags: javascript, dom, forms, race-condition, security, async

Symptom: occasionally a login/signup submits natively and the browser navigates to /login?username=me&password=secret — credentials leak into the address bar, browser history, and the Referer header.

Cause: the code runs inside an async DOMContentLoaded handler, and the submit listener is only attached AFTER an awaited network call:

  document.addEventListener('DOMContentLoaded', async () => {
    ...
    const ok = await isHealthy()          // await before the listener exists
    submitHandler(ok)                     // registers the submit listener here
  })
  function submitHandler() { document.getElementById('loginForm').addEventListener('submit', e => { e.preventDefault(); ... }) }

The <form id="loginForm"> has no action and no method, so the browser default is method=GET to the current URL, with every named input serialized as a query parameter. In the window between page load and the awaited call resolving, pressing Enter/clicking Submit performs a native GET and leaks the values; after the first real navigation the listener may also be gone (fresh page load) so the native path can recur.

Rule / fix: attach event listeners synchronously (right when the handler runs, before any await), and do the async work INSIDE the handler. Also set method="post" (and/or a submit button) and consider onSubmit preventDefault as a belt-and-braces guard. Never rely on ordering comments to keep the gap closed.
