コード例 #1
0
        private static IEnumerable<ValidationResult> ValidateCore(PackageMetadata packageMetadata)
        {
            // Validate the ID
            if (string.IsNullOrEmpty(packageMetadata.Id))
            {
                yield return new ValidationResult(Strings.Manifest_MissingId);
            }
            else
            {
                if (packageMetadata.Id.Length > MaxPackageIdLength)
                {
                    yield return new ValidationResult(Strings.Manifest_IdTooLong);
                }
                else if (!PackageIdValidator.IsValidPackageId(packageMetadata.Id))
                {
                    yield return new ValidationResult(String.Format(
                        CultureInfo.CurrentCulture,
                        Strings.Manifest_InvalidId,
                        packageMetadata.Id));
                }
            }

            // Check URL properties
            foreach (var result in CheckUrls(
                packageMetadata.GetValueFromMetadata("IconUrl"),
                packageMetadata.GetValueFromMetadata("ProjectUrl"), 
                packageMetadata.GetValueFromMetadata("LicenseUrl")))
            {
                yield return result;
            }

            // Check version
            if (packageMetadata.Version == null)
            {
                var version = packageMetadata.GetValueFromMetadata("version");

                yield return new ValidationResult(String.Format(
                    CultureInfo.CurrentCulture,
                    Strings.Manifest_InvalidVersion,
                    version));
            }

            // Check dependency groups
            var dependencyGroups = packageMetadata.GetDependencyGroups();
            if (dependencyGroups != null)
            {
                foreach (var dependency in dependencyGroups.SelectMany(set => set.Packages))
                {
                    if (!PackageIdValidator.IsValidPackageId(dependency.Id))
                    {
                        yield return new ValidationResult(String.Format(
                            CultureInfo.CurrentCulture,
                            Strings.Manifest_InvalidDependency,
                            dependency.Id,
                            dependency.VersionRange));
                    }
                }
            }
        }
コード例 #2
0
 private void ValidatePackageTitle(PackageMetadata packageMetadata)
 {
     if (_packageNamingConflictValidator.TitleConflictsWithExistingRegistrationId(packageMetadata.Id, packageMetadata.Title))
     {
         throw new EntityException(Strings.TitleMatchesExistingRegistration, packageMetadata.Title);
     }
 }
