Continuous re-validation of session credentials - #403
Open
pg-hub-mirror[bot] wants to merge 5 commits into
Open
pg-hub-mirror[bot] wants to merge 5 commits into
pg-hub-mirror[bot] wants to merge 5 commits into
Conversation
Introduce a mechanism that periodically re-validates the credentials of an active session and terminates the session if they are no longer valid. A per-backend timer (CREDENTIAL_VALIDATION_TIMEOUT) fires at a configurable interval; the pending flag is acted on at the next command boundary in the main loop, where the check runs inside a short-lived transaction (reusing the session's open transaction if one exists, so a long-running transaction block is still validated at each command boundary). This commit adds the framework and the baseline, auth-method-independent check: the authenticated (login) role must still exist and must not have passed its rolvaliduntil expiration. This is checked against GetAuthenticatedUserId(), not the current session or effective role, so SET SESSION AUTHORIZATION / SET ROLE cannot mask the original login role's expiry or removal. Method-specific validators plug in via RegisterCredentialValidator() and are added in following commits. Two GUCs control the feature: credential_validation_enabled (default off) and credential_validation_interval (5..3600 seconds, default 60).
Register a method-specific validator for certificate-authenticated sessions (CVT_CERT). The client certificate presented at connection time is retained on the Port, so its notAfter date can be re-checked locally with no network round-trip; once the certificate has expired the session is terminated at the next validation cycle. be_tls_get_peer_cert_expired() reports whether the peer certificate's validity period has passed, conservatively treating an absent or unparsable notAfter as not expired.
Register a method-specific validator for OAuth-authenticated sessions (CVT_OAUTH) that asks the loaded OAuth validator module whether the bearer token has expired. This requires a new optional callback, expire_cb, in OAuthValidatorCallbacks. To add it without breaking existing modules, the validator ABI magic is versioned: PG_OAUTH_VALIDATOR_MAGIC_V1 (the original layout, without expire_cb) and PG_OAUTH_VALIDATOR_MAGIC_V2 (which adds expire_cb). The server accepts both; expire_cb is only consulted for V2 modules that provide it, and a token is assumed still valid otherwise.
Register a method-specific validator for GSSAPI/Kerberos sessions (CVT_GSS). The new helper be_gssapi_get_context_expired() uses gss_context_time() on the acceptor context retained on the Port to report, with no round-trip to the KDC, whether the context lifetime derived from the client's Kerberos ticket has elapsed. A short-lived-ticket TAP test (009_gss_continuous_validation.pl) confirms an idle GSS session is terminated once the credential lapses; PostgreSQL::Test::Kerberos gains clockskew and ticket-lifetime parameters and namespaces its KDC files by test script name.
For LDAP-authenticated sessions, re-run the configured search filter on each validation cycle and terminate the session once the account no longer exists in the directory. Search+bind only; simple-bind sessions are treated as valid.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
pgsql-hackersCAER375N35XAWLhAiokyVs65bFQgq_n_=LgNs61o+ZtYdEmPEqw@mail.gmail.comPatch files:
Hi Hackers,
Attached is v2 of the series, with three fixes folded into 0001 (no
changes to 0002-0005):
CheckCredentialValidity() no longer skips validation under
RecoveryInProgress(). Hot-standby sessions run continuously "in
recovery", so this was silently disabling the feature for read
replicas -- all the checks involved are read-only/local, so
there's no reason to exclude them.
ValidateRoleValidity() now checks GetAuthenticatedUserId()
instead of GetSessionUserId(), so a superuser can't mask the
login role's expiry/removal by switching session authorization.
This also keeps it consistent with the method-specific
validators, which always check the fixed login identity. Added
regression coverage.
EnableCredentialValidationTimeout() and CheckCredentialValidity()
now explicitly skip AmWalSenderProcess()
Request a review.
Thanks & Best Regards,
Ajit
On Wed, 1 Jul 2026 at 17:45, Ajit Awekar <ajitpostgres(at)gmail(dot)com> wrote: