Skip to content

sdk/js: wait for a browser state delete to commit - #982

Open
ersinkoc wants to merge 1 commit into
google:mainfrom
ersinkoc:audit/sdk-js-state-remove
Open

ersinkoc wants to merge 1 commit into
google:mainfrom
ersinkoc:audit/sdk-js-state-remove

Conversation

@ersinkoc

Copy link
Copy Markdown
Contributor

forget() deletes the saved credential with the state store's remove(). In the browser store, remove() resolved as soon as the IndexedDB delete request succeeded, before its transaction committed. If the transaction then aborted, forget() reported the credential gone while it was still stored.

remove() now settles on the transaction's oncomplete and rejects on onabort (with the transaction's error, or a generic one when it has none), as write() already does since #936.

The new test drives the transaction by hand: the delete request succeeds first, and the test checks that remove() has not settled until the transaction commits or aborts. It failed on the previous behavior for all three outcomes.

Validation: type checking (tsconfig.json, test and webapp-test configs) is clean, and the state tests pass. In the full SDK suite, the only failures are the two Windows-only tests that fail without this change too (SIGTERM handling and file mode), plus one load-dependent timeout in biscuit.test.ts that passes when run alone on this branch and on main.

forget() deletes the stored credential with remove(), which resolved once the delete request succeeded. A transaction that then aborted left the credential in IndexedDB while forget() reported it gone. remove() now settles with its transaction, as write() does.
@ersinkoc
ersinkoc requested a review from aojea as a code owner November 28, 2026 16:58

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the browser state store's remove method to wait for the IndexedDB transaction to complete or abort before settling, and adds corresponding test coverage. The review feedback suggests adding a tx.onerror handler to prevent unhandled transaction errors from bubbling up globally, and replacing the Node-specific setImmediate with setTimeout in the tests to ensure compatibility with standard browser environments.

Comment on lines +61 to +65
await new Promise<void>((resolve, reject) => {
tx.oncomplete = () => resolve();
tx.onabort = () => reject(tx.error ?? new Error("IndexedDB delete transaction aborted"));
tx.objectStore(STORE).delete(name);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

In IndexedDB, if a request or transaction error is not caught and event.preventDefault() is not called, the error bubbles up to the global scope (triggering window.onerror or global error events). This can cause false positives in error monitoring tools (like Sentry) or cause some test runners to fail. Consider adding a tx.onerror handler that calls event.preventDefault() to prevent the error from bubbling up globally.

      await new Promise<void>((resolve, reject) => {
        tx.oncomplete = () => resolve();
        tx.onabort = () => reject(tx.error ?? new Error("IndexedDB delete transaction aborted"));
        tx.onerror = (event) => event.preventDefault();
        tx.objectStore(STORE).delete(name);
      });

opened.onsuccess();
await queued;
deleted.onsuccess();
await new Promise<void>((resolve) => setImmediate(resolve));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

setImmediate is a non-standard, Node.js-specific global and is not supported in standard browser environments. Since this is a browser-targeted state store test, using setImmediate can cause runtime errors if the tests are executed in a real browser environment (e.g., via Karma, Web Test Runner, or Playwright) without a polyfill. Consider using setTimeout(resolve, 0) for cross-platform compatibility.

Suggested change
await new Promise<void>((resolve) => setImmediate(resolve));
await new Promise<void>((resolve) => setTimeout(resolve, 0));

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

1 participant