Skip to content

Commit 41c7a8f

Browse files
kevmoosuryaKommana2662
authored andcommitted
[flutter_tools] Remove AzureDetector IMDS probe from BotDetector (flutter#194530)
Remove `AzureDetector` (`http://169.254.169.254/metadata/instance`) and persistent `is-bot` caching (`PersistentToolState.isRunningOnBot` / `setIsRunningOnBot`) from `BotDetector`, relying strictly on deterministic CI environment variables. - **Remove IMDS probe and disk cache (`bot_detector.dart`, `persistent_tool_state.dart`)**: Eliminates the link-local HTTP probe on tool startup, prevents cloud developer workstations on Azure/GCE/AWS from being permanently misclassified as bots in `.flutter_tool_state`, and removes unused `httpClientFactory` and `persistentToolState` parameters from `BotDetector`. - **Tighten `BOT` and `CI` environment checks (`bot_detector.dart`)**: Treat `BOT=false` and `BOT=0` as explicit non-bot overrides, require `CI` to be non-empty and not `'false'` or `'0'`, accept `BOT=true` or `BOT=1`, and check `AZURE_HTTP_USER_AGENT` alongside `TF_BUILD` (matching `dartdev`). - **Async getter retention**: `Future<bool> get isRunningOnBot` is intentionally kept async (`Future<bool>`) in this PR to avoid churn across the 12 production `await` sites and `FakeBotDetector` in tests; once `package:unified_analytics` rolls with `Analytics.isBot(Map<String, String> environment)` (tracked in dart-lang/tools#2672 / dart-lang/tools#2687), a follow-up PR for flutter#194405 can delegate directly or simplify callers if desired. Part of flutter#194405
1 parent dfff13a commit 41c7a8f

5 files changed

Lines changed: 56 additions & 307 deletions

File tree

‎packages/flutter_tools/lib/src/base/bot_detector.dart‎

Lines changed: 12 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,18 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'dart:async';
6-
7-
import 'package:meta/meta.dart';
8-
9-
import '../persistent_tool_state.dart';
10-
import 'io.dart';
11-
import 'net.dart';
125
import 'platform.dart';
136

147
class BotDetector {
15-
BotDetector({
16-
required HttpClientFactory httpClientFactory,
17-
required this._platform,
18-
required this._persistentToolState,
19-
}) : _azureDetector = AzureDetector(httpClientFactory: httpClientFactory);
8+
const BotDetector({required this._platform});
209

2110
final Platform _platform;
22-
final AzureDetector _azureDetector;
23-
final PersistentToolState _persistentToolState;
2411

2512
Future<bool> get isRunningOnBot async {
13+
final String? bot = _platform.environment['BOT']?.trim().toLowerCase();
2614
if ( // Explicitly stated to not be a bot.
27-
_platform.environment['BOT'] == 'false'
15+
bot == 'false' ||
16+
bot == '0'
2817
// Set by the IDEs to the IDE name, so a strong signal that this is not a bot.
2918
||
3019
_platform.environment.containsKey('FLUTTER_HOST')
@@ -34,12 +23,14 @@ class BotDetector {
3423
return false;
3524
}
3625

37-
if (_platform.environment['BOT'] == 'true'
26+
final String? ci = _platform.environment['CI']?.trim().toLowerCase();
27+
return bot == 'true' ||
28+
bot == '1'
3829
// https://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables
3930
||
4031
_platform.environment['TRAVIS'] == 'true' ||
4132
_platform.environment['CONTINUOUS_INTEGRATION'] == 'true' ||
42-
_platform.environment.containsKey('CI') // Travis and AppVeyor
33+
(ci != null && ci.isNotEmpty && ci != 'false' && ci != '0') // Travis and AppVeyor
4334
// https://www.appveyor.com/docs/environment-variables/
4435
||
4536
_platform.environment.containsKey('APPVEYOR')
@@ -64,59 +55,11 @@ class BotDetector {
6455
// Property when running on borg.
6556
||
6657
_platform.environment.containsKey('BORG_ALLOC_DIR')
58+
// Property when running on Azure.
59+
||
60+
_platform.environment.containsKey('AZURE_HTTP_USER_AGENT')
6761
// https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables
6862
||
69-
_platform.environment.containsKey('TF_BUILD')) {
70-
return true;
71-
}
72-
73-
if (_persistentToolState.isRunningOnBot != null) {
74-
return _persistentToolState.isRunningOnBot!;
75-
}
76-
77-
final bool result = await _azureDetector.isRunningOnAzure;
78-
_persistentToolState.setIsRunningOnBot(result);
79-
return result;
80-
}
81-
}
82-
83-
// Are we running on Azure?
84-
// https://docs.microsoft.com/en-us/azure/virtual-machines/linux/instance-metadata-service
85-
@visibleForTesting
86-
class AzureDetector {
87-
AzureDetector({required this._httpClientFactory});
88-
89-
static const _serviceUrl = 'http://169.254.169.254/metadata/instance';
90-
91-
final HttpClientFactory _httpClientFactory;
92-
93-
bool? _isRunningOnAzure;
94-
95-
Future<bool> get isRunningOnAzure async {
96-
if (_isRunningOnAzure != null) {
97-
return _isRunningOnAzure!;
98-
}
99-
const connectionTimeout = Duration(milliseconds: 250);
100-
const requestTimeout = Duration(seconds: 1);
101-
final HttpClient client = _httpClientFactory()..connectionTimeout = connectionTimeout;
102-
try {
103-
final HttpClientRequest request = await client
104-
.getUrl(Uri.parse(_serviceUrl))
105-
.timeout(requestTimeout);
106-
request.headers.add('Metadata', true);
107-
await request.close();
108-
} on HttpException {
109-
// The connection was established but an HTTP error occurred.
110-
// This still indicates we're running on Azure.
111-
return _isRunningOnAzure = true;
112-
} on Object {
113-
// Metadata detection is best-effort. Any other failure (socket errors,
114-
// timeouts, malformed redirect URIs, WSL1 networking issues, etc.)
115-
// should not prevent Flutter from starting.
116-
return _isRunningOnAzure = false;
117-
}
118-
119-
// We got a response. We're running on Azure.
120-
return _isRunningOnAzure = true;
63+
_platform.environment.containsKey('TF_BUILD');
12164
}
12265
}

‎packages/flutter_tools/lib/src/context/tool_dependencies.dart‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,7 @@ class ToolDependencies {
276276
persistentToolState ??
277277
PersistentToolState(fileSystem: finalFS, logger: finalLogger, platform: finalPlatform);
278278

279-
final BotDetector finalBotDetector =
280-
botDetector ??
281-
BotDetector(
282-
httpClientFactory: () => HttpClient(),
283-
platform: finalPlatform,
284-
persistentToolState: finalPersistentToolState,
285-
);
279+
final BotDetector finalBotDetector = botDetector ?? BotDetector(platform: finalPlatform);
286280

287281
final bool isBot = await finalBotDetector.isRunningOnBot;
288282

‎packages/flutter_tools/lib/src/globals.dart‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,7 @@ LocalEngineLocator? get localEngineLocator => context.get<LocalEngineLocator>();
8181
PersistentToolState? get persistentToolState => PersistentToolState.instance;
8282

8383
BotDetector get botDetector => context.get<BotDetector>() ?? _defaultBotDetector;
84-
final _defaultBotDetector = BotDetector(
85-
httpClientFactory: context.get<HttpClientFactory>() ?? () => HttpClient(),
86-
platform: platform,
87-
persistentToolState:
88-
persistentToolState ??
89-
PersistentToolState(fileSystem: fs, logger: logger, platform: platform),
90-
);
84+
final _defaultBotDetector = BotDetector(platform: platform);
9185
Future<bool> get isRunningOnBot => botDetector.isRunningOnBot;
9286

9387
// Analytics instance for package:unified_analytics for analytics

‎packages/flutter_tools/lib/src/persistent_tool_state.dart‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ abstract class PersistentToolState {
4343
String? get lastActiveLicenseTermsHash;
4444
void setLastActiveLicenseTermsHash(String value); // Enforced nonnull setter.
4545

46-
/// Whether this client was already determined to be or not be a bot.
47-
bool? get isRunningOnBot;
48-
void setIsRunningOnBot(bool value); // Enforced nonnull setter.
49-
5046
/// Whether the Intel Mac warning message should be displayed.
5147
///
5248
/// This is typically set to false after the message has been shown once.
@@ -73,7 +69,6 @@ class _DefaultPersistentToolState implements PersistentToolState {
7369
Channel.beta: 'last-active-beta-version',
7470
Channel.stable: 'last-active-stable-version',
7571
};
76-
static const _kBotKey = 'is-bot';
7772
static const _kLicenseHash = 'license-hash';
7873
static const _kHasShownIntelMacWarningKey = 'displayed-intel-warning-message';
7974

@@ -115,14 +110,6 @@ class _DefaultPersistentToolState implements PersistentToolState {
115110
return _lastActiveVersionKeys[channel];
116111
}
117112

118-
@override
119-
bool? get isRunningOnBot => _config.getValue(_kBotKey) as bool?;
120-
121-
@override
122-
void setIsRunningOnBot(bool value) {
123-
_config.setValue(_kBotKey, value);
124-
}
125-
126113
@override
127114
bool get shouldShowIntelMacWarning => _config.getValue(_kHasShownIntelMacWarningKey) == null;
128115

0 commit comments

Comments
 (0)