A free open source IT asset/license management system
Fixed #19755 - preserve query string when pagination is empty
snipe5fa0d29e626f
diff --git a/app/SyncAdapters/CustomHttp/CustomHttpAdapter.php b/app/SyncAdapters/CustomHttp/CustomHttpAdapter.phpindex b25079c52a..2659bacb4d 100644--- a/app/SyncAdapters/CustomHttp/CustomHttpAdapter.php+++ b/app/SyncAdapters/CustomHttp/CustomHttpAdapter.php@@ -869,6 +869,8 @@ class CustomHttpAdapter extends SyncAdapter implements PushableAdapter$endpoint = $baseUrl.$pullPath;}+ [$endpoint, $mergedQuery] = $this->mergeQueryIntoEndpoint($endpoint, $queryParams);+$request = Http::withOptions(['allow_redirects' => false])->acceptJson()->timeout(300);$request = $this->applyAuth($request);@@ -876,7 +878,42 @@ class CustomHttpAdapter extends SyncAdapter implements PushableAdapter// and render a red-flash sanitized summary. Swallowing here// masked 401 (bad bearer) and 5xx as "Synced 0 host(s), 0// error(s)", which reads as false success to an admin.- return $request->get($endpoint, $queryParams)->throw()->json();+ //+ // Only pass the second arg when non-empty: Laravel's HTTP+ // client sets Guzzle's `query` option whenever any value is+ // present (including `[]`), and Guzzle then rewrites the URL's+ // query with `http_build_query` on that value. An empty array+ // therefore strips the query already on $endpoint. One-arg+ // get() preserves it.+ $response = $mergedQuery === []+ ? $request->get($endpoint)+ : $request->get($endpoint, $mergedQuery);++ return $response->throw()->json();+ }++ /**+ * Fold any query already present on $endpoint into $queryParams+ * so pagination keys don't silently discard the admin's+ * filtered-pull_path query (reporter #19755), and the next_url+ * cursor's own query (e.g. `$skiptoken=XYZ`) survives the second+ * hop. Pagination keys win on conflict so adapter-driven iteration+ * can override an identically-named admin param.+ *+ * @param array<string, mixed> $queryParams+ * @return array{0: string, 1: array<string, mixed>}+ */+ private function mergeQueryIntoEndpoint(string $endpoint, array $queryParams): array+ {+ $queryStart = strpos($endpoint, '?');+ if ($queryStart === false) {+ return [$endpoint, $queryParams];+ }++ $existing = [];+ parse_str(substr($endpoint, $queryStart + 1), $existing);++ return [substr($endpoint, 0, $queryStart), array_replace($existing, $queryParams)];}/**diff --git a/tests/Feature/SyncAdapters/CustomHttp/CustomHttpAdapterTest.php b/tests/Feature/SyncAdapters/CustomHttp/CustomHttpAdapterTest.phpindex a0ff316a5e..3ee26bd46d 100644--- a/tests/Feature/SyncAdapters/CustomHttp/CustomHttpAdapterTest.php+++ b/tests/Feature/SyncAdapters/CustomHttp/CustomHttpAdapterTest.php@@ -574,9 +574,82 @@ class CustomHttpAdapterTest extends TestCaseHttp::assertNotSent(fn ($request) => str_contains($request->url(), 'attacker.example'));Http::assertSent(fn ($request) => str_contains($request->url(), 'vendor.example/exfil')+ && str_contains($request->url(), 'token=steal')&& $request->hasHeader('Authorization', 'Bearer leak-canary'));}+ public function test_pull_path_query_string_survives_pagination_style_none()+ {+ // Regression test for #19755. The admin configures a filtered+ // pull_path (ancestor_group_id=...) and expects every pull to+ // honor it. With pagination_style=none the adapter passed an+ // empty $queryParams to ->get(), which Laravel/Guzzle turned+ // into a `query` option that overwrote the URL's existing+ // query, silently dropping the filter.+ $adapter = $this->configuredAdapter([+ 'records_path' => 'data',+ 'field_source_id' => 'id',+ 'pagination_style' => 'none',+ 'pull_path' => '/v2/devices?ancestor_group_id=42&limit=100',+ ]);++ Http::fake(['vendor.example/*' => Http::response(['data' => [['id' => 'x']]])]);++ iterator_to_array($adapter->pull());++ Http::assertSent(fn($request) => str_contains($request->url(), 'ancestor_group_id=42')+ && str_contains($request->url(), 'limit=100'));+ }++ public function test_pull_path_query_string_survives_offset_limit_pagination()+ {+ // Same bug class as the None case but with pagination params+ // present. The admin's query + the adapter's offset/limit must+ // coexist on the outgoing URL.+ $adapter = $this->configuredAdapter([+ 'records_path' => 'data',+ 'field_source_id' => 'id',+ 'pagination_style' => 'offset_limit',+ 'pagination_page_size' => '50',+ 'pull_path' => '/v2/devices?ancestor_group_id=42',+ ]);++ Http::fake(['vendor.example/*' => Http::response(['data' => [['id' => 'only']]])]);++ iterator_to_array($adapter->pull());++ Http::assertSent(fn($request) => str_contains($request->url(), 'ancestor_group_id=42')+ && str_contains($request->url(), 'limit=50')+ && str_contains($request->url(), 'offset=0'));+ }++ public function test_next_url_cursor_query_string_survives_second_hop()+ {+ // Graph-style cursors carry their pagination state in query+ // (`$skiptoken=XYZ`). The second-hop request must preserve+ // that query or pagination terminates on page one with partial+ // results. Companion to the host-pinning check above.+ $adapter = $this->configuredAdapter([+ 'records_path' => 'rows',+ 'field_source_id' => 'id',+ 'pagination_style' => 'next_url',+ 'pagination_next_path' => 'links.next',+ ]);++ Http::fake([+ 'vendor.example/exfil*' => Http::response(['rows' => [['id' => 'b']], 'links' => ['next' => null]]),+ 'vendor.example/api' => Http::response([+ 'rows' => [['id' => 'a']],+ 'links' => ['next' => 'https://vendor.example/exfil?skiptoken=next-page-cursor'],+ ]),+ ]);++ $records = iterator_to_array($adapter->pull());++ $this->assertSame(['a', 'b'], array_map(fn($r) => $r->sourceId, $records));+ Http::assertSent(fn($request) => str_contains($request->url(), 'skiptoken=next-page-cursor'));+ }+public function test_pagination_style_none_yields_one_page_and_stops(){$adapter = $this->configuredAdapter([