Files
ipal-kit/services/cookie-crawler/crawl.mjs
T
Radosław SmolińskiandClaude Opus 5.5 4bb9c663fd Cookie scanner, phase 2: browser crawl through a shared scanner service
cookieScanner.crawl (opt-in) adds "Crawl site" to the Cookie review box. The
site asks the shared scanner service (services/cookie-crawler: Node +
Playwright, Dockerfile, deployed once on Coolify) to crawl its sitemap pages
twice, with no consent and after "Accept all". The service posts cookies,
storage keys and third-party hosts back to POST /api/ipal/cookie-crawl/results
(bearer token, running job only). No browser in the site.

Findings land as drafts through the same merge rules. Anything stored before
consent that is not necessary, and tracker requests before consent, are
violations, flagged on the review entries from the latest crawl only. The
cookie-crawl global keeps the latest result (no history). A dashboard warning
(CookieCrawlAlert) and an opt-in e-mail through the project's mail adapter
follow a crawl with violations or new unknown cookies. Scheduled crawls: a
Coolify scheduled task with the token. ipalEnvRules({ cookieCrawl: true }).

Also: the six older real-Payload tests no longer leave an orphaned
payload generate:types process (typescript.autoGenerate: false).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-27 15:14:01 +01:00

77 lines
3.1 KiB
JavaScript

// The crawl itself: two fresh browser profiles over the same pages — one with
// no consent decision, one after clicking "Accept all". Records cookies,
// localStorage / sessionStorage keys and third-party request hosts per page.
import { bareHost, discoverPages, isFirstParty, mergeRuns } from './lib.mjs'
async function run(browser, pages, { accept, acceptSelector, pageTimeoutMs, siteHost, waitMs }) {
const context = await browser.newContext()
const result = { accepted: false, cookies: [], requests: [], storage: [] }
let current = '/'
const seen = new Set()
const once = (key) => (seen.has(key) ? false : (seen.add(key), true))
context.on('request', (request) => {
let host
try {
host = new URL(request.url()).hostname.toLowerCase()
} catch {
return
}
if (!host || isFirstParty(host, siteHost) || !once(`r:${host}:${current}`)) {return}
result.requests.push({ host, page: current })
})
try {
const page = await context.newPage()
await page.setExtraHTTPHeaders({ 'X-Ipal-Cookie-Crawler': '1' })
for (const [i, url] of pages.entries()) {
current = new URL(url).pathname
await page.goto(url, { timeout: pageTimeoutMs, waitUntil: 'load' }).catch(() => {})
if (accept && i === 0) {
const button = page.locator(acceptSelector).first()
if (await button.count().catch(() => 0)) {
await button.click({ timeout: 5_000 }).then(() => (result.accepted = true)).catch(() => {})
}
}
await page.waitForTimeout(waitMs)
const keys = await page
.evaluate(() => ({ localStorage: Object.keys(localStorage), sessionStorage: Object.keys(sessionStorage) }))
.catch(() => ({ localStorage: [], sessionStorage: [] }))
for (const [storage, names] of Object.entries(keys)) {
for (const name of names) {if (once(`s:${storage}:${name}:${current}`)) {result.storage.push({ name, page: current, storage })}}
}
for (const c of await context.cookies()) {
if (once(`c:${c.name}:${current}`)) {result.cookies.push({ domain: c.domain, name: c.name, page: current })}
}
}
} finally {
await context.close()
}
return result
}
/**
* Crawls a site and returns the wire format (without jobId):
* `{ ok: true, pages, observations, requests, accepted }`. `chromium` is
* Playwright's (injected — the caller decides how it is installed).
*/
export async function crawlSite({
acceptSelector = '[data-ipal="accept"]',
chromium,
fetchImpl = fetch,
maxPages = 20,
pageTimeoutMs = 30_000,
siteUrl,
waitMs = 3_000,
}) {
const pages = await discoverPages(siteUrl, Math.min(100, Math.max(0, maxPages)), fetchImpl)
const siteHost = bareHost(siteUrl)
const browser = await chromium.launch()
try {
const options = { acceptSelector, pageTimeoutMs, siteHost, waitMs }
const before = await run(browser, pages, { ...options, accept: false })
const after = await run(browser, pages, { ...options, accept: true })
return { accepted: after.accepted, ok: true, pages: pages.map((p) => new URL(p).pathname), ...mergeRuns(before, after) }
} finally {
await browser.close()
}
}