Add optional self-hosted ALTCHA protection and dependency maintenance
This commit is contained in:
1 parent
3b558409f3
commit
2a2f1d5878
59 files changed
+2648
-12
No files matched your search
@@ -0,0 +1,82 @@
|
||||
// Usage: HGCF_BASE_URL=https://test.example PLAYWRIGHT_MODULE=/path/to/playwright node tests/browser.cjs /contact/ /en/contact/
|
||||
// Valid form POSTs are intercepted in the browser; they never reach the mail handler.
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const { chromium } = require(process.env.PLAYWRIGHT_MODULE || 'playwright');
|
||||
const base = process.env.HGCF_BASE_URL;
|
||||
if (!base || process.argv.length < 3) throw new Error('Provide HGCF_BASE_URL and contact page paths.');
|
||||
const artifacts = process.env.HGCF_ARTIFACTS || '/tmp/hg-contact-browser';
|
||||
fs.mkdirSync(artifacts, { recursive: true, mode: 0o700 });
|
||||
(async () => {
|
||||
const browser = await chromium.launch({ headless: true });
|
||||
const proofs = [];
|
||||
try {
|
||||
for (const [index, route] of process.argv.slice(2).entries()) {
|
||||
for (const mobile of [false, true]) {
|
||||
const context = await browser.newContext({ viewport: mobile ? { width: 390, height: 844 } : { width: 1440, height: 1000 } });
|
||||
const page = await context.newPage();
|
||||
const errors = []; const requests = []; let submitted = null;
|
||||
page.on('pageerror', error => errors.push(error.message));
|
||||
page.on('request', request => requests.push(request.url()));
|
||||
const url = new URL(route, base).href;
|
||||
await page.route('**/*', async handler => {
|
||||
const request = handler.request();
|
||||
if (request.method() === 'POST' && new URL(request.url()).pathname === new URL(url).pathname) {
|
||||
submitted = Object.fromEntries(new URLSearchParams(request.postData()));
|
||||
await handler.fulfill({ status: 200, contentType: 'text/html', body: '<p>Test POST intercepted; no mail sent.</p>' });
|
||||
} else await handler.continue();
|
||||
});
|
||||
const response = await page.goto(url, { waitUntil: 'networkidle' });
|
||||
assert.equal(response.status(), 200);
|
||||
await page.waitForFunction(() => !!customElements.get('altcha-widget'));
|
||||
const widget = page.locator('altcha-widget');
|
||||
assert.equal(await widget.count(), 1);
|
||||
const lang = await widget.getAttribute('language');
|
||||
assert.equal(await page.locator('script#hgcf-altcha-js').getAttribute('type'), 'module');
|
||||
const challengeURL = await widget.getAttribute('challenge');
|
||||
assert.equal(new URL(challengeURL).origin, new URL(base).origin);
|
||||
assert(await page.locator('.mlhc-form').evaluate(form => !form.dispatchEvent(new Event('submit', { bubbles: true, cancelable: true }))));
|
||||
assert.equal(await page.locator('.mlhc-form button[type=submit]').isDisabled(), false);
|
||||
await page.locator('[name=mlhc_name]').fill('Test Visitor');
|
||||
await page.locator('[name=mlhc_email]').fill('visitor@example.com');
|
||||
await page.locator('[name=mlhc_message]').fill('Browser verification test intercepted before delivery.');
|
||||
await page.locator('[name=mlhc_consent]').check();
|
||||
await page.waitForFunction(() => {
|
||||
const input = document.querySelector('.mlhc-form input[name=mlhc_altcha]');
|
||||
return !!input?.value;
|
||||
}, null, { timeout: 45000 }).catch(async error => {
|
||||
console.error('Widget diagnostic', await widget.textContent(), errors);
|
||||
await page.screenshot({ path: path.join(artifacts, 'failure.png'), fullPage: true });
|
||||
throw error;
|
||||
});
|
||||
const proof = await page.locator('input[name=mlhc_altcha]').inputValue();
|
||||
const decoded = JSON.parse(Buffer.from(proof, 'base64').toString());
|
||||
assert.equal(decoded.challenge.parameters.algorithm, 'PBKDF2/SHA-256');
|
||||
assert.equal(decoded.challenge.parameters.data.page, Number(new URL(challengeURL).searchParams.get('page')));
|
||||
assert(await widget.textContent().then(text => text.includes(lang === 'nl' ? 'Geverifieerd' : 'Verified')));
|
||||
assert.equal(await page.locator('[data-hgcf-field=service]').isVisible(), false);
|
||||
const requestMode = page.locator('[name=mlhc_mode][value=request]');
|
||||
if (await requestMode.count()) {
|
||||
await requestMode.check();
|
||||
assert.equal(await page.locator('[data-hgcf-field=service]').isVisible(), true);
|
||||
assert.equal(await page.locator('[name=mlhc_message]').getAttribute('required'), null);
|
||||
await page.locator('[name=mlhc_mode][value=question]').check();
|
||||
}
|
||||
const geometry = await widget.boundingBox();
|
||||
assert(geometry.x >= 0 && geometry.x + geometry.width <= (mobile ? 390 : 1440));
|
||||
await page.locator('.hgcf-contact').screenshot({ path: path.join(artifacts, `form-${lang}-${mobile ? 'mobile' : 'desktop'}.png`) });
|
||||
assert.deepEqual(errors, []);
|
||||
const runtimeExternal = requests.filter(request => new URL(request).protocol.startsWith('http') && new URL(request).origin !== new URL(base).origin);
|
||||
assert.deepEqual(runtimeExternal, [], 'No external requests from contact page or ALTCHA');
|
||||
await page.locator('.mlhc-form button[type=submit]').click();
|
||||
await page.waitForFunction(() => document.body.textContent.includes('Test POST intercepted'));
|
||||
assert.equal(submitted.mlhc_altcha, proof);
|
||||
proofs.push({ page: decoded.challenge.parameters.data.page, proof });
|
||||
console.log(`PASS ${lang} ${mobile ? 'mobile' : 'desktop'}: local widget, solved proof, workflow switch, submit interception, no console errors`);
|
||||
await context.close();
|
||||
}
|
||||
}
|
||||
fs.writeFileSync(path.join(artifacts, 'proofs.json'), JSON.stringify(proofs), { mode: 0o600 });
|
||||
} finally { await browser.close(); }
|
||||
})().catch(error => { console.error(error); process.exitCode = 1; });
|
||||
Reference in new issue
Block a user