Skip to content

fix(users): enforce role limits on bulk quota and expiry updates - #1005

Merged
T3ST3ST3R0N merged 1 commit into
PasarGuard:devfrom
dr-hoseyn:codex/fix-bulk-user-role-limits
Oct 12, 2026
Merged

T3ST3ST3R0N merged 1 commit into
PasarGuard:devfrom
dr-hoseyn:codex/fix-bulk-user-role-limits

Conversation

@dr-hoseyn

@dr-hoseyn dr-hoseyn commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Bulk quota and expiry adjustments currently discard the authenticated admin, allowing non-owner roles with users.update=ALL to bypass caps enforced by individual updates. Pass the actor through the API and Telegram handlers and check manual-write access and effective role/per-admin limits for every final value before writing. Dry runs enforce the same rules.

For constrained roles, read only the target IDs and changed values, lock the rows before writes, and apply the update to that validated selection. Recheck final-value bounds in SQL and roll back the entire batch with HTTP 409 if a target changed and no longer meets the guards. Owners and roles without relevant bounds retain the existing bulk SQL path.

Quota previews, updates, and status reloads now share the same target predicate, including the existing subtraction rule that skips nonpositive resulting quotas. Unlimited values remain unchanged. Expiry validation and write bounds use one reference time for the batch.

Type of change

  • Bug fix

Testing

  • 121 local tests passed on fresh SQLite databases: API bulk operations, permission/RBAC regressions, Telegram bulk handlers, and 29 dedicated batch-limit regressions.
  • Reproduced both API bypasses and verified quota/expiry max and min limits, valid additions/subtractions, dry runs, template-only roles, OWN-scope rejection, overrides, unchanged unlimited values and unrelated limits.
  • Injected target changes before both conditional updates and verified full rollback, including otherwise-valid rows.
  • Ruff lint/format and git diff --check passed.

Notes for reviewers

Four production Python files change. No test files, documentation files, migrations or dependencies are included. The existing success response and node sync for changed statuses are preserved.

Integration validation of the five open audit PRs on current dev: all patches applied together without conflicts, and 400 local tests passed in isolated fresh SQLite runs. Ruff passed on all 11 touched production files.

Summary by CodeRabbit

  • Bug Fixes
    • Bulk expiration and data-limit updates now apply only to eligible users and enforce configured value bounds.
    • Updates require appropriate access, and conflicts are reported instead of silently applying to an unexpected number of users.
    • Dry-run counts reflect the users whose values pass validation.

@coderabbitai

coderabbitai Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f815d54f-a516-4952-b949-cadf68b75b68

📥 Commits

Reviewing files that changed from the base of the PR and between 0c9b1e5 and 4536476.


📒 Files selected for processing (4)
  • app/db/crud/bulk.py
  • app/operation/user.py
  • app/routers/user.py
  • app/telegram/handlers/admin/bulk_actions.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.



Walkthrough

Bulk expiration and data-limit updates now share target eligibility rules, validate adjusted values against role limits, and apply guarded updates. API routes and Telegram handlers pass the acting admin to these operations.

Changes

Bulk user limit updates

Layer / File(s) Summary
Shared target selection and guarded updates
app/db/crud/bulk.py
Shared filters select eligible expiration and data-limit targets. Updates accept explicit target IDs and final-value bounds, and roll back if an explicit target set is not fully updated.
Role-bound validation and bulk operations
app/operation/user.py
Bulk operations enforce access requirements and validate selected targets against role limits. Dry runs count validated targets when constraints apply, and update conflicts are translated to HTTP 409 errors.
API and Telegram admin wiring
app/routers/user.py, app/telegram/handlers/admin/bulk_actions.py
API routes and Telegram handlers pass the acting admin to the bulk expiration and data-limit operations.

Priority: ➖ Normal

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant Admin
  participant UserRouter
  participant UserOperation
  participant BulkCRUD
  Admin->>UserRouter: Submit bulk expiration request
  UserRouter->>UserOperation: Pass session, request, and admin
  UserOperation->>BulkCRUD: Select and lock eligible targets
  BulkCRUD-->>UserOperation: Return target IDs and current values
  UserOperation->>UserOperation: Validate adjusted values against role limits
  UserOperation->>BulkCRUD: Update targets within final-value bounds
  BulkCRUD-->>UserOperation: Return update result or conflict
Loading

Suggested reviewers: x0sina

Merge Risk: ⚪ Minimal · up to 45364

Bulk quota and expiry updates now apply the same role limits as individual updates, and the API and Telegram handlers pass the acting admin through. No merge-blocking issue was found.

Pre-merge checks | Passed 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 clearly and concisely describes the main change: enforcing role limits on bulk quota and expiry updates.
Docstring Coverage Passed Docstring coverage is 94.74% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 19 functions across 4 files.
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.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

A rabbit checks the limits with care,
Then picks the targets fit to share.
The guarded updates take their turn,
While bounds keep every change in burn.
Admins pass the keys along,
And bunnies thump a happy song.

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

