Skip to content

A free open source IT asset/license management system

Fixed route parameter $next, updated language, added tests

snipe16bc195767a9
diff --git a/app/Http/Middleware/EnforceApiUserAgent.php b/app/Http/Middleware/EnforceApiUserAgent.php
index bca25930ab..ae07c1bb60 100644
--- a/app/Http/Middleware/EnforceApiUserAgent.php
+++ b/app/Http/Middleware/EnforceApiUserAgent.php
@@ -10,27 +10,42 @@ use Symfony\Component\HttpFoundation\Response;
class EnforceApiUserAgent
{
- public function handle(Request $request, Closure $next): Response
+ /**
+ * The route-level parameter that lets a route opt out of blank-User-Agent
+ * blocking. Routes that legitimately receive blank UAs (e.g. Entra ID SCIM
+ * provisioning) declare this via `EnforceApiUserAgent::class.':allow_blank_user_agent'`.
+ */
+ private const ALLOW_BLANK_PARAMETER = 'allow_blank_user_agent';
+
+ public function handle(Request $request, Closure $next, ?string $parameter = null): Response
{
$setting = Setting::getSettings();
+
+ // Bail out entirely when User-Agent blocking is disabled, regardless of
+ // whether a route opts out of blank blocking — the whole feature is off.
+ if (! $setting?->block_api_user_agents) {
+ return $next($request);
+ }
+
$rawUserAgent = $request->header('User-Agent');
$userAgent = trim((string) $rawUserAgent);
- // Handle the blank-UA case up front. Blank blocking is independent of the
- // pattern master so an admin can leave it off for integrations that legitimately
- // send a blank User-Agent (e.g. Entra SCIM provisioning).
if ($userAgent === '') {
- if ($setting?->block_blank_api_user_agents) {
- return $this->reject($rawUserAgent);
+ // Hard override: routes that legitimately receive blank UAs (SCIM,
+ // hit by Entra ID provisioning) pass the allow_blank_user_agent
+ // parameter and always succeed here, regardless of admin settings.
+ if ($parameter === self::ALLOW_BLANK_PARAMETER) {
+ return $next($request);
}
- return $next($request);
- }
+ // Otherwise the admin's block_blank_api_user_agents toggle decides.
+ // Pattern blocking on + blank blocking off lets blank-UA integrations
+ // through on the regular API surface; flipping blank blocking on
+ // tightens it.
+ if ($setting->block_blank_api_user_agents) {
+ return $this->reject($rawUserAgent);
+ }
- // From here on we know the UA is non-blank.
- // Pattern-based blocking is gated behind block_api_user_agents so the textarea
- // can be pre-populated with sensible defaults without auto-enabling blocking.
- if (! $setting?->block_api_user_agents) {
return $next($request);
}
diff --git a/resources/lang/en-US/admin/settings/general.php b/resources/lang/en-US/admin/settings/general.php
index 061024262e..7129dfa9b2 100644
--- a/resources/lang/en-US/admin/settings/general.php
+++ b/resources/lang/en-US/admin/settings/general.php
@@ -170,7 +170,7 @@ return [
'blocked_api_user_agents_text' => 'Blocked API User-Agent patterns',
'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>This will not apply to SCIM requests, as Microsoft EntraID sends a blank User-Agent.</strong>',
+ 'block_blank_api_user_agents_help' => 'Also reject requests that arrive with a blank or missing User-Agent header. <strong>SCIM endpoints ignore this setting and always accept blank User-Agents, since Microsoft Entra ID SCIM provisioning sends a blank User-Agent.</strong> Leave this off if you have other integrations that legitimately send blank User-Agents.',
'blocked_api_user_agent_rejected' => 'This API endpoint rejected the request based on its User-Agent.',
'login_common_disabled_text' => 'Disable other authentication mechanisms',
'login_common_disabled_help' => 'This option disables other authentication mechanisms. Just enable this option if you are sure that your REMOTE_USER login is already working',
diff --git a/resources/views/settings/api.blade.php b/resources/views/settings/api.blade.php
index 45baab7785..2060d1303e 100644
--- a/resources/views/settings/api.blade.php
+++ b/resources/views/settings/api.blade.php
@@ -78,7 +78,7 @@
</div>
</div>
- <!-- Reject blank User-Agents (independent toggle) -->
+ <!-- Reject blank User-Agents (SCIM routes ignore this) -->
<div class="form-group {{ $errors->has('block_blank_api_user_agents') ? 'error' : '' }}">
<div class="col-md-8 col-md-offset-3">
<label class="form-control">
diff --git a/tests/Feature/Api/EnforceApiUserAgentTest.php b/tests/Feature/Api/EnforceApiUserAgentTest.php
index fec3947eda..cc29c612e6 100644
--- a/tests/Feature/Api/EnforceApiUserAgentTest.php
+++ b/tests/Feature/Api/EnforceApiUserAgentTest.php
@@ -9,13 +9,14 @@ use Tests\TestCase;
class EnforceApiUserAgentTest extends TestCase
{
- public function test_master_off_allows_matching_user_agents_to_pass()
+ public function test_master_off_bails_out_entirely_so_nothing_is_blocked()
{
- // Master gate off — even with a populated pattern list, nothing should be blocked.
+ // Master off — even a curl UA AND a blank UA pass through, regardless of
+ // any other setting or per-route parameter.
$this->settings->set([
'block_api_user_agents' => '0',
'blocked_api_user_agents' => "curl/\nPostmanRuntime/",
- 'block_blank_api_user_agents' => '0',
+ 'block_blank_api_user_agents' => '1',
]);
Passport::actingAs(User::factory()->superuser()->create());
@@ -23,6 +24,10 @@ class EnforceApiUserAgentTest extends TestCase
$this->withHeader('User-Agent', 'curl/8.5.0')
->getJson(route('api.users.selectlist'))
->assertOk();
+
+ $this->withHeader('User-Agent', '')
+ ->getJson(route('api.users.selectlist'))
+ ->assertOk();
}
public function test_master_on_blocks_matching_pattern_and_echoes_user_agent()
@@ -74,12 +79,13 @@ class EnforceApiUserAgentTest extends TestCase
->assertOk();
}
- public function test_master_on_blank_user_agent_passes_when_blank_blocking_is_off()
+ public function test_master_on_blank_blocking_off_lets_blank_user_agent_through_on_api()
{
- // The Entra SCIM scenario: pattern blocking is on, blanks are explicitly allowed.
+ // Pattern blocking on but blank blocking off: an admin who has webhooks/other
+ // integrations that send blanks can keep them working.
$this->settings->set([
'block_api_user_agents' => '1',
- 'blocked_api_user_agents' => "curl/\nPostmanRuntime/",
+ 'blocked_api_user_agents' => 'curl/',
'block_blank_api_user_agents' => '0',
]);
@@ -90,11 +96,10 @@ class EnforceApiUserAgentTest extends TestCase
->assertOk();
}
- public function test_blank_user_agent_blocked_when_blank_blocking_on_even_if_master_is_off()
+ public function test_master_on_blank_blocking_on_rejects_blank_user_agent_on_api()
{
- // Blank blocking is independent of the master pattern gate.
$this->settings->set([
- 'block_api_user_agents' => '0',
+ 'block_api_user_agents' => '1',
'blocked_api_user_agents' => null,
'block_blank_api_user_agents' => '1',
]);
@@ -105,29 +110,10 @@ class EnforceApiUserAgentTest extends TestCase
->getJson(route('api.users.selectlist'))
->assertForbidden()
->assertJson([
- 'status' => 'error',
'payload' => ['user_agent' => ''],
]);
}
- public function test_whitespace_only_user_agent_treated_as_blank_and_echoed_verbatim()
- {
- $this->settings->set([
- 'block_api_user_agents' => '0',
- 'blocked_api_user_agents' => null,
- 'block_blank_api_user_agents' => '1',
- ]);
-
- Passport::actingAs(User::factory()->superuser()->create());
-
- $this->withHeader('User-Agent', ' ')
- ->getJson(route('api.users.selectlist'))
- ->assertForbidden()
- ->assertJson([
- 'payload' => ['user_agent' => ' '],
- ]);
- }
-
public function test_pattern_is_matched_only_at_start_of_user_agent()
{
$this->settings->set([
@@ -138,12 +124,10 @@ class EnforceApiUserAgentTest extends TestCase
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();
@@ -165,7 +149,7 @@ class EnforceApiUserAgentTest extends TestCase
->assertOk();
}
- public function test_middleware_is_applied_to_scim_routes()
+ public function test_scim_routes_block_pattern_matches()
{
$this->settings->set([
'block_api_user_agents' => '1',
@@ -179,24 +163,26 @@ class EnforceApiUserAgentTest extends TestCase
->getJson('/scim/v2/Users')
->assertForbidden()
->assertJson([
- 'status' => 'error',
'payload' => ['user_agent' => 'curl/8.5.0'],
]);
}
- public function test_blank_blocking_applies_to_scim_routes()
+ public function test_scim_routes_allow_blank_user_agent_even_when_admin_blocks_blanks()
{
+ // Hard override: even with the admin toggle set to block blanks, SCIM routes
+ // pass through because Entra ID SCIM provisioning sends a blank User-Agent.
$this->settings->set([
- 'block_api_user_agents' => '0',
+ 'block_api_user_agents' => '1',
'blocked_api_user_agents' => null,
'block_blank_api_user_agents' => '1',
]);
Passport::actingAs(User::factory()->superuser()->create());
- $this->withHeader('User-Agent', '')
- ->getJson('/scim/v2/Users')
- ->assertForbidden();
+ $response = $this->withHeader('User-Agent', '')
+ ->getJson('/scim/v2/Users');
+
+ $this->assertNotEquals(403, $response->status());
}
public function test_default_patterns_constant_is_non_empty()
diff --git a/tests/Feature/Settings/ApiRequestFiltersTest.php b/tests/Feature/Settings/ApiRequestFiltersTest.php
index 8caaa44c7c..12484829c5 100644
--- a/tests/Feature/Settings/ApiRequestFiltersTest.php
+++ b/tests/Feature/Settings/ApiRequestFiltersTest.php
@@ -52,6 +52,21 @@ class ApiRequestFiltersTest extends TestCase
$this->assertTrue($fresh->block_blank_api_user_agents);
}
+ public function test_post_without_blank_flag_treats_it_as_false()
+ {
+ Setting::getSettings()->forceFill(['block_blank_api_user_agents' => true])->save();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('settings.oauth.request_filters.save'), [
+ 'block_api_user_agents' => '1',
+ 'blocked_api_user_agents' => 'curl/',
+ // block_blank_api_user_agents intentionally absent — hidden=0 sentinel covers it
+ ])
+ ->assertRedirect();
+
+ $this->assertFalse(Setting::getSettings()->fresh()->block_blank_api_user_agents);
+ }
+
public function test_post_with_empty_textarea_clears_patterns_to_null()
{
Setting::getSettings()->forceFill(['blocked_api_user_agents' => 'curl/'])->save();
@@ -60,7 +75,6 @@ class ApiRequestFiltersTest extends TestCase
->post(route('settings.oauth.request_filters.save'), [
'block_api_user_agents' => '1',
'blocked_api_user_agents' => '',
- 'block_blank_api_user_agents' => '0',
])
->assertRedirect();
@@ -78,7 +92,6 @@ class ApiRequestFiltersTest extends TestCase
$this->actingAs(User::factory()->superuser()->create())
->post(route('settings.oauth.request_filters.save'), [
- // block_api_user_agents intentionally absent (unchecked + hidden=0 sentinel)
'block_api_user_agents' => '0',
'blocked_api_user_agents' => "curl/\nfoo-client/",
])
@@ -89,18 +102,17 @@ class ApiRequestFiltersTest extends TestCase
$this->assertSame("curl/\nfoo-client/", $fresh->blocked_api_user_agents);
}
- public function test_post_without_blank_flag_treats_it_as_false()
+ public function test_post_without_master_flag_treats_it_as_false()
{
- Setting::getSettings()->forceFill(['block_blank_api_user_agents' => true])->save();
+ Setting::getSettings()->forceFill(['block_api_user_agents' => true])->save();
$this->actingAs(User::factory()->superuser()->create())
->post(route('settings.oauth.request_filters.save'), [
- 'block_api_user_agents' => '0',
+ // block_api_user_agents intentionally absent — hidden=0 sentinel covers it
'blocked_api_user_agents' => '',
- // block_blank_api_user_agents intentionally absent
])
->assertRedirect();
- $this->assertFalse(Setting::getSettings()->fresh()->block_blank_api_user_agents);
+ $this->assertFalse(Setting::getSettings()->fresh()->block_api_user_agents);
}
}