diff --git a/ProcessMaker/Http/Controllers/TaskController.php b/ProcessMaker/Http/Controllers/TaskController.php index bb905f35f5..a87b48e0ed 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). 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 { @@ -456,6 +465,10 @@ private function restoreAbeClickQuery(HttpRequest $request, ProcessAbeRequestTok return; } + if ($request->query->has('varName') || $request->query->has('varValue')) { + return; + } + $query = $this->pullPendingAbeClickQuery($request, $abe); if ($query === null) { return; @@ -476,16 +489,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..df1dfacb65 100644 --- a/tests/Feature/TaskControllerTest.php +++ b/tests/Feature/TaskControllerTest.php @@ -314,4 +314,137 @@ 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); + } + + /** + * 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); + } }