コード例 #3
0
        private static void ValidateNuGetPackageMetadata(PackageMetadata packageMetadata)
        {
            // TODO: Change this to use DataAnnotations
            if (packageMetadata.Id.Length > CoreConstants.MaxPackageIdLength)
            {
                throw new EntityException(Strings.NuGetPackagePropertyTooLong, "Id", CoreConstants.MaxPackageIdLength);
            }
            if (packageMetadata.Version.IsPrerelease)
            {
                var release = packageMetadata.Version.Release;

                if (release.Contains("."))
                {
                    throw new EntityException(Strings.NuGetPackageReleaseVersionWithDot, "Version");
                }

                long temp;
                if (long.TryParse(release, out temp))
                {
                    throw new EntityException(Strings.NuGetPackageReleaseVersionContainsOnlyNumerics, "Version");
                }
            }
            if (packageMetadata.Authors != null && packageMetadata.Authors.Flatten().Length > 4000)
            {
                throw new EntityException(Strings.NuGetPackagePropertyTooLong, "Authors", "4000");
            }
            if (packageMetadata.Copyright != null && packageMetadata.Copyright.Length > 4000)
            {
                throw new EntityException(Strings.NuGetPackagePropertyTooLong, "Copyright", "4000");
            }
            if (packageMetadata.Description != null && packageMetadata.Description.Length > 4000)
            {
                throw new EntityException(Strings.NuGetPackagePropertyTooLong, "Description", "4000");
            }
            if (packageMetadata.IconUrl != null && packageMetadata.IconUrl.AbsoluteUri.Length > 4000)
            {
                throw new EntityException(Strings.NuGetPackagePropertyTooLong, "IconUrl", "4000");
            }
            if (packageMetadata.LicenseUrl != null && packageMetadata.LicenseUrl.AbsoluteUri.Length > 4000)
            {
                throw new EntityException(Strings.NuGetPackagePropertyTooLong, "LicenseUrl", "4000");
            }
            if (packageMetadata.ProjectUrl != null && packageMetadata.ProjectUrl.AbsoluteUri.Length > 4000)
            {
                throw new EntityException(Strings.NuGetPackagePropertyTooLong, "ProjectUrl", "4000");
            }
            if (packageMetadata.Summary != null && packageMetadata.Summary.Length > 4000)
            {
                throw new EntityException(Strings.NuGetPackagePropertyTooLong, "Summary", "4000");
            }
            if (packageMetadata.Tags != null && packageMetadata.Tags.Length > 4000)
            {
                throw new EntityException(Strings.NuGetPackagePropertyTooLong, "Tags", "4000");
            }
            if (packageMetadata.Title != null && packageMetadata.Title.Length > 256)
            {
                throw new EntityException(Strings.NuGetPackagePropertyTooLong, "Title", "256");
            }

            if (packageMetadata.Version != null && packageMetadata.Version.ToString().Length > 64)
            {
                throw new EntityException(Strings.NuGetPackagePropertyTooLong, "Version", "64");
            }

            if (packageMetadata.Language != null && packageMetadata.Language.Length > 20)
            {
                throw new EntityException(Strings.NuGetPackagePropertyTooLong, "Language", "20");
            }

            // Validate dependencies
            if (packageMetadata.GetDependencyGroups() != null)
            {
                var packageDependencies = packageMetadata.GetDependencyGroups().ToList();

                foreach (var dependency in packageDependencies.SelectMany(s => s.Packages))
                {
                    // NuGet.Core compatibility - dependency package id can not be > 128 characters
                    if (dependency.Id != null && dependency.Id.Length > CoreConstants.MaxPackageIdLength)
                    {
                        throw new EntityException(Strings.NuGetPackagePropertyTooLong, "Dependency.Id", CoreConstants.MaxPackageIdLength);
                    }

                    // NuGet.Core compatibility - dependency versionspec can not be > 256 characters
                    if (dependency.VersionRange != null && dependency.VersionRange.ToString().Length > 256)
                    {
                        throw new EntityException(Strings.NuGetPackagePropertyTooLong, "Dependency.VersionSpec", "256");
                    }
                }

                // NuGet.Core compatibility - flattened dependencies should be < Int16.MaxValue
                if (packageDependencies.Flatten().Length > Int16.MaxValue)
                {
                    throw new EntityException(Strings.NuGetPackagePropertyTooLong, "Dependencies", Int16.MaxValue);
                }
            }
        }
