/** * Copyright 2026 aphrody-code * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * Soft-block detection: responses that "succeed" at the HTTP layer but did not * deliver the page. * * Covers JS-only app shells (200 + empty mount point + heavy scripts), WAF / * bot-manager interstitials (Cloudflare, Akamai, Imperva/Incapsula, PerimeterX, * Kasada, AWS WAF, Sucuri, DataDome), meta-refresh loops into challenge URLs, * and generic "access denied / unusual traffic / verify you are human" pages. * * Every body marker is gated on the *visible* text length (computed after * stripping script/style/noscript/template/svg and comments): a real article * that merely mentions a CAPTCHA or embeds a bot-manager sensor script carries * far more text than any interstitial, and is never flagged. Only response * headers that are emitted exclusively on mitigated responses are trusted on * their own. * * Pure and dependency-free: safe to import from hot paths. */ export type SoftBlockReason = | "none" /** 200 with an empty client-side app shell: needs a JS-capable profile. */ | "js_required" /** Vendor WAF / bot-manager interstitial or block page. */ | "waf_block" /** Meta refresh into a challenge URL, or onto itself with no content. */ | "challenge_redirect" /** Generic denial / human-verification text on a near-empty page. */ | "access_denied"; export type SoftBlockProvider = | "cloudflare" | "akamai" | "imperva" | "perimeterx" | "kasada" | "aws-waf" | "sucuri" | "datadome" | "unknown"; export type HeadersInput = | Headers | Map | Record; export interface SoftBlockInput { status?: number; html: string; url?: string; headers?: HeadersInput; /** Document title when already known (e.g. from `page.title()`). */ title?: string; } export interface SoftBlockResult { blocked: boolean; reason: SoftBlockReason; /** 0..1. `blocked` is `confidence >= SOFT_BLOCK_THRESHOLD`. */ confidence: number; provider?: SoftBlockProvider; /** Short human-readable marker that triggered the verdict. */ evidence?: string; /** Visible text length after stripping non-rendered content. */ visibleTextLength: number; } /** Minimum confidence for `blocked: true`. */ export const SOFT_BLOCK_THRESHOLD = 0.5; /** Visible-text budgets used to gate body markers. */ const SHELL_TEXT_MAX = 200; const SHELL_EMPTY_TEXT_MAX = 30; const TINY_TEXT_MAX = 1000; const SMALL_TEXT_MAX = 2000; /** Bodies beyond this size are analysed on their prefix only. */ const MAX_ANALYSED_BYTES = 1_000_000; const NOT_BLOCKED = (visibleTextLength: number): SoftBlockResult => ({ blocked: false, reason: "none", confidence: 0, visibleTextLength, }); // --------------------------------------------------------------------------- // Text extraction // --------------------------------------------------------------------------- const COMMENT_RE = //g; const SCRIPT_RE = /]*)>([\s\S]*?)<\/script\s*>/gi; const STYLE_LIKE_RE = /<(style|template|svg)\b[^>]*>[\s\S]*?<\/\1\s*>/gi; const NOSCRIPT_RE = /]*>([\s\S]*?)<\/noscript\s*>/gi; const TAG_RE = /<[^>]*>/g; const ENTITY_RE = /&(#x[0-9a-f]+|#\d+|[a-z]+);/gi; const NAMED_ENTITIES: Record = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'", nbsp: " ", }; function decodeEntities(text: string): string { return text.replace(ENTITY_RE, (match, body: string) => { if (body[0] === "#") { const code = body[1] === "x" || body[1] === "X" ? Number.parseInt(body.slice(2), 16) : Number.parseInt(body.slice(1), 10); return Number.isFinite(code) && code > 0 && code < 0x110000 ? String.fromCodePoint(code) : " "; } return NAMED_ENTITIES[body.toLowerCase()] ?? match; }); } function collapse(text: string): string { return decodeEntities(text.replace(TAG_RE, " ")).replace(/\s+/g, " ").trim(); } interface PageText { /** Rendered text (no script/style/noscript/template/svg/comments). */ visible: string; /** Lowercased visible text plus `