コード例 #1
0
            public void UsesConfiguredSiteRootInAbsoluteUri(
                string returnUrl,
                string protocol,
                string hostName,
                string expectedReturnUrl)
            {
                var absoluteReturnUrl = UrlExtensions.GetAbsoluteReturnUrl(returnUrl, protocol, hostName);

                Assert.Equal(expectedReturnUrl, absoluteReturnUrl);
            }
コード例 #2
0
        public static string Absolute(this UrlHelper url, string path)
        {
            UriBuilder builder = GetCanonicalUrl(url);

            if (path.StartsWith("~/", StringComparison.OrdinalIgnoreCase))
            {
                path = UrlExtensions.MakeSecure(VirtualPathUtility.ToAbsolute(path, url.RequestContext.HttpContext.Request.ApplicationPath));
            }
            builder.Path = path;
            return(UrlExtensions.MakeSecure(builder.Uri.AbsoluteUri));
        }
コード例 #3
0
        internal async Task <ActionResult> CreateDownloadFileActionResult(
            HttpContextBase httpContext,
            string folderName,
            string fileName)
        {
            var container = await GetContainerAsync(folderName);

            var blob = container.GetBlobReference(fileName);

            var redirectUri = UrlExtensions.MakeSecure(GetRedirectUri(httpContext.Request.Url, blob.Uri));

            return(new RedirectResult(redirectUri.AbsoluteUri, false));
        }
コード例 #4
0
            public DependencyViewModel(string id, string versionSpec)
            {
                Id = id;

                if (!String.IsNullOrEmpty(versionSpec))
                {
                    VersionSpec = VersionRange.Parse(versionSpec).PrettyPrint();
                }

                if (HttpContext.Current != null)
                {
                    PackageUrl = UrlExtensions.Package(new UrlHelper(HttpContext.Current.Request.RequestContext), id);
                }
            }
コード例 #5
0
            public void ReturnsCorrectRouteLink(string siteRoot, bool relativeUrl, string expectedUrl)
            {
                // Arrange
                var configurationService = GetConfigurationService();

                configurationService.Current.SiteRoot = siteRoot;

                var urlHelper = TestUtility.MockUrlHelper(siteRoot);

                // Act
                var result = UrlExtensions.Package(urlHelper, "id", "1.0.0", relativeUrl);

                // Assert
                Assert.Equal(expectedUrl, result);
            }
コード例 #6
0
            public void GeneratesTheCorrectActionLink(string siteRoot, string actionName, string controllerName, RouteValueDictionary routeValues, bool relativeUrl, string expectedActionLink)
            {
                // Arrange
                var configurationService = GetConfigurationService();

                configurationService.Current.SiteRoot = siteRoot;

                var urlHelper = TestUtility.MockUrlHelper(siteRoot);

                // Act
                var result = UrlExtensions.GetActionLink(urlHelper, actionName, controllerName, relativeUrl, routeValues);

                // Assert
                Assert.Equal(expectedActionLink, result);
            }
コード例 #7
0
            public void UsesNormalizedVersionInUrls()
            {
                var package = new Package
                {
                    PackageRegistration = new PackageRegistration
                    {
                        Id = "TestPackageId"
                    },
                    NormalizedVersion = "1.0.0-alpha.1",
                    Version           = "1.0.0-alpha.1+metadata"
                };

                string fixedUrl = UrlExtensions.Package(TestUtility.MockUrlHelper(), package);

                Assert.DoesNotContain("metadata", fixedUrl);
                Assert.EndsWith(package.NormalizedVersion, fixedUrl);
            }
