Skip to content

Commit de6824d

Browse files
Merge pull request #11909 from umbraco/v9/feature/allowlist-for-help-page
V9: Add allowlist for HelpPage
2 parents 4d4aff4 + d4c43b6 commit de6824d

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

src/JsonSchema/AppSettings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ public class CmsDefinition
8989
public LegacyPasswordMigrationSettings LegacyPasswordMigration { get; set; }
9090

9191
public ContentDashboardSettings ContentDashboard { get; set; }
92+
93+
public HelpPageSettings HelpPage { get; set; }
9294
}
9395

9496
/// <summary>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Umbraco.Cms.Core.Configuration.Models
2+
{
3+
[UmbracoOptions(Constants.Configuration.ConfigHelpPage)]
4+
public class HelpPageSettings
5+
{
6+
/// <summary>
7+
/// Gets or sets the allowed addresses to retrieve data for the content dashboard.
8+
/// </summary>
9+
public string[] HelpPageUrlAllowList { get; set; }
10+
}
11+
}

src/Umbraco.Core/Constants-Configuration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public static class Configuration
5555
public const string ConfigRichTextEditor = ConfigPrefix + "RichTextEditor";
5656
public const string ConfigPackageMigration = ConfigPrefix + "PackageMigration";
5757
public const string ConfigContentDashboard = ConfigPrefix + "ContentDashboard";
58+
public const string ConfigHelpPage = ConfigPrefix + "HelpPage";
5859
}
5960
}
6061
}

src/Umbraco.Core/DependencyInjection/UmbracoBuilder.Configuration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ public static IUmbracoBuilder AddConfiguration(this IUmbracoBuilder builder)
8787
.AddUmbracoOptions<RuntimeMinificationSettings>()
8888
.AddUmbracoOptions<LegacyPasswordMigrationSettings>()
8989
.AddUmbracoOptions<PackageMigrationSettings>()
90-
.AddUmbracoOptions<ContentDashboardSettings>();
90+
.AddUmbracoOptions<ContentDashboardSettings>()
91+
.AddUmbracoOptions<HelpPageSettings>();
9192

9293
builder.Services.Configure<RequestHandlerSettings>(options => options.MergeReplacements(builder.Config));
9394

src/Umbraco.Web.BackOffice/Controllers/HelpController.cs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net;
25
using System.Net.Http;
36
using System.Runtime.Serialization;
47
using System.Threading.Tasks;
8+
using Microsoft.Extensions.DependencyInjection;
59
using Microsoft.Extensions.Logging;
10+
using Microsoft.Extensions.Options;
611
using Newtonsoft.Json;
12+
using Umbraco.Cms.Core.Configuration.Models;
713
using Umbraco.Cms.Web.Common.Attributes;
14+
using Umbraco.Cms.Web.Common.DependencyInjection;
815
using Constants = Umbraco.Cms.Core.Constants;
916

1017
namespace Umbraco.Cms.Web.BackOffice.Controllers
@@ -13,15 +20,44 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
1320
public class HelpController : UmbracoAuthorizedJsonController
1421
{
1522
private readonly ILogger<HelpController> _logger;
23+
private HelpPageSettings _helpPageSettings;
1624

25+
[Obsolete("Use constructor that takes IOptions<HelpPageSettings>")]
1726
public HelpController(ILogger<HelpController> logger)
27+
: this(logger, StaticServiceProvider.Instance.GetRequiredService<IOptionsMonitor<HelpPageSettings>>())
28+
{
29+
}
30+
31+
[ActivatorUtilitiesConstructor]
32+
public HelpController(
33+
ILogger<HelpController> logger,
34+
IOptionsMonitor<HelpPageSettings> helpPageSettings)
1835
{
1936
_logger = logger;
37+
38+
ResetHelpPageSettings(helpPageSettings.CurrentValue);
39+
helpPageSettings.OnChange(ResetHelpPageSettings);
40+
}
41+
42+
private void ResetHelpPageSettings(HelpPageSettings settings)
43+
{
44+
_helpPageSettings = settings;
2045
}
2146

2247
private static HttpClient _httpClient;
48+
2349
public async Task<List<HelpPage>> GetContextHelpForPage(string section, string tree, string baseUrl = "https://our.umbraco.com")
2450
{
51+
if (IsAllowedUrl(baseUrl) is false)
52+
{
53+
_logger.LogError($"The following URL is not listed in the allowlist for HelpPage in web.config: {baseUrl}");
54+
HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
55+
56+
// Ideally we'd want to return a BadRequestResult here,
57+
// however, since we're not returning ActionResult this is not possible and changing it would be a breaking change.
58+
return new List<HelpPage>();
59+
}
60+
2561
var url = string.Format(baseUrl + "/Umbraco/Documentation/Lessons/GetContextHelpDocs?sectionAlias={0}&treeAlias={1}", section, tree);
2662

2763
try
@@ -44,6 +80,17 @@ public async Task<List<HelpPage>> GetContextHelpForPage(string section, string t
4480

4581
return new List<HelpPage>();
4682
}
83+
84+
private bool IsAllowedUrl(string url)
85+
{
86+
if (_helpPageSettings.HelpPageUrlAllowList is null ||
87+
_helpPageSettings.HelpPageUrlAllowList.Contains(url))
88+
{
89+
return true;
90+
}
91+
92+
return false;
93+
}
4794
}
4895

4996
[DataContract(Name = "HelpPage")]

0 commit comments

Comments
 (0)