A free open source IT asset/license management system
Fixed #19916 - Intune sync adapter re-fetching page 1 forever
snipeeee12cff2b8f
diff --git a/app/SyncAdapters/Intune/IntuneClient.php b/app/SyncAdapters/Intune/IntuneClient.phpindex b2aa2c7040..bd1e9f6af7 100644--- a/app/SyncAdapters/Intune/IntuneClient.php+++ b/app/SyncAdapters/Intune/IntuneClient.php@@ -74,17 +74,19 @@ class IntuneClient{$graphOrigin = $this->origin($this->graphBaseUrl);$url = rtrim($this->graphBaseUrl, '/').'/v1.0/deviceManagement/managedDevices';- $query = [];do {- $response = $this->request()->get($url, $query)->throw()->json();+ // Pass no second argument. Laravel forwards it as Guzzle's+ // `query` option, which replaces the URL's query string even+ // when empty, stripping the $skiptoken off `@odata.nextLink`.+ // Tenants with more than one page would loop on page 1 forever.+ // See #19916.+ $response = $this->request()->get($url)->throw()->json();foreach (($response['value'] ?? []) as $device) {yield $device;}- $query = [];-$url = $this->rebasedCursor($response['@odata.nextLink'] ?? null, $graphOrigin);} while ($url !== null);}diff --git a/tests/Feature/SyncAdapters/Intune/IntuneAdapterTest.php b/tests/Feature/SyncAdapters/Intune/IntuneAdapterTest.phpindex 3c8f3b510e..948609db8e 100644--- a/tests/Feature/SyncAdapters/Intune/IntuneAdapterTest.php+++ b/tests/Feature/SyncAdapters/Intune/IntuneAdapterTest.php@@ -92,6 +92,48 @@ class IntuneAdapterTest extends TestCase$this->assertNotNull($record->lastSeen);}+ public function test_nextlink_pagination_follows_skiptoken_cursor_through_every_page()+ {+ // Regression for #19916. Before the fix, managedDevices() passed+ // an empty-array $query to Http::get(), which Laravel forwards as+ // Guzzle's `query` option. Guzzle replaces the URL's query string+ // even when the option is an empty array, so the $skiptoken on+ // the @odata.nextLink cursor got stripped and Graph served page 1+ // forever. Tenants under ~1,000 devices never saw it because they+ // only ever had one page.+ $adapter = $this->configuredAdapter();++ $callsPerPage = ['page1' => 0, 'page2' => 0];++ Http::fake([+ '*/oauth2/v2.0/token' => Http::response(['access_token' => 'stub-bearer', 'expires_in' => 3600]),+ '*/v1.0/deviceManagement/managedDevices*' => function ($request) use (&$callsPerPage) {+ if (str_contains($request->url(), '$skiptoken=cursor-abc')) {+ $callsPerPage['page2']++;++ return Http::response([+ 'value' => [$this->intuneDevice(id: 'guid-page2', name: 'host-page2')],+ ]);+ }++ $callsPerPage['page1']++;++ return Http::response([+ 'value' => [$this->intuneDevice(id: 'guid-page1', name: 'host-page1')],+ '@odata.nextLink' => 'https://graph.microsoft.com/v1.0/deviceManagement/managedDevices?$skiptoken=cursor-abc',+ ]);+ },+ ]);++ $records = iterator_to_array($adapter->pull(), preserve_keys: false);++ $ids = array_map(fn ($record) => $record->sourceId, $records);+ $this->assertEqualsCanonicalizing(['guid-page1', 'guid-page2'], $ids,+ 'Pagination must follow @odata.nextLink and yield both pages, not loop on page 1.');+ $this->assertSame(1, $callsPerPage['page1'], 'Page 1 should be fetched exactly once.');+ $this->assertSame(1, $callsPerPage['page2'], 'Page 2 (via $skiptoken) should be fetched exactly once.');+ }+public function test_nextlink_cursor_cannot_redirect_bearer_to_arbitrary_host(){// Graph normally serves `@odata.nextLink` from the same host as