forked from flutter/engine
-
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.
Reland "Provide better messaging when user attempts to use non-secure…
… http connection. (flutter#26226)" (flutter#26400) This reverts commit c7ef259 as dart sdk with corresponding changes has landed in internal repo.
- Loading branch information
Showing
6 changed files
with
176 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2021 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// @dart = 2.9 | ||
|
||
import 'dart:async'; | ||
import 'dart:io'; | ||
|
||
import 'package:litetest/litetest.dart'; | ||
|
||
import 'http_disallow_http_connections_test.dart'; | ||
|
||
void main() { | ||
test('Normal HTTP request succeeds', () async { | ||
final String host = await getLocalHostIP(); | ||
await bindServerAndTest(host, (HttpClient httpClient, Uri uri) async { | ||
await httpClient.getUrl(uri); | ||
}); | ||
}); | ||
|
||
test('We can ban HTTP explicitly.', () async { | ||
final String host = await getLocalHostIP(); | ||
await bindServerAndTest(host, (HttpClient httpClient, Uri uri) async { | ||
asyncExpectThrows<UnsupportedError>( | ||
() async => runZoned(() => httpClient.getUrl(uri), | ||
zoneValues: <dynamic, dynamic>{#flutter.io.allow_http: false})); | ||
}); | ||
}); | ||
} |
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,86 @@ | ||
// Copyright 2021 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// @dart = 2.9 | ||
|
||
// FlutterTesterOptions=--disallow-insecure-connections | ||
|
||
import 'dart:async'; | ||
import 'dart:io'; | ||
|
||
import 'package:litetest/litetest.dart'; | ||
|
||
/// Asserts that `callback` throws an exception of type `T`. | ||
Future<void> asyncExpectThrows<T>(Function callback) async { | ||
bool threw = false; | ||
try { | ||
await callback(); | ||
} catch (e) { | ||
expect(e is T, true); | ||
threw = true; | ||
} | ||
expect(threw, true); | ||
} | ||
|
||
Future<String> getLocalHostIP() async { | ||
final List<NetworkInterface> interfaces = await NetworkInterface.list( | ||
includeLoopback: false, type: InternetAddressType.IPv4); | ||
return interfaces.first.addresses.first.address; | ||
} | ||
|
||
Future<void> bindServerAndTest(String serverHost, | ||
Future<void> Function(HttpClient client, Uri uri) testCode) async { | ||
final HttpClient httpClient = HttpClient(); | ||
final HttpServer server = await HttpServer.bind(serverHost, 0); | ||
final Uri uri = Uri(scheme: 'http', host: serverHost, port: server.port); | ||
try { | ||
await testCode(httpClient, uri); | ||
} finally { | ||
httpClient.close(force: true); | ||
await server.close(); | ||
} | ||
} | ||
|
||
/// Answers the question whether this computer supports binding to IPv6 addresses. | ||
Future<bool> _supportsIPv6() async { | ||
try { | ||
final ServerSocket socket = await ServerSocket.bind(InternetAddress.loopbackIPv6, 0); | ||
await socket.close(); | ||
return true; | ||
} on SocketException catch (_) { | ||
return false; | ||
} | ||
} | ||
|
||
void main() { | ||
test('testWithHostname', () async { | ||
await bindServerAndTest(await getLocalHostIP(), (HttpClient httpClient, Uri httpUri) async { | ||
asyncExpectThrows<UnsupportedError>( | ||
() async => httpClient.getUrl(httpUri)); | ||
asyncExpectThrows<UnsupportedError>( | ||
() async => runZoned(() => httpClient.getUrl(httpUri), | ||
zoneValues: <dynamic, dynamic>{#flutter.io.allow_http: 'foo'})); | ||
asyncExpectThrows<UnsupportedError>( | ||
() async => runZoned(() => httpClient.getUrl(httpUri), | ||
zoneValues: <dynamic, dynamic>{#flutter.io.allow_http: false})); | ||
await runZoned(() => httpClient.getUrl(httpUri), | ||
zoneValues: <dynamic, dynamic>{#flutter.io.allow_http: true}); | ||
}); | ||
}); | ||
|
||
test('testWithLoopback', () async { | ||
await bindServerAndTest('127.0.0.1', (HttpClient httpClient, Uri uri) async { | ||
await httpClient.getUrl(Uri.parse('http://localhost:${uri.port}')); | ||
await httpClient.getUrl(Uri.parse('http://127.0.0.1:${uri.port}')); | ||
}); | ||
}); | ||
|
||
test('testWithIPV6', () async { | ||
if (await _supportsIPv6()) { | ||
await bindServerAndTest('::1', (HttpClient httpClient, Uri uri) async { | ||
await httpClient.getUrl(uri); | ||
}); | ||
} | ||
}); | ||
} |
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