forked from Jigsaw-Code/outline-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Jigsaw-Code#1090 from Jigsaw-Code/bemasc-custom-fetch
fix(manager): Proxy Shadowbox HTTP requests through Node
- Loading branch information
Showing
22 changed files
with
2,964 additions
and
3,483 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
;enforces that the user is `npm install`ing with the correct node version | ||
; Enforces that the user is `npm install`ing with the correct node version. | ||
engine-strict=true | ||
|
||
; Workaround for conflict between the default location(s) of node-forge and the | ||
; location expected by Typescript, Jasmine, and Electron. | ||
prefer-dedupe=true |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2022 The Outline Authors | ||
// | ||
// 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. | ||
|
||
import * as crypto from 'crypto'; | ||
import * as tls from 'tls'; | ||
import * as https from 'https'; | ||
import * as forge from 'node-forge'; | ||
|
||
import {fetchWithPin} from './fetch'; | ||
import {AddressInfo} from 'net'; | ||
|
||
describe('fetchWithPin', () => { | ||
it('throws on pin mismatch (remote)', async () => { | ||
const result = fetchWithPin( | ||
{url: 'https://www.gstatic.com/', method: 'GET'}, | ||
'incorrect fingerprint' | ||
); | ||
await expectAsync(result).toBeRejectedWithError(Error, /Fingerprint mismatch/); | ||
}); | ||
|
||
// Make a certificate. | ||
const {privateKey, publicKey} = forge.pki.rsa.generateKeyPair(1024); | ||
const cert = forge.pki.createCertificate(); | ||
cert.publicKey = publicKey; | ||
cert.sign(privateKey); // Self-signed cert | ||
|
||
// Serialize the certificate for `tls.createServer()`. | ||
const keyPem = forge.pki.privateKeyToPem(privateKey); | ||
const certPem = forge.pki.certificateToPem(cert); | ||
|
||
// Compute the certificate fingerprint. | ||
const certDer = forge.pki.pemToDer(certPem); | ||
const sha256 = crypto.createHash('sha256'); | ||
const certSha256 = sha256.update(certDer.data, 'binary').digest().toString('binary'); | ||
|
||
it('throws on pin mismatch (local)', async () => { | ||
const server = tls.createServer({key: keyPem, cert: certPem}); | ||
await new Promise<void>((fulfill) => server.listen(0, fulfill)); | ||
|
||
const address = server.address() as AddressInfo; | ||
const req = { | ||
url: `https://localhost:${address.port}/foo`, | ||
method: 'GET', | ||
}; | ||
|
||
// Fail if the TLS handshake completes. | ||
server.on('secureConnection', fail); | ||
|
||
const clientClosed = new Promise((fulfill) => | ||
server.on('connection', (socket) => socket.on('close', fulfill)) | ||
); | ||
|
||
const result = fetchWithPin(req, 'incorrect fingerprint'); | ||
await expectAsync(result).toBeRejectedWithError(Error, /Fingerprint mismatch/); | ||
|
||
// Don't stop the test until the client has closed the TCP socket. | ||
await clientClosed; | ||
}); | ||
|
||
it('succeeds on pin match', async () => { | ||
const server = https.createServer({key: keyPem, cert: certPem}); | ||
await new Promise<void>((fulfill) => server.listen(0, fulfill)); | ||
|
||
const address = server.address() as AddressInfo; | ||
const req = { | ||
url: `https://localhost:${address.port}/foo`, | ||
method: 'GET', | ||
}; | ||
server.on('request', (incoming, response) => { | ||
expect(incoming.url).toBe('/foo'); | ||
expect(incoming.method).toBe('GET'); | ||
response.writeHead(200); | ||
response.write('test test'); | ||
response.end(); | ||
}); | ||
|
||
const result = fetchWithPin(req, certSha256); | ||
await expectAsync(result).toBeResolvedTo({ | ||
status: 200, | ||
body: 'test test', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2022 The Outline Authors | ||
// | ||
// 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. | ||
|
||
import * as https from 'https'; | ||
import {TLSSocket} from 'tls'; | ||
import {urlToHttpOptions} from 'url'; | ||
|
||
import type {IncomingMessage} from 'http'; | ||
|
||
import type {HttpRequest, HttpResponse} from '../infrastructure/path_api'; | ||
|
||
export const fetchWithPin = async ( | ||
req: HttpRequest, | ||
fingerprint: string | ||
): Promise<HttpResponse> => { | ||
const response = await new Promise<IncomingMessage>((resolve, reject) => { | ||
const options: https.RequestOptions = { | ||
...urlToHttpOptions(new URL(req.url)), | ||
method: req.method, | ||
headers: req.headers, | ||
rejectUnauthorized: false, // Disable certificate chain validation. | ||
}; | ||
const request = https.request(options, resolve).on('error', reject); | ||
|
||
// Enforce certificate fingerprint match. | ||
request.on('socket', (socket: TLSSocket) => | ||
socket.on('secureConnect', () => { | ||
const certificate = socket.getPeerCertificate(); | ||
// Parse fingerprint in "AB:CD:EF" form. | ||
const sha2hex = certificate.fingerprint256.replace(/:/g, ''); | ||
const sha2binary = Buffer.from(sha2hex, 'hex').toString('binary'); | ||
if (sha2binary !== fingerprint) { | ||
request.emit( | ||
'error', | ||
new Error(`Fingerprint mismatch: expected ${fingerprint}, not ${sha2binary}`) | ||
); | ||
request.destroy(); | ||
return; | ||
} | ||
}) | ||
); | ||
|
||
if (req.body) { | ||
request.write(req.body); | ||
} | ||
|
||
request.end(); | ||
}); | ||
|
||
const chunks: Buffer[] = []; | ||
for await (const chunk of response) { | ||
chunks.push(chunk); | ||
} | ||
|
||
return { | ||
status: response.statusCode, | ||
body: Buffer.concat(chunks).toString(), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2022 The Outline Authors | ||
// | ||
// 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. | ||
|
||
import {PathApiClient} from './path_api'; | ||
|
||
describe('PathApi', () => { | ||
// Mock fetcher | ||
let lastRequest: HttpRequest; | ||
let nextResponse: Promise<HttpResponse>; | ||
|
||
const fetcher = (request: HttpRequest) => { | ||
lastRequest = request; | ||
return nextResponse; | ||
}; | ||
|
||
beforeEach(() => { | ||
lastRequest = undefined; | ||
nextResponse = undefined; | ||
}); | ||
|
||
const api = new PathApiClient('https://asdf.test/foo', fetcher); | ||
|
||
it('GET', async () => { | ||
const response = {status: 200, body: '{"asdf": true}'}; | ||
nextResponse = Promise.resolve(response); | ||
expect(await api.request('bar')).toEqual({asdf: true}); | ||
expect(lastRequest).toEqual({ | ||
url: 'https://asdf.test/foo/bar', | ||
method: 'GET', | ||
}); | ||
}); | ||
|
||
it('PUT form data', async () => { | ||
const response = {status: 200, body: '{"asdf": true}'}; | ||
nextResponse = Promise.resolve(response); | ||
expect(await api.requestForm('bar', 'PUT', {name: 'value'})).toEqual({asdf: true}); | ||
expect(lastRequest).toEqual({ | ||
url: 'https://asdf.test/foo/bar', | ||
method: 'PUT', | ||
headers: {'Content-Type': 'application/x-www-form-urlencoded'}, | ||
body: 'name=value', | ||
}); | ||
}); | ||
|
||
it('POST JSON data', async () => { | ||
const response = {status: 200, body: '{"asdf": true}'}; | ||
nextResponse = Promise.resolve(response); | ||
expect(await api.requestJson('bar', 'POST', {key: 'value'})).toEqual({asdf: true}); | ||
expect(lastRequest).toEqual({ | ||
url: 'https://asdf.test/foo/bar', | ||
method: 'POST', | ||
headers: {'Content-Type': 'application/json'}, | ||
body: '{"key":"value"}', | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.