Skip to content

Harden user deletion handling - #34

Merged
memurats merged 7 commits into
stable33from
nmc/harden-user-deletion
Nov 17, 2026
Merged

memurats merged 7 commits into
stable33from
nmc/harden-user-deletion

Conversation

@memurats

Copy link
Copy Markdown
Contributor

This PR hardens the user deletion and account state handling to prevent accounts from being deleted based on incomplete, stale or ambiguous provisioning events.

Changes

  • Treat the booking claim f556 as a tri-state value:
    • 1 = booked
    • 0 = withdrawn
    • missing = unknown and must not trigger deletion
  • Use changeTime as the authoritative timestamp for account state changes.
  • Do not fall back to authentication timestamps such as auth_time for retention handling.
  • Ignore stale provisioning events so older state changes cannot overwrite newer account state.
  • Cancel pending deletion when an account is explicitly booked again.
  • Ensure account locking is handled separately from withdrawal and never schedules deletion on its own.
  • Require both an explicit withdrawal and a valid changeTime before scheduling deletion.
  • Fail safely if deletion scheduling cannot be persisted:
    • disable access
    • remove any existing deletion marker
    • do not schedule deletion
  • Protect against races where a newer account state is written while a withdrawal is being processed.
  • Avoid logging complete OIDC claim payloads; log claim names and derived provisioning results instead.
  • Keep tariff and display-name claim evaluation dynamic through the existing mappings rather than maintaining duplicate claim lists in the event listener.

Result

User deletion is now only scheduled when the provisioning state unambiguously confirms a withdrawal and provides a valid state-change timestamp. Missing claims, locks, stale events and processing errors can no longer accidentally arm or preserve a destructive deletion state.

@memurats memurats self-assigned this Nov 12, 2026
@memurats memurats added the custom Changes for Telekom provisioning label Nov 12, 2026

@juliusknorr juliusknorr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The general approach looks good to me 👍

I left some comments inside. The main one: existing deletion markers would all be disarmed, so it would need a plan for those users.

As a side note, tests do not seem to be running. The phpunit-mysql workflow fails after 0s on every push.

Comment thread lib/User/UserAccountDeletionJob.php Outdated
* Old/partial deletion markers are deliberately not trusted.
* Supporting metadata is required before irreversible deletion.
*/
if ($state['withdrawal'] === null || $state['markedAt'] === null) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All markers created before this PR lack withdrawal/deletion_marked_at, so the first run after the update disarms every pending deletion and those users are never deleted. Is that intended? If yes, we probably need a repair step for them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I made a few adjustments based on your comments.

Existing deletion entries from the old implementation are no longer invalidated just because the newer withdrawal metadata is missing. This ensures that deploying the change does not unintentionally cancel deletion processes that are already pending in production.

Comment thread lib/User/UserAccountDeletionJob.php Outdated
return;
}

$initialFingerprint = $this->fingerprint($state);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The fingerprint plus double re-read isn't atomic either, and userManager->get()/isEnabled() are cached in-process iirc. Can we just validate once against the DB right before delete()?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The fingerprint logic and repeated getDeletionState() / user re-reads were removed. The relevant deletion state is now read and validated directly before deletion instead.

$changeTime = $this->withdrawDate($claims);

if ($bookingState === true) {
if ($changeTime !== null && $this->nmcUserService->isStaleAccountChange($uid, $changeTime)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What about a changeTime in the future? It becomes last_account_change and every later booking is ignored as stale, so a rebooked customer stays locked out. Could we reject or clamp changeTime > now?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Timestamps in the future are clamped to now before being stored/used, so a bad timestamp cannot poison last_account_change and make later legitimate events look stale.

Comment thread lib/Rules/UserAccountRules.php Outdated

// Missing tariff state is not a confirmed withdrawal.
if ($bookingState === null) {
$this->nmcUserService->unmarkDeletion($uid);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Should an unclear event touch the deletion state at all? This cancels a correct pending deletion if the token just lacks f556. I'd rather leave the marker alone here (neither arm nor disarm).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Missing f556 / invalid changeTime no longer call unmarkDeletion(). They do not schedule a new deletion, but they also do not cancel a previously valid deletion state.

Comment thread lib/Rules/UserAccountRules.php Outdated

// Withdrawal needs a valid authoritative timestamp.
if ($changeTime === null) {
$this->nmcUserService->unmarkDeletion($uid);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same as above

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Missing f556 / invalid changeTime no longer call unmarkDeletion(). They do not schedule a new deletion, but they also do not cancel a previously valid deletion state.

Comment thread lib/User/NmcUserService.php Outdated
$safetyDelay = $this->getConfiguredInterval('userdeletionsafetydelay', self::DEFAULT_SAFETY_DELAY);

$retentionDate = $withdrawal->add($retention);
$safetyDate = $markedAt->add($safetyDelay);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Existing users have no last_account_change yet, so a late or replayed withdrawal still deletes them after 1 day. Would max(changeTime, now) + retention be simpler? Then we could drop the safety delay setting and deletion_marked_at.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I changed the calculation to max(changeTime, now) + retention and removed both the additional safety delay and deletion_marked_at.
That makes the behavior simpler and also ensures that a late or replayed withdrawal always gets the full retention period from the time we process it.

Comment thread lib/User/NmcUserService.php Outdated
$this->config->setUserValue($userId, Application::APP_ID, self::KEY_DELETION, $deletionDate->getTimestamp());

/*
* Narrow the race window with a concurrent reactivation/newer event.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

IConfig caches user values per process iirc, so this re-read never sees a concurrent request. I'd drop it, the job checks again anyway.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, that makes sense. I removed the re-read from markDeletion().

Comment thread lib/Db/UserQueries.php
<?php

declare(strict_types=1);
/**

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I guess it is fine dropping, the code i think was never started by Roeland

Comment thread lib/Rules/TariffRules.php
/**
* The generic tariff rules evaluator
*/
class TariffRules {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Unrelated refactor (also changes behaviour slightly, int 1 now counts as set). Mind splitting TariffRules/DisplaynameRules into a separate PR? ;)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The unrelated DisplaynameRules / TariffRules changes were moved into a separate PR (#35) as suggested.

@memurats
memurats merged commit d7adf6b into stable33 Nov 17, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

custom Changes for Telekom provisioning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants