Skip to content

Commit 83f4691

Browse files
committed
Conditionally restrict static files from ErrorController
Custom errors provide human audiences with friendly error messages. Most static files—at least on an OnTopic website—will not be presented to human audiences, however, as they will instead be loaded as resources via e.g. `<script />`, `<style />`, or `<image />` tags (among others). As such, processing the entire `ErrorController` logic and returning a bunch of markup isn't necessary, and is even wasteful in terms of CPU load and bandwidth. That's especially true of sites subjected to a lot of robot activity for identifying potential exploits (which is the majority of public facing sites). To better control this, the `HttpAsync` action of the `ErrorController` now accepts an optional `includeStaticFiles` parameter, which defaults to true. If this is set in the querystring or a route variable, it will exclude static files from subsequent processing by the `ErrorController`, and instead return a canned string. This addresses the core requirements of Feature #101.
1 parent e1abb1d commit 83f4691

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

OnTopic.AspNetCore.Mvc/Controllers/ErrorController.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
| Project Topics Library
55
\=============================================================================================================================*/
66
using Microsoft.AspNetCore.Builder;
7+
using Microsoft.AspNetCore.Diagnostics;
78
using OnTopic.Mapping;
89

910
namespace OnTopic.AspNetCore.Mvc.Controllers {
@@ -48,7 +49,17 @@ ITopicMappingService topicMappingService
4849
/// content available.
4950
/// </summary>
5051
/// <returns>A view associated with the requested current <paramref name="statusCode"/>.</returns>
51-
public async virtual Task<IActionResult> HttpAsync([FromRoute(Name="id")] int statusCode) {
52+
public async virtual Task<IActionResult> HttpAsync([FromRoute(Name="id")] int statusCode, bool includeStaticFiles = true) {
53+
54+
/*------------------------------------------------------------------------------------------------------------------------
55+
| Bypass for resources
56+
\-----------------------------------------------------------------------------------------------------------------------*/
57+
if (!includeStaticFiles) {
58+
var feature = HttpContext.Features.Get<IStatusCodeReExecuteFeature>();
59+
if (feature?.OriginalPath.Contains('.', StringComparison.Ordinal)?? false) {
60+
return Content("The resource requested could not found.");
61+
}
62+
}
5263

5364
/*------------------------------------------------------------------------------------------------------------------------
5465
| Identify base path

0 commit comments

Comments
 (0)