コード例 #8
0
        protected override void Load(ContainerBuilder builder)
        {
            var telemetryClient = TelemetryClientWrapper.Instance;

            builder.RegisterInstance(telemetryClient)
            .AsSelf()
            .As <ITelemetryClient>()
            .SingleInstance();

            var diagnosticsService = new DiagnosticsService(telemetryClient);

            builder.RegisterInstance(diagnosticsService)
            .AsSelf()
            .As <IDiagnosticsService>()
            .SingleInstance();

            var configuration = new ConfigurationService(new SecretReaderFactory(diagnosticsService));

            UrlExtensions.SetConfigurationService(configuration);

            builder.RegisterInstance(configuration)
            .AsSelf()
            .As <PoliteCaptcha.IConfigurationSource>();

            builder.RegisterInstance(configuration)
            .AsSelf()
            .As <IGalleryConfigurationService>();

            builder.Register(c => configuration.Current)
            .AsSelf()
            .As <IAppConfiguration>();

            // Force the read of this configuration, so it will be initialized on startup
            builder.Register(c => configuration.Features)
            .AsSelf()
            .As <FeatureConfiguration>();

            builder.Register(c => configuration.PackageDelete)
            .As <IPackageDeleteConfiguration>();

            builder.RegisterType <TelemetryService>().As <ITelemetryService>().SingleInstance();
            builder.RegisterType <CredentialBuilder>().As <ICredentialBuilder>().SingleInstance();
            builder.RegisterType <CredentialValidator>().As <ICredentialValidator>().SingleInstance();

            builder.RegisterInstance(LuceneCommon.GetDirectory(configuration.Current.LuceneIndexLocation))
            .As <Lucene.Net.Store.Directory>()
            .SingleInstance();

            ConfigureSearch(builder, configuration);

            builder.RegisterType <DateTimeProvider>().AsSelf().As <IDateTimeProvider>().SingleInstance();

            builder.RegisterType <HttpContextCacheService>()
            .AsSelf()
            .As <ICacheService>()
            .InstancePerLifetimeScope();

            builder.Register(c => new EntitiesContext(configuration.Current.SqlConnectionString, readOnly: configuration.Current.ReadOnlyMode))
            .AsSelf()
            .As <IEntitiesContext>()
            .As <DbContext>()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <User> >()
            .AsSelf()
            .As <IEntityRepository <User> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <ReservedNamespace> >()
            .AsSelf()
            .As <IEntityRepository <ReservedNamespace> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <CuratedFeed> >()
            .AsSelf()
            .As <IEntityRepository <CuratedFeed> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <CuratedPackage> >()
            .AsSelf()
            .As <IEntityRepository <CuratedPackage> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <PackageRegistration> >()
            .AsSelf()
            .As <IEntityRepository <PackageRegistration> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <Package> >()
            .AsSelf()
            .As <IEntityRepository <Package> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <PackageDependency> >()
            .AsSelf()
            .As <IEntityRepository <PackageDependency> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <PackageDelete> >()
            .AsSelf()
            .As <IEntityRepository <PackageDelete> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <AccountDelete> >()
            .AsSelf()
            .As <IEntityRepository <AccountDelete> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <Credential> >()
            .AsSelf()
            .As <IEntityRepository <Credential> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <PackageOwnerRequest> >()
            .AsSelf()
            .As <IEntityRepository <PackageOwnerRequest> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <CuratedFeedService>()
            .AsSelf()
            .As <ICuratedFeedService>()
            .InstancePerLifetimeScope();

            builder.Register(c => new SupportRequestDbContext(configuration.Current.SqlConnectionStringSupportRequest))
            .AsSelf()
            .As <ISupportRequestDbContext>()
            .InstancePerLifetimeScope();

            builder.RegisterType <SupportRequestService>()
            .AsSelf()
            .As <ISupportRequestService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <UserService>()
            .AsSelf()
            .As <IUserService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <PackageNamingConflictValidator>()
            .AsSelf()
            .As <IPackageNamingConflictValidator>()
            .InstancePerLifetimeScope();

            builder.RegisterType <PackageService>()
            .AsSelf()
            .As <IPackageService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <PackageDeleteService>()
            .AsSelf()
            .As <IPackageDeleteService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <DeleteAccountService>()
            .AsSelf()
            .As <IDeleteAccountService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <PackageOwnerRequestService>()
            .AsSelf()
            .As <IPackageOwnerRequestService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <FormsAuthenticationService>()
            .As <IFormsAuthenticationService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <CookieTempDataProvider>()
            .As <ITempDataProvider>()
            .InstancePerLifetimeScope();

            builder.RegisterType <StatusService>()
            .AsSelf()
            .As <IStatusService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <SecurityPolicyService>()
            .AsSelf()
            .As <ISecurityPolicyService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <ReservedNamespaceService>()
            .AsSelf()
            .As <IReservedNamespaceService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <PackageUploadService>()
            .AsSelf()
            .As <IPackageUploadService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <PackageOwnershipManagementService>()
            .AsSelf()
            .As <IPackageOwnershipManagementService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <ValidationService>()
            .AsSelf()
            .As <IValidationService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <ReadMeService>()
            .AsSelf()
            .As <IReadMeService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <ApiScopeEvaluator>()
            .AsSelf()
            .As <IApiScopeEvaluator>()
            .InstancePerLifetimeScope();

            builder.RegisterType <SecurePushSubscription>()
            .SingleInstance();

            builder.RegisterType <RequireSecurePushForCoOwnersPolicy>()
            .SingleInstance();

            var mailSenderThunk = new Lazy <IMailSender>(
                () =>
            {
                var settings = configuration;
                if (settings.Current.SmtpUri != null && settings.Current.SmtpUri.IsAbsoluteUri)
                {
                    var smtpUri = new SmtpUri(settings.Current.SmtpUri);

                    var mailSenderConfiguration = new MailSenderConfiguration
                    {
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                        Host           = smtpUri.Host,
                        Port           = smtpUri.Port,
                        EnableSsl      = smtpUri.Secure
                    };

                    if (!string.IsNullOrWhiteSpace(smtpUri.UserName))
                    {
                        mailSenderConfiguration.UseDefaultCredentials = false;
                        mailSenderConfiguration.Credentials           = new NetworkCredential(
                            smtpUri.UserName,
                            smtpUri.Password);
                    }

                    return(new MailSender(mailSenderConfiguration));
                }
                else
                {
                    var mailSenderConfiguration = new MailSenderConfiguration
                    {
                        DeliveryMethod          = SmtpDeliveryMethod.SpecifiedPickupDirectory,
                        PickupDirectoryLocation = HostingEnvironment.MapPath("~/App_Data/Mail")
                    };

                    return(new MailSender(mailSenderConfiguration));
                }
            });

            builder.Register(c => mailSenderThunk.Value)
            .AsSelf()
            .As <IMailSender>()
            .InstancePerLifetimeScope();

            builder.RegisterType <MessageService>()
            .AsSelf()
            .As <IMessageService>()
            .InstancePerLifetimeScope();

            builder.Register(c => HttpContext.Current.User)
            .AsSelf()
            .As <IPrincipal>()
            .InstancePerLifetimeScope();

            IAuditingService defaultAuditingService = null;

            switch (configuration.Current.StorageType)
            {
            case StorageType.FileSystem:
            case StorageType.NotSpecified:
                ConfigureForLocalFileSystem(builder, configuration);
                defaultAuditingService = GetAuditingServiceForLocalFileSystem(configuration);
                break;

            case StorageType.AzureStorage:
                ConfigureForAzureStorage(builder, configuration, telemetryClient);
                defaultAuditingService = GetAuditingServiceForAzureStorage(builder, configuration);
                break;
            }

            RegisterAsynchronousValidation(builder, configuration);

            RegisterAuditingServices(builder, defaultAuditingService);

            RegisterCookieComplianceService(builder, configuration, diagnosticsService);

            // todo: bind all package curators by convention
            builder.RegisterType <WebMatrixPackageCurator>()
            .AsSelf()
            .As <IAutomaticPackageCurator>()
            .InstancePerLifetimeScope();

            builder.RegisterType <Windows8PackageCurator>()
            .AsSelf()
            .As <IAutomaticPackageCurator>()
            .InstancePerLifetimeScope();

            // todo: bind all commands by convention
            builder.RegisterType <AutomaticallyCuratePackageCommand>()
            .AsSelf()
            .As <IAutomaticallyCuratePackageCommand>()
            .InstancePerLifetimeScope();

            if (configuration.Current.Environment == Constants.DevelopmentEnvironment)
            {
                builder.RegisterType <AllowLocalHttpRedirectPolicy>()
                .As <ISourceDestinationRedirectPolicy>()
                .InstancePerLifetimeScope();
            }
            else
            {
                builder.RegisterType <NoLessSecureDestinationRedirectPolicy>()
                .As <ISourceDestinationRedirectPolicy>()
                .InstancePerLifetimeScope();
            }

            ConfigureAutocomplete(builder, configuration);
        }
