Skip to content

fix: validate implicit default role grants - #27717

Merged
XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
Lundomn:fix/issue-27651
Aug 27, 2026
Merged

XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
Lundomn:fix/issue-27651

Conversation

@Lundomn

@Lundomn Lundomn commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

close #27651

What this PR does / why we need it:

Implicit login trusted mo_user.default_role without confirming that the role was still granted to the user. After revoking the default role, a new implicit session could therefore activate that role and retain its privileges, while explicit-role login correctly rejected it.

This change:

  • validates an implicit default role against both mo_role and mo_user_grant before activation;
  • falls back to a valid public grant when the stored default is stale, NULL, invalid, or missing, and fails closed when no valid fallback exists;
  • preserves explicit-role validation, existing-session behavior, and default-role recovery after regrant;
  • adds focused unit coverage for valid, revoked, regranted, NULL, missing, malformed, out-of-range, and query-error paths;
  • adds BVT coverage for explicit and implicit role behavior, new and existing sessions, protected privileges, secondary-role revoke control, and regrant recovery.

Local verification:

  • go test ./pkg/frontend/... -count=1
  • go test -race ./pkg/frontend/... -count=1
  • go vet ./pkg/frontend/...
  • changed functions coverage: 100%; frontend package coverage: 63.7%
  • revoked_default_role_login BVT: 39/39 passed
  • local 1 Log / 1 TN / 1 CN protocol E2E with privilege cache enabled and disabled
  • make config
  • make err-check
  • make build-with-prebuilt-native
  • git diff --check

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label Aug 27, 2026
@mergify mergify Bot added the kind/bug Something isn't working label Aug 27, 2026
@Lundomn
Lundomn marked this pull request as draft August 27, 2026 03:41
@Lundomn
Lundomn marked this pull request as ready for review August 27, 2026 05:48
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep-reviewed exact head 0d300397c358efcaad112d215730782a1557deb1.

The fix closes #27651 at the correct trust boundary: an implicit login no longer activates mo_user.default_role metadata directly; it requires a matching mo_role row and mo_user_grant membership in the same lookup. A stale/NULL/invalid default can fall back only to role ID 1 when both the user's public grant and the canonical public role name are present; missing rows, malformed values, and query failures fail closed. Explicit-role validation remains unchanged, existing-session semantics are intentionally unchanged, and retaining the stored default allows a later regrant to recover it.

Performance is acceptable: the normal implicit-login path still performs one role lookup, now an indexed join on mo_role.role_id and mo_user_grant(role_id,user_id); only the revoked/stale path performs the second public lookup. There is no per-statement cost or unbounded state.

Unhappy paths are covered at helper and protocol/BVT levels: revoked/missing/NULL/malformed/out-of-range defaults, invalid public fallback, catalog failures, non-default revocation control, protected privilege denial, existing sessions, and regrant recovery. git diff --check passes and exact-head Ubuntu UT, SCA, coverage, Proxy BVT, and pessimistic BVT are green. No blocker found.

@mergify mergify Bot added the queued label Aug 27, 2026
@mergify

mergify Bot commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-27 07:56 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-08-27 08:21 UTC · at a5547a12837e23cbaff027d04829b7d06088253a

This pull request spent 24 minutes 54 seconds in the queue, with no time running CI.

Waiting for
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
    • check-success = Matrixone CI / SCA Test on Linux/arm64
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-success = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
All conditions
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
    • check-success = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-success = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]

Reason

Pull request #27717 has been dequeued

Pull request from fork cannot be queued. This pull request comes from a fork, and Mergify needs the author's permission to update its branch.

The author needs to enable "Allow edits from maintainers" on this pull request.

Failing checks:

Hint

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.
If you do update this pull request, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

@XuPeng-SH
XuPeng-SH merged commit 424fe3d into matrixorigin:main Aug 27, 2026
25 of 26 checks passed
@mergify mergify Bot added dequeued and removed queued labels Aug 27, 2026
XuPeng-SH added a commit that referenced this pull request Aug 28, 2026
)

## What changed

This PR makes normal-user authentication establish one fresh catalog
snapshot
before reading account, user, password, lock, role, grant,
system-variable, or
login-database state.

- Capture the serving CN's HLC uncertainty upper bound and install the
smallest
  timestamp that dominates every HLC value at that bound.
