[PR #6936] [MERGED] fix(ldap): keep trusted baseline after rejected fetch #6531

Closed
opened 2026-11-02 04:09:26 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6936
Author: @muditagrawal2007
Created: 10/25/2026
Status: ✅ Merged
Merged: 11/2/2026
Merged by: @MHSanaei

Base: main ← Head: fix/ldap-auto-delete-safe-baseline


📝 Commits (2)

  • ac7663a fix(ldap): keep trusted baseline after rejected fetch
  • 169f465 docs(ldap): explain the empty-fetch guard

📊 Changes

3 files changed (+277 additions, -12 deletions)

View changed files

➕ internal/web/job/ldap_auto_delete_baseline_test.go (+263 -0)
📝 internal/web/job/ldap_auto_delete_guard_test.go (+1 -1)
📝 internal/web/job/ldap_sync_job.go (+13 -11)

📄 Description

Fixes #6935

Summary

The LDAP auto-delete retention guard currently protects only one cron tick. A rejected partial directory result is still stored as the next trusted population, so the following tick can detach clients that are still present in LDAP.

This PR keeps the baseline unchanged whenever the safety guard rejects a fetch.

Root cause

The guard and the baseline update were separated:

if autoDelete && j.autoDeleteSafeForFetch(len(flags)) {
        // ... detach clients absent from flags ...
}
j.lastFlagCount.Store(int64(len(flags)))

lastFlagCount was updated even when autoDeleteSafeForFetch returned false.

flowchart LR
    A["Trusted baseline: 10"] --> B["Partial fetch: 4"]
    B --> C["Guard rejects 4"]
    C --> D["Bug stores 4 as trusted"]
    D --> E["Next identical fetch passes guard"]
    E --> F["6 clients detached"]

The fix

  • Return before advancing the baseline when auto-delete rejects a directory result.
  • Rename lastFlagCount to lastTrustedFlagCount so the invariant is explicit.
  • Preserve the existing behavior when auto-delete is disabled.
  • Keep the safety-first semantics: an implausibly small directory population leaves clients attached until the directory recovers.
flowchart LR
    A["Trusted baseline: 10"] --> B["Partial fetch: 4"]
    B --> C["Guard rejects 4"]
    C --> D["Baseline stays 10"]
    D --> E["Next identical fetch is rejected too"]

Policy note

The retention guard already deliberately refuses an implausible directory collapse. This PR makes that policy consistent instead of weakening it on the next tick. A genuine offboarding of more than half the directory therefore remains blocked by design; an operator can accept a legitimate large offboarding by temporarily disabling auto-delete for one successful sync (which advances the baseline), then re-enabling it. No new override switch is introduced because it would weaken the existing safety boundary.

Regression coverage

internal/web/job/ldap_auto_delete_baseline_test.go adds two end-to-end job tests:

  • TestLdapAutoDeleteKeepsBaselineAfterRefusedFetch
  • TestLdapAutoDeleteKeepsBaselineAfterEmptyFetch

The tests use:

  • a real LDAP server speaking the LDAP wire protocol;
  • the real LdapSyncJob.Run path;
  • the real client/inbound services;
  • a real SQLite database.

They assert the exact set of clients still attached to the inbound after each tick.

Red before the fix

Fetched 10 LDAP flags
Fetched 4 LDAP flags
LDAP auto-delete skipped: fetched 4 users, previous successful sync saw 10 (below 50% retention)
Fetched 4 LDAP flags
Deleted client u4@ldap from inbound id=1(tag=ldap-baseline-in)
Deleted client u5@ldap from inbound id=1(tag=ldap-baseline-in)
Deleted client u6@ldap from inbound id=1(tag=ldap-baseline-in)
Deleted client u7@ldap from inbound id=1(tag=ldap-baseline-in)
Deleted client u8@ldap from inbound id=1(tag=ldap-baseline-in)
Deleted client u9@ldap from inbound id=1(tag=ldap-baseline-in)
Xray restart scheduled after batch deletion

Green after the fix

Fetched 10 LDAP flags
Fetched 4 LDAP flags
LDAP auto-delete skipped: fetched 4 users, previous trusted sync saw 10 (below 50% retention)
Fetched 4 LDAP flags
LDAP auto-delete skipped: fetched 4 users, previous trusted sync saw 10 (below 50% retention)

Verification

go test ./internal/web/job -run 'TestLdapAutoDeleteKeepsBaseline|TestAutoDeleteAllowed' -count=1
go test ./internal/web/job -count=1
go test -race ./internal/web/job -run 'TestLdapAutoDeleteKeepsBaseline|TestAutoDeleteAllowed' -count=1
make verify

All commands pass. make verify includes generated-file freshness, Go and frontend lint, formatting, typecheck, all Go tests, all 1,978 frontend tests, the production build, and the Storybook build.

No database migration, API, or frontend change is required.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6936 **Author:** [@muditagrawal2007](https://github.com/muditagrawal2007) **Created:** 10/25/2026 **Status:** ✅ Merged **Merged:** 11/2/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/ldap-auto-delete-safe-baseline` --- ### 📝 Commits (2) - [`ac7663a`](https://github.com/MHSanaei/3x-ui/commit/ac7663a6d7836c129384596d14f9b629d48fc21b) fix(ldap): keep trusted baseline after rejected fetch - [`169f465`](https://github.com/MHSanaei/3x-ui/commit/169f46593f0b349c1a41dd19b06e3df7bab74353) docs(ldap): explain the empty-fetch guard ### 📊 Changes **3 files changed** (+277 additions, -12 deletions) <details> <summary>View changed files</summary> ➕ `internal/web/job/ldap_auto_delete_baseline_test.go` (+263 -0) 📝 `internal/web/job/ldap_auto_delete_guard_test.go` (+1 -1) 📝 `internal/web/job/ldap_sync_job.go` (+13 -11) </details> ### 📄 Description Fixes #6935 ## Summary The LDAP auto-delete retention guard currently protects only one cron tick. A rejected partial directory result is still stored as the next trusted population, so the following tick can detach clients that are still present in LDAP. This PR keeps the baseline unchanged whenever the safety guard rejects a fetch. ## Root cause The guard and the baseline update were separated: ```go if autoDelete && j.autoDeleteSafeForFetch(len(flags)) { // ... detach clients absent from flags ... } j.lastFlagCount.Store(int64(len(flags))) ``` `lastFlagCount` was updated even when `autoDeleteSafeForFetch` returned `false`. ```mermaid flowchart LR A["Trusted baseline: 10"] --> B["Partial fetch: 4"] B --> C["Guard rejects 4"] C --> D["Bug stores 4 as trusted"] D --> E["Next identical fetch passes guard"] E --> F["6 clients detached"] ``` ## The fix - Return before advancing the baseline when auto-delete rejects a directory result. - Rename `lastFlagCount` to `lastTrustedFlagCount` so the invariant is explicit. - Preserve the existing behavior when auto-delete is disabled. - Keep the safety-first semantics: an implausibly small directory population leaves clients attached until the directory recovers. ```mermaid flowchart LR A["Trusted baseline: 10"] --> B["Partial fetch: 4"] B --> C["Guard rejects 4"] C --> D["Baseline stays 10"] D --> E["Next identical fetch is rejected too"] ``` ## Policy note The retention guard already deliberately refuses an implausible directory collapse. This PR makes that policy consistent instead of weakening it on the next tick. A genuine offboarding of more than half the directory therefore remains blocked by design; an operator can accept a legitimate large offboarding by temporarily disabling auto-delete for one successful sync (which advances the baseline), then re-enabling it. No new override switch is introduced because it would weaken the existing safety boundary. ## Regression coverage `internal/web/job/ldap_auto_delete_baseline_test.go` adds two end-to-end job tests: - `TestLdapAutoDeleteKeepsBaselineAfterRefusedFetch` - `TestLdapAutoDeleteKeepsBaselineAfterEmptyFetch` The tests use: - a real LDAP server speaking the LDAP wire protocol; - the real `LdapSyncJob.Run` path; - the real client/inbound services; - a real SQLite database. They assert the exact set of clients still attached to the inbound after each tick. ### Red before the fix ```text Fetched 10 LDAP flags Fetched 4 LDAP flags LDAP auto-delete skipped: fetched 4 users, previous successful sync saw 10 (below 50% retention) Fetched 4 LDAP flags Deleted client u4@ldap from inbound id=1(tag=ldap-baseline-in) Deleted client u5@ldap from inbound id=1(tag=ldap-baseline-in) Deleted client u6@ldap from inbound id=1(tag=ldap-baseline-in) Deleted client u7@ldap from inbound id=1(tag=ldap-baseline-in) Deleted client u8@ldap from inbound id=1(tag=ldap-baseline-in) Deleted client u9@ldap from inbound id=1(tag=ldap-baseline-in) Xray restart scheduled after batch deletion ``` ### Green after the fix ```text Fetched 10 LDAP flags Fetched 4 LDAP flags LDAP auto-delete skipped: fetched 4 users, previous trusted sync saw 10 (below 50% retention) Fetched 4 LDAP flags LDAP auto-delete skipped: fetched 4 users, previous trusted sync saw 10 (below 50% retention) ``` ## Verification ```text go test ./internal/web/job -run 'TestLdapAutoDeleteKeepsBaseline|TestAutoDeleteAllowed' -count=1 go test ./internal/web/job -count=1 go test -race ./internal/web/job -run 'TestLdapAutoDeleteKeepsBaseline|TestAutoDeleteAllowed' -count=1 make verify ``` All commands pass. `make verify` includes generated-file freshness, Go and frontend lint, formatting, typecheck, all Go tests, all 1,978 frontend tests, the production build, and the Storybook build. No database migration, API, or frontend change is required. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi changed title from [PR #6936] fix(ldap): keep trusted baseline after rejected fetch to [PR #6936] [MERGED] fix(ldap): keep trusted baseline after rejected fetch 2026-11-07 14:33:50 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6531
No description provided.