aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2022-08-28 22:29:52 -0500
committerGitHub <noreply@github.com>2022-08-28 22:29:52 -0500
commit0085fa79cbe0b2589abc9ca6a99c367ff5ff0fd5 (patch)
tree675e219f54e210080315cff9f005163722a18845
parentfd91051fbe04cd3db23b197f75dff8a566b2e007 (diff)
parent485accc1e17420ae02310fe5ca864704599bd5af (diff)
downloadslskd-0085fa79cbe0b2589abc9ca6a99c367ff5ff0fd5.tar.gz
Merge pull request #618 from slskd/injection
Fix bug causing html to be injected in prometheus metrics
-rw-r--r--src/slskd/Common/Middleware/HTMLInjectionMiddleware.cs25
-rw-r--r--src/slskd/Program.cs2
2 files changed, 21 insertions, 6 deletions
diff --git a/src/slskd/Common/Middleware/HTMLInjectionMiddleware.cs b/src/slskd/Common/Middleware/HTMLInjectionMiddleware.cs
index 3e2c003a..5a073b54 100644
--- a/src/slskd/Common/Middleware/HTMLInjectionMiddleware.cs
+++ b/src/slskd/Common/Middleware/HTMLInjectionMiddleware.cs
@@ -18,6 +18,7 @@
namespace slskd
{
using System;
+ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
@@ -34,8 +35,9 @@ namespace slskd
/// </summary>
/// <param name="builder"></param>
/// <param name="html"></param>
+ /// <param name="excludedRoutes"></param>
/// <returns></returns>
- public static IApplicationBuilder UseHTMLInjection(this IApplicationBuilder builder, string html)
+ public static IApplicationBuilder UseHTMLInjection(this IApplicationBuilder builder, string html, IEnumerable<string> excludedRoutes)
{
return builder.UseMiddleware<HTMLInjectionMiddleware>(html);
}
@@ -51,14 +53,17 @@ namespace slskd
/// </summary>
/// <param name="next"></param>
/// <param name="html"></param>
- public HTMLInjectionMiddleware(RequestDelegate next, string html)
+ /// <param name="excludedRoutes"></param>
+ public HTMLInjectionMiddleware(RequestDelegate next, string html, IEnumerable<string> excludedRoutes)
{
Next = next;
HTML = html;
+ ExcludedRoutes = excludedRoutes;
}
private string HTML { get; }
private RequestDelegate Next { get; }
+ private IEnumerable<string> ExcludedRoutes { get; }
/// <summary>
/// Executes this middleware, returning the contents of the requested HTML file with the specified HTML appended.
@@ -74,10 +79,10 @@ namespace slskd
.Intersect(injectableTypes, StringComparer.InvariantCultureIgnoreCase)
.Any();
- var isApiRoute = context.Request.Path.ToString().StartsWith("/api");
+ var isExcludedRoute = ExcludedRoutes.Any(route => context.Request.Path.ToString().StartsWith(route));
var isGET = context.Request.Method == "GET";
- if (!isApiRoute && isGET && isInjectableType)
+ if (!isExcludedRoute && isGET && isInjectableType)
{
var originalStream = context.Response.Body;
@@ -106,7 +111,17 @@ namespace slskd
context.Response.Headers.Add(header);
}
- await context.Response.WriteAsync(body + HTML);
+ // check the response content type to make sure it's still injectable. the server might return something other
+ // than what was requested. if it's no longer injectable, don't inject.
+ var isInjectableResponseType = injectableTypes.Any(injectableType => context.Response.Headers.ContentType.Contains(injectableType));
+ if (!isInjectableResponseType)
+ {
+ await context.Response.WriteAsync(body);
+ }
+ else
+ {
+ await context.Response.WriteAsync(body + HTML);
+ }
}
// rewind the stream we injected to the beginning, then replay the data to the
diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs
index f9294208..6357302b 100644
--- a/src/slskd/Program.cs
+++ b/src/slskd/Program.cs
@@ -648,7 +648,7 @@ namespace slskd
// prepend the url base.
app.UsePathBase(urlBase);
app.UseHTMLRewrite("((\\.)?\\/static)", $"{(urlBase == "/" ? string.Empty : urlBase)}/static");
- app.UseHTMLInjection($"<script>window.urlBase=\"{urlBase}\"</script>");
+ app.UseHTMLInjection($"<script>window.urlBase=\"{urlBase}\"</script>", excludedRoutes: new[] { "/api", "/swagger" });
Log.Information("Using base url {UrlBase}", urlBase);
// serve static content from the configured path