- Wait for the local logtail through the effective monotonic session
minimum
  **before** constructing the authentication executor or entering user
  transaction admission. `TxnClient.New` then rechecks the same minimum.
- Preserve a larger caller minimum in both transaction freshness modes.
- Keep `max-clock-offset` as the ordering bound even when active local
clock
  monitoring is disabled, in process and embedded launchers.
- Make the handshake request context the single timeout owner of
authentication
freshness waiting and transaction creation. Ordinary transactions retain
  `CreateTxnOpTimeout`.
- Reject authentication-enabled CN configurations that cannot cover the
pairwise clock geometry: `connectTimeout > 2 * max-clock-offset + 1ns`,
with
checked arithmetic. `skipCheckUser=true` bypasses only this unreachable
authentication-specific budget check; general clock validation still
runs.
- Revalidate public embedded `Adjust`/pre-start mutations before
creating
resources. The operator's immutable service type owns validation, and a
callback cannot relabel a CN as a TN to bypass the authentication
budget.
- Let the built-in timestamp waiter observe transaction-client closure
directly,
avoiding a derived context and `context.AfterFunc` allocation per login
while
retaining the compatibility fallback for legacy waiter implementations.

The complete design and validation map are in

[`docs/design/authentication_catalog_freshness.md`](docs/design/authentication_catalog_freshness.md).

## Root cause

With sacrificing freshness enabled, a new session has no prior commit
timestamp,
so authentication could start from the latest logtail already applied by
its
local CN. A security mutation could already have committed and returned
on
another CN while this CN still read the previous catalog generation.

The observed failure was `REVOKE` followed by an immediate `GRANT` of
the stored
default role. A new connection routed to a lagging CN validated the old
revoked
generation, fell back to `public`, and denied a table read that the
regranted
role permits. The same stale-snapshot shape applies to account state,
user/password and lock state, explicit role grants, and login database
resolution, so the repair belongs at the authentication snapshot
boundary.

## Correctness and unhappy paths

- The login linearization point is the HLC uncertainty-bound capture
under the
  existing cluster clock contract.
- The pre-admission wait must report a watermark at or beyond the
effective
session minimum; missing runtime/clock/transaction client, negative
offset,
timestamp overflow, wait failure, cancellation, client close, and
regressing
  watermark all fail closed.
- A failed pre-admission wait creates no transaction operator. The
timestamp
waiter owns and removes its entry on success, cancellation,
notification, or
close. Later transaction-creation failure retains the existing abort
owner.
- Bootstrap-only special users and `skipCheckUser` retain their existing
  catalog-free paths.
- Embedded post-parse mutations are revalidated before resource
creation;
invalid budgets and service-identity mismatches leave no cleanup
obligation.
- One waiter exists per in-flight normal login, bounded by the handshake
  deadline and connection count. No new goroutine, RPC, persisted state,
  collection, or rolling-upgrade dependency is introduced.

This repairs new-login catalog freshness. Existing authenticated-session
role
revocation remains the separate lifecycle handled by PR #27737.

## Performance

- The default ordinary-SQL transaction hot path is unchanged.
- The unavoidable uncertainty/logtail wait occurs before transaction
admission,
so it no longer consumes a `max-active` user-transaction slot.
Connection
  pooling amortizes this connection-establishment cost for TP workloads.
- A normal login adds one local HLC read and reuses the existing
timestamp
  waiter; it adds no cross-CN RPC or goroutine.
- The built-in waiter close fast path removes one derived context and
  `context.AfterFunc` allocation from each authentication wait.
- The non-default freshness-preserving branch adds one timestamp
comparison and
  no allocation, lock, or I/O.
- Embedded revalidation is startup-only and occurs before resource
allocation;
  it has no query-path cost.

## Validation

Candidate head: `9a646c2a33` on main `745d8364c5` (Go 1.26.4,
`-mod=readonly`, repository CGo/native libraries).

- Focused config/authentication/waiter tests passed, including
`skipCheckUser`,
  monotonic existing minimum, missing dependencies, downstream failure,
  watermark regression, executor ordering, cancellation, client close,
  post-parse invalid budget, and mutable-service-type bypass.
- Full normal suites passed for `pkg/config`, `cmd/mo-service`,
`pkg/embed`,
  `pkg/frontend`, `pkg/txn/client`, and `pkg/txn/clock`.
