Skip to content

Commit

Permalink
Sanity check pana report size in pub_worker. (#6917)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Aug 15, 2023
1 parent 4383aed commit f2315db
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions pkg/pub_worker/lib/src/bin/pana_wrapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:convert' show json;
import 'dart:io' show Directory, File, Platform, exit;
import 'dart:convert' show json, utf8;
import 'dart:io' show Directory, File, Platform, exit, gzip;

import 'package:logging/logging.dart' show Logger, Level, LogRecord;
import 'package:pana/pana.dart';
Expand All @@ -15,6 +15,10 @@ import 'package:pub_worker/src/sdks.dart';

final _log = Logger('pana');

/// The maximum of the compressed pana report. Larger reports will be dropped and
/// replaced with a placeholder report.
final _reportSizeDropThreshold = 32 * 1024;

/// Program to be used as subprocess for running pana, ensuring that we capture
/// all the output, and only run analysis in a subprocess that can timeout and
/// be killed.
Expand Down Expand Up @@ -102,7 +106,7 @@ Future<void> main(List<String> args) async {
final pana = PackageAnalyzer(toolEnv);
// TODO: add a cache purge + retry if the download would fail
// (e.g. the package version cache wasn't invalidated).
final summary = await pana.inspectPackage(
var summary = await pana.inspectPackage(
package,
version: version,
options: InspectOptions(
Expand Down Expand Up @@ -138,6 +142,30 @@ Future<void> main(List<String> args) async {
final updatedReport = summary.report?.joinSection(docSection);
final updatedSummary = summary.change(report: updatedReport);

// sanity check on pana report size
final reportSize =
gzip.encode(utf8.encode(json.encode(summary.toJson()))).length;
if (reportSize > _reportSizeDropThreshold) {
summary = Summary(
createdAt: summary.createdAt,
runtimeInfo: summary.runtimeInfo,
tags: ['has:pana-report-exceeds-size-threshold'],
report: Report(
sections: [
ReportSection(
id: 'error',
title: 'Report exceeded size limit.',
grantedPoints: summary.report?.grantedPoints ?? 0,
maxPoints: summary.report?.maxPoints ?? 0,
status: ReportStatus.partial,
summary: 'The `pana` report exceeded size limit. '
'Please review pana logs or contact the site admins.',
)
],
),
);
}

_log.info('Writing summary.json');
await File(
p.join(outputFolder, 'summary.json'),
Expand Down

0 comments on commit f2315db

Please sign in to comment.