From 1c1308bee4f0748cb7c0006867a26c35025fc16b Mon Sep 17 00:00:00 2001 From: Falguni Moridhara Date: Fri, 17 Jul 2026 20:19:04 +0530 Subject: [PATCH] feat: detect URLs in PII Redaction Middleware --- .../src/Defaults/PIIRedactorDefaults.php | 1 + src/PIIRedactor/src/Enums/EntityTypes.php | 1 + src/PIIRedactor/src/PIIRedactor.php | 4 ++++ src/PIIRedactor/tests/PIIRedactorTest.php | 19 +++++++++++++++++++ src/Support/config/intercept.php | 1 + 5 files changed, 26 insertions(+) diff --git a/src/PIIRedactor/src/Defaults/PIIRedactorDefaults.php b/src/PIIRedactor/src/Defaults/PIIRedactorDefaults.php index 00603e8..9a4de7e 100644 --- a/src/PIIRedactor/src/Defaults/PIIRedactorDefaults.php +++ b/src/PIIRedactor/src/Defaults/PIIRedactorDefaults.php @@ -23,6 +23,7 @@ public static function values(): array 'api_key', 'bearer_token', 'mac_address', + 'url', ], 'block_entities' => [ 'credit_card', diff --git a/src/PIIRedactor/src/Enums/EntityTypes.php b/src/PIIRedactor/src/Enums/EntityTypes.php index bd0a10a..df69886 100644 --- a/src/PIIRedactor/src/Enums/EntityTypes.php +++ b/src/PIIRedactor/src/Enums/EntityTypes.php @@ -24,4 +24,5 @@ enum EntityTypes: string case IP_ADDRESS = 'ip_address'; case PHONE = 'phone'; case MAC_ADDRESS = 'mac_address'; + case URL = 'url'; } diff --git a/src/PIIRedactor/src/PIIRedactor.php b/src/PIIRedactor/src/PIIRedactor.php index 5d3c669..7300637 100644 --- a/src/PIIRedactor/src/PIIRedactor.php +++ b/src/PIIRedactor/src/PIIRedactor.php @@ -373,6 +373,10 @@ protected function defaultDetectors(): array EntityTypes::MAC_ADDRESS->value, '/\b(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}\b/' ), + new RegexDetector( + EntityTypes::URL->value, + '/\b(?:https?:\/\/|www\.)[a-zA-Z0-9+&@#\/%?=~_|!:,.;]*[a-zA-Z0-9+&@#\/%=~_|]|\b(?prompt)->toBe('The router MAC is [MAC_ADDRESS_1]'); }); +it('redacts URLs by default', function (): void { + Log::shouldReceive('warning')->once(); + + $redactor = new PIIRedactor; + + $forwardedPrompt = null; + + $redactor->handle( + makePIIRedactorAgentPrompt('Look at http://example.com, check the page https://example.com, visit the site at www.example.com, or https://127.0.0.1:8080, or view the example.com/dashboard.'), + function (AgentPrompt $prompt) use (&$forwardedPrompt): string { + $forwardedPrompt = $prompt; + + return 'continued'; + }, + ); + + expect($forwardedPrompt->prompt)->toBe('Look at [URL_1], check the page [URL_2], visit the site at [URL_3], or [URL_4], or view the [URL_5].'); +}); + it('blocks credit cards by default', function (): void { Log::shouldReceive('warning')->once(); diff --git a/src/Support/config/intercept.php b/src/Support/config/intercept.php index 9e4959f..95f3b92 100644 --- a/src/Support/config/intercept.php +++ b/src/Support/config/intercept.php @@ -90,6 +90,7 @@ 'api_key', 'bearer_token', 'mac_address', + 'url', ], /**