fix(scraper): harden Bloomberg login state (#40)
This commit is contained in:
@ -6,17 +6,22 @@ const vm = require('node:vm')
|
||||
const { createRequire } = require('node:module')
|
||||
const html = '<meta name="iso-8601-publish-date" content="2026-01-02T03:04:05Z"><article class="articleLayout_basic__gcpQk"><div class="body-content"><p class="ArticleBodyText_articleBodyContent__17wqE">Synthetic editorial paragraph.</p></div></article>'
|
||||
|
||||
async function request (byparr, failOne = false, poolSize = 2, authTimeout = false, httpDenied = false) {
|
||||
async function request (byparr, failOne = false, poolSize = 2, authTimeout = false, httpDenied = false, loginOutcome = 'authenticated') {
|
||||
const routes = {}; const loads = []; const logs = []; const timers = []; let pages = 0; let closed = false; let active = 0; let peak = 0; let byparrCalls = 0; const typed = []
|
||||
const app = { use () {}, get () {}, post (url, handler) { routes[url] = handler }, listen () {} }
|
||||
const express = () => app; express.json = () => () => {}
|
||||
function page (auth) {
|
||||
let url = ''; let evals = 0
|
||||
let url = ''; let evals = 0; let submits = 0
|
||||
return {
|
||||
async setViewport () {}, async evaluateOnNewDocument () {}, async waitForSelector () {}, async type (selector,value) {typed.push(value)}, async waitForFunction () {},
|
||||
async waitForNavigation () { url = 'https://www.bloomberg.com/markets' }, async evaluate () { return evals++ === 0 ? [] : true },
|
||||
async waitForNavigation () { url = 'https://www.bloomberg.com/markets' }, async evaluate (fn) {
|
||||
if (fn.name === 'clearLoginInput') return true
|
||||
if (fn.name === 'submitInputForm') {if (++submits === 2) url='https://www.bloomberg.com/markets';return true}
|
||||
if (fn.name === 'inspectLoginState') return {authenticated:loginOutcome==='authenticated',challenge:loginOutcome==='challenge',twoFactor:loginOutcome==='two_factor_required',rejected:loginOutcome==='auth_rejected',loginForm:false}
|
||||
return evals++ === 0 ? [] : true
|
||||
},
|
||||
async $ () { return null }, keyboard: { async press () {} },
|
||||
async goto (value) { url = value; if (auth && authTimeout) return new Promise(() => {}); if (!auth) { loads.push(value); active++; peak = Math.max(peak, active); await new Promise(resolve => setTimeout(resolve, 1)); active--; if (failOne && value.endsWith('/b')) throw new Error('PRIVATE loader failed') }; return { status () { return !auth && httpDenied ? 403 : 200 } } }, url () { return url },
|
||||
async goto (value) { url = auth && loginOutcome === 'login_origin_unavailable' ? 'https://outside.invalid/login' : value; if (auth && authTimeout) return new Promise(() => {}); if (!auth) { loads.push(value); active++; peak = Math.max(peak, active); await new Promise(resolve => setTimeout(resolve, 1)); active--; if (failOne && value.endsWith('/b')) throw new Error('PRIVATE loader failed') }; return { status () { return auth && loginOutcome === 'access_denied' ? 403 : auth && loginOutcome === 'login_http_unavailable' ? 503 : !auth && httpDenied ? 403 : 200 } } }, url () { return url },
|
||||
async content () { return `<link rel="canonical" href="${url}">` + html }, async close () {}
|
||||
}
|
||||
}
|
||||
@ -44,12 +49,12 @@ async function request (byparr, failOne = false, poolSize = 2, authTimeout = fal
|
||||
let result
|
||||
await routes['/bloomberg/enrich']({ body: { username: 'PRIVATE_USER', password: 'PRIVATE_SECRET', urls } }, { json (value) { result = value }, status () { return this } })
|
||||
assert.deepEqual(result.articles.map(a => a.url), urls)
|
||||
assert.equal(loads.length, authTimeout ? 0 : poolSize)
|
||||
assert.ok(logs.every(line => !line.includes('PRIVATE') && !line.includes('http') && !line.includes('Synthetic editorial')))
|
||||
return { result, pages, peak, closed, timers, byparrCalls, typed }
|
||||
assert.equal(loads.length, authTimeout || loginOutcome !== 'authenticated' ? 0 : poolSize)
|
||||
assert.ok(logs.every(line => !line.includes('PRIVATE') && !/https?:\/\//i.test(line) && !line.includes('Synthetic editorial')))
|
||||
return { result, pages, peak, closed, timers, byparrCalls, typed, logs }
|
||||
}
|
||||
test('enrichment always uses authenticated Puppeteer even when Byparr is configured', async () => {
|
||||
const { result, peak, pages, closed, byparrCalls, typed } = await request(true)
|
||||
const { result, peak, pages, closed, byparrCalls, typed, logs } = await request(true)
|
||||
assert.equal(byparrCalls, 0)
|
||||
assert.equal(pages, 3); assert.equal(closed,true)
|
||||
assert.ok(typed.includes('PRIVATE_USER') && typed.includes('PRIVATE_SECRET'))
|
||||
@ -94,3 +99,26 @@ test('HTTP denied article responses cannot be complete even with a plausible edi
|
||||
assert.equal(byparrCalls,0)
|
||||
assert.ok(result.articles.every(a => a.content_state === 'unavailable' && a.content === ''))
|
||||
})
|
||||
|
||||
for (const outcome of ['challenge', 'two_factor_required', 'auth_rejected', 'authentication_unconfirmed']) {
|
||||
test(`login with ${outcome} cannot launch article requests despite leaving sign-in`, async () => {
|
||||
const {result,byparrCalls,closed} = await request(true,false,2,false,false,outcome)
|
||||
assert.equal(byparrCalls,0);assert.equal(closed,true)
|
||||
assert.ok(result.articles.every(a => a.content_state==='unavailable' && a.content===''))
|
||||
})
|
||||
}
|
||||
|
||||
for (const outcome of ['access_denied','login_http_unavailable']) {
|
||||
test(`login HTTP ${outcome} stops before any credential entry`, async () => {
|
||||
const {result,typed,logs,byparrCalls} = await request(true,false,2,false,false,outcome)
|
||||
assert.equal(typed.length,0);assert.equal(byparrCalls,0)
|
||||
assert.ok(logs.some(line => line.endsWith(outcome)))
|
||||
assert.ok(result.articles.every(a => a.content_state==='unavailable' && a.content===''))
|
||||
})
|
||||
}
|
||||
|
||||
test('unexpected login origin stops before credential entry', async () => {
|
||||
const {result,typed,byparrCalls} = await request(true,false,2,false,false,'login_origin_unavailable')
|
||||
assert.equal(typed.length,0);assert.equal(byparrCalls,0)
|
||||
assert.ok(result.articles.every(a => a.content_state==='unavailable' && a.content===''))
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user