AtlatestRepositorysigil-http3

sigil-http3 / tree / scriptscheck-page-certificate.mjs

1// Explicitly exercise the known test page's ordinary certificate warning.
2// This never installs trust or changes browser-wide certificate validation.
3const pause = ms => new Promise(done => setTimeout(done, ms));
4export async function navigatePhonePage(cdp, pageUrl, configuration, sessionId, signal,
5 continuePageWarning = false, queryTarget = false) {
6 const configuredPage = new URL(pageUrl);
7 if (queryTarget) {
8 const target = new URL(configuration.get('url'));
9 configuredPage.search = new URLSearchParams({ host: target.hostname, port: target.port || '443',
10 hash: configuration.get('hash'), autoconnect: configuration.get('autoconnect') || '0' }).toString();
11 } else configuredPage.hash = configuration.toString();
12 const navigation = await cdp('Page.navigate', { url: configuredPage.href }, sessionId);
13 if (navigation.errorText && (!continuePageWarning || navigation.errorText !== 'net::ERR_CERT_AUTHORITY_INVALID'))
14 throw new Error(`Phone page navigation: ${navigation.errorText}`);
15 const receipt = { origin: new URL(pageUrl).origin, continued: false,
16 navigationError: navigation.errorText ?? null };
17 const deadline = Date.now() + 10000;
18 while (!signal.aborted && Date.now() < deadline) {
19 let result;
20 try {
21 result = await cdp('Runtime.evaluate', { returnByValue: true, expression: `(() => ({
22 page: !!document.getElementById('configuration'),
23 proceed: !!document.getElementById('proceed-link'),
24 error: document.querySelector('.error-code, #error-code')?.textContent ?? '',
25 title: document.title
26 }))()` }, sessionId);
27 } catch (error) {
28 if (!/context|navigat/i.test(error.message)) throw error;
29 await pause(50); continue;
30 }
31 const value = result.result?.value;
32 if (value?.page) return receipt;
33 if (value?.proceed && !receipt.continued) {
34 if (!continuePageWarning || !value.error.includes('ERR_CERT_AUTHORITY_INVALID'))
35 throw new Error(`Unexpected page certificate warning: ${JSON.stringify(value)}`);
36 receipt.continued = true;
37 receipt.warning = value.error;
38 // The host was created from our verified exact certificate, bound to
39 // loopback in this test; the visible Proceed link scopes this action.
40 try {
41 await cdp('Runtime.evaluate', { userGesture: true, expression:
42 "document.getElementById('details-button')?.click(); document.getElementById('proceed-link').click();" }, sessionId);
43 } catch (error) { if (!/context|navigat/i.test(error.message)) throw error; }
44 }
45 await pause(50);
46 }
47 throw new Error(`Phone HTTPS page did not load after its explicit warning: ${JSON.stringify(receipt)}`);
50export async function pageResult(cdp, sessionId, signal, milliseconds) {
51 const deadline = Date.now() + milliseconds;
52 while (!signal.aborted && Date.now() < deadline) {
53 const result = await cdp('Runtime.evaluate', { expression: 'window.__result || null', returnByValue: true }, sessionId);
54 if (result.result?.value) return result.result.value;
55 await pause(100);
56 }
57 throw new Error('Page result deadline');