Browse Source

Warning on forms with passwords

mj-deploy
Standa Lukeš 4 years ago
parent
commit
f6ca2e163b
  1. 34
      server/Ksp.WebServer/.vscode/launch.json
  2. 42
      server/Ksp.WebServer/.vscode/tasks.json
  3. 32
      server/Ksp.WebServer/KspPageRewriter.cs
  4. 17
      server/Ksp.WebServer/Startup.cs

34
server/Ksp.WebServer/.vscode/launch.json

@ -0,0 +1,34 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/Ksp.WebServer.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}

42
server/Ksp.WebServer/.vscode/tasks.json

@ -0,0 +1,42 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/Ksp.WebServer.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/Ksp.WebServer.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/Ksp.WebServer.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}

32
server/Ksp.WebServer/KspPageRewriter.cs

@ -0,0 +1,32 @@
using System.IO;
using AngleSharp.Html;
using Microsoft.AspNetCore.Http;
namespace Ksp.WebServer
{
public class KspPageRewriter
{
public string RewriteHtml(string source, HttpContext context)
{
var p = new AngleSharp.Html.Parser.HtmlParser();
var document = p.ParseDocument(source);
foreach (var form in document.QuerySelectorAll("form"))
{
if (form.QuerySelector("input[type=password]") is null)
continue;
var warning = document.CreateElement("div");
warning.SetAttribute("style", "color: red; font-size: 3em; font-weight: bold");
warning.TextContent = "Web běží na magické proxy, které byste měli věřit!!!";
form.Prepend(warning);
}
var outputHtml = new StringWriter();
document.ToHtml(outputHtml, new PrettyMarkupFormatter() { Indentation = "\t", NewLine = "\n" });
return outputHtml.ToString();
}
}
}

17
server/Ksp.WebServer/Startup.cs

@ -5,6 +5,7 @@ using System.Linq;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using AspNetCore.Proxy; using AspNetCore.Proxy;
using AspNetCore.Proxy.Builders; using AspNetCore.Proxy.Builders;
@ -39,15 +40,17 @@ namespace Ksp.WebServer
.ConfigurePrimaryHttpMessageHandler(h => { .ConfigurePrimaryHttpMessageHandler(h => {
return new HttpClientHandler { return new HttpClientHandler {
AllowAutoRedirect = false, AllowAutoRedirect = false,
UseCookies = false UseCookies = false,
AutomaticDecompression = DecompressionMethods.All
}; };
}); });
services.AddProxies(); services.AddProxies();
services.Configure<KspProxyConfig>(Configuration.GetSection(nameof(KspProxyConfig))); services.Configure<KspProxyConfig>(Configuration.GetSection(nameof(KspProxyConfig)));
services.AddSingleton<KspPageRewriter>();
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptions<KspProxyConfig> kspProxyConfig) public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptions<KspProxyConfig> kspProxyConfig, KspPageRewriter pageRewriter)
{ {
Console.WriteLine($"Running {env.EnvironmentName} env, root={env.ContentRootPath}, host={kspProxyConfig.Value.Host}"); Console.WriteLine($"Running {env.EnvironmentName} env, root={env.ContentRootPath}, host={kspProxyConfig.Value.Host}");
@ -97,7 +100,7 @@ namespace Ksp.WebServer
// Console.WriteLine(request); // Console.WriteLine(request);
return Task.CompletedTask; return Task.CompletedTask;
}); });
opt.WithAfterReceive((cx, response) => { opt.WithAfterReceive(async (cx, response) => {
// Console.WriteLine(response); // Console.WriteLine(response);
if (response.Headers.Location is object && response.Headers.Location.Host == baseUri.Host) if (response.Headers.Location is object && response.Headers.Location.Host == baseUri.Host)
{ {
@ -115,7 +118,13 @@ namespace Ksp.WebServer
.Replace($"; domain={baseUri.Host}", $"; domain={cx.Request.Host.Host}") .Replace($"; domain={baseUri.Host}", $"; domain={cx.Request.Host.Host}")
)); ));
} }
return Task.CompletedTask;
if (new [] { "text/html", "application/xhtml+xml" }.Contains(response.Content.Headers.ContentType.MediaType))
{
var str = await response.Content.ReadAsStringAsync();
str = pageRewriter.RewriteHtml(str, cx);
response.Content = new StringContent(str, Encoding.UTF8, "text/html");
}
}); });
})); }));
} }