Skip to content

Commit 242db2d

Browse files
committed
Fixed-time verification of aborted tokens
1 parent 384f4ea commit 242db2d

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

app/lib/task/backend.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,18 +1190,18 @@ PackageVersionStateInfo _authorizeWorkerCallback(
11901190
PackageState state,
11911191
String token,
11921192
) {
1193+
// fixed-time verification of aborted tokens
1194+
final isKnownAbortedToken = state.abortedTokens
1195+
?.map((t) => t.isAuthorized(token))
1196+
.fold<bool>(false, (a, b) => a || b);
1197+
if (isKnownAbortedToken ?? false) {
1198+
throw TaskAbortedException('$package/$version has been aborted.');
1199+
}
1200+
11931201
final versionState = state.versions![version];
11941202
if (versionState == null) {
1195-
// check if the task was aborted
1196-
final abortedToken =
1197-
state.abortedTokens?.firstWhereOrNull((t) => t.token == token);
1198-
if (abortedToken != null && abortedToken.expires.isBefore(clock.now())) {
1199-
throw TaskAbortedException('$package/$version has been aborted.');
1200-
}
1201-
// otherwise throw a generic not found error
12021203
throw NotFoundException.resource('$package/$version');
12031204
}
1204-
12051205
// Check the secret token
12061206
if (!versionState.isAuthorized(token)) {
12071207
throw AuthenticationException.authenticationRequired();

app/lib/task/models.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'dart:convert' show json;
77
import 'package:clock/clock.dart';
88
import 'package:json_annotation/json_annotation.dart';
99
import 'package:pub_dev/admin/actions/actions.dart';
10+
import 'package:pub_dev/shared/utils.dart';
1011

1112
import '../shared/datastore.dart' as db;
1213
import '../shared/versions.dart' as shared_versions;
@@ -433,6 +434,10 @@ class AbortedTokenInfo {
433434
Map<String, dynamic> toJson() => _$AbortedTokenInfoToJson(this);
434435

435436
bool get isNotExpired => clock.now().isBefore(expires);
437+
438+
bool isAuthorized(String token) {
439+
return fixedTimeEquals(this.token, token) && isNotExpired;
440+
}
436441
}
437442

438443
/// A [db.Property] encoding a List os [AbortedTokenInfo] as JSON.

0 commit comments

Comments
 (0)