-
-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement request blocking based on whitelist for the main proc…
…ess of the electrum
- Loading branch information
1 parent
ad8e308
commit e66a922
Showing
20 changed files
with
446 additions
and
68 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
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
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
35 changes: 27 additions & 8 deletions
35
packages/request-manager/src/interceptor/interceptTlsConnect.ts
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,25 +1,44 @@ | ||
import tls from 'tls'; | ||
|
||
import { InterceptorContext, isWhitelistedHost } from './interceptorTypesAndUtils'; | ||
import { Interceptor, isWhitelistedHost } from './interceptorTypesAndUtils'; | ||
|
||
export const interceptTlsConnect = (context: InterceptorContext) => { | ||
export const interceptTlsConnect: Interceptor = ({ context, validateRequest }) => { | ||
const originalTlsConnect = tls.connect; | ||
|
||
tls.connect = (...args) => { | ||
const [options] = args; | ||
if (typeof options === 'object') { | ||
const [optionsOrPort, optionsOrHost] = args; | ||
if (typeof optionsOrPort === 'object') { | ||
context.handler({ | ||
type: 'INTERCEPTED_REQUEST', | ||
method: 'tls.connect', | ||
details: options.host || options.servername || 'unknown', | ||
details: optionsOrPort.host || optionsOrPort.servername || 'unknown', | ||
}); | ||
|
||
// allow untrusted/self-signed certificates for whitelisted domains (like https://*.sldev.cz) | ||
options.rejectUnauthorized = | ||
options.rejectUnauthorized ?? | ||
!isWhitelistedHost(options.host, context.notRequiredTorDomainsList); | ||
optionsOrPort.rejectUnauthorized = | ||
optionsOrPort.rejectUnauthorized ?? | ||
!isWhitelistedHost(optionsOrPort.host, context.notRequiredTorDomainsList); | ||
} | ||
|
||
const getHostname = (): string => { | ||
if (typeof optionsOrPort === 'object') { | ||
return optionsOrPort.host || optionsOrPort.servername || 'unknown'; | ||
} | ||
|
||
if (typeof optionsOrHost === 'string') { | ||
return optionsOrHost; | ||
} | ||
|
||
return ''; | ||
}; | ||
|
||
const hostname = getHostname().split(':')[0] ?? ''; | ||
|
||
// This is here for defensive reasons, the original `tls.connect` implementation (AFAIK) | ||
// uses net.connect to create new socket, and it already contains the interception logic. | ||
// But to be 100% sure, lets do the check here as well. | ||
validateRequest({ hostname }); | ||
|
||
return originalTlsConnect(...(args as Parameters<typeof tls.connect>)); | ||
}; | ||
}; |
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
Oops, something went wrong.