Repository navigation
Harden user deletion handling - #34
Conversation
juliusknorr
left a comment
There was a problem hiding this comment.
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.
| * Old/partial deletion markers are deliberately not trusted. | ||
| * Supporting metadata is required before irreversible deletion. | ||
| */ | ||
| if ($state['withdrawal'] === null || $state['markedAt'] === null) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| return; | ||
| } | ||
|
|
||
| $initialFingerprint = $this->fingerprint($state); |
There was a problem hiding this comment.
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()?
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
|
|
||
| // Missing tariff state is not a confirmed withdrawal. | ||
| if ($bookingState === null) { | ||
| $this->nmcUserService->unmarkDeletion($uid); |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
|
|
||
| // Withdrawal needs a valid authoritative timestamp. | ||
| if ($changeTime === null) { | ||
| $this->nmcUserService->unmarkDeletion($uid); |
There was a problem hiding this comment.
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.
| $safetyDelay = $this->getConfiguredInterval('userdeletionsafetydelay', self::DEFAULT_SAFETY_DELAY); | ||
|
|
||
| $retentionDate = $withdrawal->add($retention); | ||
| $safetyDate = $markedAt->add($safetyDelay); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| $this->config->setUserValue($userId, Application::APP_ID, self::KEY_DELETION, $deletionDate->getTimestamp()); | ||
|
|
||
| /* | ||
| * Narrow the race window with a concurrent reactivation/newer event. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Yes, that makes sense. I removed the re-read from markDeletion().
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** |
There was a problem hiding this comment.
I guess it is fine dropping, the code i think was never started by Roeland
| /** | ||
| * The generic tariff rules evaluator | ||
| */ | ||
| class TariffRules { |
There was a problem hiding this comment.
Unrelated refactor (also changes behaviour slightly, int 1 now counts as set). Mind splitting TariffRules/DisplaynameRules into a separate PR? ;)
There was a problem hiding this comment.
The unrelated DisplaynameRules / TariffRules changes were moved into a separate PR (#35) as suggested.
Added copyright and author information to UserQueries.php
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
f556as a tri-state value:1= booked0= withdrawnchangeTimeas the authoritative timestamp for account state changes.auth_timefor retention handling.changeTimebefore scheduling deletion.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.