- Full race suites passed for `pkg/embed`, `pkg/frontend`, and
`pkg/txn/client`.
- The cancellation/close/snapshot barrier tests passed 100 repetitions
under
  the race detector; they use no sleeps, polling, retries, or weakened
  assertions.
- `go vet` passed for every changed Go package; formatting, module-file,
and
  diff checks passed.
- Exact-head `make build` passed with `CommitID=9a646c2a33`.
- The existing public revoke/regrant semantics were exercised through
Proxy on
one LogService, one TN, and two CNs. New sessions immediately observed
revoke
fallback and regrant on alternating CN connections; a deliberately
retained
  old session kept its old role, matching the separate #27737 scope.

Incident evidence:

https://github.com/matrixorigin/matrixone/actions/runs/33067372509/job/98502820591?pr=27758

Related: #27717, #27737, #27758
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

close matrixorigin#27651

## What this PR does / why we need it:

Implicit login trusted `mo_user.default_role` without confirming that
the role was still granted to the user. After revoking the default role,
a new implicit session could therefore activate that role and retain its
privileges, while explicit-role login correctly rejected it.

This change:

- validates an implicit default role against both `mo_role` and
`mo_user_grant` before activation;
- falls back to a valid `public` grant when the stored default is stale,
NULL, invalid, or missing, and fails closed when no valid fallback
exists;
- preserves explicit-role validation, existing-session behavior, and
default-role recovery after regrant;
- adds focused unit coverage for valid, revoked, regranted, NULL,
missing, malformed, out-of-range, and query-error paths;
- adds BVT coverage for explicit and implicit role behavior, new and
existing sessions, protected privileges, secondary-role revoke control,
and regrant recovery.

Local verification:

- `go test ./pkg/frontend/... -count=1`
- `go test -race ./pkg/frontend/... -count=1`
- `go vet ./pkg/frontend/...`
- changed functions coverage: 100%; frontend package coverage: 63.7%
- revoked_default_role_login BVT: 39/39 passed
- local 1 Log / 1 TN / 1 CN protocol E2E with privilege cache enabled
and disabled
- `make config`
- `make err-check`
- `make build-with-prebuilt-native`
- `git diff --check`

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…rixorigin#27763)

## What changed

This PR makes normal-user authentication establish one fresh catalog
snapshot
before reading account, user, password, lock, role, grant,
system-variable, or
login-database state.

- Capture the serving CN's HLC uncertainty upper bound and install the
smallest
  timestamp that dominates every HLC value at that bound.
- Wait for the local logtail through the effective monotonic session
minimum
  **before** constructing the authentication executor or entering user
  transaction admission. `TxnClient.New` then rechecks the same minimum.
- Preserve a larger caller minimum in both transaction freshness modes.
- Keep `max-clock-offset` as the ordering bound even when active local
clock
  monitoring is disabled, in process and embedded launchers.
- Make the handshake request context the single timeout owner of
authentication
freshness waiting and transaction creation. Ordinary transactions retain
  `CreateTxnOpTimeout`.
- Reject authentication-enabled CN configurations that cannot cover the
pairwise clock geometry: `connectTimeout > 2 * max-clock-offset + 1ns`,
with
checked arithmetic. `skipCheckUser=true` bypasses only this unreachable
authentication-specific budget check; general clock validation still
runs.
- Revalidate public embedded `Adjust`/pre-start mutations before
creating
resources. The operator's immutable service type owns validation, and a
callback cannot relabel a CN as a TN to bypass the authentication
budget.
- Let the built-in timestamp waiter observe transaction-client closure
directly,
avoiding a derived context and `context.AfterFunc` allocation per login
while
retaining the compatibility fallback for legacy waiter implementations.

The complete design and validation map are in

[`docs/design/authentication_catalog_freshness.md`](docs/design/authentication_catalog_freshness.md).

## Root cause

With sacrificing freshness enabled, a new session has no prior commit
timestamp,
so authentication could start from the latest logtail already applied by
its
local CN. A security mutation could already have committed and returned
on
another CN while this CN still read the previous catalog generation.

The observed failure was `REVOKE` followed by an immediate `GRANT` of
the stored
default role. A new connection routed to a lagging CN validated the old
revoked
generation, fell back to `public`, and denied a table read that the
regranted
role permits. The same stale-snapshot shape applies to account state,
user/password and lock state, explicit role grants, and login database
resolution, so the repair belongs at the authentication snapshot
boundary.

## Correctness and unhappy paths

- The login linearization point is the HLC uncertainty-bound capture
under the
  existing cluster clock contract.
- The pre-admission wait must report a watermark at or beyond the
effective
session minimum; missing runtime/clock/transaction client, negative
offset,
timestamp overflow, wait failure, cancellation, client close, and
regressing
  watermark all fail closed.
- A failed pre-admission wait creates no transaction operator. The
timestamp
waiter owns and removes its entry on success, cancellation,
notification, or
close. Later transaction-creation failure retains the existing abort
owner.
- Bootstrap-only special users and `skipCheckUser` retain their existing
  catalog-free paths.
- Embedded post-parse mutations are revalidated before resource
creation;
invalid budgets and service-identity mismatches leave no cleanup
obligation.
- One waiter exists per in-flight normal login, bounded by the handshake
  deadline and connection count. No new goroutine, RPC, persisted state,
  collection, or rolling-upgrade dependency is introduced.

This repairs new-login catalog freshness. Existing authenticated-session
role
revocation remains the separate lifecycle handled by PR matrixorigin#27737.

## Performance

- The default ordinary-SQL transaction hot path is unchanged.
- The unavoidable uncertainty/logtail wait occurs before transaction
admission,
so it no longer consumes a `max-active` user-transaction slot.
Connection
  pooling amortizes this connection-establishment cost for TP workloads.
- A normal login adds one local HLC read and reuses the existing
timestamp
  waiter; it adds no cross-CN RPC or goroutine.
- The built-in waiter close fast path removes one derived context and
  `context.AfterFunc` allocation from each authentication wait.
- The non-default freshness-preserving branch adds one timestamp
comparison and
  no allocation, lock, or I/O.
- Embedded revalidation is startup-only and occurs before resource
allocation;
  it has no query-path cost.

## Validation

Candidate head: `9a646c2a33` on main `745d8364c5` (Go 1.26.4,
`-mod=readonly`, repository CGo/native libraries).

- Focused config/authentication/waiter tests passed, including
`skipCheckUser`,
  monotonic existing minimum, missing dependencies, downstream failure,
  watermark regression, executor ordering, cancellation, client close,
  post-parse invalid budget, and mutable-service-type bypass.
- Full normal suites passed for `pkg/config`, `cmd/mo-service`,
`pkg/embed`,
  `pkg/frontend`, `pkg/txn/client`, and `pkg/txn/clock`.
- Full race suites passed for `pkg/embed`, `pkg/frontend`, and
`pkg/txn/client`.
- The cancellation/close/snapshot barrier tests passed 100 repetitions
under
  the race detector; they use no sleeps, polling, retries, or weakened
  assertions.
- `go vet` passed for every changed Go package; formatting, module-file,
and
  diff checks passed.
- Exact-head `make build` passed with `CommitID=9a646c2a33`.
- The existing public revoke/regrant semantics were exercised through
Proxy on
one LogService, one TN, and two CNs. New sessions immediately observed
revoke
fallback and regrant on alternating CN connections; a deliberately
retained
  old session kept its old role, matching the separate matrixorigin#27737 scope.

Incident evidence:

https://github.com/matrixorigin/matrixone/actions/runs/33067372509/job/98502820591?pr=27758

Related: matrixorigin#27717, matrixorigin#27737, matrixorigin#27758
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

close matrixorigin#27651

## What this PR does / why we need it:

Implicit login trusted `mo_user.default_role` without confirming that
the role was still granted to the user. After revoking the default role,
a new implicit session could therefore activate that role and retain its
privileges, while explicit-role login correctly rejected it.

This change:

- validates an implicit default role against both `mo_role` and
`mo_user_grant` before activation;
- falls back to a valid `public` grant when the stored default is stale,
NULL, invalid, or missing, and fails closed when no valid fallback
exists;
- preserves explicit-role validation, existing-session behavior, and
default-role recovery after regrant;
- adds focused unit coverage for valid, revoked, regranted, NULL,
missing, malformed, out-of-range, and query-error paths;
- adds BVT coverage for explicit and implicit role behavior, new and
existing sessions, protected privileges, secondary-role revoke control,
and regrant recovery.

Local verification:

