Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moderation subject: user:<email> #7699

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions app/lib/admin/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,30 +98,36 @@ class ModerationSubject {
/// The kind of moderation as described by [ModerationSubjectKind], one of:
/// - package,
/// - package-version,
/// - publisher.
/// - publisher,
/// - user.
final String kind;

/// The local name part of the subject, may be a composite, one of:
/// - <package>,
/// - <package>/<version>,
/// - <publisherId>.
/// - <publisherId>,
/// - <email>.
final String localName;

/// The package name of the subject (if not a publisher).
/// The package name of the subject (if not a publisher, or user).
final String? package;

/// The package version of the subject (if the version was specified).
final String? version;

/// The publisher id (if not a package or package/version).
/// The publisher id (if not a package or package/version, or user).
final String? publisherId;

/// The email address of the user (if not a package or publisher).
final String? email;

ModerationSubject._({
required this.kind,
required this.localName,
this.package,
this.version,
this.publisherId,
this.email,
});

factory ModerationSubject.package(String package, [String? version]) {
Expand Down Expand Up @@ -183,6 +189,13 @@ class ModerationSubject {
localName: publisherId,
publisherId: publisherId,
);
case ModerationSubjectKind.user:
final email = parts[1];
return ModerationSubject._(
kind: kind,
localName: email,
email: email,
);
default:
return null;
}
Expand All @@ -195,4 +208,5 @@ class ModerationSubjectKind {
static const package = 'package';
static const packageVersion = 'package-version';
static const publisher = 'publisher';
static const user = 'user';
}
12 changes: 11 additions & 1 deletion app/lib/frontend/handlers/report.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ Future<shelf.Response> reportPageHandler(shelf.Request request) async {
Future<void> _verifySubject(ModerationSubject? subject) async {
final package = subject?.package;
final version = subject?.version;
final publisherId = subject?.publisherId;
if (package != null) {
final p = await packageBackend.lookupPackage(package);
if (p == null) {
Expand All @@ -61,12 +60,23 @@ Future<void> _verifySubject(ModerationSubject? subject) async {
}
}
}

final publisherId = subject?.publisherId;
if (publisherId != null) {
final p = await publisherBackend.getPublisher(publisherId);
if (p == null) {
throw NotFoundException('Publisher "$publisherId" does not exist.');
}
}

final email = subject?.email;
if (email != null) {
InvalidInputException.check(
isValidEmail(email), '"$email" is not a valid email.');

// NOTE: We are not going to lookup and reject the requests based on the
// email address, as it would leak the existence of user accounts.
}
}

/// Handles POST /api/report
Expand Down
13 changes: 13 additions & 0 deletions app/test/admin/models_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ void main() {
expect(ms.package, 'x');
expect(ms.version, isNull);
expect(ms.publisherId, isNull);
expect(ms.email, isNull);
});

test('package version', () {
Expand All @@ -30,6 +31,7 @@ void main() {
expect(ms.package, 'x');
expect(ms.version, '1.0.0');
expect(ms.publisherId, isNull);
expect(ms.email, isNull);
});

test('publisher', () {
Expand All @@ -39,6 +41,17 @@ void main() {
expect(ms.package, isNull);
expect(ms.version, isNull);
expect(ms.publisherId, 'example.com');
expect(ms.email, isNull);
});

test('user', () {
final ms = ModerationSubject.tryParse('user:a@example.com');
expect(ms!.kind, ModerationSubjectKind.user);
expect(ms.localName, 'a@example.com');
expect(ms.package, isNull);
expect(ms.version, isNull);
expect(ms.publisherId, isNull);
expect(ms.email, 'a@example.com');
});
});
}
Loading