Skip to content

Commit

Permalink
Restore pre-transaction rate limit check for package uploads. (#7578)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Mar 21, 2024
1 parent cf823e1 commit 7344dca
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/lib/package/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import 'package:meta/meta.dart';
import 'package:pool/pool.dart';
import 'package:pub_dev/package/export_api_to_bucket.dart';
import 'package:pub_dev/service/async_queue/async_queue.dart';
import 'package:pub_dev/service/rate_limit/rate_limit.dart';
import 'package:pub_dev/shared/versions.dart';
import 'package:pub_dev/task/backend.dart';
import 'package:pub_package_reader/pub_package_reader.dart';
Expand Down Expand Up @@ -1042,6 +1043,13 @@ class PackageBackend {
'Package "${newVersion.package}" has no admin email to notify.');
}

// check rate limits before the transaction
await verifyPackageUploadRateLimit(
agent: agent,
package: newVersion.package,
isNew: isNew,
);

final email = createPackageUploadedEmail(
packageName: newVersion.package,
packageVersion: newVersion.version!,
Expand Down
31 changes: 31 additions & 0 deletions app/lib/service/rate_limit/rate_limit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,37 @@ import '../../shared/configuration.dart';
import '../../shared/exceptions.dart';
import '../../shared/redis_cache.dart';

/// Verifies if the current package upload has a rate limit and throws
/// if the limit has been exceeded.
Future<void> verifyPackageUploadRateLimit({
required AuthenticatedAgent agent,
required String package,
required bool isNew,
}) async {
final packagePublishedOp = AuditLogRecordKind.packagePublished;

await _verifyRateLimit(
rateLimit: _getRateLimit(packagePublishedOp, RateLimitScope.user),
agentId: agent.agentId,
);

if (isNew) {
await _verifyRateLimit(
rateLimit: _getRateLimit(
AuditLogRecordKind.packageCreated,
RateLimitScope.user,
),
agentId: agent.agentId,
);
}

// regular package-specific limits
await _verifyRateLimit(
rateLimit: _getRateLimit(packagePublishedOp, RateLimitScope.package),
package: package,
);
}

Future<void> verifyAuditLogRecordRateLimits(AuditLogRecord record) async {
final agentId = record.agent;
await _verifyRateLimit(
Expand Down

0 comments on commit 7344dca

Please sign in to comment.