diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-10-26 20:08:21 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-10-26 20:08:21 -0500 |
| commit | be0bc671c057babfd953300066806e5f79a6ce0e (patch) | |
| tree | 99ea401c6fceee55a4cc22a07e6f5452899b701a /src | |
| parent | dad997da75fbccdad4988cea9ce9147f72b493a6 (diff) | |
| download | slskd-be0bc671c057babfd953300066806e5f79a6ce0e.tar.gz | |
ensure exception can be returned if injection throws
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/Middleware/HTMLInjectionMiddleware.cs | 60 |
1 files changed, 39 insertions, 21 deletions
diff --git a/src/slskd/Common/Middleware/HTMLInjectionMiddleware.cs b/src/slskd/Common/Middleware/HTMLInjectionMiddleware.cs index 4f3a743b..18851c0d 100644 --- a/src/slskd/Common/Middleware/HTMLInjectionMiddleware.cs +++ b/src/slskd/Common/Middleware/HTMLInjectionMiddleware.cs @@ -110,37 +110,55 @@ namespace slskd var stream = new MemoryStream(); context.Response.Body = stream; - await Next.Invoke(context); - - if (context.Response.StatusCode == 200) + try { - // something downstream responded with a 200, meaning there's data in the body - // we need to read it, so we can reset then play it back with the modified HTML - context.Response.Body.Seek(0, SeekOrigin.Begin); - var body = await new StreamReader(context.Response.Body).ReadToEndAsync(); + await Next.Invoke(context); - var headers = context.Response.Headers - .ToDictionary(kvp => kvp.Key, kvp => kvp.Value) - .Where(kvp => !string.Equals(kvp.Key, "Content-Length", StringComparison.InvariantCultureIgnoreCase)); + if (context.Response.StatusCode == 200) + { + // something downstream responded with a 200, meaning there's data in the body + // we need to read it, so we can reset then play it back with the modified HTML + context.Response.Body.Seek(0, SeekOrigin.Begin); + var body = await new StreamReader(context.Response.Body).ReadToEndAsync(); - context.Response.Clear(); + var headers = context.Response.Headers + .ToDictionary(kvp => kvp.Key, kvp => kvp.Value) + .Where(kvp => !string.Equals(kvp.Key, "Content-Length", StringComparison.InvariantCultureIgnoreCase)); - foreach (var header in headers) - { - context.Response.Headers.Add(header); - } + context.Response.Clear(); + + foreach (var header in headers) + { + context.Response.Headers.Add(header); + } + // 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. // drop the semicolon and everything after it (e.g. '; charset=utf-8') before comparing var responseType = context.Response.ContentType?.Split(';')[0].Trim(); var isInjectableResponseType = injectableTypes.Contains(responseType, StringComparer.InvariantCultureIgnoreCase); - // rewind the stream we injected to the beginning, then replay the data to the - // original stream - stream.Seek(0, SeekOrigin.Begin); - await stream.CopyToAsync(originalStream); + if (!isInjectableResponseType) + { + await context.Response.WriteAsync(body); + } + else + { + await context.Response.WriteAsync(body + HTML); + } + } - // swap the original stream back in - context.Response.Body = originalStream; + // rewind the stream we injected to the beginning, then replay the data to the + // original stream + stream.Seek(0, SeekOrigin.Begin); + await stream.CopyToAsync(originalStream); + } + finally + { + // swap the original stream back in, even if something downstream threw, so that + // upstream middleware (e.g. the exception handler) can write to the response + context.Response.Body = originalStream; + } } else { |
