Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(warehouse-sources): resolve relative pagination URLs (#86784)

posthog[bot]99d93f0054bb
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/paginators.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/paginators.py
index 82872e711655..e7b2e3a483c7 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/paginators.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/paginators.py
@@ -2,7 +2,7 @@ import re
import logging
from abc import ABC, abstractmethod
from typing import Any, Literal, Optional
-from urllib.parse import urlsplit
+from urllib.parse import urljoin, urlsplit
from requests import Request, Response
@@ -70,9 +70,17 @@ class BaseNextUrlPaginator(BasePaginator):
self._next_url: Optional[str] = None
self._previous_next_url: Optional[str] = None
- def _advance_to(self, next_url: Optional[str]) -> None:
+ def _advance_to(self, next_url: Optional[str], base_url: Optional[str] = None) -> None:
"""Follow ``next_url`` as the next page, or stop when there is none.
+ ``next_url`` may be relative: some APIs (e.g. Ably) return the next link as
+ a path like ``./stats?...``. Resolve it against ``base_url`` (the response
+ URL it came from) so ``requests`` receives an absolute URL. Only the first
+ request gets the base applied via ``_join_url``, so a relative next link that
+ is not resolved here reaches ``requests`` with no scheme or host and fails
+ with ``MissingSchema``. Resolving here also keeps the repeat guard, the resume
+ state, and the allowed-host check working on absolute URLs.
+
A next URL identical to the one just followed is treated as the last page:
some APIs (e.g. Paddle) return a populated next link even on their final
page, and following it would refetch the same page forever. Without this
@@ -82,6 +90,10 @@ class BaseNextUrlPaginator(BasePaginator):
if not next_url:
self._has_next_page = False
return
+ # `Response.url` is a str once a request has been sent, but stays unset (or a test
+ # double) before that, so only resolve against a real string base.
+ if isinstance(base_url, str) and base_url:
+ next_url = urljoin(base_url, next_url)
if next_url == self._previous_next_url:
self._handle_non_advancing_page(next_url)
return
@@ -142,7 +154,7 @@ class HeaderLinkPaginator(BaseNextUrlPaginator):
def update_state(self, response: Response, data: Optional[list[Any]] = None) -> None:
next_link = response.links.get(self.links_next_key)
- self._advance_to(next_link.get("url") if next_link else None)
+ self._advance_to(next_link.get("url") if next_link else None, response.url)
def __str__(self) -> str:
return f"HeaderLinkPaginator(links_next_key={self.links_next_key})"
@@ -160,7 +172,7 @@ class JSONResponsePaginator(BaseNextUrlPaginator):
values = find_values(self.next_url_path, response.json())
except Exception:
values = []
- self._advance_to(values[0] if values else None)
+ self._advance_to(values[0] if values else None, response.url)
def __str__(self) -> str:
return f"JSONResponsePaginator(next_url_path={self.next_url_path})"
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/tests/test_paginators.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/tests/test_paginators.py
index 84404f92339d..2d82db043382 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/tests/test_paginators.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/tests/test_paginators.py
@@ -53,6 +53,20 @@ class TestHeaderLinkPaginator:
p.update_state(resp)
assert p.has_next_page is False
+ def test_resolves_relative_next_link_against_response_url(self) -> None:
+ # Ably returns its next link as a relative path (e.g. `./stats?...`). Without
+ # resolving it against the response URL, `requests` gets a schemeless URL and
+ # raises MissingSchema, so the sync dies after page one.
+ p = HeaderLinkPaginator()
+ resp = _make_response()
+ resp.url = "https://rest.ably.io/stats?unit=hour"
+ resp.headers["Link"] = '<./stats?start=1&unit=hour&direction=forwards>; rel="next"'
+ p.update_state(resp)
+ assert p.has_next_page is True
+ req = Request(method="GET", url="https://rest.ably.io/stats")
+ p.update_request(req)
+ assert req.url == "https://rest.ably.io/stats?start=1&unit=hour&direction=forwards"
+
class TestJSONResponsePaginator:
def test_follows_next_url(self) -> None:
@@ -64,6 +78,16 @@ class TestJSONResponsePaginator:
p.update_request(req)
assert req.url == "https://api.example.com/page2"
+ def test_resolves_relative_next_url_against_response_url(self) -> None:
+ p = JSONResponsePaginator(next_url_path="next")
+ resp = _make_response({"next": "/v2/items?page=2", "data": []})
+ resp.url = "https://api.example.com/v2/items?page=1"
+ p.update_state(resp)
+ assert p.has_next_page is True
+ req = Request(method="GET", url="https://api.example.com/v2/items")
+ p.update_request(req)
+ assert req.url == "https://api.example.com/v2/items?page=2"
+
def test_stops_when_next_is_null(self) -> None:
p = JSONResponsePaginator(next_url_path="next")
resp = _make_response({"next": None, "data": []})