Skip to content

A free open source IT asset/license management system

SCIM Group: Fixed #19493 subclass Group for scoped create semantics

snipe68dbab57e8f8
diff --git a/app/Models/SCIMGroup.php b/app/Models/SCIMGroup.php
new file mode 100644
index 0000000000..84ebf2e869
--- /dev/null
+++ b/app/Models/SCIMGroup.php
@@ -0,0 +1,62 @@
+<?php
+
+namespace App\Models;
+
+class SCIMGroup extends Group
+{
+ protected $table = 'permission_groups';
+
+ /**
+ * Override the default firstOrNew() to always return a fresh instance
+ * when called with an empty conditions array.
+ *
+ * The upstream SCIM library's create path lives at
+ * vendor/arietimmerman/laravel-scim-server/src/Http/Controllers/ResourceController.php,
+ * and hardcodes a username-only lookup for the "does this resource
+ * already exist" check:
+ *
+ * $resourceObject = $class::firstOrNew(
+ * $request->has('userName') ? ['username' => $input['userName']] : []
+ * );
+ *
+ * For User POSTs that's fine: firstOrNew(['username' => ...]) resolves
+ * to an existing user or a new one. For Group POSTs the ternary always
+ * lands on the empty-array branch (groups have no userName in their
+ * SCIM payload), and Laravel's Model::firstOrNew([]) issues a WHERE
+ * with no conditions, which returns THE FIRST ROW OF THE TABLE. Every
+ * subsequent group POST resolves to that same row and the attribute
+ * mappers overwrite its name / externalId / members instead of
+ * creating a new group. The observed symptom is that every synced
+ * group collapses into whatever group happens to be id=1, and every
+ * SCIM-provisioned user ends up as a member of that one group. If the
+ * pre-existing id=1 group carried elevated permissions (e.g. an
+ * "Admins" group created before SCIM sync was enabled), every synced
+ * user auto-elevated to those permissions. See #19493.
+ *
+ * Overriding firstOrNew here is the least invasive fix: SCIM's Group
+ * route creates go through this subclass (via SnipeSCIMConfig::getGroupClass),
+ * so the create path always returns a new model. Any callers passing
+ * real conditions get the default Eloquent behavior.
+ */
+ public static function firstOrNew(array $attributes = [], array $values = [])
+ {
+ if (empty($attributes)) {
+ return new self;
+ }
+
+ return parent::firstOrNew($attributes, $values);
+ }
+
+ // Have to re-define these here because Eloquent will try to 'guess' a
+ // foreign key of s_c_i_m_group_id from SCIMGroup. Mirrors the reason
+ // SCIMUser re-defines groups() the same way.
+ public function users()
+ {
+ return $this->belongsToMany(\App\Models\User::class, 'users_groups', 'group_id', 'user_id');
+ }
+
+ public function members()
+ {
+ return $this->users();
+ }
+}
diff --git a/app/Models/SnipeSCIMConfig.php b/app/Models/SnipeSCIMConfig.php
index 94e5a2c54f..b789a3f892 100644
--- a/app/Models/SnipeSCIMConfig.php
+++ b/app/Models/SnipeSCIMConfig.php
@@ -269,8 +269,8 @@ class SnipeMutableCollection extends MutableCollection
{
$missing = [];
foreach ((array) $value as $index => $entry) {
- if (!is_array($entry)
- || !array_key_exists('value', $entry)
+ if (! is_array($entry)
+ || ! array_key_exists('value', $entry)
|| $entry['value'] === null
|| $entry['value'] === ''
) {
@@ -279,7 +279,7 @@ class SnipeMutableCollection extends MutableCollection
}
if ($missing !== []) {
throw new SCIMException(
- 'Every members entry must include a "value" field. Missing at indices: ' . implode(',', $missing),
+ 'Every members entry must include a "value" field. Missing at indices: '.implode(',', $missing),
400
);
}
@@ -423,7 +423,7 @@ class SCIMMultiCompanyArray extends Attribute
// This seems to be how Okta does it?
$names[] = $company;
} else {
- throw new SCIMException("Unknown 'companies' value: '" . print_r($company, true) . "' of type: " . get_debug_type($company), 400);
+ throw new SCIMException("Unknown 'companies' value: '".print_r($company, true)."' of type: ".get_debug_type($company), 400);
}
}
@@ -434,25 +434,25 @@ class SCIMMultiCompanyArray extends Attribute
if ($object->exists) {
$object->companies()->sync($ids);
} else {
- $object->saved(fn() => $object->companies()->sync($ids));
+ $object->saved(fn () => $object->companies()->sync($ids));
}
}
public function add($value, Model &$object)
{
- \Log::debug('MC ADD VALUE IS: ' . print_r($value, true));
+ \Log::debug('MC ADD VALUE IS: '.print_r($value, true));
$this->applyCompanies($value, $object);
}
public function replace($value, Model &$object, $path = null, $removeIfNotSet = false)
{
- \Log::debug('MC REPLACE VALUE IS: ' . print_r($value, true));
+ \Log::debug('MC REPLACE VALUE IS: '.print_r($value, true));
$this->applyCompanies($value, $object);
}
public function patch($operation, $value, Model &$object, ?Path $path = null, $removeIfNotSet = false)
{
- \Log::debug('MC PATCH VALUE IS: ' . print_r($value, true));
+ \Log::debug('MC PATCH VALUE IS: '.print_r($value, true));
$this->applyCompanies($value, $object);
}
}
@@ -506,7 +506,11 @@ class SnipeSCIMConfig
public function getGroupClass()
{
- return Group::class;
+ // Route SCIM group operations through SCIMGroup so that the
+ // upstream library's create-path firstOrNew([]) call resolves
+ // to a fresh Group instance rather than the first existing row.
+ // See SCIMGroup and #19493 for the underlying library bug.
+ return SCIMGroup::class;
}
const ENTERPRISE = 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User';
@@ -737,7 +741,7 @@ class SnipeSCIMConfig
} else {
// Okta hits this one for creating a user - it does a full PUT for their ID
\Log::debug("GetValuePAthFilter is null for path: $path");
- \Log::debug('GetValuePathFilter is now null and trying to set value of: ' . print_r($value, true));
+ \Log::debug('GetValuePathFilter is now null and trying to set value of: '.print_r($value, true));
// the Addresses object is a 'list' (array with numeric indices) by definition...
if (is_array($value) && array_is_list($value)) {
foreach ($value as $address) {
@@ -745,7 +749,7 @@ class SnipeSCIMConfig
if (@$address['type'] == 'work') {
foreach ($address as $key => $v) {
if (array_key_exists($key, self::$addressmap)) {
- \Log::debug('Addresses: Setting ' . self::$addressmap[$key] . " to '$v'");
+ \Log::debug('Addresses: Setting '.self::$addressmap[$key]." to '$v'");
$object->{self::$addressmap[$key]} = $v;
}
}
@@ -755,8 +759,8 @@ class SnipeSCIMConfig
}
}
} else {
- \Log::debug('Unknown Address Object: ' . print_r($value, true));
- throw new SCIMException('Unknown Address object of type: ' . gettype($value), 422);
+ \Log::debug('Unknown Address Object: '.print_r($value, true));
+ throw new SCIMException('Unknown Address object of type: '.gettype($value), 422);
}
}
}
diff --git a/tests/Feature/Scim/CreateMultipleGroupsTest.php b/tests/Feature/Scim/CreateMultipleGroupsTest.php
new file mode 100644
index 0000000000..6a025c0d86
--- /dev/null
+++ b/tests/Feature/Scim/CreateMultipleGroupsTest.php
@@ -0,0 +1,97 @@
+<?php
+
+namespace Tests\Feature\Scim;
+
+use App\Models\Group;
+use App\Models\User;
+use Laravel\Passport\Passport;
+use Tests\TestCase;
+
+/**
+ * Regression coverage for #19493. Reporter (sajax) traced the SCIM group
+ * bug to the upstream library's create path in
+ * vendor/arietimmerman/laravel-scim-server/src/Http/Controllers/ResourceController.php:71,
+ * which does:
+ *
+ * $resourceObject = $class::firstOrNew(
+ * $request->has('userName') ? ['username' => $input['userName']] : []
+ * );
+ *
+ * For User POSTs that's fine. For Group POSTs the ternary always lands
+ * on the empty-array branch, and Laravel's Model::firstOrNew([]) returns
+ * the first existing row of the table. So the second and every
+ * subsequent group POST resolved to the same row (id=1), and the
+ * attribute mappers overwrote its name / externalId in place instead
+ * of creating a new group. Fix routes SCIM group operations through
+ * a SCIMGroup subclass whose firstOrNew always returns a fresh instance
+ * on empty-conditions calls.
+ */
+class CreateMultipleGroupsTest extends TestCase
+{
+ public function test_two_consecutive_group_posts_create_two_distinct_groups()
+ {
+ Passport::actingAs(User::factory()->superuser()->create());
+
+ $first = $this->postJson('/scim/v2/Groups', [
+ 'schemas' => ['urn:ietf:params:scim:schemas:core:2.0:Group'],
+ 'externalId' => 'test-group-a',
+ 'displayName' => 'SCIM Test Group A',
+ 'members' => [],
+ 'meta' => ['resourceType' => 'Group'],
+ ]);
+
+ $second = $this->postJson('/scim/v2/Groups', [
+ 'schemas' => ['urn:ietf:params:scim:schemas:core:2.0:Group'],
+ 'externalId' => 'test-group-b',
+ 'displayName' => 'SCIM Test Group B',
+ 'members' => [],
+ 'meta' => ['resourceType' => 'Group'],
+ ]);
+
+ $first->assertStatus(201);
+ $second->assertStatus(201);
+
+ $groupA = Group::where('name', 'SCIM Test Group A')->firstOrFail();
+ $groupB = Group::where('name', 'SCIM Test Group B')->firstOrFail();
+
+ $this->assertNotSame(
+ $groupA->id,
+ $groupB->id,
+ 'Two consecutive SCIM group POSTs must create two distinct rows, not overwrite the first row.',
+ );
+ }
+
+ public function test_second_group_post_does_not_overwrite_a_pre_existing_group()
+ {
+ // Angl0r's scenario. If an admin group existed BEFORE SCIM sync
+ // was enabled, the first SCIM group POST used to resolve to that
+ // pre-existing row via firstOrNew([]), rewrite its name to the
+ // synced group's name, and then every subsequently synced user
+ // landed in what was originally the Admins row, silently
+ // inheriting the pre-existing group's permission bits.
+ $preexisting = Group::factory()->create(['name' => 'Preexisting Admins']);
+ $preexistingId = $preexisting->id;
+
+ Passport::actingAs(User::factory()->superuser()->create());
+
+ $response = $this->postJson('/scim/v2/Groups', [
+ 'schemas' => ['urn:ietf:params:scim:schemas:core:2.0:Group'],
+ 'externalId' => 'entra-staff-group',
+ 'displayName' => 'Staff From Entra',
+ 'members' => [],
+ 'meta' => ['resourceType' => 'Group'],
+ ]);
+
+ $response->assertStatus(201);
+
+ $preexisting->refresh();
+ $this->assertSame('Preexisting Admins', $preexisting->name, 'The pre-existing group name must not be overwritten by a SCIM group create.');
+
+ $newGroup = Group::where('name', 'Staff From Entra')->firstOrFail();
+ $this->assertNotSame(
+ $preexistingId,
+ $newGroup->id,
+ 'The new group must be a new row, not the pre-existing row rewritten.',
+ );
+ }
+}