コード例 #4
0
        public virtual Package EnrichPackageFromNuGetPackage(
            Package package, 
            PackageArchiveReader packageArchive, 
            PackageMetadata packageMetadata,
            PackageStreamMetadata packageStreamMetadata,
            User user)
        {
            // Version must always be the exact string from the nuspec, which ToString will return to us.
            // However, we do also store a normalized copy for looking up later.
            package.Version = packageMetadata.Version.ToString();
            package.NormalizedVersion = packageMetadata.Version.ToNormalizedString();

            package.Description = packageMetadata.Description;
            package.ReleaseNotes = packageMetadata.ReleaseNotes;
            package.HashAlgorithm = packageStreamMetadata.HashAlgorithm;
            package.Hash = packageStreamMetadata.Hash;
            package.PackageFileSize = packageStreamMetadata.Size;
            package.Language = packageMetadata.Language;
            package.Copyright = packageMetadata.Copyright;
            package.FlattenedAuthors = packageMetadata.Authors.Flatten();
            package.IsPrerelease = packageMetadata.Version.IsPrerelease;
            package.Listed = true;
            package.RequiresLicenseAcceptance = packageMetadata.RequireLicenseAcceptance;
            package.Summary = packageMetadata.Summary;
            package.Tags = PackageHelper.ParseTags(packageMetadata.Tags);
            package.Title = packageMetadata.Title;
            package.User = user;

            package.IconUrl = packageMetadata.IconUrl.ToEncodedUrlStringOrNull();
            package.LicenseUrl = packageMetadata.LicenseUrl.ToEncodedUrlStringOrNull();
            package.ProjectUrl = packageMetadata.ProjectUrl.ToEncodedUrlStringOrNull();
            package.MinClientVersion = packageMetadata.MinClientVersion.ToStringOrNull();

#pragma warning disable 618 // TODO: remove Package.Authors completely once prodution services definitely no longer need it
            foreach (var author in packageMetadata.Authors)
            {
                package.Authors.Add(new PackageAuthor { Name = author });
            }
#pragma warning restore 618

            var supportedFrameworks = GetSupportedFrameworks(packageArchive).Select(fn => fn.ToShortNameOrNull()).ToArray();
            if (!supportedFrameworks.AnySafe(sf => sf == null))
            {
                ValidateSupportedFrameworks(supportedFrameworks);

                foreach (var supportedFramework in supportedFrameworks)
                {
                    package.SupportedFrameworks.Add(new PackageFramework {TargetFramework = supportedFramework});
                }
            }

            package.Dependencies = packageMetadata
                .GetDependencyGroups()
                .AsPackageDependencyEnumerable()
                .ToList();

            package.FlattenedDependencies = package.Dependencies.Flatten();

            return package;
        }
コード例 #5
0
        private Package CreatePackageFromNuGetPackage(PackageRegistration packageRegistration, PackageArchiveReader nugetPackage, PackageMetadata packageMetadata, PackageStreamMetadata packageStreamMetadata, User user)
        {
            var package = packageRegistration.Packages.SingleOrDefault(pv => pv.Version == packageMetadata.Version.ToString());

            if (package != null)
            {
                throw new EntityException(
                    "A package with identifier '{0}' and version '{1}' already exists.", packageRegistration.Id, package.Version);
            }

            package = new Package();
            package.PackageRegistration = packageRegistration;

            package = EnrichPackageFromNuGetPackage(package, nugetPackage, packageMetadata, packageStreamMetadata, user);

            return package;
        }
コード例 #6
0
        private PackageRegistration CreateOrGetPackageRegistration(User currentUser, PackageMetadata packageMetadata)
        {
            var packageRegistration = FindPackageRegistrationById(packageMetadata.Id);

            if (packageRegistration != null && !packageRegistration.Owners.Contains(currentUser))
            {
                throw new EntityException(Strings.PackageIdNotAvailable, packageMetadata.Id);
            }

            if (packageRegistration == null)
            {
                if (_packageNamingConflictValidator.IdConflictsWithExistingPackageTitle(packageMetadata.Id))
                {
                    throw new EntityException(Strings.NewRegistrationIdMatchesExistingPackageTitle, packageMetadata.Id);
                }

                packageRegistration = new PackageRegistration
                {
                    Id = packageMetadata.Id
                };

                packageRegistration.Owners.Add(currentUser);

                _packageRegistrationRepository.InsertOnCommit(packageRegistration);
            }

            return packageRegistration;
        }
