WEB-932: Fix null dereference in HasPermissionDirective on unauthenticated session#3531
Conversation
Signed-off-by: Aditya Tiwari <adityatiwari342005@gmail.com>
|
Note
|
| Cohort / File(s) | Summary |
|---|---|
Defensive Initialization src/app/directives/has-permission/has-permission.directive.ts |
Updated constructor to defensively initialize userPermissions using optional chaining and nullish coalescing operator, providing an empty array fallback when getCredentials() returns null/undefined or permissions are absent. |
Estimated code review effort
🎯 1 (Trivial) | ⏱️ ~5 minutes
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check | ✅ Passed | The title accurately describes the main change: fixing a null dereference in HasPermissionDirective during unauthenticated sessions, which aligns with the defensive initialization of userPermissions and the guard against null credentials. |
| 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.
Comment @coderabbitai help to get the list of available commands and usage tips.
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/app/directives/has-permission/has-permission.directive.ts (1)
28-28: Optional: tightenuserPermissionstype fromany[]tostring[].The
Credentials.permissionsfield is typed asstring[], and the empty-array fallback is also assignable tostring[]. Narrowing this declaration would align with the strict typing convention without behavior change.Proposed change
- private userPermissions: any[]; + private userPermissions: string[];As per coding guidelines: "Use TypeScript for all application code with strict typing conventions".
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/app/directives/has-permission/has-permission.directive.ts` at line 28, The field userPermissions is declared as any[] but should be strongly typed to string[] to match Credentials.permissions and the empty-array fallback; update the declaration of userPermissions in the HasPermissionDirective (or has-permission.directive.ts) from any[] to string[] and ensure any assignments/uses of userPermissions (e.g., in the directive's constructor/inputs and methods like ngOnInit or checkPermission) remain compatible with string[] so no runtime behavior changes.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/app/directives/has-permission/has-permission.directive.ts`:
- Line 28: The field userPermissions is declared as any[] but should be strongly
typed to string[] to match Credentials.permissions and the empty-array fallback;
update the declaration of userPermissions in the HasPermissionDirective (or
has-permission.directive.ts) from any[] to string[] and ensure any
assignments/uses of userPermissions (e.g., in the directive's constructor/inputs
and methods like ngOnInit or checkPermission) remain compatible with string[] so
no runtime behavior changes.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 0722807f-e965-41c5-b86a-4480e5981d1e
📒 Files selected for processing (1)
src/app/directives/has-permission/has-permission.directive.ts
Description
Guards against a null dereference crash in
HasPermissionDirective.AuthenticationService.getCredentials()returnsCredentials | null-- when no credentials exist in storage (unauthenticated session), it returnsnullviaJSON.parse(null). The directive constructor accessed.permissionsdirectly on this null value, throwing aTypeErrorbefore any RBAC logic could run.Changed
savedCredentials.permissionstosavedCredentials?.permissions ?? []so the directive initialises safely with an empty array when no credentials are present.Jira: https://mifosforge.jira.com/browse/WEB-932
Related issues and discussion
#3527
Screenshots, if any
Checklist
Please make sure these boxes are checked before submitting your pull request - thanks!
If you have multiple commits please combine them into one commit by squashing them.
Read and understood the contribution guidelines at
web-app/.github/CONTRIBUTING.md.Summary by CodeRabbit