From b1f9eb2d3f53ccf20c58fa2ddad7935af80bb4ac Mon Sep 17 00:00:00 2001 From: shanshi <3316401569@qq.com> Date: Wed, 2 Dec 2026 09:41:15 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B8=85=E7=90=86=E8=BF=87=E6=9C=9F?= =?UTF-8?q?=E5=8D=8E=E5=A4=8F=E9=AA=8C=E8=AF=81=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Services/HxbOtpRelayService.php | 19 +++++++++++++++++-- tests/Feature/HxbOtpRelayTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/app/Services/HxbOtpRelayService.php b/app/Services/HxbOtpRelayService.php index e44e550ff..ee591ace2 100644 --- a/app/Services/HxbOtpRelayService.php +++ b/app/Services/HxbOtpRelayService.php @@ -29,6 +29,8 @@ class HxbOtpRelayService */ public function create(string $clientRunId): array { + $this->expireStale(); + $clientRunId = trim($clientRunId); if (! preg_match('/^[A-Za-z0-9-]{8,64}$/', $clientRunId)) { throw new \InvalidArgumentException('任务编号格式不正确。'); @@ -157,13 +159,14 @@ class HxbOtpRelayService private function expireIfNeeded(object $row): object { - if ((string) $row->status === self::STATUS_PENDING + if (in_array((string) $row->status, [self::STATUS_PENDING, self::STATUS_READY], true) && now()->greaterThanOrEqualTo($row->expires_at)) { $this->connection()->table(self::TABLE) ->where('id', (int) $row->id) - ->where('status', self::STATUS_PENDING) + ->whereIn('status', [self::STATUS_PENDING, self::STATUS_READY]) ->update([ 'status' => self::STATUS_EXPIRED, + 'code_encrypted' => null, 'updated_at' => now(), ]); $row->status = self::STATUS_EXPIRED; @@ -171,4 +174,16 @@ class HxbOtpRelayService return $row; } + + private function expireStale(): void + { + $this->connection()->table(self::TABLE) + ->whereIn('status', [self::STATUS_PENDING, self::STATUS_READY]) + ->where('expires_at', '<=', now()) + ->update([ + 'status' => self::STATUS_EXPIRED, + 'code_encrypted' => null, + 'updated_at' => now(), + ]); + } } diff --git a/tests/Feature/HxbOtpRelayTest.php b/tests/Feature/HxbOtpRelayTest.php index 953862207..b95b61ecf 100644 --- a/tests/Feature/HxbOtpRelayTest.php +++ b/tests/Feature/HxbOtpRelayTest.php @@ -88,6 +88,30 @@ class HxbOtpRelayTest extends FeatureTestCase ->assertSee('验证码链接已过期'); } + public function test_submitted_code_is_cleared_after_link_expires(): void + { + Carbon::setTestNow('2026-12-02 09:00:00'); + $created = $this->postSignedJson('/api/hxb-verification/requests', [ + 'client_run_id' => (string) Str::uuid(), + ])->assertCreated()->json(); + $fillToken = basename((string) parse_url($created['fill_url'], PHP_URL_PATH)); + $this->post('/hxb-verification/' . $fillToken, ['code' => '112233']) + ->assertRedirect(); + + Carbon::setTestNow('2026-12-02 09:06:00'); + $pollUrl = '/api/hxb-verification/requests/' . $created['run_id'] . '/code'; + $this->postJson($pollUrl, [], [ + 'Authorization' => 'Bearer ' . $created['poll_token'], + ])->assertStatus(410) + ->assertJsonPath('status', 'expired'); + + $this->assertDatabaseHas('finance_verification_requests', [ + 'client_run_id' => $created['run_id'], + 'status' => 'expired', + 'code_encrypted' => null, + ]); + } + private function postSignedJson(string $path, array $payload) { $body = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); -- 2.47.2