Open source alternative to Semrush and Ahrefs
TypeScript backlink-analysisgoogle-search-console-mcpkeyword-researchmcpseoseo-agentseo-auditseo-automationseo-skillsseo-toolssite-audit
Read sites behind redirect chains or a www-only certificate during AI visibility setup (EVE-412) (#907)
Carter Smith039d70ad02a4
diff --git a/src/server/lib/scrape.test.ts b/src/server/lib/scrape.test.tsindex c5c38678..6cfea683 100644--- a/src/server/lib/scrape.test.ts+++ b/src/server/lib/scrape.test.ts@@ -44,3 +44,58 @@ describe("readPages SSRF guard", () => {expect(fetch).not.toHaveBeenCalled();});});++describe("readSite", () => {+ // Unlisted URLs (sitemaps, DoH lookups) 404; a missing route value throws+ // like a failed TLS handshake.+ function stubSite(routes: Record<string, string | null>) {+ vi.stubGlobal(+ "fetch",+ vi.fn(async (url: string) => {+ if (!(url in routes)) return new Response(null, { status: 404 });+ const target = routes[url];+ if (target === null) throw new TypeError("fetch failed");+ return target.startsWith("http")+ ? new Response(null, { status: 302, headers: { location: target } })+ : new Response(target);+ }),+ );+ }++ afterEach(() => {+ vi.unstubAllGlobals();+ });++ it("follows a multi-hop redirect chain to the homepage", async () => {+ stubSite({+ "https://example.com/": "http://www.example.com/",+ "http://www.example.com/": "https://www.example.com/",+ "https://www.example.com/": "Example homepage",+ });++ const result = await readSite("example.com");++ expect(result.pages.map((page) => page.text)).toEqual(["Example homepage"]);+ });++ it("falls back to www when the bare domain can't be read", async () => {+ stubSite({+ "https://example.com/": null,+ "https://www.example.com/": "Example homepage",+ });++ const result = await readSite("example.com");++ expect(result.pages.map((page) => page.url)).toEqual([+ "https://www.example.com/",+ ]);+ });++ it("does not retry an unreadable IP address on www", async () => {+ stubSite({ "https://93.184.216.34/": null });++ const result = await readSite("93.184.216.34");++ expect(result.blocked).toBe(true);+ });+});diff --git a/src/server/lib/scrape.ts b/src/server/lib/scrape.tsindex d8bd7186..deff560c 100644--- a/src/server/lib/scrape.ts+++ b/src/server/lib/scrape.ts@@ -10,6 +10,8 @@ import { normalizeAndValidateStartUrl } from "@/server/lib/audit/url-policy";const MAX_PAGES = 5;const PER_PAGE_CHAR_LIMIT = 4000;const FETCH_TIMEOUT_MS = 10_000;+// Covers chains like https://apex -> http://www -> https://www.+const MAX_REDIRECTS = 3;const MAX_RESPONSE_BYTES = 2_000_000;const USER_AGENT = "OpenSEO-Onboarding/1.0 (+https://openseo.so)";@@ -49,38 +51,26 @@ async function readBoundedText(response: Response): Promise<string | null> {async function fetchText(url: string): Promise<string | null> {try {- const response = await fetch(url, {- headers: { "user-agent": USER_AGENT, accept: "text/html,*/*" },- // Manual redirects so we can re-validate each hop against the SSRF guard;- // redirect:"follow" would let a 30x to an internal host bypass it.- redirect: "manual",- signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),- });-- if (response.status >= 300 && response.status < 400) {- const location = response.headers.get("location");- if (!location) return null;- let redirectUrl: string;- try {- // Re-validates host, blocks private IPs, and does DoH DNS resolution.- redirectUrl = await normalizeAndValidateStartUrl(- new URL(location, url).toString(),- );- } catch {- return null; // blocked or invalid redirect destination- }- // One hop only; fetch the validated destination without following further.- const redirected = await fetch(redirectUrl, {+ let current = url;+ for (let hop = 0; hop <= MAX_REDIRECTS; hop++) {+ const response = await fetch(current, {headers: { "user-agent": USER_AGENT, accept: "text/html,*/*" },+ // Manual redirects so we can re-validate each hop against the SSRF guard;+ // redirect:"follow" would let a 30x to an internal host bypass it.redirect: "manual",signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),});- if (!redirected.ok) return null;- return await readBoundedText(redirected);+ if (response.status < 300 || response.status >= 400)+ return response.ok ? await readBoundedText(response) : null;+ const location = response.headers.get("location");+ if (!location) return null;+ // Re-validates host, blocks private IPs, and does DoH DNS resolution.+ // Throws on a blocked or invalid destination.+ current = await normalizeAndValidateStartUrl(+ new URL(location, current).toString(),+ );}-- if (!response.ok) return null;- return await readBoundedText(response);+ return null;} catch {return null;}@@ -219,5 +209,13 @@ export async function readSite(if (discovered.blocked) {return { pages: [], blocked: true };}- return readPages(discovered.urls, maxPages);+ const site = await readPages(discovered.urls, maxPages);+ const root = new URL(discovered.urls[0]);+ if (!site.blocked || root.hostname.startsWith("www.")) return site;+ // Some sites only serve their www host (e.g. the bare domain's TLS+ // certificate is missing), so retry there before giving up.+ root.hostname = `www.${root.hostname}`;+ // The setter ignores invalid hosts, so IP literals keep their original host.+ if (!root.hostname.startsWith("www.")) return site;+ return readSite(root.toString(), maxPages);}