Skip to content

fix: resolve ReferenceError in hasEncryptionKey service - #2405

Open
CodeBySayak wants to merge 2 commits into
inji:masterfrom
CodeBySayak:syntax
Open

fix: resolve ReferenceError in hasEncryptionKey service#2405
CodeBySayak wants to merge 2 commits into
inji:masterfrom
CodeBySayak:syntax

Conversation

@CodeBySayak

@CodeBySayak CodeBySayak commented Apr 24, 2026

Copy link
Copy Markdown

Summary

This PR fixes a ReferenceError in the hasEncryptionKey service within machines/store.ts. The code was attempting to access event.requester when handling a BiometricCancellationError, but the event object is not defined within the scope of this callback service.

Changes

  • machines/store.ts: Removed the usage of event.requester in the hasEncryptionKey callback.
  • Called model.events.BIOMETRIC_CANCELLED() without arguments to safely handle biometric cancellation.

Impact

This fix prevents an application crash during the startup/initialization flow. Specifically, it addresses the scenario where a user cancels a biometric authentication prompt while the app is checking for an encryption key. Previously, this cancellation would trigger a ReferenceError: event is not defined, leading to an unstable app state.

Testing

  • Verified that the BIOMETRIC_CANCELLED event model supports an optional requester.
  • Confirmed that removing the reference prevents the ReferenceError in the async callback scope.

closes #2404

Summary by CodeRabbit

  • Bug Fixes
    • Improved biometric cancellation error handling to prevent unreliable event processing.

ckm007 and others added 2 commits March 16, 2026 20:56
Signed-off-by: Chandra Keshav Mishra <chandrakeshavmishra@gmail.com>
@coderabbitai

coderabbitai Bot commented Apr 24, 2026

Copy link
Copy Markdown

Walkthrough

The PR adjusts biometric cancellation error handling in the hasEncryptionKey service by removing the requester argument from the dispatched BIOMETRIC_CANCELLED event, eliminating reliance on an undefined event property in that code path.

Changes

Cohort / File(s) Summary
Biometric Error Handling
machines/store.ts
Removed requester argument from BIOMETRIC_CANCELLED event dispatch in the BiometricCancellationError catch block to prevent undefined reference.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Possibly related issues

Poem

🐰 A requester that was never there,
Now vanishes into thin air,
The biometric path runs true,
With one small fix, the error flew! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix: resolve ReferenceError in hasEncryptionKey service' directly and clearly summarizes the main change - fixing a ReferenceError in a specific service.
Description check ✅ Passed The description provides a comprehensive summary of changes, detailed impact analysis, and testing verification, though it lacks the issue ticket number and screenshots sections from the template.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
machines/store.ts (1)

365-379: ⚠️ Potential issue | 🟠 Major

Add return statements to prevent event cascade after cancellation and error handling.

The control flow in the hasEncryptionKey service callback (lines 365–379) allows execution to fall through after dispatching BIOMETRIC_CANCELLED, causing the machine to receive conflicting events in rapid succession:

  1. Line 367 calls callback(model.events.BIOMETRIC_CANCELLED()) but does not return, so execution continues to line 369 (sendErrorEvent), polluting telemetry for a user-initiated cancel.
  2. Lines 371–377 then dispatch either KEY_INVALIDATE_ERROR or STORE_ERROR for the same cancellation event.
  3. Line 379 calls callback(model.events.READY()) outside the try/catch, so it executes after any error path as well.

This causes the state machine to receive BIOMETRIC_CANCELLEDSTORE_ERROR/KEY_INVALIDATE_ERRORREADY in quick succession. Since BIOMETRIC_CANCELLED targets checkFreshInstall (lines 305–308) while READY targets ready (line 135), the final state becomes unpredictable.

Add return statements after BIOMETRIC_CANCELLED dispatch and after the error handling block to short-circuit execution and prevent READY from firing on error paths.

🛠 Proposed fix
           } catch (e) {
             if (e instanceof BiometricCancellationError) {
               callback(model.events.BIOMETRIC_CANCELLED());
+              return;
             }
             sendErrorEvent(getErrorEventData('ENCRYPTION', '', e));

             if (e.message.includes(keyinvalidatedString)) {
               await clear();
               callback(model.events.KEY_INVALIDATE_ERROR());
               sendUpdate();
             } else {
               callback(model.events.STORE_ERROR(e));
             }
+            return;
           }
           callback(model.events.READY());
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@machines/store.ts` around lines 365 - 379, In the hasEncryptionKey service
callback, stop fall-through after biometric cancellation and after handling
errors: when catching BiometricCancellationError and calling
callback(model.events.BIOMETRIC_CANCELLED()) insert an immediate return to avoid
subsequent sendErrorEvent and STORE_ERROR/KEY_INVALIDATE_ERROR dispatches, and
ensure you also return after the error-handling branch (after sending
sendErrorEvent and dispatching either model.events.KEY_INVALIDATE_ERROR() or
model.events.STORE_ERROR(e)) so execution does not continue to
callback(model.events.READY()); this keeps the machine from receiving
BIOMETRIC_CANCELLED → STORE_ERROR/KEY_INVALIDATE_ERROR → READY in quick
succession.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@machines/store.ts`:
- Around line 365-379: In the hasEncryptionKey service callback, stop
fall-through after biometric cancellation and after handling errors: when
catching BiometricCancellationError and calling
callback(model.events.BIOMETRIC_CANCELLED()) insert an immediate return to avoid
subsequent sendErrorEvent and STORE_ERROR/KEY_INVALIDATE_ERROR dispatches, and
ensure you also return after the error-handling branch (after sending
sendErrorEvent and dispatching either model.events.KEY_INVALIDATE_ERROR() or
model.events.STORE_ERROR(e)) so execution does not continue to
callback(model.events.READY()); this keeps the machine from receiving
BIOMETRIC_CANCELLED → STORE_ERROR/KEY_INVALIDATE_ERROR → READY in quick
succession.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c637244a-3911-4a37-9e63-1d551cfc815c

📥 Commits

Reviewing files that changed from the base of the PR and between 21743f7 and aa46f61.

📒 Files selected for processing (1)
  • machines/store.ts

@swatigoel

Copy link
Copy Markdown
Contributor

@CodeBySayak can you raise PR for develop branch and sign off the commit too?

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.

ReferenceError in hasEncryptionKey

3 participants