コード例 #7
0
ファイル: PackageService.cs プロジェクト: goitsk/NuGetGallery
        private Package CreatePackageFromNuGetPackage(PackageRegistration packageRegistration, PackageArchiveReader nugetPackage, PackageMetadata packageMetadata, PackageStreamMetadata packageStreamMetadata, User user)
        {
            var package = packageRegistration.Packages.SingleOrDefault(pv => pv.Version == packageMetadata.Version.ToString());

            if (package != null)
            {
                throw new EntityException(
                    "A package with identifier '{0}' and version '{1}' already exists.", packageRegistration.Id, package.Version);
            }

            package = new Package
            {
                // Version must always be the exact string from the nuspec, which ToString will return to us.
                // However, we do also store a normalized copy for looking up later.
                Version = packageMetadata.Version.ToString(),
                NormalizedVersion = packageMetadata.Version.ToNormalizedString(),

                Description = packageMetadata.Description,
                ReleaseNotes = packageMetadata.ReleaseNotes,
                HashAlgorithm = packageStreamMetadata.HashAlgorithm,
                Hash = packageStreamMetadata.Hash,
                PackageFileSize = packageStreamMetadata.Size,
                Language = packageMetadata.Language,
                Copyright = packageMetadata.Copyright,
                FlattenedAuthors = packageMetadata.Authors.Flatten(),
                IsPrerelease = packageMetadata.Version.IsPrerelease,
                Listed = true,
                PackageRegistration = packageRegistration,
                RequiresLicenseAcceptance = packageMetadata.RequireLicenseAcceptance,
                Summary = packageMetadata.Summary,
                Tags = PackageHelper.ParseTags(packageMetadata.Tags),
                Title = packageMetadata.Title,
                User = user,
            };

            package.IconUrl = packageMetadata.IconUrl.ToEncodedUrlStringOrNull();
            package.LicenseUrl = packageMetadata.LicenseUrl.ToEncodedUrlStringOrNull();
            package.ProjectUrl = packageMetadata.ProjectUrl.ToEncodedUrlStringOrNull();
            package.MinClientVersion = packageMetadata.MinClientVersion.ToStringOrNull();

#pragma warning disable 618 // TODO: remove Package.Authors completely once prodution services definitely no longer need it
            foreach (var author in packageMetadata.Authors)
            {
                package.Authors.Add(new PackageAuthor { Name = author });
            }
#pragma warning restore 618

            var supportedFrameworks = GetSupportedFrameworks(nugetPackage).Select(fn => fn.ToShortNameOrNull()).ToArray();
            if (!supportedFrameworks.AnySafe(sf => sf == null))
            {
                ValidateSupportedFrameworks(supportedFrameworks);

                foreach (var supportedFramework in supportedFrameworks)
                {
                    package.SupportedFrameworks.Add(new PackageFramework { TargetFramework = supportedFramework });
                }
            }

            package.Dependencies = packageMetadata
                .GetDependencyGroups()
                .AsPackageDependencyEnumerable()
                .ToList();
            package.FlattenedDependencies = package.Dependencies.Flatten();

            return package;
        }
