Skip to content

Commit e05f76b

Browse files
authored
Properly handle case where the client is unable to fetch advisories (#4275)
1 parent 0e870f2 commit e05f76b

File tree

4 files changed

+107
-1
lines changed

4 files changed

+107
-1
lines changed

lib/src/source/hosted.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,10 +594,23 @@ class HostedSource extends CachedSource {
594594
result = _extractAdvisoryDetailsForPackage(decoded, ref.name);
595595
} on FormatException catch (error, stackTrace) {
596596
log.warning(
597-
'Failed to fetch advisories for $packageName from $hostedUrl.\n'
597+
'Failed to decode advisories for $packageName from $hostedUrl.\n'
598598
'$error\n'
599599
'${Chain.forTrace(stackTrace)}');
600600
return null;
601+
} on PubHttpResponseException catch (error, stackTrace) {
602+
if (isPubDevUrl(hostedUrl)) {
603+
fail(
604+
'Failed to fetch advisories for "$packageName" from "$hostedUrl".\n',
605+
error,
606+
stackTrace,
607+
);
608+
} else {
609+
log.warning(
610+
'Warning: Unable to fetch advisories for "$packageName" from "$hostedUrl".\n',
611+
);
612+
}
613+
return null;
601614
}
602615

603616
// Cache the response on disk.

test/get/hosted/advisory_test.dart

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:shelf/shelf.dart';
6+
57
import '../../descriptor.dart' as d;
68
import '../../golden_file.dart';
79
import '../../package_server.dart';
@@ -284,6 +286,75 @@ Future<void> main() async {
284286
await ctx.run(['get']);
285287
});
286288

289+
testWithGolden('no advisory available from pub.dev', (ctx) async {
290+
final server = await servePackages();
291+
server
292+
..serve('foo', '1.0.0')
293+
..serve('no_advisory_pkg', '1.0.0');
294+
295+
await d.dir(appPath, [
296+
d.pubspec({
297+
'name': 'app',
298+
'dependencies': {
299+
'foo': '^1.0.0',
300+
'no_advisory_pkg': '^1.0.0',
301+
},
302+
}),
303+
]).create();
304+
305+
server.addAdvisory(
306+
advisoryId: '123',
307+
displayUrl: 'https://github.com/advisories/123',
308+
affectedPackages: [
309+
AffectedPackage(name: 'no_advisory_pkg', versions: ['1.0.0']),
310+
AffectedPackage(name: 'foo', versions: ['1.0.0']),
311+
],
312+
);
313+
314+
server.handle(
315+
'/api/packages/no_advisory_pkg/advisories',
316+
(request) => Response.notFound(null),
317+
);
318+
319+
await ctx.run(
320+
['get'],
321+
environment: {'_PUB_TEST_DEFAULT_HOSTED_URL': globalServer.url},
322+
);
323+
});
324+
325+
testWithGolden('no advisory available', (ctx) async {
326+
final server = await servePackages();
327+
server
328+
..serve('foo', '1.0.0')
329+
..serve('no_advisory_pkg', '1.0.0');
330+
331+
await d.dir(appPath, [
332+
d.pubspec({
333+
'name': 'app',
334+
'dependencies': {
335+
'foo': '^1.0.0',
336+
'no_advisory_pkg': '^1.0.0',
337+
},
338+
}),
339+
]).create();
340+
341+
server.addAdvisory(
342+
advisoryId: '123',
343+
displayUrl: 'https://github.com/advisories/123',
344+
affectedPackages: [
345+
AffectedPackage(name: 'no_advisory_pkg', versions: ['1.0.0']),
346+
AffectedPackage(name: 'foo', versions: ['1.0.0']),
347+
],
348+
);
349+
350+
server.handle(
351+
'/api/packages/no_advisory_pkg/advisories',
352+
(request) => Response.notFound(null),
353+
);
354+
355+
await ctx.run(['get']);
356+
});
357+
287358
testWithGolden('show id if no display url is present', (ctx) async {
288359
final server = await servePackages();
289360
server
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# GENERATED BY: test/get/hosted/advisory_test.dart
2+
3+
## Section 0
4+
$ pub get
5+
Resolving dependencies...
6+
Downloading packages...
7+
[STDERR] Failed to fetch advisories for "no_advisory_pkg" from "http://localhost:$PORT".
8+
[EXIT CODE] 69
9+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# GENERATED BY: test/get/hosted/advisory_test.dart
2+
3+
## Section 0
4+
$ pub get
5+
Resolving dependencies...
6+
Downloading packages...
7+
+ foo 1.0.0 (affected by advisory: [^0])
8+
+ no_advisory_pkg 1.0.0
9+
Changed 2 dependencies!
10+
Dependencies are affected by security advisories:
11+
[^0]: https://github.com/advisories/123
12+
[STDERR] Warning: Unable to fetch advisories for "no_advisory_pkg" from "http://localhost:$PORT".
13+

0 commit comments

Comments
 (0)