Skip to content

Commit

Permalink
Disregard ignored advisories in pub get/upgrade report (#4064)
Browse files Browse the repository at this point in the history
  • Loading branch information
szakarias authored Nov 30, 2023
1 parent bd7d37b commit 3d4a298
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/src/pubspec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ class Pubspec extends PubspecBase {
collectError(() => executables);
collectError(() => falseSecrets);
collectError(() => sdkConstraints);
collectError(() => ignoredAdvisories);
return errors;
}

Expand Down
34 changes: 34 additions & 0 deletions lib/src/pubspec_parse.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,40 @@ abstract class PubspecBase {
bool _parsedPublishTo = false;
String? _publishTo;

/// The list of advisory IDs to be ignored when reporting security advisories
/// affecting dependencies.
List<String> get ignoredAdvisories {
var advisoryIDs = _ignoredAdvisories;
if (advisoryIDs != null) {
return advisoryIDs;
}
advisoryIDs = <String>[];

Never ignoredAdvisoriesError(SourceSpan span) => _error(
'"ignored_advisories" field must be a list of advisory IDs',
span,
);

final ignoredAdvisoriesNode = fields.nodes['ignored_advisories'];
if (ignoredAdvisoriesNode == null) {
return _ignoredAdvisories = List.unmodifiable(advisoryIDs);
}
if (ignoredAdvisoriesNode is! YamlList) {
ignoredAdvisoriesError(ignoredAdvisoriesNode.span);
}
for (final node in ignoredAdvisoriesNode.nodes) {
final value = node.value;
if (value is! String) {
ignoredAdvisoriesError(node.span);
}
advisoryIDs.add(value);
}

return _ignoredAdvisories = List.unmodifiable(advisoryIDs);
}

List<String>? _ignoredAdvisories;

/// The list of patterns covering _false-positive secrets_ in the package.
///
/// This is a list of git-ignore style patterns for files that should be
Expand Down
5 changes: 4 additions & 1 deletion lib/src/solver/report.dart
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,10 @@ $contentHashesDocumentationUrl

if (advisories != null && advisories.isNotEmpty) {
final advisoryFootnotes = <int>[];
for (final adv in advisories.take(maxAdvisoryFootnotesPerLine)) {
final reportedAdvisories = advisories
.where((adv) => !_rootPubspec.ignoredAdvisories.contains(adv.id))
.take(maxAdvisoryFootnotesPerLine);
for (final adv in reportedAdvisories) {
advisoryFootnotes.add(advisoriesIds.length);
advisoriesIds.add(adv.id);
}
Expand Down
32 changes: 32 additions & 0 deletions test/get/hosted/advisory_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,36 @@ Future<void> main() async {
);
await ctx.run(['get']);
});

testWithGolden('do not show ignored advisories', (ctx) async {
final server = await servePackages();
server
..serve('foo', '1.2.3')
..serve('foo', '2.0.0')
..serve('baz', '1.0.0');

await d.dir(appPath, [
d.pubspec(
{
'name': 'app',
'dependencies': {
'foo': '^1.0.0',
'baz': '^1.0.0',
},
'ignored_advisories': ['123'],
},
),
]).create();
server.affectVersionsByAdvisory(
name: 'foo',
advisoryId: '123',
affectedVersions: ['1.2.3'],
);
server.affectVersionsByAdvisory(
name: 'foo',
advisoryId: '456',
affectedVersions: ['1.2.3'],
);
await ctx.run(['get']);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# GENERATED BY: test/get/hosted/advisory_test.dart

## Section 0
$ pub get
Resolving dependencies...
+ baz 1.0.0
+ foo 1.2.3 (affected by advisory: [^0], 2.0.0 available)
Changed 2 dependencies!
Dependencies are affected by security advisories:
[^0]: https://github.com/advisories/456
1 package has newer versions incompatible with dependency constraints.
Try `dart pub outdated` for more information.

0 comments on commit 3d4a298

Please sign in to comment.