HQD-134: Improve safety checks in AuditController and update audit …#346
Conversation
…asset scripts - Added null checks for `user.id` and `request.trace_id` in `AuditController` to prevent potential errors. - Renamed `prod` script to `build` in `audit/package.json` for better clarity. - Updated `main.js.map` with the latest build mapping.
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (6)
📝 WalkthroughWalkthroughFrontend and backend were hardened to tolerate missing audit fields (optional request, guarded renderers, conditional link generation). Build artifacts and scripts were updated: mix-manifest cache id changed, production script key renamed to ChangesAudit data defensive handling
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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 |
There was a problem hiding this comment.
Pull request overview
Adds defensive null/missing-field checks when rendering audit data, both server-side (PHP controller) and client-side (React table columns), so that restricted or absent fields no longer break link generation. Also renames the npm prod script to build and refreshes the mix manifest.
Changes:
- Guard
user.idandrequest.trace_idaccess inAuditController::prepareData()before building URLs. - Render a
RESTRICTEDplaceholder in the React audit table whenuser.link,table,entity_id, orrequestis missing; add a "Before / After" header above the diff viewer. - Rename the
prodnpm script tobuildand update the mix-manifest hash.
Reviewed changes
Copilot reviewed 4 out of 6 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/controllers/AuditController.php | Adds null checks for user id and trace_id before building route URLs. |
| src/assets/audit/src/app/App.tsx | Adds Restricted placeholder for missing fields and a Before/After header via a new Desc component. |
| src/assets/audit/package.json | Renames prod script to build. |
| src/assets/audit/build/mix-manifest.json | Updates main.js bundle hash. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| $trace = isset($datum['request']['trace_id']); | ||
| if ($trace) { | ||
| $traceId ??= $datum['request']['trace_id']; |
There was a problem hiding this comment.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
src/assets/audit/src/app/App.tsx (2)
122-130:⚠️ Potential issue | 🟠 Major | ⚡ Quick winTable filter predicate is unsafe for missing
tablevalues.Line 130 explicitly handles missing
table, but Line 122 still doesrecord.table.indexOf(...). Using filters with restricted rows can throw.Suggested fix
- onFilter: (value, record) => record.table.indexOf(value as string) === 0, + onFilter: (value, record) => (record.table ?? "").indexOf(value as string) === 0,🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/assets/audit/src/app/App.tsx` around lines 122 - 130, The filter predicate onFilter currently calls record.table.indexOf(...) which will throw when record.table is null/undefined; update the onFilter implementation (the onFilter callback used in the column definition) to first verify record.table is a string (e.g., typeof record.table === 'string' or Boolean(record.table)) before calling indexOf (or use startsWith) so it safely returns false for missing table values; ensure the change aligns with the existing render(table: string) handling for empty/missing table.
223-229:⚠️ Potential issue | 🟠 Major | ⚡ Quick winExpanded row can crash when
requestis missing.Line 223 calls
Object.entries(record.request)unguarded. Ifrequestis absent (the case this PR is defending), this throws at runtime.Suggested fix
- const items: DescriptionsProps["items"] = Object.entries(record.request) + const items: DescriptionsProps["items"] = Object.entries(record.request ?? {}) .filter(([key]) => ["app", "log_id", "ip"].includes(key)) .map(([key, value]) => ({🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/assets/audit/src/app/App.tsx` around lines 223 - 229, The code calls Object.entries(record.request) unguarded which will throw when record.request is undefined; update the items construction (the const items: DescriptionsProps["items"] block) to safely handle a missing request by using a fallback empty object (e.g., record.request || {}) or optional chaining so Object.entries never receives undefined, preserving the same filter/map logic and types.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/assets/audit/src/app/App.tsx`:
- Line 188: The render callback in App.tsx currently only checks for request and
can render an empty <a> when request.link or request.trace_id is missing; update
the render function (the renderer for that column) to verify both request.link
and request.trace_id are truthy before returning the anchor, and return
<Restricted/> if either is missing (i.e., replace the single request check with
a presence check of request.link && request.trace_id to avoid broken links).
In `@src/controllers/AuditController.php`:
- Around line 91-95: The loop is reusing a previous row's $traceId because it
uses the null-coalescing assignment operator ($traceId ??=) — change the logic
to assign/reset $traceId for each $datum instead of preserving prior value: for
each iteration set $traceId explicitly from $datum['request']['trace_id'] (or
null when absent) and then compute $datum['request']['link'] from that per-row
$traceId; update the AuditController.php block that currently checks $trace and
uses $traceId ??= to perform a direct per-row assignment and link creation.
---
Outside diff comments:
In `@src/assets/audit/src/app/App.tsx`:
- Around line 122-130: The filter predicate onFilter currently calls
record.table.indexOf(...) which will throw when record.table is null/undefined;
update the onFilter implementation (the onFilter callback used in the column
definition) to first verify record.table is a string (e.g., typeof record.table
=== 'string' or Boolean(record.table)) before calling indexOf (or use
startsWith) so it safely returns false for missing table values; ensure the
change aligns with the existing render(table: string) handling for empty/missing
table.
- Around line 223-229: The code calls Object.entries(record.request) unguarded
which will throw when record.request is undefined; update the items construction
(the const items: DescriptionsProps["items"] block) to safely handle a missing
request by using a fallback empty object (e.g., record.request || {}) or
optional chaining so Object.entries never receives undefined, preserving the
same filter/map logic and types.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: a450418e-54a8-4441-b457-10030e8ebe96
⛔ Files ignored due to path filters (1)
src/assets/audit/build/main.js.mapis excluded by!**/*.map
📒 Files selected for processing (5)
src/assets/audit/build/main.jssrc/assets/audit/build/mix-manifest.jsonsrc/assets/audit/package.jsonsrc/assets/audit/src/app/App.tsxsrc/controllers/AuditController.php
| dataIndex: "trace_id", | ||
| key: "trace_id", | ||
| render: (_, { request }) => <a href={request.link}>{request.trace_id}</a>, | ||
| render: (_, { request }) => request ? <a href={request.link}>{request.trace_id}</a> : <Restricted/>, |
There was a problem hiding this comment.
Trace ID fallback is incomplete; this still renders broken links.
Line 188 only checks request, not request.link/request.trace_id. Records with partial request data will render an empty anchor instead of Restricted.
Suggested fix
- render: (_, { request }) => request ? <a href={request.link}>{request.trace_id}</a> : <Restricted/>,
+ render: (_, { request }) =>
+ request?.link && request?.trace_id
+ ? <a href={request.link}>{request.trace_id}</a>
+ : <Restricted/>,📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| render: (_, { request }) => request ? <a href={request.link}>{request.trace_id}</a> : <Restricted/>, | |
| render: (_, { request }) => | |
| request?.link && request?.trace_id | |
| ? <a href={request.link}>{request.trace_id}</a> | |
| : <Restricted/>, |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/assets/audit/src/app/App.tsx` at line 188, The render callback in App.tsx
currently only checks for request and can render an empty <a> when request.link
or request.trace_id is missing; update the render function (the renderer for
that column) to verify both request.link and request.trace_id are truthy before
returning the anchor, and return <Restricted/> if either is missing (i.e.,
replace the single request check with a presence check of request.link &&
request.trace_id to avoid broken links).
…asset scripts
user.idandrequest.trace_idinAuditControllerto prevent potential errors.prodscript tobuildinaudit/package.jsonfor better clarity.main.js.mapwith the latest build mapping.Summary by CodeRabbit
Bug Fixes
Chores