-
Notifications
You must be signed in to change notification settings - Fork 11
Add configuration option to disable automatically trusting all certificates #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
1bd8b0b
b9a2923
12b9d03
db53e3c
25e934b
ea20d36
f8323f1
3919bf4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,22 +14,69 @@ Future<String?> _getProxyPort() async { | |
| } | ||
|
|
||
| class HttpProxyOverride extends HttpOverrides { | ||
| late final String? host; | ||
| late final String? port; | ||
| /// The host part of the proxy address. | ||
| final String? host; | ||
|
|
||
| HttpProxyOverride._(this.host, this.port); | ||
| /// The port part of the proxy address. | ||
| final String? port; | ||
|
|
||
| static Future<HttpProxyOverride> createHttpProxy() async { | ||
| return HttpProxyOverride._(await _getProxyHost(), await _getProxyPort()); | ||
| /// Configures whether a secure connection to a host should be allowed with | ||
| /// a server certificate that cannot be authenticated by any of the trusted | ||
| /// root certificates. | ||
| final bool ignoreBadCertificates; | ||
|
|
||
| final SecurityContext securityContext; | ||
|
|
||
| HttpProxyOverride._( | ||
| this.host, this.port, this.ignoreBadCertificates, this.securityContext); | ||
|
|
||
| /// Create an instance of [HttpProxyOverride]. | ||
| /// | ||
| /// Reads the configured proxy host and port from the underlying platform | ||
| /// and configures the [HttpClient] to use the proxy. The proxy settings are | ||
| /// read once at creation time. | ||
| /// | ||
| /// [ignoreBadCertificates] configures whether a secure connection to a host | ||
| /// should be allowed with a server certificate that cannot be authenticated | ||
| /// by any of our trusted root certificates. | ||
| /// For example, this can be useful when using debugging proxies like Charles | ||
| /// or mitmproxy during development. | ||
| /// **Do not enable this in production unless you are 100% sure.** Setting | ||
| /// this enables MITM attacks. | ||
| /// Default: `false`. | ||
| /// | ||
| /// With [securityContext] a [SecurityContext] can be provided that is used to | ||
| /// construct the [HttpClient]. This can be useful to provide a | ||
| /// [SecurityContext] that is configured with certificates that a proxy | ||
| /// server requires. | ||
| /// | ||
| /// Supported platforms to read proxy settings from are **iOS** and | ||
| /// **Android**. | ||
| static Future<HttpProxyOverride> create( | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think since this library aims to use for debugging purposes
e.g. static Future<HttpProxyOverride?> create(
{bool ignoreBadCertificates = true,
SecurityContext? securityContext}) async {
if (!ignoreBadCertificates) return null;
return HttpProxyOverride._(
await _getProxyHost(),
await _getProxyPort(),
ignoreBadCertificates,
securityContext ?? SecurityContext.defaultContext);
} |
||
| {bool ignoreBadCertificates = false, | ||
| SecurityContext? securityContext}) async { | ||
| return HttpProxyOverride._( | ||
| await _getProxyHost(), | ||
| await _getProxyPort(), | ||
| ignoreBadCertificates, | ||
| securityContext ?? SecurityContext.defaultContext); | ||
| } | ||
|
|
||
| @override | ||
| HttpClient createHttpClient(SecurityContext? context) { | ||
| if (context == null) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The final context = SecurityContext.defaultContext;
context.setTrustedCertificatesBytes(...);
HttpClient httpClient = HttpClient(context: context);
HttpProxyOverride httpProxyOverride = await HttpProxyOverride.create();but not HttpClient httpClient = HttpClient();
final context = SecurityContext.defaultContext;
context.setTrustedCertificatesBytes(...);
HttpProxyOverride httpProxyOverride = await HttpProxyOverride.create(securityContext: context); |
||
| context = this.securityContext; | ||
| } | ||
|
|
||
| var client = super.createHttpClient(context); | ||
| client.badCertificateCallback = | ||
| (X509Certificate cert, String host, int port) { | ||
| return true; | ||
| }; | ||
|
|
||
| if (ignoreBadCertificates) { | ||
| client.badCertificateCallback = | ||
| (X509Certificate cert, String host, int port) { | ||
| return true; | ||
| }; | ||
| } | ||
|
|
||
| return client; | ||
| } | ||
|
|
||
|
|
@@ -47,8 +94,8 @@ class HttpProxyOverride extends HttpOverrides { | |
| environment['http_proxy'] = '$host:$port'; | ||
| environment['https_proxy'] = '$host:$port'; | ||
| } else { | ||
| environment['http_proxy'] = '$host:8888'; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, but I'm not sure if it can be set to |
||
| environment['https_proxy'] = '$host:8888'; | ||
| environment['http_proxy'] = '$host'; | ||
| environment['https_proxy'] = '$host'; | ||
| } | ||
|
|
||
| return super.findProxyFromEnvironment(url, environment); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should mark this function as
@deprecatedinstead remove it directly, to avoid break change at this time