diff --git a/Lombiq.Hosting.Tenants.EnvironmentRobots.Tests.UI/Extensions/ConfigurationExtensions.cs b/Lombiq.Hosting.Tenants.EnvironmentRobots.Tests.UI/Extensions/ConfigurationExtensions.cs new file mode 100644 index 00000000..e3f0091a --- /dev/null +++ b/Lombiq.Hosting.Tenants.EnvironmentRobots.Tests.UI/Extensions/ConfigurationExtensions.cs @@ -0,0 +1,21 @@ +using Lombiq.Tests.UI.Services; +using System.Threading.Tasks; + +namespace Lombiq.Hosting.Tenants.EnvironmentRobots.Tests.UI.Extensions; + +public static class ConfigurationExtensions +{ + public static void SetEnvironmentRobotsOptionsConfiguration( + this OrchardCoreUITestExecutorConfiguration configuration, + bool isProduction) => + configuration.OrchardCoreConfiguration.BeforeAppStart += + (_, argumentsBuilder) => + { + argumentsBuilder + .AddWithValue( + "OrchardCore:Lombiq_Hosting_Tenants_EnvironmentRobots:EnvironmentRobotsOptions:IsProduction", + value: isProduction); + + return Task.CompletedTask; + }; +} diff --git a/Lombiq.Hosting.Tenants.EnvironmentRobots.Tests.UI/Extensions/TestCaseUITestContextExtensions.cs b/Lombiq.Hosting.Tenants.EnvironmentRobots.Tests.UI/Extensions/TestCaseUITestContextExtensions.cs new file mode 100644 index 00000000..cc913f50 --- /dev/null +++ b/Lombiq.Hosting.Tenants.EnvironmentRobots.Tests.UI/Extensions/TestCaseUITestContextExtensions.cs @@ -0,0 +1,41 @@ +using Atata; +using Lombiq.Tests.UI.Extensions; +using Lombiq.Tests.UI.Services; +using OpenQA.Selenium; +using Shouldly; +using System.Threading.Tasks; + +namespace Lombiq.Hosting.Tenants.EnvironmentRobots.Tests.UI.Extensions; + +public static class TestCaseUITestContextExtensions +{ + public static async Task TestRobotMetaTagIsMissingAsync(this UITestContext context, bool shouldBeMissing) + { + var metaTagXPath = By.XPath($"//meta[@name='robots' and @content='noindex, nofollow']").OfAnyVisibility(); + + await context.SignInDirectlyAsync(); + await context.GoToHomePageAsync(); + + // The easiest way to check the response header during UI testing is with JavaScript by sending a GET request. + var isHeaderPresent = context.Driver.ExecuteAsyncScript(@" + const callback = arguments[arguments.length - 1]; + const xhr = new XMLHttpRequest(); + xhr.open('GET', window.location.href); + xhr.send(); + xhr.onload = function() { + const xRobotsTag = xhr.getResponseHeader('X-Robots-Tag') ?? ''; + callback(xRobotsTag.includes('noindex, nofollow')); + };"); + + if (shouldBeMissing) + { + context.Missing(metaTagXPath); + isHeaderPresent.ShouldBe(expected: false); + } + else + { + context.Exists(metaTagXPath); + isHeaderPresent.ShouldBe(expected: true); + } + } +} diff --git a/Lombiq.Hosting.Tenants.EnvironmentRobots.Tests.UI/License.md b/Lombiq.Hosting.Tenants.EnvironmentRobots.Tests.UI/License.md new file mode 100644 index 00000000..d57e1305 --- /dev/null +++ b/Lombiq.Hosting.Tenants.EnvironmentRobots.Tests.UI/License.md @@ -0,0 +1,13 @@ +Copyright © 2021, [Lombiq Technologies Ltd.](https://lombiq.com) + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +- Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Lombiq.Hosting.Tenants.EnvironmentRobots.Tests.UI/Lombiq.Hosting.Tenants.EnvironmentRobots.Tests.UI.csproj b/Lombiq.Hosting.Tenants.EnvironmentRobots.Tests.UI/Lombiq.Hosting.Tenants.EnvironmentRobots.Tests.UI.csproj new file mode 100644 index 00000000..9214240f --- /dev/null +++ b/Lombiq.Hosting.Tenants.EnvironmentRobots.Tests.UI/Lombiq.Hosting.Tenants.EnvironmentRobots.Tests.UI.csproj @@ -0,0 +1,33 @@ + + + + net6.0 + + + + Lombiq Hosting - Tenants EnvironmentRobots for Orchard Core - UI Test Extensions + Lombiq Technologies + Copyright © 2023, Lombiq Technologies Ltd. + Extension methods that test various features in Lombiq Hosting - Tenants EnvironmentRobots, with the help of Lombiq UI Testing Toolbox for Orchard Core. See the project website for detailed documentation. + NuGetIcon.png + OrchardCore;Lombiq;AspNetCore;Multitenancy;SaaS + https://github.com/Lombiq/Hosting-Tenants + https://github.com/Lombiq/Hosting-Tenants/blob/dev/Lombiq.Hosting.Tenants.EnvironmentRobots.Tests.UI/Readme.md + License.md + + + + + + + + + + + + + + + + + diff --git a/Lombiq.Hosting.Tenants.EnvironmentRobots.Tests.UI/NuGetIcon.png b/Lombiq.Hosting.Tenants.EnvironmentRobots.Tests.UI/NuGetIcon.png new file mode 100644 index 00000000..162a0050 Binary files /dev/null and b/Lombiq.Hosting.Tenants.EnvironmentRobots.Tests.UI/NuGetIcon.png differ diff --git a/Lombiq.Hosting.Tenants.EnvironmentRobots.Tests.UI/Readme.md b/Lombiq.Hosting.Tenants.EnvironmentRobots.Tests.UI/Readme.md new file mode 100644 index 00000000..3f0f39da --- /dev/null +++ b/Lombiq.Hosting.Tenants.EnvironmentRobots.Tests.UI/Readme.md @@ -0,0 +1,7 @@ +# Lombiq Hosting - Tenants EnvironmentRobots for Orchard Core - UI Test Extensions + +## About + +Extension methods that test features in Lombiq Hosting - Tenants EnvironmentRobots, with the help of [Lombiq UI Testing Toolbox for Orchard Core](https://github.com/Lombiq/UI-Testing-Toolbox). + +Call these from a UI test project to verify the module's basic features; as seen in [Open-Source Orchard Core Extensions](https://github.com/Lombiq/Open-Source-Orchard-Core-Extensions). diff --git a/Lombiq.Hosting.Tenants.EnvironmentRobots/Constants/FeatureNames.cs b/Lombiq.Hosting.Tenants.EnvironmentRobots/Constants/FeatureNames.cs new file mode 100644 index 00000000..eb045d32 --- /dev/null +++ b/Lombiq.Hosting.Tenants.EnvironmentRobots/Constants/FeatureNames.cs @@ -0,0 +1,10 @@ +namespace Lombiq.Hosting.Tenants.EnvironmentRobots.Constants; + +public static class FeatureNames +{ + public const string Module = "Lombiq.Hosting"; + + public const string Tenants = Module + "." + nameof(Tenants); + + public const string EnvironmentRobots = Tenants + "." + nameof(EnvironmentRobots); +} diff --git a/Lombiq.Hosting.Tenants.EnvironmentRobots/Extensions/IHostEnvironmentEnvironmentRobotsExtensions.cs b/Lombiq.Hosting.Tenants.EnvironmentRobots/Extensions/IHostEnvironmentEnvironmentRobotsExtensions.cs new file mode 100644 index 00000000..2ccba517 --- /dev/null +++ b/Lombiq.Hosting.Tenants.EnvironmentRobots/Extensions/IHostEnvironmentEnvironmentRobotsExtensions.cs @@ -0,0 +1,12 @@ +using Lombiq.Hosting.Tenants.EnvironmentRobots.Models; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Options; + +namespace Lombiq.Hosting.Tenants.EnvironmentRobots.Extensions; +public static class IHostEnvironmentEnvironmentRobotsExtensions +{ + public static bool IsProductionWithConfiguration( + this IHostEnvironment hostEnvironment, + IOptions options) => + options.Value.IsProduction ?? hostEnvironment.IsProduction(); +} diff --git a/Lombiq.Hosting.Tenants.EnvironmentRobots/Filters/EnvironmentRobotsMetaTagFilter.cs b/Lombiq.Hosting.Tenants.EnvironmentRobots/Filters/EnvironmentRobotsMetaTagFilter.cs new file mode 100644 index 00000000..ee924d8a --- /dev/null +++ b/Lombiq.Hosting.Tenants.EnvironmentRobots/Filters/EnvironmentRobotsMetaTagFilter.cs @@ -0,0 +1,42 @@ +using Lombiq.Hosting.Tenants.EnvironmentRobots.Extensions; +using Lombiq.Hosting.Tenants.EnvironmentRobots.Models; +using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Options; +using OrchardCore.ResourceManagement; + +namespace Lombiq.Hosting.Tenants.EnvironmentRobots.Filters; + +public class EnvironmentRobotsMetaTagFilter : IResultFilter +{ + private readonly IHostEnvironment _hostEnvironment; + private readonly IOptions _options; + private readonly IResourceManager _resourceManager; + + public EnvironmentRobotsMetaTagFilter( + IHostEnvironment hostEnvironment, + IOptions options, + IResourceManager resourceManager) + { + _hostEnvironment = hostEnvironment; + _options = options; + _resourceManager = resourceManager; + } + + public void OnResultExecuting(ResultExecutingContext context) + { + if (!_hostEnvironment.IsProductionWithConfiguration(_options)) + { + _resourceManager.RegisterMeta(new MetaEntry + { + Name = "robots", + Content = "noindex, nofollow", + }); + } + } + + public void OnResultExecuted(ResultExecutedContext context) + { + // Intentionally empty. Required by interface implementation only. + } +} diff --git a/Lombiq.Hosting.Tenants.EnvironmentRobots/License.md b/Lombiq.Hosting.Tenants.EnvironmentRobots/License.md new file mode 100644 index 00000000..d3963d62 --- /dev/null +++ b/Lombiq.Hosting.Tenants.EnvironmentRobots/License.md @@ -0,0 +1,15 @@ +Copyright © 2022, [Lombiq Technologies Ltd.](https://lombiq.com) + +All rights reserved. + +For more information and requests about licensing please [contact us through our website](https://lombiq.com/contact-us). + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +- Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Lombiq.Hosting.Tenants.EnvironmentRobots/Lombiq.Hosting.Tenants.EnvironmentRobots.csproj b/Lombiq.Hosting.Tenants.EnvironmentRobots/Lombiq.Hosting.Tenants.EnvironmentRobots.csproj new file mode 100644 index 00000000..b4bfd8ab --- /dev/null +++ b/Lombiq.Hosting.Tenants.EnvironmentRobots/Lombiq.Hosting.Tenants.EnvironmentRobots.csproj @@ -0,0 +1,41 @@ + + + + net6.0 + true + $(DefaultItemExcludes);.git*;node_modules\** + + + + Lombiq Hosting - Tenants EnvironmentRobots for Orchard Core + Lombiq Technologies + Copyright © 2023, Lombiq Technologies Ltd. + EnvironmentRobots for Orchard Core: This module contains the EnvironmentRobots feature, which prevents search bots from indexing non-production environments by adding a meta tag and a response header with noindex, nofollow values. + NuGetIcon.png + OrchardCore;Lombiq;AspNetCore;Multitenancy;SaaS + https://github.com/Lombiq/Hosting-Tenants + https://github.com/Lombiq/Hosting-Tenants/blob/dev/Lombiq.Hosting.Tenants.EnvironmentRobots/Readme.md + License.md + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Lombiq.Hosting.Tenants.EnvironmentRobots/Manifest.cs b/Lombiq.Hosting.Tenants.EnvironmentRobots/Manifest.cs new file mode 100644 index 00000000..693a6db9 --- /dev/null +++ b/Lombiq.Hosting.Tenants.EnvironmentRobots/Manifest.cs @@ -0,0 +1,19 @@ +using Lombiq.Hosting.Tenants.EnvironmentRobots.Constants; +using OrchardCore.Modules.Manifest; + +[assembly: Module( + Name = "Lombiq Hosting - Tenants - Environment Robots", + Author = "Lombiq Technologies", + Website = "https://github.com/Lombiq/Hosting-Tenants", + Version = "0.0.1", + Description = "Prevents search bots from indexing non-production environments by adding a meta tag and a response" + + " header with noindex, nofollow values.", + Category = "Hosting" +)] + +[assembly: Feature( + Id = FeatureNames.EnvironmentRobots, + Name = "Lombiq Hosting - Tenants - Environment Robots", + Category = "Hosting", + IsAlwaysEnabled = true +)] diff --git a/Lombiq.Hosting.Tenants.EnvironmentRobots/Middlewares/EnvironmentRobotsMiddleware.cs b/Lombiq.Hosting.Tenants.EnvironmentRobots/Middlewares/EnvironmentRobotsMiddleware.cs new file mode 100644 index 00000000..f47ce198 --- /dev/null +++ b/Lombiq.Hosting.Tenants.EnvironmentRobots/Middlewares/EnvironmentRobotsMiddleware.cs @@ -0,0 +1,54 @@ +using Lombiq.Hosting.Tenants.EnvironmentRobots.Extensions; +using Lombiq.Hosting.Tenants.EnvironmentRobots.Models; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Options; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Lombiq.Hosting.Tenants.EnvironmentRobots.Middlewares; + +public class EnvironmentRobotsMiddleware +{ + private readonly RequestDelegate _next; + private readonly IHostEnvironment _hostEnvironment; + private readonly IOptions _options; + + public EnvironmentRobotsMiddleware( + RequestDelegate next, + IHostEnvironment hostEnvironment, + IOptions options) + { + _next = next; + _hostEnvironment = hostEnvironment; + _options = options; + } + + public Task InvokeAsync(HttpContext context) + { + if (!_hostEnvironment.IsProductionWithConfiguration(_options)) + { + var headerValue = context.Response.Headers["X-Robots-Tag"].FirstOrDefault() ?? string.Empty; + + var directives = new List { headerValue }; + + if (!headerValue.Contains("noindex")) + { + directives.Add("noindex"); + } + + if (!headerValue.Contains("nofollow")) + { + directives.Add("nofollow"); + } + + if (directives.Count > 1) + { + context.Response.Headers["X-Robots-Tag"] = $"{string.Join(", ", directives)}"; + } + } + + return _next(context); + } +} diff --git a/Lombiq.Hosting.Tenants.EnvironmentRobots/Models/EnvironmentRobotsOptions.cs b/Lombiq.Hosting.Tenants.EnvironmentRobots/Models/EnvironmentRobotsOptions.cs new file mode 100644 index 00000000..0195a6e7 --- /dev/null +++ b/Lombiq.Hosting.Tenants.EnvironmentRobots/Models/EnvironmentRobotsOptions.cs @@ -0,0 +1,17 @@ +using Microsoft.Extensions.Hosting; + +namespace Lombiq.Hosting.Tenants.EnvironmentRobots.Models; + +/// +/// Further configuration options for the module. +/// +public class EnvironmentRobotsOptions +{ + /// + /// Gets or sets a value indicating whether to add a meta tag and response header with noindex, nofollow values when + /// the app is not running in a production environment. It adds the tags when is . When set it overrides 's result, + /// which the module is using by default. + /// + public bool? IsProduction { get; set; } +} diff --git a/Lombiq.Hosting.Tenants.EnvironmentRobots/NuGetIcon.png b/Lombiq.Hosting.Tenants.EnvironmentRobots/NuGetIcon.png new file mode 100644 index 00000000..162a0050 Binary files /dev/null and b/Lombiq.Hosting.Tenants.EnvironmentRobots/NuGetIcon.png differ diff --git a/Lombiq.Hosting.Tenants.EnvironmentRobots/Readme.md b/Lombiq.Hosting.Tenants.EnvironmentRobots/Readme.md new file mode 100644 index 00000000..3a1e254b --- /dev/null +++ b/Lombiq.Hosting.Tenants.EnvironmentRobots/Readme.md @@ -0,0 +1,32 @@ +# Lombiq Hosting - Tenants Environment Robots for Orchard Core + +[![Lombiq.Hosting.Tenants.EnvironmentRobots NuGet](https://img.shields.io/nuget/v/Lombiq.Hosting.Tenants.EnvironmentRobots?label=Lombiq.Hosting.Tenants.EnvironmentRobots)](https://www.nuget.org/packages/Lombiq.Hosting.Tenants.EnvironmentRobots/) + +## About + +A module that prevents search bots from indexing non-production environments. + +## Documentation + +This module contains the feature below. + +### `Lombiq.Hosting.Tenants.EnvironmentRobots` + +The module works by adding a `X-Robots-Tag` header with the value `noindex`, `nofollow` to the HTTP response of non-production apps. This instructs search engines not to index or follow the links on these pages. Additionally, the module also adds a `` tag to the HTML head of non-production apps for the same purpose. + +The module determines whether a app is non-production based on the `IsProduction` option in the `EnvironmentRobotsOptions` class. By default, this option is set to `null`, which means the module will use the `IHostEnvironment.IsProduction()` method to check the environment name. However, you can override this option by setting it to `true` or `false` in the `appsettings.json` file under the `Lombiq_Hosting_Tenants_EnvironmentRobots:EnvironmentRobotsOptions:IsProduction` section as follows: + +```json +"OrchardCore": { + "Lombiq_Hosting_Tenants_EnvironmentRobots": { + "EnvironmentRobotsOptions": { + "IsProduction": true + } + } +} +``` + +To use the module, follow these steps: + +- Enable the `Lombiq.Hosting.Tenants.EnvironmentRobots` feature from the admin dashboard. +- Optionally, configure the `IsProduction` option as described above. diff --git a/Lombiq.Hosting.Tenants.EnvironmentRobots/Startup.cs b/Lombiq.Hosting.Tenants.EnvironmentRobots/Startup.cs new file mode 100644 index 00000000..d978e6e7 --- /dev/null +++ b/Lombiq.Hosting.Tenants.EnvironmentRobots/Startup.cs @@ -0,0 +1,36 @@ +using Lombiq.Hosting.Tenants.EnvironmentRobots.Constants; +using Lombiq.Hosting.Tenants.EnvironmentRobots.Filters; +using Lombiq.Hosting.Tenants.EnvironmentRobots.Middlewares; +using Lombiq.Hosting.Tenants.EnvironmentRobots.Models; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using OrchardCore.Environment.Shell.Configuration; +using OrchardCore.Modules; +using System; + +namespace Lombiq.Hosting.Tenants.EnvironmentRobots; + +[Feature(FeatureNames.EnvironmentRobots)] +public class Startup : StartupBase +{ + private readonly IShellConfiguration _shellConfiguration; + + public Startup(IShellConfiguration shellConfiguration) => + _shellConfiguration = shellConfiguration; + + public override void ConfigureServices(IServiceCollection services) + { + var options = new EnvironmentRobotsOptions(); + var configSection = _shellConfiguration + .GetSection("Lombiq_Hosting_Tenants_EnvironmentRobots:EnvironmentRobotsOptions"); + configSection.Bind(options); + services.Configure(configSection); + + services.AddMvc(options => options.Filters.Add(typeof(EnvironmentRobotsMetaTagFilter))); + } + + public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder routes, IServiceProvider serviceProvider) => + app.UseMiddleware(); +}