Skip to content

Commit c449f8b

Browse files
committed
Add some sneaky tricks to hide the Chrome cert warning banner
1 parent d497d00 commit c449f8b

File tree

4 files changed

+151
-83
lines changed

4 files changed

+151
-83
lines changed

src/cert-check-server.ts

Lines changed: 79 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -54,85 +54,85 @@ export class CertCheckServer {
5454

5555
const certificatePem = await readFile(this.config.https.certPath);
5656

57-
this.server.get('/test-https').thenReply(200);
58-
59-
this.server.get('/download-cert').thenReply(200, certificatePem, {
60-
'Content-type': 'application/x-x509-ca-cert'
61-
});
62-
63-
this.server.get('/check-cert').thenReply(200, `
64-
<html>
65-
<title>HTTP Toolkit Certificate Setup</title>
66-
<meta charset="UTF-8" />
67-
<link href="http://fonts.googleapis.com/css?family=Lato" rel="stylesheet" />
68-
<style>
69-
body {
70-
margin: 20px;
71-
background-color: #d8e2e6;
72-
font-family: Lato, Arial;
73-
}
74-
75-
body:not(.show-content) > * {
76-
display: none;
77-
}
78-
79-
h1 {
80-
font-size: 36pt;
81-
}
82-
83-
p {
84-
font-size: 16pt;
85-
}
86-
87-
iframe {
88-
display: none;
89-
}
90-
</style>
91-
<script>
92-
let installingCert = false;
93-
const targetUrl = ${JSON.stringify(targetUrl)};
94-
95-
${ensureCertificateIsInstalled.toString()}
96-
ensureCertificateIsInstalled();
97-
</script>
98-
<body>
99-
<h1>
100-
Configuring Firefox to use HTTP Toolkit
101-
</h1>
102-
<p>
103-
To intercept HTTPS traffic, you need to trust the HTTP Toolkit certificate.
104-
</p>
105-
<p>
106-
Select 'Trust this CA to identify web sites' and press 'OK' to continue.
107-
</p>
108-
109-
<svg
110-
version="1.1"
111-
xmlns="http://www.w3.org/2000/svg"
112-
xmlns:xlink="http://www.w3.org/1999/xlink"
113-
x="0px"
114-
y="0px"
115-
width="400px"
116-
height="400px"
117-
viewBox="0 0 50 50"
118-
style="enable-background:new 0 0 50 50;"
119-
>
120-
<path fill="#b6c2ca" d="M25.251,6.461c-10.318,0-18.683,8.365-18.683,18.683h4.068c0-8.071,6.543-14.615,14.615-14.615V6.461z">
121-
<animateTransform
122-
attributeType="xml"
123-
attributeName="transform"
124-
type="rotate"
125-
from="0 25 25"
126-
to="360 25 25"
127-
dur="6s"
128-
repeatCount="indefinite"
129-
/>
130-
</path>
131-
</svg>
132-
</div>
133-
</body>
134-
</html>
135-
`);
57+
await Promise.all([
58+
this.server.get('/test-https').thenReply(200),
59+
this.server.get('/download-cert').thenReply(200, certificatePem, {
60+
'Content-type': 'application/x-x509-ca-cert'
61+
}),
62+
this.server.get('/check-cert').thenReply(200, `
63+
<html>
64+
<title>HTTP Toolkit Certificate Setup</title>
65+
<meta charset="UTF-8" />
66+
<link href="http://fonts.googleapis.com/css?family=Lato" rel="stylesheet" />
67+
<style>
68+
body {
69+
margin: 20px;
70+
background-color: #d8e2e6;
71+
font-family: Lato, Arial;
72+
}
73+
74+
body:not(.show-content) > * {
75+
display: none;
76+
}
77+
78+
h1 {
79+
font-size: 36pt;
80+
}
81+
82+
p {
83+
font-size: 16pt;
84+
}
85+
86+
iframe {
87+
display: none;
88+
}
89+
</style>
90+
<script>
91+
let installingCert = false;
92+
const targetUrl = ${JSON.stringify(targetUrl)};
93+
94+
${ensureCertificateIsInstalled.toString()}
95+
ensureCertificateIsInstalled();
96+
</script>
97+
<body>
98+
<h1>
99+
Configuring Firefox to use HTTP Toolkit
100+
</h1>
101+
<p>
102+
To intercept HTTPS traffic, you need to trust the HTTP Toolkit certificate.
103+
</p>
104+
<p>
105+
Select 'Trust this CA to identify web sites' and press 'OK' to continue.
106+
</p>
107+
108+
<svg
109+
version="1.1"
110+
xmlns="http://www.w3.org/2000/svg"
111+
xmlns:xlink="http://www.w3.org/1999/xlink"
112+
x="0px"
113+
y="0px"
114+
width="400px"
115+
height="400px"
116+
viewBox="0 0 50 50"
117+
style="enable-background:new 0 0 50 50;"
118+
>
119+
<path fill="#b6c2ca" d="M25.251,6.461c-10.318,0-18.683,8.365-18.683,18.683h4.068c0-8.071,6.543-14.615,14.615-14.615V6.461z">
120+
<animateTransform
121+
attributeType="xml"
122+
attributeName="transform"
123+
type="rotate"
124+
from="0 25 25"
125+
to="360 25 25"
126+
dur="6s"
127+
repeatCount="indefinite"
128+
/>
129+
</path>
130+
</svg>
131+
</div>
132+
</body>
133+
</html>
134+
`)
135+
]);
136136
}
137137