- `go test ./pkg/frontend/... -count=1`
- `go test -race ./pkg/frontend/... -count=1`
- `go vet ./pkg/frontend/...`
- changed functions coverage: 100%; frontend package coverage: 63.7%
- revoked_default_role_login BVT: 39/39 passed
- local 1 Log / 1 TN / 1 CN protocol E2E with privilege cache enabled
and disabled
- `make config`
- `make err-check`
- `make build-with-prebuilt-native`
- `git diff --check`

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…rixorigin#27763)

## What changed

This PR makes normal-user authentication establish one fresh catalog
snapshot
before reading account, user, password, lock, role, grant,
system-variable, or
login-database state.

- Capture the serving CN's HLC uncertainty upper bound and install the
smallest
  timestamp that dominates every HLC value at that bound.
- Wait for the local logtail through the effective monotonic session
minimum
  **before** constructing the authentication executor or entering user
  transaction admission. `TxnClient.New` then rechecks the same minimum.
- Preserve a larger caller minimum in both transaction freshness modes.
- Keep `max-clock-offset` as the ordering bound even when active local
clock
  monitoring is disabled, in process and embedded launchers.
- Make the handshake request context the single timeout owner of
authentication
freshness waiting and transaction creation. Ordinary transactions retain
  `CreateTxnOpTimeout`.
- Reject authentication-enabled CN configurations that cannot cover the
pairwise clock geometry: `connectTimeout > 2 * max-clock-offset + 1ns`,
with
checked arithmetic. `skipCheckUser=true` bypasses only this unreachable
authentication-specific budget check; general clock validation still
runs.
- Revalidate public embedded `Adjust`/pre-start mutations before
creating
resources. The operator's immutable service type owns validation, and a
callback cannot relabel a CN as a TN to bypass the authentication
budget.
- Let the built-in timestamp waiter observe transaction-client closure
directly,
avoiding a derived context and `context.AfterFunc` allocation per login
while
retaining the compatibility fallback for legacy waiter implementations.

The complete design and validation map are in

[`docs/design/authentication_catalog_freshness.md`](docs/design/authentication_catalog_freshness.md).

## Root cause

With sacrificing freshness enabled, a new session has no prior commit
timestamp,
so authentication could start from the latest logtail already applied by
its
local CN. A security mutation could already have committed and returned
on
another CN while this CN still read the previous catalog generation.

The observed failure was `REVOKE` followed by an immediate `GRANT` of
the stored
default role. A new connection routed to a lagging CN validated the old
revoked
generation, fell back to `public`, and denied a table read that the
regranted
role permits. The same stale-snapshot shape applies to account state,
user/password and lock state, explicit role grants, and login database
resolution, so the repair belongs at the authentication snapshot
boundary.

## Correctness and unhappy paths

- The login linearization point is the HLC uncertainty-bound capture
under the
  existing cluster clock contract.
- The pre-admission wait must report a watermark at or beyond the
effective
session minimum; missing runtime/clock/transaction client, negative
offset,
timestamp overflow, wait failure, cancellation, client close, and
regressing
  watermark all fail closed.
- A failed pre-admission wait creates no transaction operator. The
timestamp
waiter owns and removes its entry on success, cancellation,
notification, or
close. Later transaction-creation failure retains the existing abort
owner.
- Bootstrap-only special users and `skipCheckUser` retain their existing
  catalog-free paths.
- Embedded post-parse mutations are revalidated before resource
creation;
invalid budgets and service-identity mismatches leave no cleanup
obligation.
- One waiter exists per in-flight normal login, bounded by the handshake
  deadline and connection count. No new goroutine, RPC, persisted state,
  collection, or rolling-upgrade dependency is introduced.

This repairs new-login catalog freshness. Existing authenticated-session
role
revocation remains the separate lifecycle handled by PR matrixorigin#27737.

## Performance

- The default ordinary-SQL transaction hot path is unchanged.
- The unavoidable uncertainty/logtail wait occurs before transaction
admission,
so it no longer consumes a `max-active` user-transaction slot.
Connection
  pooling amortizes this connection-establishment cost for TP workloads.
- A normal login adds one local HLC read and reuses the existing
timestamp
  waiter; it adds no cross-CN RPC or goroutine.
- The built-in waiter close fast path removes one derived context and
  `context.AfterFunc` allocation from each authentication wait.
- The non-default freshness-preserving branch adds one timestamp
comparison and
  no allocation, lock, or I/O.
- Embedded revalidation is startup-only and occurs before resource
allocation;
  it has no query-path cost.

