Skip to content
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
2 changes: 2 additions & 0 deletions app/lib/frontend/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:uuid/uuid.dart';

import '../shared/name_tracker.dart';
import '../shared/package_memcache.dart';
import '../shared/urls.dart' as urls;
import '../shared/utils.dart';

import 'model_properties.dart';
Expand Down Expand Up @@ -675,6 +676,7 @@ Future<models.PackageVersion> parseAndValidateUpload(
if (!nameTracker.accept(pubspec.name)) {
throw new Exception('Package name is too similar to another package.');
}
urls.syntaxCheckHomepageUrl(pubspec.homepage);

String exampleFilename;
for (String candidate in exampleFileCandidates(pubspec.name)) {
Expand Down
31 changes: 31 additions & 0 deletions app/lib/shared/urls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,37 @@ String analysisTabUrl(String package) {
: pkgPageUrl(package, fragment: fragment);
}

final _invalidHostNames = const <String>[
'..',
'...',
'example.com',
'example.org',
'example.net',
'www.example.com',
'www.example.org',
'www.example.net',
'none',
];

void syntaxCheckHomepageUrl(String url) {
Uri uri;
try {
uri = Uri.parse(url);
} catch (_) {
throw new Exception('Unable to parse homepage URL: $url');
}
if (!uri.hasScheme || !uri.scheme.startsWith('http')) {
throw new Exception(
'Use http:// or https:// URL schemes for homepage URL: $url');
}
if (uri.host == null ||
uri.host.isEmpty ||
!uri.host.contains('.') ||
_invalidHostNames.contains(uri.host)) {
throw new Exception('Homepage URL has no valid host: $url');
}
}

// TODO: lib/shared/analyzer_client.dart

// TODO: lib/shared/configuration.dart
Expand Down
27 changes: 27 additions & 0 deletions app/test/shared/urls_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,31 @@ void main() {
'https://pub.dartlang.org/documentation/foo_bar/1.0.0/');
});
});

group('homepage syntax check', () {
test('no url is not accepted', () {
expect(() => syntaxCheckHomepageUrl(null), throwsException);
});

test('example urls that are accepted', () {
syntaxCheckHomepageUrl('http://github.com/user/repo/');
syntaxCheckHomepageUrl('https://github.com/user/repo/');
syntaxCheckHomepageUrl('http://some.domain.com');
});

test('urls without valid scheme are not accepted', () {
expect(() => syntaxCheckHomepageUrl('github.com/x/y'), throwsException);
expect(() => syntaxCheckHomepageUrl('ftp://github.com/x/y'),
throwsException);
});

test('urls without valid host are not accepted', () {
expect(() => syntaxCheckHomepageUrl('http://none/x/'), throwsException);
expect(() => syntaxCheckHomepageUrl('http://example.com/x/'),
throwsException);
expect(
() => syntaxCheckHomepageUrl('http://localhost/x/'), throwsException);
expect(() => syntaxCheckHomepageUrl('http://.../x/'), throwsException);
});
});
}