138138
get host(): string {

src/hide-chrome-warning-server.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { getLocal, Mockttp } from 'mockttp';
2+
3+
// Make the types for some of the browser code below happy.
4+
let targetUrl: string;
5+
6+
// The first tab that opens opens with a Chrome warning about dangerous flags
7+
// Closing it and immediately opening a new one is a bit cheeky, but
8+
// is completely gets rid that, more or less invisibly.
9+
function jumpToNewTab() {
10+
window.open(targetUrl, '_blank');
11+
window.close();
12+
}
13+
14+
export class HideChromeWarningServer {
15+
16+
private server: Mockttp = getLocal();
17+
18+
// Resolved once the server has seen at least once
19+
// request for the warning-hiding page.
20+
public completedPromise = new Promise<void>((resolve) => {
21+
this.server.on('request', (req) => {
22+
if (req.url.includes('hide-chrome-warning')) {
23+
resolve();
24+
}
25+
});
26+
})
27+
28+
async start(targetUrl: string) {
29+
await this.server.start();
30+
31+
await this.server.get('/hide-chrome-warning').thenReply(200, `
32+
<html>
33+
<title>HTTP Toolkit Chrome Warning Fix</title>
34+
<meta charset="UTF-8" />
35+
<style>
36+
body { background-color: #d8e2e6; }
37+
</style>
38+
<script>
39+
const targetUrl = ${JSON.stringify(targetUrl)};
40+
41+
${jumpToNewTab.toString()}
42+
jumpToNewTab();
43+
</script>
44+
<body>
45+
This page should disappear momentarily. If it doesn't, click
46+
<a href="${targetUrl}">this link</a>
47+
</body>
48+
</html>
49+
`);
50+
}
51+
52+
get hideWarningUrl(): string {
53+
return this.server.url.replace(/\/?$/, '/hide-chrome-warning');
54+
}
55+
56+
async stop() {
57+
await this.server.stop();
58+
}
59+
}

src/interceptors/fresh-chrome.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { HtkConfig } from '../config';
99

1010
import { getAvailableBrowsers, launchBrowser, BrowserInstance } from '../browsers';
1111
import { delay } from '../util';
12+
import { HideChromeWarningServer } from '../hide-chrome-warning-server';
1213

1314
const readFile = promisify(fs.readFile);
1415

@@ -39,14 +40,20 @@ export class FreshChrome {
3940
const certificatePem = await readFile(path.join(this.config.configPath, 'ca.pem'), 'utf8');
4041
const spkiFingerprint = generateSPKIFingerprint(certificatePem);
4142

42-
const browser = await launchBrowser('https://amiusing.httptoolkit.tech', {
43+
const hideWarningServer = new HideChromeWarningServer();
44+
await hideWarningServer.start('https://amiusing.httptoolkit.tech');
45+
46+
const browser = await launchBrowser(hideWarningServer.hideWarningUrl, {
4347
browser: 'chrome',
4448
proxy: `https://localhost:${proxyPort}`,
4549
options: [
4650
`--ignore-certificate-errors-spki-list=${spkiFingerprint}`
4751
]
4852
}, this.config.configPath);
4953

54+
await hideWarningServer.completedPromise;
55+
await hideWarningServer.stop();
56+
5057
browsers[proxyPort] = browser;
5158
browser.process.once('exit', () => {
5259
delete browsers[proxyPort];

test/interceptors.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ const interceptors = buildInterceptors({ configPath, https: { certPath, keyPath
2121
_.forEach(interceptors, (interceptor, name) =>
2222
describe(`${name} interceptor`, function () {
2323

24-
beforeEach(() => server.start());
24+
beforeEach(async () => {
25+
await server.start();
26+
await server.anyRequest().thenPassThrough();
27+
});
28+
2529
afterEach(async () => {
2630
await interceptor.deactivate(server.port);
2731
await server.stop();
@@ -49,8 +53,6 @@ _.forEach(interceptors, (interceptor, name) =>
4953
this.skip();
5054
}
5155

52-
await server.anyRequest().thenPassThrough();
53-
5456
const exampleRequestReceived = new Promise<CompletedRequest>((resolve) =>
5557
server.on('request', (req) => {
5658
if (req.url.startsWith('https://amiusing.httptoolkit.tech')) {

0 commit comments

Comments
 (0)