Skip to content

A free open source IT asset/license management system

API/User Agent: Apply to SCIM routes

snipec45e1e2928d7
diff --git a/routes/scim.php b/routes/scim.php
index 06ba02afb8..3aaae04309 100644
--- a/routes/scim.php
+++ b/routes/scim.php
@@ -1,22 +1,22 @@
<?php
+use App\Http\Middleware\EnforceApiUserAgent;
use ArieTimmerman\Laravel\SCIMServer\RouteProvider as SCIMRouteProvider;
use Illuminate\Support\Facades\Route;
-
/*
|--------------------------------------------------------------------------
| SCIM Routes
|--------------------------------------------------------------------------
|
-| These are the routes that we have to explicitly inject from the
+| These are the routes that we have to explicitly inject from the
| laravel-scim-server project, which gives Snipe-IT SCIM support
|
*/
SCIMRouteProvider::publicRoutes(); // Make sure to load public routes *FIRST*
-Route::middleware(['auth:api', 'api-throttle:api', 'authorize:superadmin'])->group(function () {
+Route::middleware(['auth:api', EnforceApiUserAgent::class, 'api-throttle:api', 'authorize:superadmin'])->group(function () {
SCIMRouteProvider::routes(
[
/*
@@ -30,7 +30,7 @@ Route::middleware(['auth:api', 'api-throttle:api', 'authorize:superadmin'])->gro
* like 4 hours of your life trying to figure out why the public routes
* aren't quite working right. Ask me how I know (BMW, 3/19/2022)
*/
- 'public_routes' => false
+ 'public_routes' => false,
]
);
diff --git a/tests/Feature/Api/EnforceApiUserAgentTest.php b/tests/Feature/Api/EnforceApiUserAgentTest.php
index 24b8ca26a7..fec3947eda 100644
--- a/tests/Feature/Api/EnforceApiUserAgentTest.php
+++ b/tests/Feature/Api/EnforceApiUserAgentTest.php
@@ -165,6 +165,40 @@ class EnforceApiUserAgentTest extends TestCase
->assertOk();
}
+ public function test_middleware_is_applied_to_scim_routes()
+ {
+ $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());
+
+ $this->withHeader('User-Agent', 'curl/8.5.0')
+ ->getJson('/scim/v2/Users')
+ ->assertForbidden()
+ ->assertJson([
+ 'status' => 'error',
+ 'payload' => ['user_agent' => 'curl/8.5.0'],
+ ]);
+ }
+
+ public function test_blank_blocking_applies_to_scim_routes()
+ {
+ $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('/scim/v2/Users')
+ ->assertForbidden();
+ }
+
public function test_default_patterns_constant_is_non_empty()
{
$this->assertNotEmpty(Setting::DEFAULT_BLOCKED_API_USER_AGENTS);