## Validation

Candidate head: `9a646c2a33` on main `745d8364c5` (Go 1.26.4,
`-mod=readonly`, repository CGo/native libraries).

- Focused config/authentication/waiter tests passed, including
`skipCheckUser`,
  monotonic existing minimum, missing dependencies, downstream failure,
  watermark regression, executor ordering, cancellation, client close,
  post-parse invalid budget, and mutable-service-type bypass.
- Full normal suites passed for `pkg/config`, `cmd/mo-service`,
`pkg/embed`,
  `pkg/frontend`, `pkg/txn/client`, and `pkg/txn/clock`.
- Full race suites passed for `pkg/embed`, `pkg/frontend`, and
`pkg/txn/client`.
- The cancellation/close/snapshot barrier tests passed 100 repetitions
under
  the race detector; they use no sleeps, polling, retries, or weakened
  assertions.
- `go vet` passed for every changed Go package; formatting, module-file,
and
  diff checks passed.
- Exact-head `make build` passed with `CommitID=9a646c2a33`.
- The existing public revoke/regrant semantics were exercised through
Proxy on
one LogService, one TN, and two CNs. New sessions immediately observed
revoke
fallback and regrant on alternating CN connections; a deliberately
retained
  old session kept its old role, matching the separate matrixorigin#27737 scope.

Incident evidence:

https://github.com/matrixorigin/matrixone/actions/runs/33067372509/job/98502820591?pr=27758

Related: matrixorigin#27717, matrixorigin#27737, matrixorigin#27758
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

close matrixorigin#27651

## What this PR does / why we need it:

Implicit login trusted `mo_user.default_role` without confirming that
the role was still granted to the user. After revoking the default role,
a new implicit session could therefore activate that role and retain its
privileges, while explicit-role login correctly rejected it.

This change:

- validates an implicit default role against both `mo_role` and
`mo_user_grant` before activation;
- falls back to a valid `public` grant when the stored default is stale,
NULL, invalid, or missing, and fails closed when no valid fallback
exists;
- preserves explicit-role validation, existing-session behavior, and
default-role recovery after regrant;
- adds focused unit coverage for valid, revoked, regranted, NULL,
missing, malformed, out-of-range, and query-error paths;
- adds BVT coverage for explicit and implicit role behavior, new and
existing sessions, protected privileges, secondary-role revoke control,
and regrant recovery.

Local verification:

- `go test ./pkg/frontend/... -count=1`
- `go test -race ./pkg/frontend/... -count=1`
- `go vet ./pkg/frontend/...`
- changed functions coverage: 100%; frontend package coverage: 63.7%
- revoked_default_role_login BVT: 39/39 passed
- local 1 Log / 1 TN / 1 CN protocol E2E with privilege cache enabled
and disabled
- `make config`
- `make err-check`
- `make build-with-prebuilt-native`
- `git diff --check`

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…rixorigin#27763)

## What changed

This PR makes normal-user authentication establish one fresh catalog
snapshot
before reading account, user, password, lock, role, grant,
system-variable, or
login-database state.

- Capture the serving CN's HLC uncertainty upper bound and install the
smallest
  timestamp that dominates every HLC value at that bound.
- Wait for the local logtail through the effective monotonic session
minimum
  **before** constructing the authentication executor or entering user
  transaction admission. `TxnClient.New` then rechecks the same minimum.
- Preserve a larger caller minimum in both transaction freshness modes.
- Keep `max-clock-offset` as the ordering bound even when active local
clock
  monitoring is disabled, in process and embedded launchers.
- Make the handshake request context the single timeout owner of
authentication
freshness waiting and transaction creation. Ordinary transactions retain
  `CreateTxnOpTimeout`.
- Reject authentication-enabled CN configurations that cannot cover the
pairwise clock geometry: `connectTimeout > 2 * max-clock-offset + 1ns`,
with
checked arithmetic. `skipCheckUser=true` bypasses only this unreachable
authentication-specific budget check; general clock validation still
runs.
- Revalidate public embedded `Adjust`/pre-start mutations before
creating
resources. The operator's immutable service type owns validation, and a
callback cannot relabel a CN as a TN to bypass the authentication
budget.
- Let the built-in timestamp waiter observe transaction-client closure
directly,
avoiding a derived context and `context.AfterFunc` allocation per login
while
retaining the compatibility fallback for legacy waiter implementations.

