From 0fb8dc20e99559d1cb7e2c719cf394fce3f99bad Mon Sep 17 00:00:00 2001 From: Teisha McRae Date: Wed, 25 Nov 2026 17:12:32 -0500 Subject: [PATCH 1/2] Preserve the latest parked ABE click Prevent mismatched ABE tokens from consuming parked click data, and ensure fresh action parameters take precedence after login. Add regression tests for ticket reuse and successive clicks. --- .../Http/Controllers/TaskController.php | 37 +++++--- ProcessMaker/Repositories/TokenRepository.php | 24 ++--- tests/Feature/TaskControllerTest.php | 89 +++++++++++++++++++ 3 files changed, 125 insertions(+), 25 deletions(-) diff --git a/ProcessMaker/Http/Controllers/TaskController.php b/ProcessMaker/Http/Controllers/TaskController.php index bb905f35f5..19ddd50e75 100755 --- a/ProcessMaker/Http/Controllers/TaskController.php +++ b/ProcessMaker/Http/Controllers/TaskController.php @@ -316,6 +316,14 @@ private function handleAbeVariableUpdate(HttpRequest $request, string $abeUuid) 'varValue' => 'required|string', ]); + return $this->resolveAbeVariableUpdateResponse($request, $abe); + } + + /** + * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse|\Illuminate\View\View + */ + private function resolveAbeVariableUpdateResponse(HttpRequest $request, ProcessAbeRequestToken $abe) + { if ($abe->require_login && Auth::user()->username === AnonymousUser::ANONYMOUS_USERNAME) { return $this->redirectAbeClickToLogin($request, $abe); } @@ -325,12 +333,9 @@ private function handleAbeVariableUpdate(HttpRequest $request, string $abeUuid) } $task = ProcessRequestToken::find($abe->process_request_token_id); - $taskError = $this->abeVariableUpdateTaskError($task); - if ($taskError !== null) { - return $taskError; - } - return $this->completeAbeVariableUpdate($request, $abe, $task); + return $this->abeVariableUpdateTaskError($task) + ?? $this->completeAbeVariableUpdate($request, $abe, $task); } private function abeVariableUpdateTaskError(?ProcessRequestToken $task) @@ -449,6 +454,10 @@ private function redirectAbeClickToLogin(HttpRequest $request, ProcessAbeRequest /** * Merge a parked ABE click query back onto the request when the signature is missing. + * + * Only restore onto the short post-login intended URL (no action params). A fresh + * Approve/Reject click already carries varName/varValue and must not be overwritten + * by an older parked query from a leftover abe_click_ticket cookie. */ private function restoreAbeClickQuery(HttpRequest $request, ProcessAbeRequestToken $abe): void { @@ -456,6 +465,11 @@ private function restoreAbeClickQuery(HttpRequest $request, ProcessAbeRequestTok return; } + if ((string) $request->query('varName', '') !== '' + && (string) $request->query('varValue', '') !== '') { + return; + } + $query = $this->pullPendingAbeClickQuery($request, $abe); if ($query === null) { return; @@ -476,16 +490,19 @@ private function pullPendingAbeClickQuery(HttpRequest $request, ProcessAbeReques return null; } - $pending = Cache::pull(self::ABE_CLICK_CACHE_PREFIX . $ticket); - if (!is_array($pending) || ($pending['abe_uuid'] ?? null) !== $abe->uuid) { - return null; - } + // Peek first: leave the parked payload alone unless this ABE token owns it. + $cacheKey = self::ABE_CLICK_CACHE_PREFIX . $ticket; + $pending = Cache::get($cacheKey); + $query = is_array($pending) && ($pending['abe_uuid'] ?? null) === $abe->uuid + ? ($pending['query'] ?? null) + : null; - $query = $pending['query'] ?? null; if (!is_array($query) || $query === []) { return null; } + Cache::forget($cacheKey); + return $query; } diff --git a/ProcessMaker/Repositories/TokenRepository.php b/ProcessMaker/Repositories/TokenRepository.php index bdacd34a7f..1e4f90da92 100644 --- a/ProcessMaker/Repositories/TokenRepository.php +++ b/ProcessMaker/Repositories/TokenRepository.php @@ -432,21 +432,15 @@ private function abeProcessModelFromTokenInstance(TokenInterface $token): ?Proce } $instance = $token->getInstance(); - if (!$instance || !method_exists($instance, 'getProcess')) { - return null; - } - - $process = $instance->getProcess(); - if (!$process || !method_exists($process, 'getOwnerDocument')) { - return null; - } - - $document = $process->getOwnerDocument(); - if (!$document || !method_exists($document, 'getModel')) { - return null; - } - - $model = $document->getModel(); + $process = ($instance && method_exists($instance, 'getProcess')) + ? $instance->getProcess() + : null; + $document = ($process && method_exists($process, 'getOwnerDocument')) + ? $process->getOwnerDocument() + : null; + $model = ($document && method_exists($document, 'getModel')) + ? $document->getModel() + : null; return $model instanceof Process ? $model : null; } diff --git a/tests/Feature/TaskControllerTest.php b/tests/Feature/TaskControllerTest.php index 733250f40f..894d913f3e 100644 --- a/tests/Feature/TaskControllerTest.php +++ b/tests/Feature/TaskControllerTest.php @@ -314,4 +314,93 @@ public function testMissingAbeClickQueryAfterFailedRestoreReturnsValidationError $response->assertJsonValidationErrors(['varName', 'varValue']); $response->assertJsonMissing(['message' => 'Error updating variable']); } + + /** + * A leftover abe_click_ticket for ABE token A must not be consumed when hitting + * update_variable for ABE token B (unsigned / no abe_sig). + */ + public function testAbeClickTicketCacheSurvivesUuidMismatch(): void + { + $bpmn = file_get_contents(__DIR__ . '/../Fixtures/action_by_email_process_require_login.bpmn'); + Screen::factory()->create(); + + $processA = Process::factory()->create(['bpmn' => $bpmn]); + $this->apiCall('POST', route('api.process_events.trigger', [$processA->id, 'event' => 'node_1']), []); + $abeA = ProcessAbeRequestToken::orderByDesc('id')->firstOrFail(); + + $processB = Process::factory()->create(['bpmn' => $bpmn]); + $this->apiCall('POST', route('api.process_events.trigger', [$processB->id, 'event' => 'node_1']), []); + $abeB = ProcessAbeRequestToken::orderByDesc('id')->firstOrFail(); + $this->assertNotSame($abeA->uuid, $abeB->uuid); + + $anonymous = User::where('username', AnonymousUser::ANONYMOUS_USERNAME)->firstOrFail(); + $parkUrl = 'tasks/update_variable/' . $abeA->uuid . '?varName=res&varValue=yes'; + $redirect = $this->actingAs($anonymous, 'web')->get($parkUrl); + $redirect->assertRedirect('login'); + $ticket = $redirect->getCookie(TaskController::ABE_CLICK_TICKET_COOKIE, false)->getValue(); + $this->assertTrue(Cache::has('abe_click:' . $ticket)); + + // Short intended URL for a different ABE token (no query) must not consume token A's park. + $user = User::factory()->create(['status' => 'ACTIVE']); + $this->actingAs($user, 'web') + ->withUnencryptedCookie(TaskController::ABE_CLICK_TICKET_COOKIE, $ticket) + ->getJson('tasks/update_variable/' . $abeB->uuid) + ->assertStatus(422); + + $this->assertTrue(Cache::has('abe_click:' . $ticket)); + $pending = Cache::get('abe_click:' . $ticket); + $this->assertSame($abeA->uuid, $pending['abe_uuid'] ?? null); + $this->assertSame('yes', $pending['query']['varValue'] ?? null); + + // Matching short URL still restores the parked Approve. + $response = $this->actingAs($user, 'web') + ->withUnencryptedCookie(TaskController::ABE_CLICK_TICKET_COOKIE, $ticket) + ->get('tasks/update_variable/' . $abeA->uuid); + $response->assertStatus(200); + + $request = ProcessRequest::findOrFail($abeA->process_request_id); + $this->assertSame('yes', $request->data['res'] ?? null); + $this->assertFalse(Cache::has('abe_click:' . $ticket)); + } + + /** + * With Require Login and tracking off, a second click must park the later + * action (Reject), not revive the first click's parked Approve via the cookie. + */ + public function testLaterAbeClickWithRequireLoginParksLatestAction(): void + { + $process = Process::factory()->create([ + 'bpmn' => file_get_contents(__DIR__ . '/../Fixtures/action_by_email_process_require_login.bpmn'), + ]); + Screen::factory()->create(); + + $this->apiCall('POST', route('api.process_events.trigger', [$process->id, 'event' => 'node_1']), []); + $abe = ProcessAbeRequestToken::firstOrFail(); + $anonymous = User::where('username', AnonymousUser::ANONYMOUS_USERNAME)->firstOrFail(); + + $first = $this->actingAs($anonymous, 'web') + ->get('tasks/update_variable/' . $abe->uuid . '?varName=res&varValue=yes'); + $first->assertRedirect('login'); + $firstTicket = $first->getCookie(TaskController::ABE_CLICK_TICKET_COOKIE, false)->getValue(); + + $second = $this->actingAs($anonymous, 'web') + ->withUnencryptedCookie(TaskController::ABE_CLICK_TICKET_COOKIE, $firstTicket) + ->get('tasks/update_variable/' . $abe->uuid . '?varName=res&varValue=no'); + $second->assertRedirect('login'); + $secondTicket = $second->getCookie(TaskController::ABE_CLICK_TICKET_COOKIE, false)->getValue(); + $this->assertNotSame($firstTicket, $secondTicket); + + $pending = Cache::get('abe_click:' . $secondTicket); + $this->assertIsArray($pending); + $this->assertSame('no', $pending['query']['varValue'] ?? null); + + $user = User::factory()->create(['status' => 'ACTIVE']); + $response = $this->actingAs($user, 'web') + ->withUnencryptedCookie(TaskController::ABE_CLICK_TICKET_COOKIE, $secondTicket) + ->get('tasks/update_variable/' . $abe->uuid); + $response->assertStatus(200); + + $request = ProcessRequest::findOrFail($abe->process_request_id); + $this->assertSame('no', $request->data['res'] ?? null); + } } From f618f82a96061337c5b46e6618c9b72e1ea3097e Mon Sep 17 00:00:00 2001 From: Teisha McRae Date: Fri, 27 Nov 2026 12:37:39 -0500 Subject: [PATCH 2/2] Preserve explicit ABE action parameters Skip restoring a parked click whenever either action parameter is present, leaving partial and invalid values to validation. Add regression coverage for partial and array-valued queries. --- .../Http/Controllers/TaskController.php | 9 ++-- tests/Feature/TaskControllerTest.php | 44 +++++++++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/ProcessMaker/Http/Controllers/TaskController.php b/ProcessMaker/Http/Controllers/TaskController.php index 19ddd50e75..a87b48e0ed 100755 --- a/ProcessMaker/Http/Controllers/TaskController.php +++ b/ProcessMaker/Http/Controllers/TaskController.php @@ -455,9 +455,9 @@ private function redirectAbeClickToLogin(HttpRequest $request, ProcessAbeRequest /** * Merge a parked ABE click query back onto the request when the signature is missing. * - * Only restore onto the short post-login intended URL (no action params). A fresh - * Approve/Reject click already carries varName/varValue and must not be overwritten - * by an older parked query from a leftover abe_click_ticket cookie. + * Only restore onto the short post-login intended URL (no action params). If either + * varName or varValue is present — even empty or non-string — skip restore and let + * validation own the values so a leftover ticket cannot overwrite a fresh click. */ private function restoreAbeClickQuery(HttpRequest $request, ProcessAbeRequestToken $abe): void { @@ -465,8 +465,7 @@ private function restoreAbeClickQuery(HttpRequest $request, ProcessAbeRequestTok return; } - if ((string) $request->query('varName', '') !== '' - && (string) $request->query('varValue', '') !== '') { + if ($request->query->has('varName') || $request->query->has('varValue')) { return; } diff --git a/tests/Feature/TaskControllerTest.php b/tests/Feature/TaskControllerTest.php index 894d913f3e..df1dfacb65 100644 --- a/tests/Feature/TaskControllerTest.php +++ b/tests/Feature/TaskControllerTest.php @@ -403,4 +403,48 @@ public function testLaterAbeClickWithRequireLoginParksLatestAction(): void $request = ProcessRequest::findOrFail($abe->process_request_id); $this->assertSame('no', $request->data['res'] ?? null); } + + /** + * Presence of either action param skips restore so validation owns the values: + * partial queries and array-valued params must 422, not revive a parked click or 500. + */ + public function testPartialOrInvalidActionParamsSkipAbeClickRestoreAndReturnValidationError(): void + { + $process = Process::factory()->create([ + 'bpmn' => file_get_contents(__DIR__ . '/../Fixtures/action_by_email_process_require_login.bpmn'), + ]); + Screen::factory()->create(); + + $this->apiCall('POST', route('api.process_events.trigger', [$process->id, 'event' => 'node_1']), []); + $abe = ProcessAbeRequestToken::firstOrFail(); + $anonymous = User::where('username', AnonymousUser::ANONYMOUS_USERNAME)->firstOrFail(); + + $park = $this->actingAs($anonymous, 'web') + ->get('tasks/update_variable/' . $abe->uuid . '?varName=res&varValue=yes'); + $park->assertRedirect('login'); + $ticket = $park->getCookie(TaskController::ABE_CLICK_TICKET_COOKIE, false)->getValue(); + $this->assertTrue(Cache::has('abe_click:' . $ticket)); + + $user = User::factory()->create(['status' => 'ACTIVE']); + + // Partial query: varName present without varValue must not restore the parked Approve. + $partial = $this->actingAs($user, 'web') + ->withUnencryptedCookie(TaskController::ABE_CLICK_TICKET_COOKIE, $ticket) + ->getJson('tasks/update_variable/' . $abe->uuid . '?varName=res'); + $partial->assertStatus(422); + $partial->assertJsonValidationErrors(['varValue']); + $partial->assertJsonMissing(['message' => 'Error updating variable']); + $this->assertTrue(Cache::has('abe_click:' . $ticket)); + $this->assertSame(0, (int) ProcessAbeRequestToken::findOrFail($abe->id)->is_answered); + + // Array-valued varName must 422 from the string rule, not HTTP 500 from a cast. + $arrayParam = $this->actingAs($user, 'web') + ->withUnencryptedCookie(TaskController::ABE_CLICK_TICKET_COOKIE, $ticket) + ->getJson('tasks/update_variable/' . $abe->uuid . '?varName[]=res&varValue=no'); + $arrayParam->assertStatus(422); + $arrayParam->assertJsonValidationErrors(['varName']); + $arrayParam->assertJsonMissing(['message' => 'Error updating variable']); + $this->assertTrue(Cache::has('abe_click:' . $ticket)); + $this->assertSame(0, (int) ProcessAbeRequestToken::findOrFail($abe->id)->is_answered); + } }