A free open source IT asset/license management system
Switch to `stripos(...) === 0`
snipe7e617bbfa311
diff --git a/app/Http/Middleware/EnforceApiUserAgent.php b/app/Http/Middleware/EnforceApiUserAgent.phpindex 02d6297d19..bca25930ab 100644--- a/app/Http/Middleware/EnforceApiUserAgent.php+++ b/app/Http/Middleware/EnforceApiUserAgent.php@@ -34,8 +34,13 @@ class EnforceApiUserAgentreturn $next($request);}+ // Prefix match (=== 0) rather than substring: scripted clients identify+ // themselves at the start of the User-Agent (curl/x, PostmanRuntime/x,+ // python-requests/x, etc.), and matching only the prefix prevents a pattern+ // from accidentally hitting an unrelated UA that mentions it later in the+ // string.foreach ($this->blockedPatterns($setting->blocked_api_user_agents) as $pattern) {- if (stripos($userAgent, $pattern) !== false) {+ if (stripos($userAgent, $pattern) === 0) {return $this->reject($rawUserAgent);}}diff --git a/resources/lang/en-US/admin/settings/general.php b/resources/lang/en-US/admin/settings/general.phpindex c16830bbd3..705134aa7a 100644--- a/resources/lang/en-US/admin/settings/general.php+++ b/resources/lang/en-US/admin/settings/general.php@@ -168,7 +168,7 @@ return ['block_api_user_agents_text' => 'Block API requests by User-Agent','block_api_user_agents_help' => 'When enabled, API requests whose User-Agent matches any of the patterns below will be rejected.','blocked_api_user_agents_text' => 'Blocked API User-Agent patterns',- 'blocked_api_user_agents_help' => 'One pattern per line, matched as a case-insensitive substring of the request\'s User-Agent. The textarea is pre-filled with common scripted clients (curl, Postman, python-requests, etc.). Add, remove, or clear lines as needed. ',+ 'blocked_api_user_agents_help' => 'One pattern per line. Each pattern is matched case-insensitively against the start of the request\'s User-Agent, so "curl/" matches "curl/8.5.0" but not a UA that only mentions "curl/" later in the string. The textarea is pre-filled with common scripted clients (curl, Postman, python-requests, etc.). Add, remove, or clear lines as needed.','block_blank_api_user_agents_text' => 'Also reject API requests with a blank User-Agent','block_blank_api_user_agents_help' => 'When enabled, API requests that arrive without a User-Agent header (or with an empty one) are rejected. <strong>Leave this off if you have integrations that legitimately send an empty User-Agent, for example, Microsoft Entra ID SCIM provisioning.</strong>','blocked_api_user_agent_rejected' => 'This API endpoint rejected the request based on its User-Agent.',diff --git a/tests/Feature/Api/EnforceApiUserAgentTest.php b/tests/Feature/Api/EnforceApiUserAgentTest.phpindex 8b02da0e2c..24b8ca26a7 100644--- a/tests/Feature/Api/EnforceApiUserAgentTest.php+++ b/tests/Feature/Api/EnforceApiUserAgentTest.php@@ -128,6 +128,27 @@ class EnforceApiUserAgentTest extends TestCase]);}+ public function test_pattern_is_matched_only_at_start_of_user_agent()+ {+ $this->settings->set([+ 'block_api_user_agents' => '1',+ 'blocked_api_user_agents' => 'curl/',+ 'block_blank_api_user_agents' => '0',+ ]);++ Passport::actingAs(User::factory()->superuser()->create());++ // Mid-string mention of "curl/" should NOT be blocked under prefix matching.+ $this->withHeader('User-Agent', 'MyWrapper/1.0 (uses curl/8.5.0 internally)')+ ->getJson(route('api.users.selectlist'))+ ->assertOk();++ // Pattern at position 0 still blocks.+ $this->withHeader('User-Agent', 'curl/8.5.0')+ ->getJson(route('api.users.selectlist'))+ ->assertForbidden();+ }+public function test_master_on_empty_pattern_lines_do_not_match_every_request(){// Without filtering, a blank line would be a zero-length substring that matches every UA.