Skip to content

A free open source IT asset/license management system

Fixed FD-57813 - better handle cursor redirects on sync adapters

snipe838ff6b76587
diff --git a/app/SyncAdapters/CustomHttp/CustomHttpAdapter.php b/app/SyncAdapters/CustomHttp/CustomHttpAdapter.php
index 041243bc86..b25079c52a 100644
--- a/app/SyncAdapters/CustomHttp/CustomHttpAdapter.php
+++ b/app/SyncAdapters/CustomHttp/CustomHttpAdapter.php
@@ -831,19 +831,37 @@ class CustomHttpAdapter extends SyncAdapter implements PushableAdapter
*/
private function fetchResponseBody(?string $overrideUrl = null, array $queryParams = []): mixed
{
+ $baseUrl = rtrim($this->url(), '/');
+ if ($baseUrl === '') {
+ Log::channel('sync-adapters')->warning(sprintf(
+ '%s pull aborted: base URL is empty',
+ $this->name(),
+ ));
+
+ return null;
+ }
+
if ($overrideUrl !== null) {
- $endpoint = $overrideUrl;
- } else {
- $baseUrl = rtrim($this->url(), '/');
- if ($baseUrl === '') {
- Log::channel('sync-adapters')->warning(sprintf(
- '%s pull aborted: base URL is empty',
- $this->name(),
- ));
+ // next_url pagination follows a URL from the response body.
+ // Strip to path+query and pin the scheme+host from the
+ // configured base URL so a hostile upstream can't redirect
+ // the next request (and its auth header) to an attacker-
+ // chosen target.
+ $origin = $this->configuredOrigin($baseUrl);
+ if ($origin === null) {
+ Log::channel('sync-adapters')->warning($this->name().' pull aborted: base URL is unparseable');
return null;
}
+ $parsed = parse_url($overrideUrl);
+ if (! is_array($parsed)) {
+ Log::channel('sync-adapters')->warning($this->name().' pull aborted: next-page URL is malformed');
+
+ return null;
+ }
+ $endpoint = $origin.($parsed['path'] ?? '/').(isset($parsed['query']) ? '?'.$parsed['query'] : '');
+ } else {
$pullPath = $this->safeCredential('pull_path');
if ($pullPath !== '' && ! str_starts_with($pullPath, '/')) {
$pullPath = '/'.$pullPath;
@@ -861,6 +879,21 @@ class CustomHttpAdapter extends SyncAdapter implements PushableAdapter
return $request->get($endpoint, $queryParams)->throw()->json();
}
+ /**
+ * Extract scheme+host(+port) from the configured base URL so a
+ * server-supplied next-page URL can be re-based onto the admin's
+ * configured host. Returns null when the base URL is unparseable.
+ */
+ private function configuredOrigin(string $baseUrl): ?string
+ {
+ $parts = parse_url($baseUrl);
+ if (! is_array($parts) || empty($parts['scheme']) || empty($parts['host'])) {
+ return null;
+ }
+
+ return $parts['scheme'].'://'.$parts['host'].(isset($parts['port']) ? ':'.$parts['port'] : '');
+ }
+
/**
* Attach the configured auth shape to the pending request.
* Silent no-op on 'none' (or an unrecognized method), so a
diff --git a/app/SyncAdapters/Intune/IntuneClient.php b/app/SyncAdapters/Intune/IntuneClient.php
index 73dd22f9be..bddde11ac5 100644
--- a/app/SyncAdapters/Intune/IntuneClient.php
+++ b/app/SyncAdapters/Intune/IntuneClient.php
@@ -35,15 +35,13 @@ class IntuneClient
/**
* Iterate every managed device visible to the app registration.
- * Uses Graph's `@odata.nextLink` for cursor pagination. The
- * cursor URL is absolute and includes the base host, so we hand
- * it straight to Http::get() rather than composing against
- * $graphBaseUrl a second time.
+ * Uses Graph's `@odata.nextLink` for cursor pagination.
*
* @return iterable<int, array<string, mixed>>
*/
public function managedDevices(): iterable
{
+ $graphOrigin = $this->origin($this->graphBaseUrl);
$url = rtrim($this->graphBaseUrl, '/').'/v1.0/deviceManagement/managedDevices';
do {
@@ -53,10 +51,46 @@ class IntuneClient
yield $device;
}
- $url = $response['@odata.nextLink'] ?? null;
+ $url = $this->rebasedCursor($response['@odata.nextLink'] ?? null, $graphOrigin);
} while ($url !== null);
}
+ /**
+ * Strip a server-supplied absolute URL down to path+query and
+ * re-base it onto the configured graph origin, so a hostile
+ * upstream can't redirect the cursor (and its bearer) to an
+ * attacker-chosen target. Returns null when the cursor is
+ * absent or unparseable, or when the configured graph URL
+ * itself has no usable origin.
+ */
+ private function rebasedCursor(?string $url, ?string $origin): ?string
+ {
+ if ($url === null || $url === '' || $origin === null) {
+ return null;
+ }
+
+ $parsed = parse_url($url);
+ if (! is_array($parsed)) {
+ return null;
+ }
+
+ return $origin.($parsed['path'] ?? '/').(isset($parsed['query']) ? '?'.$parsed['query'] : '');
+ }
+
+ /**
+ * Scheme+host(+port) of the given absolute URL, or null when it is
+ * not a parseable absolute URL.
+ */
+ private function origin(string $url): ?string
+ {
+ $parts = parse_url($url);
+ if (! is_array($parts) || empty($parts['scheme']) || empty($parts['host'])) {
+ return null;
+ }
+
+ return $parts['scheme'].'://'.$parts['host'].(isset($parts['port']) ? ':'.$parts['port'] : '');
+ }
+
/**
* Fetch (or reuse) a bearer token via OAuth 2.0 client-credentials
* against the tenant-scoped token endpoint. Token is cached on
diff --git a/tests/Feature/SyncAdapters/CustomHttp/CustomHttpAdapterTest.php b/tests/Feature/SyncAdapters/CustomHttp/CustomHttpAdapterTest.php
index fede4598bf..a0ff316a5e 100644
--- a/tests/Feature/SyncAdapters/CustomHttp/CustomHttpAdapterTest.php
+++ b/tests/Feature/SyncAdapters/CustomHttp/CustomHttpAdapterTest.php
@@ -211,11 +211,11 @@ class CustomHttpAdapterTest extends TestCase
$instance = SyncAdapterInstance::where('slug', $slug)->firstOrFail();
$adapter->saveConfig(\Illuminate\Http\Request::create('/', 'POST', [
- $slug . '_url' => 'https://vendor.example/api',
- $slug . '_source_id_path' => 'id',
- $slug . '_field_paths' => [],
- $slug . '_field_paths_pending' => ['key' => 'hostname', 'value' => 'device.name'],
- $slug . '_direction_pending' => ['value' => 'push'],
+ $slug.'_url' => 'https://vendor.example/api',
+ $slug.'_source_id_path' => 'id',
+ $slug.'_field_paths' => [],
+ $slug.'_field_paths_pending' => ['key' => 'hostname', 'value' => 'device.name'],
+ $slug.'_direction_pending' => ['value' => 'push'],
]));
$stored = json_decode(SyncAdapterConfig::get($instance->id, 'field_paths'), true);
@@ -235,11 +235,11 @@ class CustomHttpAdapterTest extends TestCase
$instance = SyncAdapterInstance::where('slug', $slug)->firstOrFail();
$adapter->saveConfig(\Illuminate\Http\Request::create('/', 'POST', [
- $slug . '_url' => 'https://vendor.example/api',
- $slug . '_source_id_path' => 'id',
- $slug . '_field_paths' => [],
- $slug . '_field_paths_pending' => ['key' => 'custom:9', 'value' => 'location.room'],
- $slug . '_direction_pending' => ['value' => 'pull'],
+ $slug.'_url' => 'https://vendor.example/api',
+ $slug.'_source_id_path' => 'id',
+ $slug.'_field_paths' => [],
+ $slug.'_field_paths_pending' => ['key' => 'custom:9', 'value' => 'location.room'],
+ $slug.'_direction_pending' => ['value' => 'pull'],
]));
$stored = json_decode(SyncAdapterConfig::get($instance->id, 'field_paths'), true);
@@ -259,11 +259,11 @@ class CustomHttpAdapterTest extends TestCase
$instance = SyncAdapterInstance::where('slug', $slug)->firstOrFail();
$adapter->saveConfig(\Illuminate\Http\Request::create('/', 'POST', [
- $slug . '_url' => 'https://vendor.example/api',
- $slug . '_source_id_path' => 'id',
- $slug . '_field_paths' => [],
- $slug . '_field_paths_pending' => ['key' => 'hostname', 'value' => ''],
- $slug . '_direction_pending' => ['value' => 'push'],
+ $slug.'_url' => 'https://vendor.example/api',
+ $slug.'_source_id_path' => 'id',
+ $slug.'_field_paths' => [],
+ $slug.'_field_paths_pending' => ['key' => 'hostname', 'value' => ''],
+ $slug.'_direction_pending' => ['value' => 'push'],
]));
$stored = json_decode(SyncAdapterConfig::get($instance->id, 'field_paths'), true);
@@ -299,8 +299,8 @@ class CustomHttpAdapterTest extends TestCase
iterator_to_array($adapter->pull());
- $matches = array_filter($captured, fn($m) => str_contains($m, 'blargh.not_a_real_field') && str_contains($m, 'serial'));
- $this->assertNotEmpty($matches, 'Expected an always-null summary warning naming the offending path. Captured: ' . json_encode($captured));
+ $matches = array_filter($captured, fn ($m) => str_contains($m, 'blargh.not_a_real_field') && str_contains($m, 'serial'));
+ $this->assertNotEmpty($matches, 'Expected an always-null summary warning naming the offending path. Captured: '.json_encode($captured));
}
public function test_pull_does_not_warn_when_path_resolves_on_at_least_one_record()
@@ -330,7 +330,7 @@ class CustomHttpAdapterTest extends TestCase
iterator_to_array($adapter->pull());
- $this->assertEmpty($captured, 'No warning expected when the path resolves on at least one record. Captured: ' . json_encode($captured));
+ $this->assertEmpty($captured, 'No warning expected when the path resolves on at least one record. Captured: '.json_encode($captured));
}
public function test_pull_does_not_warn_on_empty_vendor_response()
@@ -543,6 +543,40 @@ class CustomHttpAdapterTest extends TestCase
$this->assertSame(['a', 'b', 'c'], array_map(fn ($r) => $r->sourceId, $records));
}
+ public function test_next_url_pagination_rejects_cross_host_cursor()
+ {
+ // A hostile upstream can emit any absolute URL in the next-URL
+ // field. The adapter must keep the second-hop request on the
+ // configured host so the auth header can't be leaked to an
+ // attacker-chosen target.
+ $adapter = $this->configuredAdapter([
+ 'records_path' => 'rows',
+ 'field_source_id' => 'id',
+ 'pagination_style' => 'next_url',
+ 'pagination_next_path' => 'links.next',
+ 'auth_method' => 'bearer',
+ 'bearer_token' => 'leak-canary',
+ ]);
+
+ Http::fake([
+ 'vendor.example/api' => Http::response([
+ 'rows' => [['id' => 'first']],
+ 'links' => ['next' => 'https://attacker.example/exfil?token=steal'],
+ ]),
+ 'vendor.example/exfil*' => Http::response([
+ 'rows' => [['id' => 'second']],
+ 'links' => ['next' => null],
+ ]),
+ 'attacker.example/*' => Http::response(['rows' => [], 'links' => ['next' => null]]),
+ ]);
+
+ iterator_to_array($adapter->pull());
+
+ Http::assertNotSent(fn ($request) => str_contains($request->url(), 'attacker.example'));
+ Http::assertSent(fn ($request) => str_contains($request->url(), 'vendor.example/exfil')
+ && $request->hasHeader('Authorization', 'Bearer leak-canary'));
+ }
+
public function test_pagination_style_none_yields_one_page_and_stops()
{
$adapter = $this->configuredAdapter([
@@ -865,7 +899,6 @@ class CustomHttpAdapterTest extends TestCase
// Anything not in this set is stored plaintext.
$secretKeys = ['bearer_token', 'basic_password', 'api_key_value'];
-
if (array_key_exists('field_source_id', $config)) {
SyncAdapterConfig::put($instance->id, 'source_id_path', (string) $config['field_source_id']);
unset($config['field_source_id']);
diff --git a/tests/Feature/SyncAdapters/Intune/IntuneAdapterTest.php b/tests/Feature/SyncAdapters/Intune/IntuneAdapterTest.php
index f2fe19bb64..5e794f09cb 100644
--- a/tests/Feature/SyncAdapters/Intune/IntuneAdapterTest.php
+++ b/tests/Feature/SyncAdapters/Intune/IntuneAdapterTest.php
@@ -90,6 +90,34 @@ class IntuneAdapterTest extends TestCase
$this->assertNotNull($record->lastSeen);
}
+ public function test_nextlink_cursor_cannot_redirect_bearer_to_arbitrary_host()
+ {
+ // Graph normally serves `@odata.nextLink` from the same host as
+ // the base URL. A hostile upstream (compromised vendor API or
+ // MITM) can nominate any URL, so the adapter must keep the
+ // next hop on the configured graph host so the bearer token
+ // never travels to an attacker-chosen target.
+ $adapter = $this->configuredAdapter();
+
+ Http::fake([
+ '*/oauth2/v2.0/token' => Http::response(['access_token' => 'leak-canary', 'expires_in' => 3600]),
+ 'graph.microsoft.com/v1.0/deviceManagement/managedDevices' => Http::response([
+ 'value' => [$this->intuneDevice(id: 'guid-1', name: 'host-one')],
+ '@odata.nextLink' => 'https://attacker.example/exfil?token=steal',
+ ]),
+ 'graph.microsoft.com/exfil*' => Http::response([
+ 'value' => [$this->intuneDevice(id: 'guid-2', name: 'host-two')],
+ ]),
+ 'attacker.example/*' => Http::response(['value' => []]),
+ ]);
+
+ iterator_to_array($adapter->pull());
+
+ Http::assertNotSent(fn ($request) => str_contains($request->url(), 'attacker.example'));
+ Http::assertSent(fn ($request) => str_contains($request->url(), 'graph.microsoft.com/exfil')
+ && $request->hasHeader('Authorization', 'Bearer leak-canary'));
+ }
+
private function configuredAdapter(): IntuneAdapter
{
$instance = SyncAdapterInstance::where('slug', 'intune')->firstOrFail();