コード例 #9
0
            public void PropagatesNull()
            {
                string fixedUrl = UrlExtensions.EnsureTrailingSlash(null);

                Assert.Null(fixedUrl);
            }
コード例 #10
0
            public void Works()
            {
                string fixedUrl = UrlExtensions.EnsureTrailingSlash("http://nuget.org/packages/FooPackage.CS");

                Assert.True(fixedUrl.EndsWith("/", StringComparison.Ordinal));
            }
コード例 #11
0
 public virtual Task <ActionResult> GetNuGetExe()
 {
     return(NugetExeDownloaderService.CreateNuGetExeDownloadActionResultAsync(UrlExtensions.MakeSecure(HttpContext.Request.Url)));
 }
コード例 #12
0
        public virtual async Task <ActionResult> GetPackage(string id, string version)
        {
            // some security paranoia about URL hacking somehow creating e.g. open redirects
            // validate user input: explicit calls to the same validators used during Package Registrations
            // Ideally shouldn't be necessary?
            if (!PackageIdValidator.IsValidPackageId(id ?? string.Empty))
            {
                return(new HttpStatusCodeWithBodyResult(HttpStatusCode.BadRequest, "The format of the package id is invalid"));
            }

            // if version is non-null, check if it's semantically correct and normalize it.
            if (!String.IsNullOrEmpty(version))
            {
                NuGetVersion dummy;
                if (!NuGetVersion.TryParse(version, out dummy))
                {
                    return(new HttpStatusCodeWithBodyResult(HttpStatusCode.BadRequest, "The package version is not a valid semantic version"));
                }

                // Normalize the version
                version = NuGetVersionFormatter.Normalize(version);
            }
            else
            {
                // If version is null, get the latest version from the database.
                // This ensures that on package restore scenario where version will be non null, we don't hit the database.
                try
                {
                    var package = PackageService.FindPackageByIdAndVersion(
                        id,
                        version,
                        SemVerLevelKey.SemVer2,
                        allowPrerelease: false);

                    if (package == null)
                    {
                        return(new HttpStatusCodeWithBodyResult(HttpStatusCode.NotFound, String.Format(CultureInfo.CurrentCulture, Strings.PackageWithIdAndVersionNotFound, id, version)));
                    }
                    version = package.NormalizedVersion;
                }
                catch (SqlException e)
                {
                    QuietLog.LogHandledException(e);

                    // Database was unavailable and we don't have a version, return a 503
                    return(new HttpStatusCodeWithBodyResult(HttpStatusCode.ServiceUnavailable, Strings.DatabaseUnavailable_TrySpecificVersion));
                }
                catch (DataException e)
                {
                    QuietLog.LogHandledException(e);

                    // Database was unavailable and we don't have a version, return a 503
                    return(new HttpStatusCodeWithBodyResult(HttpStatusCode.ServiceUnavailable, Strings.DatabaseUnavailable_TrySpecificVersion));
                }
            }

            if (ConfigurationService.Features.TrackPackageDownloadCountInLocalDatabase)
            {
                await PackageService.IncrementDownloadCountAsync(id, version);
            }

            return(await PackageFileService.CreateDownloadPackageActionResultAsync(
                       UrlExtensions.MakeSecure(HttpContext.Request.Url),
                       id, version));
        }
コード例 #13
0
 public static string PackageDefaultIcon(this UrlHelper url)
 {
     return(UrlExtensions.MakeSecure(url.Home(relativeUrl: false).TrimEnd('/')
                                     + VirtualPathUtility.ToAbsolute("~/Content/Images/packageDefaultIcon-50x50.png", url.RequestContext.HttpContext.Request.ApplicationPath)));
 }
コード例 #14
0
 public static string Package(this UrlHelper url, string id, bool relativeUrl = true)
 {
     return(UrlExtensions.MakeSecure(url.Package(id, version: null, relativeUrl: relativeUrl)));
 }
コード例 #15
0
            public void Works()
            {
                string fixedUrl = UrlExtensions.EnsureTrailingSlash("http://nuget.org/packages/FooPackage.CS");

                Assert.EndsWith("/", fixedUrl);
            }