@dr-hoseyn

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@T3ST3ST3R0N
T3ST3ST3R0N merged commit ab45a61 into PasarGuard:dev Oct 12, 2026
10 checks passed
Free-Guy-IR added a commit to Free-Guy-IR/panel that referenced this pull request Nov 2, 2026
…am up to 2026-10-17

Owner rule of 2026-10-14: upstream changes are ported only from final
upstream releases. Checked with git ls-remote on 2026-10-17 20:53 UTC;
no new release since panel v5.4.1, node v0.5.4 and node_bridge_py v0.9.2
(GitHub releases, PyPI and npm also unchanged).

Panel: the 79 Port queue entries are all dev-only, so they move to the
new "Held for an upstream release" section with their security
classification and without deadlines, and the queue is empty. Upstream
dev moved to a2fb313 with 25 commits: PasarGuard#1019 is waived (the fork deleted
the notifier workflow), the other 24 are held, three of them classified
security: yes (PasarGuard#1004 role permissions on custom API keys, PasarGuard#1005 role
limits on bulk updates, PasarGuard#1008 role disconnect policy). New branches
feat/live-core-apply and two masque commits are UNMERGED-WIP;
fix/xray-wg-allocation-view is the same patch as held ba27c1cc6.

Node: five earlier dev-only picks are on fork main 0071e2c with trailers
and leave the queue; the other eight queue entries and four new dev
commits (PasarGuard#58, PasarGuard#55, PasarGuard#104, PasarGuard#106) are held, with f72ac0ef (PasarGuard#99) and a3d2d2fe
(PasarGuard#101) marked as fixed by the fork's own fixes per the 2026-10-14
decisions. PasarGuard#100 (refuse to start without a valid API_KEY) is ported by
design: fork commit 88162e2 already fails fast.

node_bridge_py: 27be206 is ported on fork main 6116922; 22f1ef79 and the
v0.9.2 bump bec9cb22 are recorded as ported by design (the one flagged
line belongs to the fork's own add_backend, and the fork labels itself
0.9.2+singbox); upstream main's new PasarGuard#24 (9fa9f264) is held.

scripts: fork main 51edabb has upstream main bba95f274 as an ancestor, so
its thirteen queue lines are ported (decision 7 keeps them).

Against live upstream the panel gate is green (282 commits, 0 unported,
103 held, 0 owed) and every fork tracker is green.
Free-Guy-IR pushed a commit to Free-Guy-IR/panel that referenced this pull request Nov 3, 2026
…arGuard#1005)

Fork resolution:
- app/db/crud/bulk.py: the limit targets are read and locked with the
  fork's lock shape (SELECT ... ORDER BY users.id FOR UPDATE, the same
  as _lock_bulk_targets), keep the fork's allowed_group_ids filter, and
  the write goes through the fork's _lock_bulk_targets /
  _apply_to_locked_targets chunked path. With validated target_ids the
  re-lock is per id chunk in ascending order and a vanished target, or a
  chunk whose guarded UPDATE matches fewer rows (bounds re-checked in
  SQL), rolls back the whole batch with BulkUserUpdateConflict (409).
- app/operation/user.py: bulk_modify_expire/datalimit now require the
  admin (every caller already passed it) and keep get_allowed_group_ids.
- app/routers/user.py and app/telegram/handlers/admin/bulk_actions.py
  already passed the admin in the fork; the fork Telegram handler is
  kept unchanged.
Tests: tests/api/test_bulk_role_limits.py (the RED commit before this pick).

(cherry picked from commit ab45a61)
Free-Guy-IR added a commit to Free-Guy-IR/panel that referenced this pull request Nov 10, 2026
…asarGuard#1005 in the sync ledger

Both port-queue entries said nothing about a port while their cherry-pick -x
carriers were already on main: 8527b40 for 4574d0c (PasarGuard#1000) and
9ea80e0 for ab45a61 (PasarGuard#1005), each with the RED test it landed with.
They become PARTIAL entries naming the carrier, the measured reflected-hunk
counts (1 of 5 and 8 of 20) and why the remainder is unreflected: upstream's
load_group_inbounds needs 0aad1b7 (PasarGuard#967), still owed, and the rest is the
fork's own lock and chunk shape standing in for upstream's.
Free-Guy-IR added a commit to Free-Guy-IR/panel that referenced this pull request Nov 22, 2026
The PasarGuard#1005 row said the two overflow-window cases fail "on MySQL error
1441, a 500". The run shows the bulk UPDATE failing with MySQL error 1441
on MySQL and no BulkUserResultOutOfRange on SQLite; the HTTP status was
never measured, so the row no longer names one.
Free-Guy-IR added a commit to Free-Guy-IR/panel that referenced this pull request Nov 22, 2026
…#1005 row

The row said the fork's overflow guard still answers 422 after 0593332
moved it before upstream's final-value expression. fb4e010 now tests
that race on SQLite and MySQL, and tests/api/test_bulk_role_limits.py has
five tests, of which four fail with _prepare_bulk_limit_update emptied.
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.

2 participants