Skip to content

fix: prevent crash in AuthWebViewScreen when params are missing or invalid - #2433

Open
synakr wants to merge 2 commits into
inji:masterfrom
synakr:fix/auth-webview-crash
Open

fix: prevent crash in AuthWebViewScreen when params are missing or invalid#2433
synakr wants to merge 2 commits into
inji:masterfrom
synakr:fix/auth-webview-crash

Conversation

@synakr

@synakr synakr commented May 2, 2026

Copy link
Copy Markdown

Fix: Prevent crash when route params are undefined in AuthWebViewScreen

Issue

route.params is destructured and used before validation. If it is undefined or contains invalid values, it can cause runtime crashes (e.g., during URL parsing with new URL()).

Changes

  • Made params destructuring null-safe:

    const { authorizationURL, clientId, redirectUri, controller } = route.params ?? {};
  • Guarded URL parsing to prevent runtime errors:

    let hostName = 'mosip.net';
    if (authorizationURL) {
      try {
        hostName = new URL(authorizationURL).hostname;
      } catch (err) {
        console.warn('Invalid authorizationURL:', authorizationURL);
      }
    }
  • Extended validation to include all required parameters:

    if (!authorizationURL || !clientId || !redirectUri || !controller)
  • Added safety check before redirect handling:

    if (redirectUri && url.startsWith(redirectUri))

Impact

Prevents crashes when navigation parameters are missing or malformed and ensures safe URL handling, without affecting existing behavior for valid inputs.

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Improved robustness of OAuth authentication by implementing safer URL parsing with fallback handling
    • Enhanced redirect validation during authentication flows to prevent edge cases with missing or invalid parameters

Signed-off-by: Chandra Keshav Mishra <chandrakeshavmishra@gmail.com>
Copilot AI review requested due to automatic review settings May 2, 2026 18:02
@coderabbitai

coderabbitai Bot commented May 2, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@synakr has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 44 minutes and 32 seconds before requesting another review.

To keep reviews running without waiting, you can enable usage-based add-on for your organization. This allows additional reviews beyond the hourly cap. Account admins can enable it under billing.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: cd380563-0d56-4232-9d27-107422c03a10

📥 Commits

Reviewing files that changed from the base of the PR and between 0fa6460 and 9a96779.

📒 Files selected for processing (1)
  • screens/AuthWebViewScreen.tsx

Walkthrough

AuthWebViewScreen.tsx adds defensive error handling: safe URL parsing with mosip.net fallback, mandatory controller parameter validation in the auth setup phase, and conditional redirect URI matching before processing navigation.

Changes

Auth Safety & Parameter Validation

Layer / File(s) Summary
URL Parsing & Defaults
screens/AuthWebViewScreen.tsx (lines 19–34)
hostName now safely derives from authorizationURL inside try/catch, defaulting to mosip.net when URL is missing or invalid.
Parameter Validation
screens/AuthWebViewScreen.tsx (lines 47–55)
Initial auth setup useEffect now requires controller in addition to authorizationURL, clientId, and redirectUri; logs error and navigates back if any are missing.
Redirect Handling
screens/AuthWebViewScreen.tsx (lines 105–111)
handleNavigationRequest now conditionally checks redirectUri presence before matching against redirect URL, preventing undefined reference errors.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A rabbit hops through safer code,
With try-catch guards on every road,
No crashes now from missing URLs—
Just fallback wisdom, tried and true.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: preventing crashes in AuthWebViewScreen when route parameters are missing or invalid.
Description check ✅ Passed The description is comprehensive and covers the issue, changes, and impact. However, it is missing the required 'Issue ticket number and link' 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
Review rate limit: 0/1 reviews remaining, refill in 44 minutes and 32 seconds.

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

Copilot AI 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.

Pull request overview

This PR hardens AuthWebViewScreen against missing or malformed navigation params to prevent runtime crashes during hostname/URL parsing and redirect handling.

Changes:

  • Made route.params destructuring null-safe.
  • Guarded new URL(authorizationURL) parsing with a fallback hostname.
  • Expanded required-param validation and added a redirectUri presence check before startsWith.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread screens/AuthWebViewScreen.tsx Outdated
Comment thread screens/AuthWebViewScreen.tsx Outdated
Comment on lines +31 to +33
} catch (err) {
console.warn('Invalid authorizationURL');
}

@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)
screens/AuthWebViewScreen.tsx (1)

105-125: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Tighten the redirect match before consuming code.

startsWith is too loose for an auth callback check and can accept look-alike URLs that share the same prefix. Compare the parsed callback components, or normalize to an exact redirect URI match, before sending the auth code onward.

Suggested fix
-    if (redirectUri && url.startsWith(redirectUri)) {
+    if (redirectUri) {
       try {
         const uri = new URL(url);
+        const expected = new URL(redirectUri);
+        if (
+          uri.protocol !== expected.protocol ||
+          uri.host !== expected.host ||
+          uri.pathname !== expected.pathname
+        ) {
+          return true;
+        }
         const code = uri.searchParams.get('code');
 
         if (!code) {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@screens/AuthWebViewScreen.tsx` around lines 105 - 125, The current check uses
url.startsWith(redirectUri) which is too permissive; instead parse both the
incoming url and the configured redirectUri (new URL(url) and new
URL(redirectUri)) and compare their normalized components (protocol,
host/hostname and port, and pathname, handling optional trailing slash
consistency) to ensure an exact callback match before extracting the
authorization code; only after those components match should you call
VciClient.getInstance().sendAuthCode(code) and perform controller.CANCEL() /
navigation.goBack(), otherwise ignore the URL and continue loading.
🤖 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 `@screens/AuthWebViewScreen.tsx`:
- Around line 105-125: The current check uses url.startsWith(redirectUri) which
is too permissive; instead parse both the incoming url and the configured
redirectUri (new URL(url) and new URL(redirectUri)) and compare their normalized
components (protocol, host/hostname and port, and pathname, handling optional
trailing slash consistency) to ensure an exact callback match before extracting
the authorization code; only after those components match should you call
VciClient.getInstance().sendAuthCode(code) and perform controller.CANCEL() /
navigation.goBack(), otherwise ignore the URL and continue loading.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: fab6ff86-5c08-49e6-b245-c5e43abc1eca

📥 Commits

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

📒 Files selected for processing (1)
  • screens/AuthWebViewScreen.tsx

…valid

Signed-off-by: Md Sayan Akram <mdsayanakram@gmail.com>
@synakr
synakr force-pushed the fix/auth-webview-crash branch from eb7c001 to 9a96779 Compare May 2, 2026 18:17
@synakr
synakr requested a review from Copilot May 2, 2026 18:19

Copilot AI 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.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +48 to 51
if (!authorizationURL || !clientId || !redirectUri || !controller) {
console.error('Missing required parameters for authentication');
navigation.goBack();
return;
Comment on lines +31 to +33
} catch {
console.warn('Invalid authorizationURL');
}
@swatigoel

swatigoel commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

@synakr please check coderabbit comment and raise PR for develop

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.

4 participants