Repository navigation
Fix 500 on attendance_cleanup_rule "run now": scope job to the rule's own userId - #157
Merged
Merged
Conversation
… own userId Production error: QueryFailedError: Field 'user_id' doesn't have a default value INSERT INTO `jobs`(..., `user_id`, ...) VALUES (..., DEFAULT, ...) runNow used getUserIdFromUser(req.auth) as the enqueued job's userId. That's wrong for this action: CrudAuthWithPermissionsFilter grants admins AND anyone with the attendanceCleanupRules permission full visibility (not just their own rows), so a requester can select rules owned by a different user - and for admin specifically, getUserIdFromUser always returns undefined (id -1 is a sentinel, not a real tenant), which is what produced the undefined user_id insert. Fix: load the selected rules and derive the job's userId from each rule's own userId, enqueuing one job per distinct owner so a multi-tenant selection is scoped correctly rather than crashing or silently misattributing the sweep. Reproduced the exact error against a real MySQL jobs table (enqueue(undefined, ...)) and confirmed the fix (enqueue(38, ...)) succeeds. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011dEYULADLgGNfZ4nv6gz8s
There was a problem hiding this comment.
🟡 Changes recommended
Unauthorized users may enqueue cleanup jobs for other tenants because the lookup bypasses authorization.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Fixes runNow attendance cleanup jobs by scoping them to selected rules’ owners.
Changes:
- Groups selected rules by
userId. - Enqueues one job per owner.
- Adds single-owner and multi-owner tests.
File summaries
| File | Summary |
|---|---|
server/src/entity-modules/attendance-cleanup-rule.config.ts |
Owner-scoped job creation; Critical: direct lookup bypasses authorization filtering. |
server/src/entity-modules/__tests__/attendance-cleanup-rule.config.spec.ts |
Tests owner grouping and job enqueueing. |
Review details
Suppressed comments (1)
server/src/entity-modules/tests/attendance-cleanup-rule.config.spec.ts:72
- This assertion does not verify which IDs are queried:
mockRepository.findalways returns its fixture regardless of arguments, andexpect.anything()accepts anyIn(...)value. The test therefore still passes if this code queries all rules instead of only the selected IDs. Assert theFindOperatorvalues (or make the mock filter by the supplied IDs) so selection scoping is covered.
expect(mockRepository.find).toHaveBeenCalledWith({ where: { id: expect.anything() } });
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
…rmission-based full visibility Addresses Copilot review comment on this PR, and explicit user direction: CrudAuthWithPermissionsFilter granted ADMIN_FILTER (full visibility, not just-your-own-rows) to anyone with the attendanceCleanupRules permission - and doAction's runNow is a custom route that bypasses @dataui/crud's normal auth-filtered query building entirely, so it never consulted that filter in the first place. Together, any authenticated user could pass arbitrary rule ids to runNow and have jobs enqueued under other tenants' identities. Fix: - crudAuth is now the standard CrudAuthFilter (admin sees all, everyone else sees only their own rows) - same as most other entities. The attendanceCleanupRules permission remains a client-side visibility gate only; it no longer grants any server-side data access. - runNow explicitly applies crudAuth.filter(req.auth) to its id lookup, since this custom route doesn't go through the filtered query path - a non-owned id in the selection just doesn't resolve, instead of being trusted from the request. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011dEYULADLgGNfZ4nv6gz8s
Simplifies the previous manual crudAuth.filter(req.auth) + raw find() into a call to the new shared helper (nra-server#43), which applies the same auth filtering through the real query-building path instead of duplicating it per action. bump server/shared to 24975d1 to pick up getManyByIds. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011dEYULADLgGNfZ4nv6gz8s
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the production 500 on
POST /attendance_cleanup_rule/action?extra.action=runNow:runNowusedgetUserIdFromUser(req.auth)as the enqueued job'suserId, which isundefinedfor admin (id: -1is a sentinel, not a real tenant) — exactly what produced theundefined→DEFAULTinsert.Also fixes a cross-tenant data exposure (Copilot review + user-flagged)
The original
crudAuth: CrudAuthWithPermissionsFilter(...)granted full visibility (not just-your-own-rows) to anyone with theattendanceCleanupRulespermission, or admin. Combined withrunNowbeing a custom/actionroute that bypasses@dataui/crud's normal auth-filtered query building, any authenticated caller could pass arbitrary rule ids and have jobs enqueued under other tenants' identities.Fix:
crudAuthis now the standardCrudAuthFilter(admin sees all, everyone else sees only their own rows) — same as most other entities. TheattendanceCleanupRulespermission is now a client-side visibility gate only; it grants no server-side data access.runNowresolves its ids via the newBaseEntityService.getManyByIds(nra-server#43) instead of a raw repository query — a shared, reusable helper that merges the id lookup into the auth-filtered query path already used everywhere else, so a non-owned id in the selection just doesn't resolve, and no custom action anywhere in the app has to hand-roll this again.Testing
jobstable:enqueue(undefined, ...)throwsField 'user_id' doesn't have a default value;enqueue(38, ...)succeeds.getManyByIds:tsc --noEmitclean,yarn test— 114 suites / 1008 tests passedgetManyByIdsboundary — the underlying auth-filtering itself is covered directly in nra-server'sbase-entity.service.spec.ts), a selection with no resolvable ids enqueues nothing, admin selection spanning users enqueues one job per owner🤖 Generated with Claude Code