コード例 #8
0
        private static IEnumerable <ValidationResult> ValidateCore(PackageMetadata packageMetadata)
        {
            // Validate the ID
            if (string.IsNullOrEmpty(packageMetadata.Id))
            {
                yield return(new ValidationResult(Strings.Manifest_MissingId));
            }
            else
            {
                if (packageMetadata.Id.Length > MaxPackageIdLength)
                {
                    yield return(new ValidationResult(Strings.Manifest_IdTooLong));
                }
                else if (!PackageIdValidator.IsValidPackageId(packageMetadata.Id))
                {
                    yield return(new ValidationResult(String.Format(
                                                          CultureInfo.CurrentCulture,
                                                          Strings.Manifest_InvalidId,
                                                          packageMetadata.Id)));
                }
            }

            // Check URL properties
            foreach (var result in CheckUrls(
                         packageMetadata.GetValueFromMetadata("IconUrl"),
                         packageMetadata.GetValueFromMetadata("ProjectUrl"),
                         packageMetadata.GetValueFromMetadata("LicenseUrl")))
            {
                yield return(result);
            }

            // Check version
            if (packageMetadata.Version == null)
            {
                var version = packageMetadata.GetValueFromMetadata("version");

                yield return(new ValidationResult(String.Format(
                                                      CultureInfo.CurrentCulture,
                                                      Strings.Manifest_InvalidVersion,
                                                      version)));
            }
            if (packageMetadata.Version.IsSemVer200())
            {
                yield return(new ValidationResult(String.Format(
                                                      CultureInfo.CurrentCulture,
                                                      Strings.Manifest_InvalidVersionSemVer200,
                                                      packageMetadata.Version.ToFullString())));
            }

            // Check framework reference groups
            var frameworkReferenceGroups = packageMetadata.GetFrameworkReferenceGroups();

            if (frameworkReferenceGroups != null)
            {
                foreach (var frameworkReferenceGroup in frameworkReferenceGroups)
                {
                    var isUnsupportedFramework = frameworkReferenceGroup?.TargetFramework?.IsUnsupported;
                    if (isUnsupportedFramework.HasValue && isUnsupportedFramework.Value)
                    {
                        yield return(new ValidationResult(String.Format(
                                                              CultureInfo.CurrentCulture,
                                                              Strings.Manifest_TargetFrameworkNotSupported,
                                                              frameworkReferenceGroup?.TargetFramework?.ToString())));
                    }
                }
            }

            // Check dependency groups
            var dependencyGroups = packageMetadata.GetDependencyGroups();

            if (dependencyGroups != null)
            {
                foreach (var dependencyGroup in dependencyGroups)
                {
                    // Keep track of duplicates
                    var dependencyIds = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

                    // Verify frameworks
                    var isUnsupportedFramework = dependencyGroup.TargetFramework?.IsUnsupported;
                    if (isUnsupportedFramework.HasValue && isUnsupportedFramework.Value)
                    {
                        yield return(new ValidationResult(String.Format(
                                                              CultureInfo.CurrentCulture,
                                                              Strings.Manifest_TargetFrameworkNotSupported,
                                                              dependencyGroup.TargetFramework?.ToString())));
                    }

                    // Verify package id's
                    foreach (var dependency in dependencyGroup.Packages)
                    {
                        bool duplicate = !dependencyIds.Add(dependency.Id);
                        if (duplicate)
                        {
                            yield return(new ValidationResult(String.Format(
                                                                  CultureInfo.CurrentCulture,
                                                                  Strings.Manifest_DuplicateDependency,
                                                                  dependencyGroup.TargetFramework.GetShortFolderName(),
                                                                  dependency.Id)));
                        }

                        if (!PackageIdValidator.IsValidPackageId(dependency.Id))
                        {
                            yield return(new ValidationResult(String.Format(
                                                                  CultureInfo.CurrentCulture,
                                                                  Strings.Manifest_InvalidDependency,
                                                                  dependency.Id,
                                                                  dependency.VersionRange.OriginalString)));
                        }
                    }
                }
            }
        }
コード例 #9
0
        public static IEnumerable <ValidationResult> Validate(Stream nuspecStream, out NuspecReader nuspecReader, out PackageMetadata packageMetadata)
        {
            packageMetadata = null;

            try
            {
                nuspecReader = new NuspecReader(nuspecStream);
                var rawMetadata = nuspecReader.GetMetadata();
                if (rawMetadata != null && rawMetadata.Any())
                {
                    packageMetadata = PackageMetadata.FromNuspecReader(nuspecReader, strict: true);
                    return(ValidateCore(packageMetadata));
                }
            }
            catch (Exception ex)
            {
                nuspecReader    = null;
                packageMetadata = null;
                return(new[] { new ValidationResult(ex.Message) });
            }

            return(Enumerable.Empty <ValidationResult>());
        }
