Skip to content

Commit

Permalink
Explicit ModeratedPackage check on package upload. (#7355)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Jan 8, 2024
1 parent 720c134 commit 238ea7d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
11 changes: 10 additions & 1 deletion app/lib/package/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1054,9 +1054,18 @@ class PackageBackend {
final pv = await withRetryTransaction(db, (tx) async {
_logger.info('Starting datastore transaction.');

final tuple = (await tx.lookup([newVersion.key, newVersion.packageKey!]));
final tuple = (await tx.lookup([
newVersion.key,
newVersion.packageKey!,
db.emptyKey.append(ModeratedPackage, id: newVersion.package),
]));
final version = tuple[0] as PackageVersion?;
package = tuple[1] as Package?;
final moderatedPackage = tuple[2] as ModeratedPackage?;

if (moderatedPackage != null) {
throw PackageRejectedException.nameReserved(newVersion.package);
}

// If the version already exists, we fail.
if (version != null) {
Expand Down
37 changes: 35 additions & 2 deletions app/test/package/upload_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1103,9 +1103,9 @@ void main() {
});
});

group('other limits', () {
group('other rejections', () {
testWithProfile(
'max versions',
'max version count',
testProfile: TestProfile(
defaultUser: 'admin@pub.dev',
packages: <TestPackage>[
Expand All @@ -1131,5 +1131,38 @@ void main() {
},
timeout: Timeout.factor(1.5),
);

testWithProfile('moderated package immediately re-published', fn: () async {
final pubspecContent = generatePubspecYaml('abcd_package', '1.0.0');
final bytes = await packageArchiveBytes(pubspecContent: pubspecContent);
final message = await createPubApiClient(authToken: adminClientToken)
.uploadPackageBytes(bytes);
expect(message.success.message, contains('Successfully uploaded'));
await nameTracker.reloadFromDatastore();

await accountBackend.withBearerToken(
siteAdminToken, () => adminBackend.removePackage('abcd_package'));

// NOTE: do not refresh name tracker and publish again
final rs1 = createPubApiClient(authToken: adminClientToken)
.uploadPackageBytes(bytes);
await expectApiException(
rs1,
status: 400,
code: 'PackageRejected',
message: 'Package name abcd_package is reserved',
);

// NOTE: refresh name tracker and publish again
await nameTracker.reloadFromDatastore();
final rs2 = createPubApiClient(authToken: adminClientToken)
.uploadPackageBytes(bytes);
await expectApiException(
rs2,
status: 400,
code: 'PackageRejected',
message: 'is too similar to a moderated package',
);
});
});
}

0 comments on commit 238ea7d

Please sign in to comment.