Skip to content

Fix 500 on attendance_cleanup_rule "run now": scope job to the rule's own userId - #157

Merged
buzz-code merged 3 commits into
mainfrom
claude/fix-runnow-userid-scope
Aug 23, 2026
Merged

buzz-code merged 3 commits into
mainfrom
claude/fix-runnow-userid-scope

Conversation

@buzz-code

@buzz-code buzz-code commented Aug 23, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fixes the production 500 on POST /attendance_cleanup_rule/action?extra.action=runNow:

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, which is undefined for admin (id: -1 is a sentinel, not a real tenant) — exactly what produced the undefined → DEFAULT insert.

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 the attendanceCleanupRules permission, or admin. Combined with runNow being a custom /action route 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:

  • crudAuth is now the standard CrudAuthFilter (admin sees all, everyone else sees only their own rows) — same as most other entities. The attendanceCleanupRules permission is now a client-side visibility gate only; it grants no server-side data access.
  • runNow resolves its ids via the new BaseEntityService.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.
  • Still enqueues one job per distinct owner when an admin's selection spans multiple tenants.

Testing

  • Reproduced the exact production error against a real MySQL jobs table: enqueue(undefined, ...) throws Field 'user_id' doesn't have a default value; enqueue(38, ...) succeeds.
  • Fresh clone + submodule bump to nra-server's getManyByIds: tsc --noEmit clean, yarn test — 114 suites / 1008 tests passed
  • New tests: a non-owned id resolves to nothing (mocked at the getManyByIds boundary — the underlying auth-filtering itself is covered directly in nra-server's base-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

… 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
Copilot AI lite review requested due to automatic review settings August 23, 2026 19:57

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.

🟡 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.find always returns its fixture regardless of arguments, and expect.anything() accepts any In(...) value. The test therefore still passes if this code queries all rules instead of only the selected IDs. Assert the FindOperator values (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.

claude added 2 commits August 23, 2026 20:07
…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
@buzz-code
buzz-code merged commit 4cd7685 into main Aug 23, 2026
4 checks passed
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.

3 participants