mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[GH-ISSUE #6935] [Bug] LDAP auto-delete detaches clients on the second tick after a partial directory fetch #4589
Labels
No labels
backlog
bug
clarification needed
documentation
duplicate
enhancement
github_actions
good first issue
invalid
javascript
New Feature
PR welcomed
pull-request
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
MHSanaei/3x-ui#4589
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @muditagrawal2007 on GitHub (Oct 25, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6935
Summary
The LDAP auto-delete safety guard protects only one cron tick. When a directory returns a partial or empty result, the guard refuses deletion, but the job then overwrites its trusted baseline with that untrusted count. The next tick compares against the lowered baseline and detaches clients that are still present in LDAP.
This is a correctness/data-loss bug rather than an authentication bypass: it requires LDAP auto-delete to be enabled and the directory to return an incomplete result.
Impact
clientsand traffic tables but are no longer attached to their inbound, so the sync does not self-heal them.Affected version
Reproduced against the latest release
v3.10.2and currentmain(de21c128).The relevant code is in
internal/web/job/ldap_sync_job.go:autoDeleteSafeForFetchcorrectly rejects the fetch, butlastFlagCount.Storeruns unconditionally. The field therefore changes from a trusted previous population to an untrusted rejected result.Failure timeline
The empty-result variant is worse: a zero baseline disables the retention comparison, so the next partial result is accepted immediately.
Reproduction
Preconditions:
Then change the directory result to a partial population and run the sync twice:
Proposed fix
Only a fetch accepted by the retention guard may advance the baseline:
Renaming
lastFlagCounttolastTrustedFlagCountmakes the invariant explicit: it stores a population that passed the safety check, never a result that was rejected.This keeps auto-delete's safety-first behavior: clients stay attached while the directory population is implausibly small, until the directory recovers to the configured retention floor.
Regression tests
The accompanying tests exercise the real LDAP wire protocol, the real cron job, the real client services, and a real SQLite database:
TestLdapAutoDeleteKeepsBaselineAfterRefusedFetchTestLdapAutoDeleteKeepsBaselineAfterEmptyFetchBoth tests were confirmed red before the fix and green after it.
Verification
All commands pass with the fix.
@muditagrawal2007 commented on GitHub (Oct 25, 2026):
Implemented in #6936.\n\nThe fix keeps the retention baseline unchanged whenever
autoDeleteSafeForFetchrejects a directory result, and renames the field tolastTrustedFlagCountto make that invariant explicit. The PR adds protocol-level regression coverage for both partial and empty directory responses, and the fullmake verifygate passes locally.@github-actions[bot] commented on GitHub (Oct 25, 2026):
Confirmed bug, still present on the default branch. The auto-delete retention guard only protects one cron tick. The job's baseline is overwritten with the fetch the guard just rejected, so the next tick compares against that lowered number and detaches clients that are still in the directory. This was introduced together with the guard and ships in
v3.10.0andv3.10.2. @MHSanaeiWhat the code does
internal/web/job/ldap_sync_job.go,(*LdapSyncJob).Run:autoDeleteSafeForFetchreads that same field as its baseline:Its own warning calls the field the "previous successful sync" (
:183), and the constant's comment says a fetch must keep 50% of "the previous successful fetch" (:16-18). The intent is clear, and line 171 breaks it. WithldapAutoDeleteMinRetainPercent = 50, your example plays out as follows:400 < 500refuses the fetch, but 4 is stored anyway.400 < 200is false, so the fetch is accepted and the 6 missing clients go toBulkDetach(:313).The empty variant you describe is also right. Line 177 refuses
fetched == 0, but line 171 then stores 0. On the next tick theprevious > 0condition at line 182 is false, so any non-empty partial result is accepted with no retention comparison at all.The existing test
TestAutoDeleteAllowed(internal/web/job/ldap_auto_delete_guard_test.go) only checksautoDeleteSafeForFetchwith a hand-setprevious. It never runs two ticks throughRun, so it cannot catch this. I could not see the regression tests you mention because they are not in this repository, so I have not verified them.Verdict: Confirmed bug
Severity: High (clients lose inbound access through an opt-in feature (
ldapAutoDelete) after a directory hiccup; the rows survive and can be re-attached)Confidence: High
Root cause:
internal/web/job/ldap_sync_job.go:171:Runcallsj.lastFlagCount.Store(int64(len(flags)))unconditionally, so a fetch the guard refused (autoDeleteSafeForFetch,:176-188) becomes the next tick's baseline. Introduced inf4b7b08e"fix(ldap): stop auto-delete from wiping every client on an empty directory".Same class: Same invariant ("only a trusted fetch may set the baseline"), different trigger:
lastFlagCountis an in-memoryatomic.Int64(:25) on a job built fresh byjob.NewLdapSyncJob()(internal/web/web.go:396). After every panel restart the baseline is 0, so the first post-restart tick accepts any non-empty partial fetch (:182previous > 0short-circuits). This is my inference from the code, and the proposed fix does not cover it.Already fixed: Still present on the default branch (
7f82daa4);f4b7b08eis inv3.10.0andv3.10.2Duplicate of: None
Evidence:
ldap_sync_job.go:162,:171,:177-179,:181-186,:16-18;ldap_auto_delete_guard_test.go:7-33;git tag --contains f4b7b08e→v3.10.0,v3.10.2Not determined: Whether the maintainer wants the baseline to persist across restarts. That is a design choice, not something the source answers.
Suggested fix: only a fetch that
autoDeleteSafeForFetchaccepted should update the baseline, so move theStoreinside the accepted branch, as the reporter proposes. When auto-delete is off, the job can still record the count. One trade-off to decide on: a real drop of more than 50% in the directory would then block auto-delete until the panel restarts, because the baseline would never come down. That case may need a deliberate override path.This reply was generated automatically; a maintainer may follow up.