コード例 #10
0
        private static IEnumerable<ValidationResult> ValidateCore(PackageMetadata packageMetadata)
        {
            // Validate the ID
            if (string.IsNullOrEmpty(packageMetadata.Id))
            {
                yield return new ValidationResult(Strings.Manifest_MissingId);
            }
            else
            {
                if (packageMetadata.Id.Length > MaxPackageIdLength)
                {
                    yield return new ValidationResult(Strings.Manifest_IdTooLong);
                }
                else if (!PackageIdValidator.IsValidPackageId(packageMetadata.Id))
                {
                    yield return new ValidationResult(String.Format(
                        CultureInfo.CurrentCulture,
                        Strings.Manifest_InvalidId,
                        packageMetadata.Id));
                }
            }

            // Check and validate URL properties
            foreach (var result in CheckUrls(
                packageMetadata.GetValueFromMetadata("IconUrl"),
                packageMetadata.GetValueFromMetadata("ProjectUrl"),
                packageMetadata.GetValueFromMetadata("LicenseUrl")))
            {
                yield return result;
            }

            // Check version
            if (packageMetadata.Version == null)
            {
                var version = packageMetadata.GetValueFromMetadata("version");

                yield return new ValidationResult(String.Format(
                    CultureInfo.CurrentCulture,
                    Strings.Manifest_InvalidVersion,
                    version));
            }
            if (packageMetadata.Version.IsSemVer200())
            {

                yield return new ValidationResult(String.Format(
                    CultureInfo.CurrentCulture,
                    Strings.Manifest_InvalidVersionSemVer200,
                    packageMetadata.Version.ToFullString()));
            }

            // Check framework reference groups
            var frameworkReferenceGroups = packageMetadata.GetFrameworkReferenceGroups();
            if (frameworkReferenceGroups != null)
            {
                foreach (var frameworkReferenceGroup in frameworkReferenceGroups)
                {
                    var isUnsupportedFramework = frameworkReferenceGroup?.TargetFramework?.IsUnsupported;
                    if (isUnsupportedFramework.HasValue && isUnsupportedFramework.Value)
                    {
                        yield return new ValidationResult(String.Format(
                            CultureInfo.CurrentCulture,
                            Strings.Manifest_TargetFrameworkNotSupported,
                            frameworkReferenceGroup?.TargetFramework?.ToString()));
                    }
                }
            }

            // Check dependency groups
            var dependencyGroups = packageMetadata.GetDependencyGroups();
            if (dependencyGroups != null)
            {
                foreach (var dependencyGroup in dependencyGroups)
                {
                    // Keep track of duplicates
                    var dependencyIds = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

                    // Verify frameworks
                    var isUnsupportedFramework = dependencyGroup.TargetFramework?.IsUnsupported;
                    if (isUnsupportedFramework.HasValue && isUnsupportedFramework.Value)
                    {
                        yield return new ValidationResult(String.Format(
                            CultureInfo.CurrentCulture,
                            Strings.Manifest_TargetFrameworkNotSupported,
                            dependencyGroup.TargetFramework?.ToString()));
                    }

                    // Verify package id's
                    foreach (var dependency in dependencyGroup.Packages)
                    {
                        bool duplicate = !dependencyIds.Add(dependency.Id);
                        if (duplicate)
                        {
                            yield return new ValidationResult(String.Format(
                                CultureInfo.CurrentCulture,
                                Strings.Manifest_DuplicateDependency,
                                dependencyGroup.TargetFramework.GetShortFolderName(),
                                dependency.Id));
                        }

                        if (!PackageIdValidator.IsValidPackageId(dependency.Id))
                        {
                            yield return new ValidationResult(String.Format(
                                CultureInfo.CurrentCulture,
                                Strings.Manifest_InvalidDependency,
                                dependency.Id,
                                dependency.VersionRange.OriginalString));
                        }
                    }
                }
            }
        }
コード例 #11
0
        private PackageRegistration CreateOrGetPackageRegistration(User currentUser, PackageMetadata packageMetadata)
        {
            var packageRegistration = FindPackageRegistrationById(packageMetadata.Id);

            if (packageRegistration != null && !packageRegistration.Owners.Contains(currentUser))
            {
                throw new EntityException(Strings.PackageIdNotAvailable, packageMetadata.Id);
            }

            if (packageRegistration == null)
            {
                packageRegistration = new PackageRegistration
                {
                    Id = packageMetadata.Id
                };

                packageRegistration.Owners.Add(currentUser);

                _packageRegistrationRepository.InsertOnCommit(packageRegistration);
            }

            return packageRegistration;
        }