The complete design and validation map are in

[`docs/design/authentication_catalog_freshness.md`](docs/design/authentication_catalog_freshness.md).

## Root cause

With sacrificing freshness enabled, a new session has no prior commit
timestamp,
so authentication could start from the latest logtail already applied by
its
local CN. A security mutation could already have committed and returned
on
another CN while this CN still read the previous catalog generation.

The observed failure was `REVOKE` followed by an immediate `GRANT` of
the stored
default role. A new connection routed to a lagging CN validated the old
revoked
generation, fell back to `public`, and denied a table read that the
regranted
role permits. The same stale-snapshot shape applies to account state,
user/password and lock state, explicit role grants, and login database
resolution, so the repair belongs at the authentication snapshot
boundary.

## Correctness and unhappy paths

- The login linearization point is the HLC uncertainty-bound capture
under the
  existing cluster clock contract.
- The pre-admission wait must report a watermark at or beyond the
effective
session minimum; missing runtime/clock/transaction client, negative
offset,
timestamp overflow, wait failure, cancellation, client close, and
regressing
  watermark all fail closed.
- A failed pre-admission wait creates no transaction operator. The
timestamp
waiter owns and removes its entry on success, cancellation,
notification, or
close. Later transaction-creation failure retains the existing abort
owner.
- Bootstrap-only special users and `skipCheckUser` retain their existing
  catalog-free paths.
- Embedded post-parse mutations are revalidated before resource
creation;
invalid budgets and service-identity mismatches leave no cleanup
obligation.
- One waiter exists per in-flight normal login, bounded by the handshake
  deadline and connection count. No new goroutine, RPC, persisted state,
  collection, or rolling-upgrade dependency is introduced.

This repairs new-login catalog freshness. Existing authenticated-session
role
revocation remains the separate lifecycle handled by PR matrixorigin#27737.

## Performance

- The default ordinary-SQL transaction hot path is unchanged.
- The unavoidable uncertainty/logtail wait occurs before transaction
admission,
so it no longer consumes a `max-active` user-transaction slot.
Connection
  pooling amortizes this connection-establishment cost for TP workloads.
- A normal login adds one local HLC read and reuses the existing
timestamp
  waiter; it adds no cross-CN RPC or goroutine.
- The built-in waiter close fast path removes one derived context and
  `context.AfterFunc` allocation from each authentication wait.
- The non-default freshness-preserving branch adds one timestamp
comparison and
  no allocation, lock, or I/O.
- Embedded revalidation is startup-only and occurs before resource
allocation;
  it has no query-path cost.

## Validation

Candidate head: `9a646c2a33` on main `745d8364c5` (Go 1.26.4,
`-mod=readonly`, repository CGo/native libraries).

- Focused config/authentication/waiter tests passed, including
`skipCheckUser`,
  monotonic existing minimum, missing dependencies, downstream failure,
  watermark regression, executor ordering, cancellation, client close,
  post-parse invalid budget, and mutable-service-type bypass.
- Full normal suites passed for `pkg/config`, `cmd/mo-service`,
`pkg/embed`,
  `pkg/frontend`, `pkg/txn/client`, and `pkg/txn/clock`.
- Full race suites passed for `pkg/embed`, `pkg/frontend`, and
`pkg/txn/client`.
- The cancellation/close/snapshot barrier tests passed 100 repetitions
under
  the race detector; they use no sleeps, polling, retries, or weakened
  assertions.
- `go vet` passed for every changed Go package; formatting, module-file,
and
  diff checks passed.
- Exact-head `make build` passed with `CommitID=9a646c2a33`.
- The existing public revoke/regrant semantics were exercised through
Proxy on
one LogService, one TN, and two CNs. New sessions immediately observed
revoke
fallback and regrant on alternating CN connections; a deliberately
retained
  old session kept its old role, matching the separate matrixorigin#27737 scope.

Incident evidence:

https://github.com/matrixorigin/matrixone/actions/runs/33067372509/job/98502820591?pr=27758

Related: matrixorigin#27717, matrixorigin#27737, matrixorigin#27758

This branch was previously deployed

1 inactive deployment
ci — a5547a12 Deployed Aug 27, 2026 by mergify[bot] via Matrixone Utils CI / Coverage #48674
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: implicit login activates a default role revoked from the user

3 participants