-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathauthenticating_artifact_updater.dart
More file actions
306 lines (265 loc) · 8.1 KB
/
Copy pathauthenticating_artifact_updater.dart
File metadata and controls
306 lines (265 loc) · 8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// ignore_for_file: implementation_imports
import 'dart:async';
import 'dart:io' as io;
import 'package:archive/archive_io.dart';
import 'package:flutterpi_tool/src/fltool/common.dart';
import 'package:flutterpi_tool/src/more_os_utils.dart';
import 'package:meta/meta.dart';
@visibleForTesting
String legalizePath(Uri url, FileSystem fileSystem) {
final pieces = [url.host, ...url.pathSegments];
final convertedPieces = pieces.map(_legalizeName);
return fileSystem.path.joinAll(convertedPieces);
}
String _legalizeName(String fileName) {
const substitutions = {
r'@': '@@',
r'/': '@s@',
r'\': '@bs@',
r':': '@c@',
r'%': '@per@',
r'*': '@ast@',
r'<': '@lt@',
r'>': '@gt@',
r'"': '@q@',
r'|': '@pip@',
r'?': '@ques@',
};
final replaced = [
for (final codeUnit in fileName.codeUnits)
if (substitutions[String.fromCharCode(codeUnit)] case String substitute)
...substitute.codeUnits
else
codeUnit,
];
return String.fromCharCodes(replaced);
}
class AuthenticatingArtifactUpdater implements ArtifactUpdater {
AuthenticatingArtifactUpdater({
required MoreOperatingSystemUtils operatingSystemUtils,
required Logger logger,
required FileSystem fileSystem,
required Directory tempStorage,
required io.HttpClient httpClient,
required Platform platform,
required List<String> allowedBaseUrls,
}) : _operatingSystemUtils = operatingSystemUtils,
_httpClient = httpClient,
_logger = logger,
_fileSystem = fileSystem,
_tempStorage = tempStorage,
_allowedBaseUrls = allowedBaseUrls;
static const int _kRetryCount = 2;
final Logger _logger;
final MoreOperatingSystemUtils _operatingSystemUtils;
final FileSystem _fileSystem;
final Directory _tempStorage;
final io.HttpClient _httpClient;
final List<String> _allowedBaseUrls;
@override
@visibleForTesting
final List<File> downloadedFiles = <File>[];
static const Set<String> _denylistedBasenames = <String>{
'entitlements.txt',
'without_entitlements.txt',
};
void _removeDenylistedFiles(Directory directory) {
for (final FileSystemEntity entity in directory.listSync(recursive: true)) {
if (entity is! File) {
continue;
}
if (_denylistedBasenames.contains(entity.basename)) {
entity.deleteSync();
}
}
}
@override
Future<void> downloadZipArchive(
String message,
Uri url,
Directory location, {
void Function(io.HttpClientRequest)? authenticate,
}) {
return _downloadArchive(
message,
url,
location,
_operatingSystemUtils.unzip,
authenticate: authenticate,
);
}
@override
Future<void> downloadZippedTarball(
String message,
Uri url,
Directory location, {
void Function(io.HttpClientRequest)? authenticate,
}) {
return _downloadArchive(
message,
url,
location,
_operatingSystemUtils.unpack,
authenticate: authenticate,
);
}
Future<void> downloadArchive(
String message,
Uri url,
Directory location, {
void Function(io.HttpClientRequest)? authenticate,
ArchiveType? archiveType,
Archive Function(File)? decoder,
}) {
return _downloadArchive(
message,
url,
location,
(File file, Directory targetDirectory) {
_operatingSystemUtils.unpack(
file,
targetDirectory,
type: archiveType,
decoder: decoder,
);
},
authenticate: authenticate,
);
}
@override
Future<void> downloadFile(String message, Uri url, Directory location) {
return _downloadArchive(message, url, location, (File file, Directory dir) {
file.copySync(dir.childFile(file.basename).path);
});
}
Future<void> _downloadArchive(
String message,
Uri url,
Directory location,
void Function(File, Directory) extractor, {
void Function(io.HttpClientRequest)? authenticate,
}) async {
final downloadPath = legalizePath(url, _fileSystem);
final tempFile = _createDownloadFile(downloadPath);
var tries = _kRetryCount;
while (tries > 0) {
final status = _logger.startProgress(message);
try {
ErrorHandlingFileSystem.deleteIfExists(tempFile);
if (!tempFile.parent.existsSync()) {
tempFile.parent.createSync(recursive: true);
}
await _download(url, tempFile, status, authenticate: authenticate);
if (!tempFile.existsSync()) {
throw Exception('Did not find downloaded file ${tempFile.path}');
}
} on Exception catch (err) {
_logger.printTrace(err.toString());
tries -= 1;
if (tries == 0) {
throwToolExit(
'Failed to download $url. Ensure you have network connectivity and then try again.\n$err',
);
}
continue;
} finally {
status.stop();
}
final destination = location.childDirectory(
tempFile.fileSystem.path.basenameWithoutExtension(tempFile.path),
);
ErrorHandlingFileSystem.deleteIfExists(destination, recursive: true);
location.createSync(recursive: true);
try {
extractor(tempFile, location);
} on Exception catch (err) {
tries -= 1;
if (tries == 0) {
throwToolExit(
'Flutter could not download and/or extract $url. Ensure you have '
'network connectivity and all of the required dependencies listed at '
'flutter.dev/setup.\nThe original exception was: $err.',
);
}
ErrorHandlingFileSystem.deleteIfExists(tempFile);
continue;
}
_removeDenylistedFiles(location);
return;
}
}
Future<void> _download(
Uri url,
File file,
Status status, {
void Function(io.HttpClientRequest)? authenticate,
}) async {
final allowed =
_allowedBaseUrls.any((baseUrl) => url.toString().startsWith(baseUrl));
// In tests make this a hard failure.
assert(
allowed,
'URL not allowed: $url\n'
'Allowed URLs must be based on one of: ${_allowedBaseUrls.join(', ')}',
);
// In production, issue a warning but allow the download to proceed.
if (!allowed) {
status.pause();
_logger.printWarning(
'Downloading an artifact that may not be reachable in some environments (e.g. firewalled environments): $url\n'
'This should not have happened. This is likely a Flutter SDK bug. Please file an issue at https://github.com/flutter/flutter/issues/new?template=1_activation.yml');
status.resume();
}
final request = await _httpClient.getUrl(url);
if (authenticate != null) {
try {
authenticate(request);
} finally {
request.close().ignore();
}
}
final response = await request.close();
if (response.statusCode != io.HttpStatus.ok) {
throw Exception(response.statusCode);
}
final handle = file.openSync(mode: FileMode.writeOnly);
try {
await for (final chunk in response) {
handle.writeFromSync(chunk);
}
} finally {
handle.closeSync();
}
}
File _createDownloadFile(String name) {
final path = _fileSystem.path.join(_tempStorage.path, name);
final file = _fileSystem.file(path);
downloadedFiles.add(file);
return file;
}
@override
void setProgressContext({
required int artifactIndex,
required int artifactTotal,
required int downloadTotal,
int downloadIndex = 0,
}) {}
@override
void resetProgressContext() {}
@override
String formatProgressMessage(String artifactName) => artifactName;
@override
void removeDownloadedFiles() {
for (final file in downloadedFiles) {
ErrorHandlingFileSystem.deleteIfExists(file);
for (var directory = file.parent;
directory.absolute.path != _tempStorage.absolute.path;
directory = directory.parent) {
// Handle race condition when the directory is deleted before this step
if (directory.existsSync() && directory.listSync().isEmpty) {
ErrorHandlingFileSystem.deleteIfExists(directory, recursive: true);
}
}
}
}
}